Skip to content

Instantly share code, notes, and snippets.

@gdebenito
Created February 28, 2019 23:18
Show Gist options
  • Save gdebenito/1b336115439649477bbe323713573d12 to your computer and use it in GitHub Desktop.
Save gdebenito/1b336115439649477bbe323713573d12 to your computer and use it in GitHub Desktop.
How does Solidity stores mapping in a ehtereum smart contract?
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