How to query balance by address?

I am developing an applicaiton on android which shows the current balance of chia wallet by address. I know that you can get it by using get_coin_records_by_puzzle_hash post method, but I am getting empty coin_recods from ths api https://chia.blockchain-list.store/full-node/get_coin_records_by_puzzle_hash. Help me please, what I am doing wrong?

try https://developers.chia.net/ :wink:

Maybe someone here will know, but dev forum should be better for you. Otherwise Keybase #dev channel

To query the balance of an address, you should use get_coin_records_by_puzzle_hash from the Full Node API.

First, you’ll need to convert the address to a puzzle hash. Once you have the puzzle hash, you can query the coin records that belong to that address.

curl --silent --insecure \
  --cert ~/.chia/mainnet/config/ssl/full_node/private_full_node.crt \
  --key ~/.chia/mainnet/config/ssl/full_node/private_full_node.key \
  -d '{"puzzle_hash":"0x2f2c9ba1b2315d413a92b5f034fa03282ccba1767fd9ae7b14d942b969ed5d57", "include_spent_coins": false}' \
  -H "Content-Type: application/json" \
  -X POST https://localhost:8555/get_coin_records_by_puzzle_hash

This returns a list of all unspent coins. You can add up the value of the unspent coins to get the address total balance.

Using jq with the previous result:

cat /tmp/coin_records.json | jq '[.coin_records[].coin.amount] | add'

This will give you the balance in mojos (e.g. 869807645798290), which can be converted to XCH.

For this to work, your full node must be synced.

2 Likes