0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Solidity】(tips)mappingとネストされたmapping

Posted at

mappingとネストされたmappingについて下記します。
Sampleのcontractではmappingを用いて、アドレスaddress→そのアドレスが持っているお金の残高uint型を定義します。
※deleteによりmappingを初期値に戻すことができます。
Sample2のcontractでは、アドレス①address→(アドレス②address→そのアドレスが持っているお金の残高uint)型を定義します。
こちらの意味に関しては、あるアドレス①が、あるアドレス②へお金を割り当てる際に使用します。
つまり、あるアドレス①があるアドレス②へ割り当てたお金の残高をデータとして管理することができます。

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

contract Sample {
    mapping(address => uint) public balance;

    function sendMoney(address _address, uint _amount) public {
        balance[_address] = _amount;
    }

    function remove(address _address) public {
        delete balance[_address];
    }
}

contract Sample2{
   mapping(address => mapping(address=>uint)) public balance;

   function sendMoney(address _address, uint _amount) public {
        balance[msg.sender][_address] = _amount;
    }

    function remove(address _address) public {
        delete balance[msg.sender][_address];
    }
}

※RemixIDEにより動作確認

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?