Get balance of specific Address

Hello again :slight_smile:

I’m interested in making a little script that gets the actual balance of a Cardano wallet, by their address.

I’ve achieved to get transactions the following command:

cardano-cli query utxo --address ${ADDRESS} --mainnet

  TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
ADDRESS                              4               1500000 lovelace

As of today, I’ve only made one transaction. I sent 1.5 ADA to this account. So getting the balance it’s easy.

But what about if this transactions ends up getting up to thousands of transactions. Is this the only method of getting the balance? By adding/deducting in/out transactions and get the final add-up of all the math done.

Or is there an easier command-line tool to get the full transaction of an address?

Thanks in advance.

3 Likes

Hey there :grinning:

Hopefully this isn’t too late or will help someone else.

I am doing something similar and this is what I used

text="$(cardano-cli query utxo --address $address --mainnet | grep lovelace)"
IFS=’ ’
read -A strarr <<< “$text”
txhash="$strarr[1]"
txix="$strarr[2]"
funds="$strarr[3]"
echo $txhash
echo $txix
echo $funds

Basically grep the output and save it as a variable. Then use IFS and read array to split the text. I’m using zsh so the read command needs a capital A. Here’s the link for reference.
https://linuxhint.com/bash_split_examples/

1 Like

Hi guys,

I use the following one-liner :slight_smile:

cardano-cli query utxo --mainnet --address $(cat /path/to/address ) | awk ‘{sum+= $3} END {print sum}’

Good luck!

1 Like

Can’t help you out with the cardano cli command.

What you can do instead is to use an api, to retrieve the sender.
https://mainnet.cutymals.com/odata/TransactionsOut?txHashInHex=3c69696d5edabb442f46758b5100839e78821051acb65a49ea2241652a3a614b&includeTxSender=true&includeTxReceiver=false&unspentOnly=false&X-API-KEY=ILoveCutyMals&%24orderby=id%20desc
Query Commands Explained:

  • txHashInHex=3c69696d5edabb442f46758b5100839e78821051acb65a49ea2241652a3a614b (the TX we want to check)
  • includeTxSender=true (only return the inputs of the TX (e.g. the sender))
  • includeTxReceiver=false (ignore the tx outputs of the TX (e.g. the receiver))
  • unspentOnly=false (Show already spent and spendable outputs)
  • X-API-KEY=ILoveCutyMals (the free API key used to get access)
  • %24orderby=id%20desc [$orderby=id desc] (show latest entries first)

Returns

 "value": [
    {
      "Id": 62717468,
      "TxId": 26803587,
      "Index": 1,
      "Address": "addr1qyq07yzzx9qjsv0kfqcqc5ks7h8zayadntgy0ygucaeqasddeumsjgy2ls3uh7hxnenwvuhvgjhxvf28a3fv0a808ksqpd8yfr",
      "AddressRaw": "0100ff104231412831f648300c52d0f5ce2e93ad9ad047911cc7720ec1adcf3709208afc23cbfae69e66e672ec44ae662547ec52c7f4ef3da0",
      "PaymentCred": "00ff104231412831f648300c52d0f5ce2e93ad9ad047911cc7720ec1",
      "StakeAddressId": 2818185,
      "Value": 8788601,
      "AddressHasScript": false,
      "DataHash": null
    }
  ]

If you’re instead interested in the whole lovelace available for the address you can use the UnspentTransactionsOutput endpoint.

Either check the address balance:

https://mainnet.cutymals.com/odata/UnspentTransactionsOutput?address=addr1q9s5nqzv2yd93cqd6x94qtjg0yp654uzwnd33g0wg2kd9pscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswa3mh7&X-API-KEY=ILoveCutyMals

