Skip to content

Commit

Permalink
fix: dot and ksm balance query stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
a0ngo committed Nov 26, 2023
1 parent c9956a7 commit 3c30f4e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
25 changes: 12 additions & 13 deletions apps/recovery-relay/lib/wallets/DOT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AccountData } from '../types';
export class Polkadot extends BaseDOT implements ConnectedWallet {
private provider: WsProvider;

private api: ApiPromise | undefined;
private polkadotApi: ApiPromise | undefined;

constructor(input: Input) {
super(input);
Expand All @@ -16,7 +16,7 @@ export class Polkadot extends BaseDOT implements ConnectedWallet {

public async getBalance(): Promise<number> {
await this._getApi();
const api = this.api!;
const api = this.polkadotApi!;
// @ts-ignore
const { data: balance } = await api.query.system.account(this.address);
return balance.free.toNumber() / 10 ** 10;
Expand All @@ -25,14 +25,13 @@ export class Polkadot extends BaseDOT implements ConnectedWallet {
public async prepare(): Promise<AccountData> {
const balance = await this.getBalance();
//@ts-ignore
const { nonce } = await this.api!.query.system.account(this.address);
const genesisHash = this.api!.genesisHash.toHex();
const blockHash = (await this.api!.rpc.chain.getBlockHash()).toHex();
const blockNum = (await this.api!.rpc.chain.getBlock()).block.header.number.toNumber();
const specVersion = this.api!.runtimeVersion.specVersion.toNumber();
const specName = this.api!.runtimeVersion.specName.toHuman();
const transactionVersion = this.api!.runtimeVersion.transactionVersion.toNumber();
const rpc = (await this.api!.rpc.state.getMetadata()).toHex();
const { nonce } = await this.polkadotApi!.query.system.account(this.address);
const genesisHash = this.polkadotApi!.genesisHash.toHex();
const blockHash = (await this.polkadotApi!.rpc.chain.getBlockHash()).toHex();
const blockNum = (await this.polkadotApi!.rpc.chain.getBlock()).block.header.number.toNumber();
const specVersion = this.polkadotApi!.runtimeVersion.specVersion.toNumber();
const specName = this.polkadotApi!.runtimeVersion.specName.toHuman();
const transactionVersion = this.polkadotApi!.runtimeVersion.transactionVersion.toNumber();

const extraParams = new Map<string, any>();
extraParams.set(this.KEY_BLOCK_HASH, blockHash);
Expand All @@ -56,7 +55,7 @@ export class Polkadot extends BaseDOT implements ConnectedWallet {
await this._getApi();
try {
const txHash = construct.txHash(tx);
await this.api!.rpc.author.submitAndWatchExtrinsic(tx);
await this.polkadotApi!.rpc.author.submitAndWatchExtrinsic(tx);
this.relayLogger.debug(`Polkadot: Broadcasted tx: ${txHash}`);
return txHash;
} catch (e) {
Expand All @@ -66,9 +65,9 @@ export class Polkadot extends BaseDOT implements ConnectedWallet {
}

private async _getApi(): Promise<void> {
if (this.api) {
if (this.polkadotApi) {
return;
}
this.api = await ApiPromise.create({ provider: this.provider });
this.polkadotApi = await ApiPromise.create({ provider: this.provider });
}
}
26 changes: 13 additions & 13 deletions apps/recovery-relay/lib/wallets/KSM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AccountData } from '../types';
export class Kusama extends BaseKSM implements ConnectedWallet {
protected provider: WsProvider;

private api: ApiPromise | undefined;
private kusamaApi: ApiPromise | undefined;

constructor(input: Input) {
super(input);
Expand All @@ -19,7 +19,7 @@ export class Kusama extends BaseKSM implements ConnectedWallet {

public async getBalance(): Promise<number> {
await this._getApi();
const api = this.api!;
const api = this.kusamaApi!;
// @ts-ignore
const { data: balance } = await api.query.system.account(this.address);
return balance.free.toNumber() / 10 ** 12;
Expand All @@ -28,14 +28,14 @@ export class Kusama extends BaseKSM implements ConnectedWallet {
public async prepare(): Promise<AccountData> {
const balance = await this.getBalance();
//@ts-ignore
const { nonce } = await this.api!.query.system.account(this.address);
const genesisHash = this.api!.genesisHash.toHex();
const blockHash = (await this.api!.rpc.chain.getBlockHash()).toHex();
const blockNum = (await this.api!.rpc.chain.getBlock()).block.header.number.toNumber();
const specVersion = this.api!.runtimeVersion.specVersion.toNumber();
const specName = this.api!.runtimeVersion.specName.toHuman();
const transactionVersion = this.api!.runtimeVersion.transactionVersion.toNumber();
const rpc = (await this.api!.rpc.state.getMetadata()).toHex();
const { nonce } = await this.kusamaApi!.query.system.account(this.address);
const genesisHash = this.kusamaApi!.genesisHash.toHex();
const blockHash = (await this.kusamaApi!.rpc.chain.getBlockHash()).toHex();
const blockNum = (await this.kusamaApi!.rpc.chain.getBlock()).block.header.number.toNumber();
const specVersion = this.kusamaApi!.runtimeVersion.specVersion.toNumber();
const specName = this.kusamaApi!.runtimeVersion.specName.toHuman();
const transactionVersion = this.kusamaApi!.runtimeVersion.transactionVersion.toNumber();
const rpc = (await this.kusamaApi!.rpc.state.getMetadata()).toHex();

const extraParams = new Map<string, any>();
extraParams.set(this.KEY_BLOCK_HASH, blockHash);
Expand All @@ -59,7 +59,7 @@ export class Kusama extends BaseKSM implements ConnectedWallet {
await this._getApi();
try {
const txHash = construct.txHash(tx);
await this.api!.rpc.author.submitAndWatchExtrinsic(tx);
await this.kusamaApi!.rpc.author.submitAndWatchExtrinsic(tx);
this.relayLogger.debug(`Kusama: Tx broadcasted: ${txHash}`);
return txHash;
} catch (e) {
Expand All @@ -69,9 +69,9 @@ export class Kusama extends BaseKSM implements ConnectedWallet {
}

private async _getApi(): Promise<void> {
if (this.api) {
if (this.kusamaApi) {
return;
}
this.api = await ApiPromise.create({ provider: this.provider });
this.kusamaApi = await ApiPromise.create({ provider: this.provider });
}
}
8 changes: 7 additions & 1 deletion packages/asset-config/config/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ export const nativeAssetPatches: NativeAssetPatches = {
DASH_TEST: btc('blockexplorer.one/dash/testnet', false),
DOGE: btc('dexplorer.dogechain.dog', false),
DOGE_TEST: btc('explorer-testnet.dogechain.dog', false),
DOT: evm('polkascan.io/polkadot', 'https://rpc.polkadot.io'),
DOT: {
derive: true,
transfer: true,
rpcUrl: 'https://rpc.polkadot.io',
getExplorerUrl: (type: string) => (value: string) =>
`https://explorer.polkascan.io/polkadot/${type === 'tx' ? 'extrinsic' : 'account'}/${value}`,
},
EOS: {
derive: true,
transfer: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/lib/sanatize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const sanatize = (value: any, depth = 0): any => {
'privateKey',
'Private Key (WIF)',
'Private Key',
'polkadotApi', // Polkadot's index.ts contains the API field which is too large and part of printing it causes issues
'kusamaApi', // Kusama's index.ts contains the API field which is too large and part of printing it causes issues
].includes(key)
) {
return;
Expand Down

0 comments on commit 3c30f4e

Please sign in to comment.