Last active
November 30, 2020 22:32
-
-
Save skozin/7c13e9ebcb513af56f98fc6cd533b5ef to your computer and use it in GitHub Desktop.
Unstructured storage demo (mapping to value types)
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.24; | |
contract SimpleMappingDemo { | |
bytes32 internal constant SHARES_MAPPING_POSITION = keccak256("org.test.storagedemo.shares"); | |
function getShares(address _address) public view returns (uint256) { | |
return _getShares(_address, SHARES_MAPPING_POSITION); | |
} | |
function setShares(address _address, uint256 _shares) public { | |
_setShares(_address, _shares, SHARES_MAPPING_POSITION); | |
} | |
function _getShares(address _address, bytes32 _sharesSlot) internal view returns (uint256 result) { | |
assembly { | |
// using scratch space | |
mstore(0, _address) | |
mstore(32, _sharesSlot) | |
let hash := keccak256(0, 64) | |
result := sload(hash) | |
} | |
} | |
function _setShares(address _address, uint256 _shares, bytes32 _sharesSlot) internal { | |
assembly { | |
// using scratch space | |
mstore(0, _address) | |
mstore(32, _sharesSlot) | |
let hash := keccak256(0, 64) | |
sstore(hash, _shares) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment