How does the eth.getBalance() function work? Which file does it call exactly? How and where all parameters are passed? Please someone explain it to me.

2 Answers 2


You can clone the repository and perform git grep getBalance and then git grep GetBalance.

You'll soon fall in the file https://github.com/ethereum/go-ethereum/blob/c8695fae359aa327da9203a57ffaf4f2d47d4370/core/state/statedb.go#L245-L251

Then you'd do git grep ") Balance", only to reach


Now, you really need to read the files.

// empty returns whether the account is considered empty.
func (s *StateObject) empty() bool {
    return s.data.Nonce == 0 && s.data.Balance.BitLen() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)

// Account is the Ethereum consensus representation of accounts.
// These objects are stored in the main account trie.
type Account struct {
    Nonce    uint64
    Balance  *big.Int
    Root     common.Hash // merkle root of the storage trie
    CodeHash []byte

From here, we can deduce that is just a variable in memory, so somebody must have to fill it sometime!

Well. I'll leave you the rest as exercise. You need to figure out who is instantiating an Account, when the state of this account is set, and where is the persistence (HINT: DB Functions).


Here's a different kind of answer.

When a dapp calls web3.eth.getBalance(), web3 prepares and sends a JSON-RPC request to the provider. If you're connected over IPC (this is the usual if you're using geth attach), then it writes to a file called geth.ipc, which geth then reads. If it's over the standard RPC (i.e. from a browser), web3 sends a HTTP POST to the node (usually at http://localhost:8545).

The end result is then calculated however the node does it (different nodes do it differently) and then the answer is sent back. web3.js decodes it and returns with a normal javascript object (in this case, a BigNumber).

What is the form of this request and reply? It's basically plain JSON. There's no reason it has to be web3.js. You can use curl (over HTTP) for the same effect. For example

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xe7466f6aa35b04ac950274833dc9c43ef8e05d33", "latest"],"id":1}' localhost:8545
# returns
# {"id":1,"jsonrpc":"2.0","result":"0x00000000000000056bc75e2d63100000"}

This was using testrpc, which stores all of its data in memory, not on the disk. So the answer is partially, "It depends."

  • you didn't tell about files which will be used?
    – sandeep
    Feb 24, 2017 at 5:40
  • Like I said, it depends on the client. testrpc does not store its chain in files at all, as far as I know. Feb 24, 2017 at 18:29

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.