ReasonJun

Solidity : mapping 본문

Blockchain/Solidity

Solidity : mapping

ReasonJun 2023. 10. 16. 16:34
728x90

A mapping in Solidity is a data structure that stores key-value pairs. The key can be of any built-in data type, except reference types, and the value can be of any type, including mappings, arrays, and structs.

 

Mappings are similar to dictionaries in other programming languages. They are often used to store data that needs to be accessed quickly and easily, such as user accounts, balances, and asset ownership.

 

To create a mapping, you use the mapping keyword followed by the type of the key and the type of the value. For example, the following code creates a mapping from addresses to balances:

mapping(address => uint256) public balances;

Once you have created a mapping, you can add and retrieve key-value pairs using the [] operator. For example, the following code adds 100 ETH to the balance of the account at address 0x1234567890abcdef1234567890abcdef123456:

balances[0x1234567890abcdef1234567890abcdef123456] += 100;

To retrieve the balance of an account, you simply use the [] operator to index the mapping with the account's address. For example, the following code retrieves the balance of the account at address 0x1234567890abcdef1234567890abcdef123456:

uint256 balance = balances[0x1234567890abcdef1234567890abcdef123456];

Mappings are a powerful data structure that can be used to store and retrieve data quickly and easily. They are often used in Solidity to store data that needs to be accessed frequently, such as user accounts, balances, and asset ownership.

 

Here are some additional things to keep in mind about mappings in Solidity:

  • Mappings can be nested. This means that you can create a mapping of mappings, such as a mapping of addresses to mappings of balances.
  • Mappings can be used as function parameters and return values.
  • Mappings cannot be stored in storage. Instead, they are stored in memory.
  • Mappings are initialized to empty values. This means that you do not need to explicitly initialize a mapping before using it.

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// Special reference type that can only be used in the storage data area
// Can only be declared as a state variable or storage reference type

// HashTable > Mapping is storing the value corresponding to the key value.
// When declaring HashTable, you must declare the types of keys and values.

contract Ex_1{
    
    mapping(address => uint) public balances;

    function addMapping(address _key, uint _amount) public {
        balances[_key] = _amount;
    }

    function getMapping(address _key) public view returns(uint) {
        return balances[_key];
    }

    function deleteMapping(address _key) public {
        delete(balances[_key]);
    }

       function deleteMapping2(address _key) public {
        balances[_key] = 0;
    }
    
    mapping(address => mapping(address => bool)) public isFriend;

    function example() external {
        balances[msg.sender] = 123;
        uint bal = balances[msg.sender];
        uint bal2 = balances[address(1)]; // 0

        balances[msg.sender] += 456; // 123 + 456

        // delete balances[msg.sender]; // 0

        isFriend[msg.sender] [address(this)] = true;


    }

}
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : array example  (0) 2023.10.16
Solidity : Difference between memory and calldata  (0) 2023.10.16
Solidity : keccak256  (0) 2023.10.16
Solidity : event / emit  (0) 2023.10.16
Solidity : function / Parameter / wei / ether / gas  (0) 2023.10.16
Comments