-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: substrate wallet class base implementation and some fixes for E…
…vm wallet
- Loading branch information
Showing
5 changed files
with
39 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Provider } from '@ethersproject/providers'; | ||
import { ExternalProvider } from '@ethersproject/providers'; | ||
|
||
declare global { | ||
interface Window { | ||
ethereum: Provider; | ||
ethereum: ExternalProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 23 additions & 33 deletions
56
packages/wallet-manager/src/wallets/Substrate/Substrate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,44 @@ | ||
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<WsProvider> { | ||
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'); | ||
const allAccounts = await web3Accounts(); | ||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters