Skip to content

Commit

Permalink
support for new balances type
Browse files Browse the repository at this point in the history
(polkadot 1.1 support)
  • Loading branch information
mrq1911 committed Dec 14, 2023
1 parent 02587d3 commit 3c69177
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
24 changes: 8 additions & 16 deletions packages/sdk/src/client/BalanceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export class BalanceClient extends PolkadotApiClient {

async getSystemBalance(accountId: string): Promise<BigNumber> {
const {
data: { free, miscFrozen, feeFrozen },
data: { free, miscFrozen, feeFrozen, frozen },
} = await this.api.query.system.account(accountId);
return this.calculateFreeBalance(
free.toString(),
miscFrozen.toString(),
feeFrozen.toString()
(miscFrozen || frozen).toString(),
(feeFrozen || 0).toString()
);
}

Expand Down Expand Up @@ -90,25 +90,17 @@ export class BalanceClient extends PolkadotApiClient {
): UnsubscribePromise {
return this.api.query.system.account(
address,
({ data: { free, miscFrozen, feeFrozen } }) => {
const freeBalance = this.calculateFreeBalance(
free.toString(),
miscFrozen.toString(),
feeFrozen.toString()
);
onChange(SYSTEM_ASSET_ID, freeBalance);
}
({ data }) => onChange(SYSTEM_ASSET_ID, this.calculateFreeBalance(data))
);
}

private calculateFreeBalance(
free: string,
miscFrozen: string,
feeFrozen: string
data: any,
): BigNumber {
const { free, miscFrozen, feeFrozen, frozen } = data;
const freeBN = new BigNumber(free);
const miscFrozenBN = new BigNumber(miscFrozen);
const feeFrozenBN = new BigNumber(feeFrozen);
const miscFrozenBN = new BigNumber(miscFrozen || frozen);
const feeFrozenBN = new BigNumber(feeFrozen || 0);
const maxFrozenBN = miscFrozenBN.gt(feeFrozenBN)
? miscFrozenBN
: feeFrozenBN;
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/test/script/examples/router/getBestBuy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class GetBestBuyPriceExample extends PolkadotExecutor {
}

new GetBestBuyPriceExample(
ApiUrl.HydraDx_Rococo,
'Get best buy price',
ApiUrl.HydraDx,
'Get best buy price Hydra',
true
).run();
).run().then(() => new GetBestBuyPriceExample(
ApiUrl.Chopsticks,
'Get best buy price 🐍',
true
).run());
74 changes: 42 additions & 32 deletions packages/sdk/test/script/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiPromise, WsProvider } from '@polkadot/api';

export enum ApiUrl {
Local = 'ws://localhost:8000',
Chopsticks = 'wss://chopsticks.rpc.hydration.cloud',
Nice = 'wss://rpc.nice.hydration.cloud',
Basilisk = 'wss://rpc.basilisk.cloud',
Basilisk_UK = 'wss://basilisk-mainnet-rpc-07.basilisk.cloud',
Expand All @@ -25,40 +26,49 @@ export abstract class PolkadotExecutor {
}

async run() {
try {
const provider = new WsProvider(this.apiUrl);
const api = new ApiPromise({
provider: provider,
});
return new Promise((resolve, reject) => {
try {
const provider = new WsProvider(this.apiUrl);
const api = new ApiPromise({
provider: provider,
});

api
.on('connected', () => console.log('API connected'))
.on('disconnected', () => console.log('API disconnected'))
.on('error', () => console.log('API error'))
.on('ready', () => {
console.log('API ready');
console.log('Running script...');
console.log(this.desc);
console.time('Execution time:');
this.script(api)
.then((output: any) => {
if (this.pretty) {
console.log(output ? JSON.stringify(output, null, 2) : '');
} else {
console.log(output);
}
return null;
})
.catch((e) => console.log(e))
.finally(() => {
console.timeEnd('Execution time:');
api.disconnect();
});
});
} catch (error) {
console.log(error);
}
api
.on('connected', () => console.log('API connected'))
.on('disconnected', () => console.log('API disconnected'))
.on('error', () => console.log('API error'))
.on('ready', () => {
const {specName, specVersion} = api.consts.system.version;
console.log(`Runtime ready ${specName}/${specVersion}`);
console.log('Running script...');
console.log(this.desc);
console.time('Execution time:');
this.script(api)
.then((output: any) => {
if (this.pretty) {
console.log(output ? JSON.stringify(output, null, 2) : '');
} else {
console.log(output);
}
return null;
})
.catch((e) => {
console.log(e)
reject(e);
})
.finally(() => {
console.timeEnd('Execution time:');
api.disconnect();
resolve();
});
});
} catch (error) {
console.log(error);
reject(error);
}
});
}


abstract script(api: ApiPromise): Promise<any>;
}

0 comments on commit 3c69177

Please sign in to comment.