Search code examples
constantsstatesolidity

Why does ERC20 balanceof() use constant?


The ERC20 token standard specifies balanceof() with a constant:

function balanceOf(address _owner) public constant returns (uint256 balance) {
    return balances[_owner];
}

But the function doesn't change the state of anything anyway. So what is the sense of constant here?


Solution

  • Functions marked with the constant modifier do 2 things.

    • Any attempts to change state in the function will not be written to the chain.
    • Calls from a client will not use gas, unless called from another non-constant function.

    Specifying this in the ERC20 standard just ensures you're following those rules. A client using the token should not have to unexpectedly pay for gas consumed just to get balance information.