4

It's not clear in the documentation, but this function works if the 2nd parameter is not passed. What's the default if not passed?

1

Behavior that might not be supported across clients

The default is to use the latest block, which means that eth_getBalance will return the balance of the account according to the most recently mined block that the client/node has. The latest block is equivalent to eth_blocknumber.

Behavior according to JSON-RPC spec

You are correct that per the JSON-RPC spec, the second parameter is required. Technically all clients should implement the spec, but some clients are not 100% compliant to the spec (and bugs should be filed). When using eth_getBalance it is advisable to call it according to the JSON-RPC spec, since there are no guarantees about all clients being able to handle the single parameter. The single parameter request may work with EthereumJ, but might not work Geth or cpp-ethereum.

Contrast this with web3.eth.getBalance, where the second parameter is indeed optional.

1

At least for the EthereumJ (i.e. Java) implementation there are 2 versions of the function: one that takes the two arguments mentioned in the documentation, and one that takes only the first of the two arguments (i.e. the address string).

If you pass just the address string, the single-argument version is invoked.

The Java implementation is below. It shows the second argument is defaulted to "latest".

See JsonRpcImpl.java for the code.

   public String eth_getBalance(String address) throws Exception {
        String s = null;
        try {
            return s = eth_getBalance(address, "latest");
        } finally {
            if (logger.isDebugEnabled()) logger.debug("eth_getBalance(" + address + "): " + s);
        }
    }

Edit: See Eth's answer for advice on other clients.

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.