Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts

Mapping in Solidity allows for efficient storage of key-value pairs in smart contracts.

Mapping in Solidity is a key-value data structure that is used to store information within a smart contract. It is similar to JavaScript objects or dictionaries …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Shlok Kumar

Mapping in Solidity allows for efficient storage of key-value pairs in smart contracts.

Mapping in Solidity is a key-value data structure that is used to store information within a smart contract. It is similar to JavaScript objects or dictionaries in other programming languages. Mapping allows for the storage of values, where each value is associated with a unique key, making it easier to access and retrieve information stored within the smart contract.

Mapping in Solidity can be declared as a global state variable, and the key and value data types can be any elementary data type, including addresses and user-defined data types. The size of the mapping can be dynamic, allowing it to grow or shrink as the need arises. Mapping values can be accessed and updated directly, providing a flexible and efficient way to manage the data stored within a smart contract.

Additionally, mapping values can be iterated over, making it possible to retrieve information about all of the keys and values stored in the mapping. Overall, mapping is a powerful and versatile tool for managing data within a Solidity smart contract, and is a key component for many decentralized applications.

Mapping is like a hash table or a dictionary in any other language when you use it in Solidity . A key can be one of any of the built-in types, but reference types aren't allowed. The value can be any type, too. Mappings are mostly used to link an Ethereum address to a value type.

 

Syntax:

mapping(key => value) <access specifier> <name>; 

Creating a Mapping:

Mapping is defined as any other variable type, which accepts a key type and a value type.

Example 1

// Solidity program to 
// demonstrate mapping
pragma solidity ^0.8.0; 
   
// Defining contract 
contract mapping_example {
      
    //Defining structure
    struct student 
    {
        // Declaring different 
        // structure elements
        string name;
        string subject;
        uint8 marks;
    }


      

 

Example 2: Mapping and nested mapping

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

contract Mapping {
    // Mapping from address to uint
    mapping(address => uint) public myMap;

    function get(address addr) public 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];
    }
}

contract NestedMapping {
    // Nested mapping (mapping from address to another mapping)
    mapping(address => mapping(uint => bool)) public nested;

    function get(address addr1, uint i) public 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];
    }
} 

For more content, follow me on - https://linktr.ee/shlokkumar2303


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Shlok Kumar


Print Share Comment Cite Upload Translate Updates
APA

Shlok Kumar | Sciencx (2023-02-03T16:14:40+00:00) Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts. Retrieved from https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/

MLA
" » Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts." Shlok Kumar | Sciencx - Friday February 3, 2023, https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/
HARVARD
Shlok Kumar | Sciencx Friday February 3, 2023 » Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts., viewed ,<https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/>
VANCOUVER
Shlok Kumar | Sciencx - » Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/
CHICAGO
" » Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts." Shlok Kumar | Sciencx - Accessed . https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/
IEEE
" » Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts." Shlok Kumar | Sciencx [Online]. Available: https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/. [Accessed: ]
rf:citation
» Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts | Shlok Kumar | Sciencx | https://www.scien.cx/2023/02/03/unleashing-the-power-of-mapping-in-solidity-simplifying-complexity-in-ethereum-contracts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.