0

How does one fetch the balance and nonce for a given address without using the polkadot.js client?

This is how you do it with the client but I can't seem to find the rpc methods or tutorials to do it without..

const Alice = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
let { data: { free: previousFree }, nonce: previousNonce } = await api.query.system.account(Alice);

console.log(`${Alice} has a balance of ${previousFree}, nonce ${previousNonce}`);

1 Answer 1

1

There are many ways to read these values, so you can adjust approach to your use-case.

1. Read from UI

Go to Developer tab, then Chain state and Storage. On this page you should query about system: account with Alice as parameter (see the screenshot below). As a response you will get a small object describing account data, including nonce and free balances. enter image description here

2. With json RPC call

You can use curl command like:

curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "state_getStorage", "params":["0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"]}' localhost:9933

Two things here:

  • params array should contain storage key, which is a long hex string, which you can compute on your own or read from e.g. UI (see the "encoded storage key" entry in the screenshot above). Basically, it consists of three pieces concatenated together: first two encode where the corresponding storage map for all accounts resides. The last one is the target account ID hashed with Blake2_128Concat algorithm.
  • change localhost:9933 to your RPC port

The problem is that you will get encoded data here:

{"jsonrpc":"2.0","result":"0x000000000000000001000000000000000000703b1bd2aa400300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","id":1}

This should decode to the same structure you get in the previous step, but for that you will have to use e.g. parity scale codec library.

3. Use library

You can check one of these libraries for interacting with Substrate-based chain:

Note: Depending how you configured frame_system::Config::AccountData for you runtime, these values might be stored somewhere else / in other form. The steps I presented are applicable for pallet_balances::AccountData<Balance>.

3
  • I get result null when running your curl example with rpc node: rpc.polkadot.io
    – 124141251
    Jun 14, 2022 at 12:11
  • @124141251 this is probably because //Alice does not 'exist` on polkadot.io. Try with some account that has some history / balances Jun 14, 2022 at 13:25
  • I think its due to the Blake2 algorithm. I can't find an implementation for dart lang.
    – 124141251
    Jun 14, 2022 at 13:57

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.