diff --git a/packages/wallet-manager/src/test/EvmWallet.test.ts b/packages/wallet-manager/src/test/EvmWallet.test.ts index accc3933..d4fae693 100644 --- a/packages/wallet-manager/src/test/EvmWallet.test.ts +++ b/packages/wallet-manager/src/test/EvmWallet.test.ts @@ -1,13 +1,7 @@ -import { ExternalProvider } from '@ethersproject/providers'; +import { ethers } from 'ethers'; import { describe, it, expect, beforeEach } from 'vitest'; import { EvmWallet } from '../'; -declare global { - interface Window { - ethereum: ExternalProvider; - } -} - describe('EvmWallet', () => { describe('EvmWallet + provider on window', () => { beforeEach(() => { @@ -16,10 +10,10 @@ describe('EvmWallet', () => { }; }); it('should be able to create an instance of EvmWallet', () => { - const evmWallet = new EvmWallet('metamask', 'icon', window.ethereum); + const w3Provider = new ethers.providers.Web3Provider(window.ethereum); + const evmWallet = new EvmWallet(w3Provider); expect(evmWallet).toBeInstanceOf(EvmWallet); - expect(evmWallet.name).toBe('metamask'); }); }); }); diff --git a/packages/wallet-manager/src/types.ts b/packages/wallet-manager/src/types.ts index 97b18c1c..0e70975b 100644 --- a/packages/wallet-manager/src/types.ts +++ b/packages/wallet-manager/src/types.ts @@ -1,7 +1,7 @@ -import { Provider } from '@ethersproject/providers'; +import { ExternalProvider } from '@ethersproject/providers'; declare global { interface Window { - ethereum: Provider; + ethereum: ExternalProvider; } } diff --git a/packages/wallet-manager/src/wallets/Evm/Evm.ts b/packages/wallet-manager/src/wallets/Evm/Evm.ts index 7325bf5c..b48c19d1 100644 --- a/packages/wallet-manager/src/wallets/Evm/Evm.ts +++ b/packages/wallet-manager/src/wallets/Evm/Evm.ts @@ -9,15 +9,11 @@ import { IEvmWallet } from '../interfaces'; class EvmWallet extends events.EventEmitter implements IEvmWallet { public account: string | undefined; - public icon: string; - public name: string; public web3Provider!: Web3Provider; public windowConnector: Provider; - constructor(name: string, icon: string, provider?: Web3Provider) { + constructor(provider?: Web3Provider) { super(); - this.name = name; - this.icon = icon; if (!window.ethereum) { throw new Error('window.ethereum is not defined.'); @@ -34,16 +30,12 @@ class EvmWallet extends events.EventEmitter implements IEvmWallet { this.appendProviderEvents(); } - static initFromWeb3Provider( - name: string, - icon: string, - web3Provider: Web3Provider - ) { - return new EvmWallet(name, icon, web3Provider); + static initFromWeb3Provider(web3Provider: Web3Provider) { + return new EvmWallet(web3Provider); } - static initFromWindow(name: string, icon: string) { - return new EvmWallet(name, icon); + static initFromWindow() { + return new EvmWallet(); } private async calculateAccountData(accounts?: string[]) { diff --git a/packages/wallet-manager/src/wallets/Substrate/Substrate.ts b/packages/wallet-manager/src/wallets/Substrate/Substrate.ts index 9989a1ac..8a59382c 100644 --- a/packages/wallet-manager/src/wallets/Substrate/Substrate.ts +++ b/packages/wallet-manager/src/wallets/Substrate/Substrate.ts @@ -1,21 +1,37 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; import { web3Accounts, web3Enable } from '@polkadot/extension-dapp'; -import { formatBalance } from '@polkadot/util'; - -formatBalance.setDefaults({ unit: 'DOT' }); class Substrate { substrateAccount?: string; - api?: ApiPromise; + apiPromise?: ApiPromise; wssProvider?: WsProvider; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - balance?: any; + + constructor(apiPromise?: ApiPromise) { + this.apiPromise = apiPromise; + } + + static async connectFromWssProvider(wssProvider: string) { + const wsProvider = await Substrate.conntectToApi(wssProvider); + const apiPromise = await ApiPromise.create({ provider: wsProvider }); + return new Substrate(apiPromise); + } + + static connectFromApiPromise(apiPromise: ApiPromise) { + return new Substrate(apiPromise); + } + + static async conntectToApi(wssProvider: string): Promise { + const wsProvider = new WsProvider(wssProvider); + return wsProvider; + } public async connect() { const injectors = await web3Enable('Polkadot Wallet'); + // using polkadot-js extension const polkadotInjector = injectors.find( (injector) => injector.name === 'polkadot-js' ); + if (polkadotInjector) { // eslint-disable-next-line no-console console.log('polkadot-js extension found'); @@ -23,32 +39,6 @@ class Substrate { this.substrateAccount = allAccounts[0].address; } } - - public async conntectToApi() { - this.wssProvider = new WsProvider('wss://rpc.polkadot.io'); - this.api = await ApiPromise.create({ provider: this.wssProvider }); - } - - public async getBalance() { - const { data: balance } = (await this.api?.query.system.account( - this.substrateAccount! - // eslint-disable-next-line @typescript-eslint/no-explicit-any - )) as any; - const chainDecimals = this.api?.registry.chainDecimals[0]; - this.balance = formatBalance(balance.free, { decimals: chainDecimals }); - } - - get currentAccount() { - return this.substrateAccount; - } - - get currentApi(): ApiPromise { - return this.api as ApiPromise; - } - - get currentBalance() { - return this.balance; - } } -export { Substrate } +export { Substrate }; diff --git a/packages/wallet-manager/src/wallets/interfaces/index.ts b/packages/wallet-manager/src/wallets/interfaces/index.ts index 7d41e246..43b86036 100644 --- a/packages/wallet-manager/src/wallets/interfaces/index.ts +++ b/packages/wallet-manager/src/wallets/interfaces/index.ts @@ -1,4 +1,5 @@ import { Provider, Web3Provider } from '@ethersproject/providers'; +import { ApiPromise, WsProvider } from '@polkadot/api'; export interface SupportedWallet { id: string; @@ -10,8 +11,6 @@ export interface SupportedWallet { export interface IEvmWallet { web3Provider: Web3Provider; windowConnector: Provider; - name: string; - icon: string; account?: string; connect(): Promise; addChain({ @@ -30,4 +29,8 @@ export interface IEvmWallet { }): Promise; } -export interface SusbtrateWallet {} +export interface SusbtrateWallet { + wssProvider?: WsProvider; + apiPromise?: ApiPromise; + substrateAccount?: string; +}