Created
February 28, 2019 23:18
-
-
Save gdebenito/1b336115439649477bbe323713573d12 to your computer and use it in GitHub Desktop.
How does Solidity stores mapping in a ehtereum smart contract?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract Mapping { | |
mapping(string => uint256) store; // stored at slot 0 | |
function getAssembly(string calldata _key) external view returns(uint256 _value) { | |
bytes32 dir = keccak256(abi.encodePacked(_key,uint256(0))); // concatenation of key and slot | |
assembly{ | |
_value := sload(dir) // load value | |
} | |
} | |
function get(string calldata key) external view returns(uint256) { | |
return store[key]; | |
} | |
function set(string calldata key, uint256 _value) external { | |
store[key] = _value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment