Skip to content

Instantly share code, notes, and snippets.

@skozin
Last active November 30, 2020 22:33
Show Gist options
  • Save skozin/344d9c7d2270cac0c3f9373c2f43c846 to your computer and use it in GitHub Desktop.
Save skozin/344d9c7d2270cac0c3f9373c2f43c846 to your computer and use it in GitHub Desktop.
Unstructured storage demo (mapping to structs)
pragma solidity ^0.6.0;
contract StructMappingDemo {
bytes32 internal constant NOP_MAPPING_POSITION = keccak256("org.test.storagedemo.nopMapping");
struct NodeOperator {
bool active;
address rewardAddress;
string name;
}
function getNopMapping(bytes32 _position) internal view returns (mapping(uint256 => NodeOperator) storage result) {
assembly {
result_slot := _position
}
}
function getOp(uint256 _id) public view returns (bool active, address rewardAddress, string memory name) {
mapping(uint256 => NodeOperator) storage ops = getNopMapping(NOP_MAPPING_POSITION);
NodeOperator storage op = ops[_id];
active = op.active;
rewardAddress = op.rewardAddress;
name = op.name;
}
function setOp(uint256 _id, bool _active, address _rewardAddress, string memory _name) public {
mapping(uint256 => NodeOperator) storage ops = getNopMapping(NOP_MAPPING_POSITION);
NodeOperator storage op = ops[_id];
op.active = _active;
op.rewardAddress = _rewardAddress;
op.name = _name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment