#100DaysOfSolidity πŸ” Exploring the Power of Mapping in Solidity πŸ” #12

#100DaysOfSolidity Series 012 β€œMapping”

Solidity Academy
4 min readJul 2, 2023

Welcome to the 12th article of the #100DaysOfSolidity series! In this article, we will delve deep into the world of mappings in the Solidity programming language. Mappings are a fundamental data structure in Solidity, allowing developers to create powerful key-value pairs for efficient storage and retrieval of data. We will explore the syntax, properties, and usage of mappings, along with practical examples to enhance your understanding. So let’s get started and unlock the potential of mappings in Solidity! πŸš€

#100DaysOfSolidity πŸ” Exploring the Power of Mapping in Solidity πŸ”

Understanding Mappings:

In Solidity, mappings are created using the syntax `mapping(keyType => valueType)`. The `keyType` can be any built-in value type, such as `address`, `bytes`, `string`, or even another contract. On the other hand, the `valueType` can be any type, including another mapping or an array. Mappings provide an efficient way to associate values with unique keys and retrieve them later with ease.

Creating a Simple Mapping:

To begin our journey, let’s take a look at a simple mapping example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}

In this `Mapping` contract, we have defined a mapping named `myMap` that maps addresses (`address`) to unsigned integers (`uint`). The `get` function allows us to retrieve the value associated with a specific address. If the value was never set for that address, it will return the default value of `uint`, which is 0. The `set` function updates the value at a given address, and the `remove` function resets the value to the default value.

Nested Mappings:

Mappings become even more powerful when used in a nested form. Let’s explore the concept of nested mappings with an example:

contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(address _addr1, uint _i, bool _boo) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}

In the `NestedMapping` contract, we have introduced a nested mapping named `nested`. It maps addresses (`address`) to another mapping, which, in turn, maps unsigned integers (`uint`) to booleans (`bool`). This allows for a two-dimensional storage structure, enabling efficient access to values based on both the address and the integer index.

Analyzing the Smart Contracts:

Now, let’s analyze the behavior and functionality of the provided smart contracts in detail.

The `Mapping` contract:

The `Mapping` contract demonstrates a basic mapping usage scenario. It includes three functions:

1. `get`: This function takes an address `_addr` as a parameter and returns the associated unsigned integer value stored in the `myMap` mapping. If no value is set for the given address, it will return the default value of `uint`, which is 0.

2. `set`: The `set` function allows users to update the value at a specific address `_addr` in the `myMap` mapping. It takes a uint `_i` as the new value and assigns it to the corresponding address.

3. `remove`: With the `remove` function, users can reset the value at a given address `_addr` in the `myMap` mapping. It deletes the value, setting it back to the default value of `uint`, which is 0.

The `NestedMapping` contract:

The `NestedMapping` contract demonstrates the usage of nested mappings. It includes three functions:

1. `get`: This function takes an address `_addr1` and an unsigned integer `_i` as parameters and returns the boolean value associated with the nested mapping `nested`. Even if the nested mapping is not initialized, Solidity will return the default value of `bool`, which is `false`.

2. `set`: The `set` function allows users to update the boolean value in the nested mapping `nested`. It takes an address `_addr1`, an unsigned integer `_i`, and a boolean `_boo` as parameters, and assigns the boolean value to the corresponding address and integer index.

3. `remove`: The `remove` function deletes the boolean value at a specific address `_addr1` and unsigned integer `_i` in the nested mapping `nested`. The deleted value will revert to the default value of `bool`, which is `false`.

Conclusion:

In this article, we have explored the power of mappings in Solidity. We learned how to create simple mappings and how to use nested mappings for more complex data structures. Mappings provide an efficient and flexible way to store and retrieve data in Solidity contracts, making them an essential tool for smart contract development.

Remember, mappings are not iterable, so accessing their values requires specifying the exact key. Additionally, it’s important to handle default values when working with mappings to ensure correct behavior in cases where values are not explicitly set.

By mastering mappings in Solidity, you have gained a powerful tool to manage and organize data within your smart contracts. Utilize this knowledge wisely, and you’ll be well-equipped to build robust and efficient decentralized applications.

πŸ”— Additional Resources:

Keep learning, keep exploring, and keep pushing the boundaries of what you can achieve with Solidity! πŸŒŸπŸ“š

Happy coding! πŸ’»πŸ’‘

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/