"value": [
    {
      "Id": 62720388,
      "TxId": 26804779,
      "Index": 0,
      "Address": "addr1q9s5nqzv2yd93cqd6x94qtjg0yp654uzwnd33g0wg2kd9pscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswa3mh7",
      "AddressRaw": "016149804c511a58e00dd18b502e487903aa578274db18a1ee42acd286184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "6149804c511a58e00dd18b502e487903aa578274db18a1ee42acd286",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    }
  ]

Or check all addresses linked by their stake key (e.g. whole Wallet):
https://mainnet.cutymals.com/odata/UnspentTransactionsOutput?stakeAddress=stake1uyvys5tfdcqcsww467pgv4mxwjrng9qw504s7urgdtskzmcxrh4t4&X-API-KEY=ILoveCutyMals

  "value": [
    {
      "Id": 62720004,
      "TxId": 26804611,
      "Index": 4,
      "Address": "addr1qxy2088dsn2sswk2796dhvmddhvhyqerwhrqt50406n4mjscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsyk3vxf",
      "AddressRaw": "0188a79ced84d5083acaf174dbb36d6dd972032375c605d1f57ea75dca184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "88a79ced84d5083acaf174dbb36d6dd972032375c605d1f57ea75dca",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62720388,
      "TxId": 26804779,
      "Index": 0,
      "Address": "addr1q9s5nqzv2yd93cqd6x94qtjg0yp654uzwnd33g0wg2kd9pscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswa3mh7",
      "AddressRaw": "016149804c511a58e00dd18b502e487903aa578274db18a1ee42acd286184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "6149804c511a58e00dd18b502e487903aa578274db18a1ee42acd286",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62720984,
      "TxId": 26804994,
      "Index": 9,
      "Address": "addr1q8th4hl5d3res6t4m6s8apy3hxqr6s66k44ukc4ujxf98qqcfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswjrj28",
      "AddressRaw": "01d77adff46c47986975dea07e8491b9803d435ab56bcb62bc91925380184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "d77adff46c47986975dea07e8491b9803d435ab56bcb62bc91925380",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62721151,
      "TxId": 26805066,
      "Index": 4,
      "Address": "addr1qxm9tgpcrph3kv880svcajzg9yddt7q2vmuzdtjqxw4sltscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hs9838ec",
      "AddressRaw": "01b655a038186f1b30e77c198ec848291ad5f80a66f826ae4033ab0fae184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "b655a038186f1b30e77c198ec848291ad5f80a66f826ae4033ab0fae",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62721736,
      "TxId": 26805294,
      "Index": 9,
      "Address": "addr1qx62j3sckqkw8g6c5k470w28cfmwu60atq8mmjz0knprsqqcfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsz5762s",
      "AddressRaw": "01b4a94618b02ce3a358a5abe7b947c276ee69fd580fbdc84fb4c23800184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "b4a94618b02ce3a358a5abe7b947c276ee69fd580fbdc84fb4c23800",
      "StakeAddressId": 4244596,
      "Value": 1500000,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62721910,
      "TxId": 26805376,
      "Index": 1,
      "Address": "addr1q93gf2rs3ka8zgme5nmxeslu9gu2sv6nlwn0kdjfvcfdwgccfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswuma05",
      "AddressRaw": "016284a8708dba712379a4f66cc3fc2a38a83353fba6fb36496612d723184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "6284a8708dba712379a4f66cc3fc2a38a83353fba6fb36496612d723",
      "StakeAddressId": 4244596,
      "Value": 2210806,
      "AddressHasScript": false,
      "DataHash": null
    },
    {
      "Id": 62722204,
      "TxId": 26805487,
      "Index": 1,
      "Address": "addr1q8rtu4zghrcjaeuu9j2cgyjzmxguc3m8fgtljrzphdujxnscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsagk4qh",
      "AddressRaw": "01c6be5448b8f12ee79c2c95841242d991cc47674a17f90c41bb79234e184851696e018839d5d782865766748734140ea3eb0f70686ae1616f",
      "PaymentCred": "c6be5448b8f12ee79c2c95841242d991cc47674a17f90c41bb79234e",
      "StakeAddressId": 4244596,
      "Value": 2317603,
      "AddressHasScript": false,
      "DataHash": null
    }
  ]

You now have all Unspent Transactions which exist for that stake key at the moment 2021-12-27.
If you SUM up all Values, you have the current total balance of the stake key (e.g. the holders wallet)

You can simplify the counting, when you trim the results.
Select only those values which you really need for processing:

https://mainnet.cutymals.com/odata/UnspentTransactionsOutput?stakeAddress=stake1uyvys5tfdcqcsww467pgv4mxwjrng9qw504s7urgdtskzmcxrh4t4&X-API-KEY=ILoveCutyMals&%24select=Address%2CValue

Query Commands Explained:

  • %24select=Address%2CValue [$select=Address,Value] (Only return those two selected)
  • X-API-KEY=ILoveCutyMals (the free API key used to get access)
"value": [
    {
      "Address": "addr1q93gf2rs3ka8zgme5nmxeslu9gu2sv6nlwn0kdjfvcfdwgccfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswuma05",
      "Value": 2210806
    },
    {
      "Address": "addr1q9e27j89rjmks2ec93u9hrqsezyjcza9ay4ql3we7w890rscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsm24s2e",
      "Value": 1500000
    },
    {
      "Address": "addr1qx58k8c6amwqkvukrpmxqgg53sh20798tn6nn4rgyqxaerccfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsvyddr3",
      "Value": 1500000
    },
    {
      "Address": "addr1q8p4a8e2tska3przykf2pjj39hnl9arzg35hjmrgtgemmlqcfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsgsju9s",
      "Value": 1500000
    },
    {
      "Address": "addr1q8ahg2jz78r6gzxxtmk0lmrskndhc8a5stg8r5h4j2ueghgcfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hsmyvclu",
      "Value": 2741261
    }
  ]

If you count the entries you get to the conclusion that the Stake key holds 9.452.067 ADA at the moment (2021-12-27).

If you know only the address and not the stake key, you can derive the stake key from the address using the following query:
https://mainnet.cutymals.com/api/CardanoTools/GetStakeAddressFromAddress/addr1q9s5nqzv2yd93cqd6x94qtjg0yp654uzwnd33g0wg2kd9pscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswa3mh7?X-API-KEY=ILoveCutyMals

{
  "address": "addr1q9s5nqzv2yd93cqd6x94qtjg0yp654uzwnd33g0wg2kd9pscfpgkjmsp3quat4uzsetkvay8xs2qagltpacxs6hpv9hswa3mh7",
  "stakeAddress": "stake1uyvys5tfdcqcsww467pgv4mxwjrng9qw504s7urgdtskzmcxrh4t4"
}

Let me know when you have further questions!

Feel free to try out more endpoints of the API:
https://mainnet.cutymals.com/swagger/index.html