4

Documentation for getBalance doesn't seem very clear to me. I would like to know the balance but want to know how many confirmations does the balance returned have.

I was assuming that passing the "pending" parameter would include the balance from all transactions (even the ones with no confirmations or very few), but if this is true, how to do the opposite? I want to know the balance with a high number of confirmations, and the alternative params to pass (instead of "pending") are "latest" and "earliest", whose documented meaning doesn't seem clear to me:

"earliest" - for the earliest/genesis block
"latest" - for the latest mined block
"pending" - for the pending state/transactions

Any ideas? Thanks!

5

getBalance With A Block Number

You can pass in the block number as part of the parameter.

Here is Kraken's address: 0x2910543af39aba0cd09dbb2d50200b3e800a63d2.

Let's check what the latest block number is:

> eth.blockNumber
1456129

Let's get the latest balance for Kraken:

> eth.getBalance("0x2910543af39aba0cd09dbb2d50200b3e800a63d2", "latest")
4.46304933021672757555818e+23

Let's get the balance at block 1456109:

> eth.getBalance("0x2910543af39aba0cd09dbb2d50200b3e800a63d2", 1456109)
4.46511558309272757555818e+23

Let's get the balance at block 145110:

> eth.getBalance("0x2910543af39aba0cd09dbb2d50200b3e800a63d2", 1456110)
4.46304933021672757555818e+23


Confirmations

The number of confirmations is just the current block number minus the block number you specified for the getBalance(...) call.

In this example, the balance at block 1456109 had 10 confirmations (= 1456129 - 1456109).


Pending

The balance with the "pending" parameter will include any transactions that are pending, i.e., broadcasted onto the peer-to-peer network, but not included into any mined blocks yet.


Latest

The balance with the "latest" parameter will be the balance as of the latest block, which is the block number reported by eth.blockNumber.

  • I had no idea. Thats awesome! – Aakil Fernandes May 4 '16 at 16:17
  • thanks, that's much more clear now! if someone could improve the docs then, it would be awesome – knocte May 5 '16 at 2:00
  • 1
    Thanks, this is helpful, but beware that checking only one block is not secure. Ideally we want to know how long the balance has been static. If we want 10 confirmations, then we should check all of the last 10 blocks, to ensure there hasn't been a withdrawal or a deposit during that time. (BTW thanks for your testing token!) – joeytwiddle Jun 8 '18 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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