Skip to content
Snippets Groups Projects
Commit f5b5c1c3 authored by Bernardo Cardoso's avatar Bernardo Cardoso
Browse files

Add script to get balance of an account

parent a47fc304
No related branches found
No related tags found
No related merge requests found
const { ApiPromise, WsProvider } = require('@polkadot/api');
const readline = require('readline');
require('dotenv').config();
const ALICE = '6a6XHUGV5mu33rX1vn3iivxxUR4hwoUapsuECGyHpPzfaNtt';
/**
* Connect to an xx network node using websocket
* @return {Promise} chain api
*/
async function connect() {
const provider = new WsProvider(process.env.WS_CHAIN);
const api = await ApiPromise.create({
provider,
});
await api.isReady;
return api;
}
async function main () {
// Connect to the node
const api = await connect();
// Get account data
const data = await api.query.system.account(ALICE);
// Total balance is sum of free and reserved
const balance = data.data.free.add(data.data.reserved);
console.log(balance.toString());
// Get account data at a past block (wait 20 seconds to let some blocks be produced)
await new Promise(r => setTimeout(r, 20000));
const hash = await api.rpc.chain.getBlockHash(2);
const apiAt = await api.at(hash);
const pastdata = await apiAt.query.system.account(ALICE);
const pastBalance = pastdata.data.free.add(pastdata.data.reserved);
console.log(pastBalance.toString());
}
main().catch(console.error).finally(() => process.exit());
{
"name": "balance",
"version": "1.0.0",
"description": "Get the balance of an account",
"main": "index.js",
"repository": "github.com/xx-labs/scripts/balance",
"author": "xx labs",
"license": "MIT",
"scripts": {
"start": "WS_CHAIN='ws://localhost:63007' node index.js"
},
"dependencies": {
"@polkadot/api": "^7.15.1",
"dotenv": "^16.0.0"
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment