mappings

https://medium.com/coinmonks/solidity-tutorial-all-about-mappings-29a12269ee14

  • key-value pair

  • a mapping is a HashMap (JS) -> no index -> similar to dictionary in python

  • no indexing/order -> myMapping[1] will not return the "first book"

  • mappings don't have a length

Mappings are accessed like arrays, but there are no index-out-of-bounds-exceptions. All possible key/value pairs are already initialized.

Initialization & Assignment

On Initialization, all possible key/value pairs are already initialized. You can simply access any key and get back "false" or 0 - whichever the default value is for the value type.

This is because the hash that points to the location in storage has not been generated & we have not written a value thus far.

Assignment of values must be done inside a function

  • cannot simply do myMapping[1] = "test" (like python)

Example

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract SimpleMappingExample {

    mapping(uint => bool) public myMapping;
    mapping(address => bool) public myAddressMapping;

    function setValue(uint _index) public {
        myMapping[_index] = true;
    }

    function setMyAddressToTrue() public {
        myAddressMapping[msg.sender] = true;
    }
}

  1. On initialization, myMapping[1] will return the default bool value of false.

  2. Then we call setValue setting -> myMapping[1] = true

  3. Subsequently, when we call myMapping[1], we are returned true as set earlier.

Whitelisting

If an address is allowed to do a certain action in our Smart Contract then we can white-list it. To achieve whitelisting, we can map addresses to boolean values.

On initialization, any address passed into the mapping would return false: myAddressMapping

We can whitelist using a function variation on the example: setMyAddressToTrue()

Nested Mapping

For the nested mapping above, the outer mapping maps a host of integers to a set of mappings.

  • outermapping[1] -> innermapping_1

  • outermapping[2] -> innermapping_2[?] -> value?

Each key leads to a different inner mapping. As such, you would need a 2nd parameter (_key2) for the inner mapping.

Key and value types allowed

Not every data type can be used as a key -> struct and mapping cannot be used as keys.

Solidity does not limit the data type for values. It can be anything, including struct and mapping.

Example: tracking balances and withdrawals

Our accounting ledger is the mapping balanceReceived.

Iterable mappings can be implemented using libraries

Last updated