Created
August 28, 2018 11:54
-
-
Save adamdossa/e272cdddc639ef08ae65b4558ff40a51 to your computer and use it in GitHub Desktop.
EternalStorage with mappings from uint256 / address to bytes32[] or uint256[]
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; | |
/** | |
* @title EternalStorage | |
* @dev This contract holds all the necessary state variables to carry out the storage of any contract. | |
*/ | |
contract EternalStorage { | |
mapping(bytes32 => uint256) public uintStorage; | |
mapping(bytes32 => string) public stringStorage; | |
mapping(bytes32 => address) public addressStorage; | |
mapping(bytes32 => bytes) public bytesStorage; | |
mapping(bytes32 => bool) public boolStorage; | |
mapping(bytes32 => int256) public intStorage; | |
mapping(bytes32 => bytes32) public bytes32Storage; | |
mapping(bytes32 => bytes32[]) public bytes32ArrayStorage; | |
mapping(bytes32 => uint256[]) public uintArrayStorage; | |
function set(string _name, uint256 _value) internal { | |
uintStorage[keccak256(abi.encodePacked(_name))] = _value; | |
} | |
function set(string _name, address _value) internal { | |
addressStorage[keccak256(abi.encodePacked(_name))] = _value; | |
} | |
function set(string _name, string _value) internal { | |
stringStorage[keccak256(abi.encodePacked(_name))] = _value; | |
} | |
function setMap(string _name, address _key, uint256 _value) internal { | |
uintStorage[keccak256(abi.encodePacked(_name, _key))] = _value; | |
} | |
function setMap(string _name, address _key, address _value) internal { | |
addressStorage[keccak256(abi.encodePacked(_name, _key))] = _value; | |
} | |
function setMap(string _name, uint256 _key, uint256 _value) internal { | |
uintStorage[keccak256(abi.encodePacked(_name, _key))] = _value; | |
} | |
function setMap(string _name, uint256 _key, address _value) internal { | |
addressStorage[keccak256(abi.encodePacked(_name, _key))] = _value; | |
} | |
function getUint(string _name) internal view returns (uint256) { | |
return uintStorage[keccak256(abi.encodePacked(_name))]; | |
} | |
function getMapAddress(string _name, uint256 _key) internal view returns (address) { | |
return addressStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
function getMapAddress(string _name, address _key) internal view returns (address) { | |
return addressStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
function getMapUint(string _name, address _key) internal view returns (uint256) { | |
return uintStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
function getMapUint(string _name, uint256 _key) internal view returns (uint256) { | |
return uintStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
function example() public { | |
doSomeStuff(10, 0x1, "blah", 0x2); | |
} | |
function doSomeStuff(uint256 _someInt, address _someAddr, string _someString, address _someAddr2) public { | |
//NB - explicit casts only needed as inline constants aren't typed - shouldn't usually be needed | |
//uint256 someInteger = _someInt; | |
set('someInteger', _someInt); | |
//address someAddress = _someAddr; | |
set('someAddress', _someAddr); | |
//string someString = _someString | |
set('someString', _someString); | |
//mapping(address => address) issuerToToken -- issuerToToken[_someAddr] = _someAddr2; | |
setMap('issuerToToken', _someAddr2, _someAddr); | |
//mapping(uint256 => address) indexToIssuer -- indexToIssuer[_someInt] = _someAddr; | |
setMap('indexToIssuer', _someInt, _someAddr); | |
//uint256 temp = someInteger; | |
uint256 temp = getUint('someInteger'); | |
assert(temp == _someInt); | |
address tempAddress = getMapAddress('indexToIssuer', _someInt); | |
address tempAddress2 = getMapAddress('issuerToToken', _someAddr2); | |
assert(tempAddress == tempAddress2); | |
} | |
// Adds to mapping(uint256 => bytes32[]) | |
function pushMapArray(string _name, uint256 _key, bytes32 _value) internal { | |
bytes32ArrayStorage[keccak256(abi.encodePacked(_name, _key))].push(_value); | |
} | |
// Adds to mapping(uint256 => uint256[]) | |
function pushMapArray(string _name, uint256 _key, uint256 _value) internal { | |
uintArrayStorage[keccak256(abi.encodePacked(_name, _key))].push(_value); | |
} | |
// Adds to mapping(address => bytes32[]) | |
function pushMapArray(string _name, address _key, bytes32 _value) internal { | |
bytes32ArrayStorage[keccak256(abi.encodePacked(_name, _key))].push(_value); | |
} | |
// Adds to mapping(address => uint256[]) | |
function pushMapArray(string _name, address _key, uint256 _value) internal { | |
uintArrayStorage[keccak256(abi.encodePacked(_name, _key))].push(_value); | |
} | |
// Gets from mapping(uint256 => bytes32[]) | |
function getMapArrayBytes32(string _name, uint256 _key) internal view returns (bytes32[]) { | |
return bytes32ArrayStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
// Gets from mapping(address => bytes32[]) | |
function getMapArrayBytes32(string _name, address _key) internal view returns (bytes32[]) { | |
return bytes32ArrayStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
// Gets from mapping(address => uint256[]) | |
function getMapArrayUint(string _name, address _key) internal view returns (uint256[]) { | |
return uintArrayStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
// Gets from mapping(uint256 => uint256[]) | |
function getMapArrayUint(string _name, uint256 _key) internal view returns (uint256[]) { | |
return uintArrayStorage[keccak256(abi.encodePacked(_name, _key))]; | |
} | |
//Deletes from mapping (address => bytes32[]) at index _index | |
function deleteMapArrayBytes32(string _name, address _key, uint256 _index) internal { | |
bytes32[] storage array = bytes32ArrayStorage[keccak256(abi.encodePacked(_name, _key))]; | |
require(_index < array.length); | |
array[_index] = array[array.length - 1]; | |
array.length = array.length - 1; | |
} | |
function doSomeArrayStuff() public { | |
//mapping (address => bytes32[]) ownerToSymbols; | |
address ownerAddress = 0x1; | |
pushMapArray("ownerToSymbols", ownerAddress, bytes32("SYM1")); | |
pushMapArray("ownerToSymbols", ownerAddress, bytes32("SYM2")); | |
pushMapArray("ownerToSymbols", ownerAddress, bytes32("SYM3")); | |
address ownerAddress2 = 0x2; | |
pushMapArray("ownerToSymbols", ownerAddress2, bytes32("NEWSYM1")); | |
pushMapArray("ownerToSymbols", ownerAddress2, bytes32("NEWSYM2")); | |
//mapping (uint256 => uint256[]) indexToIntegers; | |
uint256 index1 = 1; | |
pushMapArray("indexToIntegers", index1, uint256(1)); | |
pushMapArray("indexToIntegers", index1, uint256(2)); | |
pushMapArray("indexToIntegers", index1, uint256(3)); | |
//Need one function per return argument type | |
assert(getMapArrayBytes32("ownerToSymbols", ownerAddress).length == 3); | |
assert(getMapArrayBytes32("ownerToSymbols", ownerAddress2).length == 2); | |
assert(getMapArrayUint("indexToIntegers", index1).length == 3); | |
assert(getMapArrayBytes32("ownerToSymbols", ownerAddress)[1] == bytes32("SYM2")); | |
deleteMapArrayBytes32("ownerToSymbols", ownerAddress, 1); | |
assert(getMapArrayBytes32("ownerToSymbols", ownerAddress).length == 2); | |
assert(getMapArrayBytes32("ownerToSymbols", ownerAddress)[1] == bytes32("SYM3")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment