Skip to content

Commit

Permalink
feat: substrate wallet class base implementation and some fixes for E…
Browse files Browse the repository at this point in the history
…vm wallet
  • Loading branch information
wainola committed Sep 28, 2023
1 parent 94fd2b0 commit e6caa8d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 60 deletions.
12 changes: 3 additions & 9 deletions packages/wallet-manager/src/test/EvmWallet.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -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');
});
});
});
4 changes: 2 additions & 2 deletions packages/wallet-manager/src/types.ts
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;
}
}
18 changes: 5 additions & 13 deletions packages/wallet-manager/src/wallets/Evm/Evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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[]) {
Expand Down
56 changes: 23 additions & 33 deletions packages/wallet-manager/src/wallets/Substrate/Substrate.ts
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 };
9 changes: 6 additions & 3 deletions packages/wallet-manager/src/wallets/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Provider, Web3Provider } from '@ethersproject/providers';
import { ApiPromise, WsProvider } from '@polkadot/api';

export interface SupportedWallet {
id: string;
Expand All @@ -10,8 +11,6 @@ export interface SupportedWallet {
export interface IEvmWallet {
web3Provider: Web3Provider;
windowConnector: Provider;
name: string;
icon: string;
account?: string;
connect(): Promise<void>;
addChain({
Expand All @@ -30,4 +29,8 @@ export interface IEvmWallet {
}): Promise<void>;
}

export interface SusbtrateWallet {}
export interface SusbtrateWallet {
wssProvider?: WsProvider;
apiPromise?: ApiPromise;
substrateAccount?: string;
}

0 comments on commit e6caa8d

Please sign in to comment.