Last active
November 20, 2024 18:28
-
-
Save jak-pan/91d5e9cba4550c73c6fdc0708e61e427 to your computer and use it in GitHub Desktop.
Example script
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
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