Skip to content

Instantly share code, notes, and snippets.

@jak-pan
Last active November 20, 2024 18:28
Show Gist options
  • Save jak-pan/91d5e9cba4550c73c6fdc0708e61e427 to your computer and use it in GitHub Desktop.
Save jak-pan/91d5e9cba4550c73c6fdc0708e61e427 to your computer and use it in GitHub Desktop.
Example script
async subscribeTokenBalance(
address: string,
assets: Asset[],
onChange: (balances: [string, BigNumber][]) => void
): UnsubscribePromise {
const supported = assets
.filter((a) => a.type !== 'Erc20')
.filter((a) => a.id !== SYSTEM_ASSET_ID);
const callArgs = supported.map((a) => [address, a.id]);
return this.api.query.tokens.accounts.multi(callArgs, (balances) => {
console.log('===========', new Date().toLocaleString(), '===========');
const result: [string, BigNumber][] = [];
balances.forEach((data, i) => {
const freeBalance = this.calculateFreeBalance(data);
const token = callArgs[i][1];
console.log(address, token, freeBalance.toString());
result.push([token, freeBalance]);
});
onChange(result);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment