Query Balances
Query account balances on Avail DA using the TypeScript, Rust, or Go SDK.
Setting up the dev environment
Querying the balance of an account
system_account extrinsic from the system pallet on an Avail node.
- Inside
your-file-name.ts, add the following code:
import * as dotenv from 'dotenv';
import { SDK, Pallets } from 'avail-js-sdk';
dotenv.config();
export async function getAccountBalance() {
// Initialize SDK with Turing endpoint
const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
// Address to check balance for
const address = "5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk";
console.log(`Checking balance for address: ${address}`);
// Get the current block hash
const blockHash = await sdk.client.bestBlockHash();
// Get storage at the current block
const storageAt = await sdk.client.storageAt(blockHash);
// Fetch account information
const accountInfo = await Pallets.SystemStorage.Account.fetch(storageAt, address);
// Format balances in AVAIL (dividing by 10^18)
const free = accountInfo.value.accountData.free.toString();
const reserved = accountInfo.value.accountData.reserved.toString();
const frozen = accountInfo.value.accountData.frozen.toString();
console.log("The following balances are in the smallest units, divide by 10^18 to get the balance in AVAIL");
console.log(`Free Balance: ${free}`);
console.log(`Reserved Balance: ${reserved}`);
console.log(`Frozen Balance: ${frozen}`);
console.log("Account information fetched successfully");
process.exit(0);
}
// Execute the function
getAccountBalance();- Run the code using:
ts-node your-file-name.tsSample Response:
{
nonce: '109',
consumers: '2',
providers: '1',
sufficients: '0',
data: {
free: '1,000,000,000,000',
reserved: '100,000,300,000,000,000,000',
frozen: '100,000,000,000,000,000,000',
flags: '170,141,183,460,469,231,731,687,303,715,884,105,728'
}
}
Free balance: 28.3046577073 AVAIL
Fetch account information using a simple curl request via the Subscan API
- The Subscan API can be used to make API queries to the Subscan indexer to make various kinds of queries.
- Subscan supports a variety of Substrate-based chains, including Avail DA mainnet and the Turing testnet.
- You can either use their publicly available endpoints for the Avail networks, or use a dedicated endpoint.
- The examples below use publicly available Subscan API endpoints for both networks.
- Open a terminal and run the following command:
curl --location --request POST 'https://avail.api.subscan.io/api/v2/scan/search' \
--header 'Content-Type: application/json' \
--data-raw '{
"key": "<your-avail-address>"
}'- Replace
<your-avail-address>with the address you want to query.
Sample Response:
{
"code": 0,
"message": "Success",
"generated_at": 1741796675,
"data": {
"account": {
"address": "5HbUMBK8SBKH22qrm2sRDtWNtfyT331tUHpKkFMUKyirkJ6G",
"balance": "169389.0977233184983879",
"lock": "168914.456888072538284769",
"balance_lock": "168914.456888072538284769",
"is_evm_contract": false,
"account_display": {
"address": "5HbUMBK8SBKH22qrm2sRDtWNtfyT331tUHpKkFMUKyirkJ6G"
},
"substrate_account": null,
"evm_account": "",
"registrar_info": null,
"count_extrinsic": 0,
"nft_amount": "0",
"extra": null,
"display": "",
"web": "",
"riot": "",
"email": "",
"legal": "",
"twitter": "",
"github": "",
"matrix": "",
"discord": "",
"judgements": null,
"reserved": "13000000000000000000",
"bonded": "168914456888072538284769",
"unbonding": "0",
"democracy_lock": "0",
"conviction_lock": "0",
"election_lock": "0",
"staking_info": null,
"nonce": 0,
"role": "validator",
"stash": "5HbUMBK8SBKH22qrm2sRDtWNtfyT331tUHpKkFMUKyirkJ6G",
"is_council_member": false,
"is_techcomm_member": false,
"is_registrar": false,
"is_fellowship_member": false,
"is_module_account": false,
"assets_tag": null,
"is_erc20": false,
"is_erc721": false,
"vesting": null,
"proxy": {
"proxy_account": [
{
"account_display": {
"address": "5HmgLcTCi2trnz4AeEiWs4ngAjALu7o4mFnpKdMBnA8jhFnq"
},
"proxy_type": "Staking"
}
]
},
"multisig": {
"multi_account_member": [
{
"address": "5DfE73hjEYs44JHsuMKzrTMBzkEb6fosaV5xSUEVNjrHwqdB"
},
{
"address": "5DtGLPULvxXoSPvVv2YZdWmCJy8eEYnWj7MwPpdwGgDnFiua"
},
{
"address": "5DyHKiRY2ssTWU4CtbbEPoYFmNKaN2X39KRcgb33fnZqk2Yd"
}
],
"threshold": 2
},
"delegate": null
}
}
}How is this guide?