Mapping And Structure In Solidity

Arrays are comparable to mapping. In Solidity, mapping functions similarly to a hash table or dictionary in any other language. These are used to store data in the form of key-value pairs, where the key can be any of the built-in data types but not reference types, and the value can be any type. Mappings are commonly used to link a unique Ethereum address to the related value type. Whereas Structure allows users to create and define their own type in the form of solidity. The structure is a collection of multiple kinds, even if it cannot contain a member of its own type. The structure is a variable of the reference type that can include both value and reference types.

Example for Mapping

Mapping (address => uint)  public myMapping;

Example for Structure

Struct Payments{
Uint amount;
Uint timestamp;
}

Struct Balance{
Uint totalBalance;
Uint numPayaments;

Mapping(uint => Payments) payments;

}
Mapping(address => balance) public balanceReceived;

Mapping

  • Mapping are datatypes in solidity.
  • They don’t have a “length”.
  • Iterable mapping can be implemented through lib
  • They are accessed like arrays but they have one major advantage, all the key/value pairs are initialized with their default value.
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;
  • One that maps uint 256 to Boolean, that’s called , myMapping. Another that maps address to Boolean is called myAddressMapping.
  • We can access a mapping with the brackets[]. If we want to access the want to access the key “123” in out Mymapping, the we’d simply write myMapping[123].
  • Our mapping here are public, so solidity will automatically generate a getter function for us.
  • All possible key/value pairs are already initialized. You simply access any key and get back “false” since that’s the default Boolean value.
  • To overcome the above problem write a function setValue(uint _index), then you can initialize _index to “True”. So if the index Is zero, then myMapping [0] will be set to true.

Address Keys in Mapping

  • Address are like bank account number. You know who transacts with your smart contract and smart contract know who you are.
  • Address can be keys for arrays and mapping.
  • In this we map address to Boolean values.

In setMyAddressToTrue() function, It uses global msg.sender to obtain the sender address. So, if you're engaging with a specific address, that address will be in msg.sender in the smart contract. And then it access the myAddressMapping and sets the value to “true” for the current address.

Structure

  1. Structure helps us to create our own custom variable types.
  2. Members of the struct cannot be of the type struct.
  3. it is better to define structs than objects to reduce gas consumption.
struct People {
        uint256 favoriteNumber;
        string name;
    }   

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }
  • In the above code, is the simple example of structure where we can assign a favorite number .
  • We can even extend the code by adding some arrays so that we can access the favourite numbers of different names
contract Storage {

    uint256 favoriteNumber;

    struct People {
        uint256 favoriteNumber;
        string name;

    }
    // uint256[] public anArray;

    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {

        favoriteNumber = _favoriteNumber;

    }
    function retrieve() public view returns (uint256) {
        return favoriteNumber;

    }
    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        people.push(People(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
}
  • The above, code contains arrays which stores favourite number of different names.
  • And it is returned through retrive() function which returns the favourite number.
  • The function addPerson helps us to add data inside the array. It pushes the name and his favourite number to the array and through retrieve function we can access the data.

In this way structure and mapping can be used in an efficient manner.