4

In ERC20 standard, _allowed is defined as:

mapping (address => mapping (address => uint256)) private _allowed;

and the getter function is defined as:

function allowance(address owner, address spender) public view returns (uint256) {
        return _allowed[owner][spender];
}

I'm testing the function, and calling allowance(address1, address2) I receive 0. Why is that happening? The keys address1, and address2 were not initialized in _allowed.

I was expecting a KeyError, like in another languages, (python, etc.).

3

As per the solidity docs:

You can think of mappings as hash tables, which are virtually initialised such that every possible key exists and is mapped to a value whose byte-representation is all zeros, a type’s default value.

And regarding the default values here, it states:

A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false. The default value for the uint or int types is 0. For statically-sized arrays and bytes1 to bytes32, each individual element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, bytes and string, the default value is an empty array or string.

So you are getting default initialized value of uint256 i.e. 0.

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.