Skip to content

Instantly share code, notes, and snippets.

@knopki
Last active May 25, 2023 02:08
Show Gist options
  • Select an option

  • Save knopki/91af33a8455dca54986c8b7a4601b31c to your computer and use it in GitHub Desktop.

Select an option

Save knopki/91af33a8455dca54986c8b7a4601b31c to your computer and use it in GitHub Desktop.
Genshiro Get Balances
const MASTER_ADDR = '<insert your address here>';
// go here https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fnode.ksm.genshiro.io%2F#/js
// paste && click on 'play'
const DECIMALS = 9;
function hex2a(hexx) {
const hex = hexx.toString();
let str = '';
for (let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16))
};
return str;
}
async function getSubAccount(addr, subtype) {
return (await api.query.subaccounts.subaccount(addr, subtype)).toJSON()
}
const assetsMap = new Map(
(await api.query.eqAssets.assets()).toJSON().map(asset => (
[asset.id, hex2a(asset.id.toString(16))]
))
);
for (const accType of ["Master", "Bailsman", "Trader", "Borrower"]) {
const addr = accType == "Master" ? MASTER_ADDR : (await api.query.subaccounts.subaccount(MASTER_ADDR, accType)).toJSON();
if (addr === null) continue;
console.log(`${accType} balances:\n`);
const balances = (await api.query.system.account(addr)).toJSON().data.v0.balance;
for (const [assetId, balance] of balances) {
const assetSymbol = assetsMap.get(assetId);
const assetBalance = ((balance?.positive || 0) - (balance?.negative || 0)) / 10**DECIMALS;
console.log(`${assetSymbol.padStart(5, ' ')}:\t${assetBalance}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment