Portal
Search…
Query Balance
This section shows how to connect to Clover mainnet and query the account balance

Prerequisites

Install @polkadot/api, the suggested version is ^4.15.1 (equal or above 4.15.1)

Sample code

1
const API = require("@polkadot/api")
2
const cloverTypes = require('@clover-network/node-types');
3
4
async function getBalance(address) {
5
const wsProvider = new API.WsProvider('wss://api-ivy.clover.finance');
6
const api = await API.ApiPromise.create({
7
provider: wsProvider,
8
types: cloverTypes
9
});
10
const { parentHash } = await api.rpc.chain.getHeader();
11
const {
12
data: { free: balance },
13
} = await api.query.system.account.at(parentHash, address);
14
return balance.toString();
15
}
16
17
getBalance('5DZabq7G2m12g1GUuqs66CCAVcmzdWRkXsGxpi4dgXFEcLEb').then(console.log)
Copied!
Furthermore you may use formatBalance from @polkadot/util to format the display
Last modified 3mo ago
Copy link