9

Which api/rpc call and method do I need to use to extract the state of all balances at a certain block?

Let's use Polkadot and recent block as an example: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fpolkadot.api.onfinality.io%2Fpublic-ws#/rpc

2 Answers 2

10

First, set up a Polkadot JS API connection for the block you want:

const provider = new WsProvider(endpoint);
const api = await ApiPromise.create({ provider });
const blockHash = await api.rpc.chain.getBlockHash(blockNumber);
const api_at = await api.at(blockHash);

Then, you can easily get a list of all the account entries. I recommend using entriesPaged, else the query may be too overwhelming for the RPC endpoint:

let lastKey = "";
let pageSize = 100;
let all_accounts = await api_at.query.system.account.entriesPaged(
    {
        args: [],
        pageSize: pageSize,
        startKey: lastKey,
    }
);

Then you can query those accounts for their balance information:

for (account of all_accounts) {
    // `account` will be an array of two items: key and value.
    // We know that the last 32 bytes of the storage key is the address of the account.
    let address = encodeAddress(account[0].slice(-32));

    // We can then extract the user's balance information.
    let free = account[1].data.free;
    let reserved = account[1].data.reserved;

    // Update the last key.
    lastKey = account[0].toString();
}

Then repeat this process until you have queried all the accounts.

You can find an example of this here: https://github.com/shawntabrizi/substrate-balance-snapshot

3

If you want to use the Subscan API you may query their accounts list request url to extract the state of all balances at the latest block.

I created this script to query all the accounts at the latest block using the Subscan API and store them in a file. If you wanted all the Polkadot accounts on its latest block you would get a Subscan API Key, switch to a recent version of Node.js, and run the following, and then wait for it to fetch all the data and store it in a file that is generated in the data/ subfolder:

git clone https://github.com/DataHighway-DHX/open-polkadot-js-library
cd subscan
yarn
SUBSCAN_API_KEY=<INSERT_SUBSCAN_API_KEY> ENDPOINT=polkadot.api.subscan.io node index.js 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.