Skip to content

Instantly share code, notes, and snippets.

View daniellehrner's full-sized avatar

daniellehrner

View GitHub Profile
@daniellehrner
daniellehrner / iterable_eternal_storage_getter.sol
Created April 8, 2019 13:39
Iterable Eternal Storage Getter
address[] memory addressKeys = getAddressKeys("balanceKeys");
function getAddressKeys(bytes32 _listId) external view returns (address[] memory) {
// get the number of keys with _listId "balanceKeys"
uint256 size = getUInt256(keccak256(abi.encodePacked(_listId, "KEY_SIZE")));
// create an address array with the size of the list
address[] memory keys = new address[](size);
// iterate over the list
@daniellehrner
daniellehrner / iterable_eternal_storage.sol
Last active April 8, 2019 13:33
Iterable Eternal Storage
addAddressKey("balanceKeys", "0x12...");
function addAddressKey(bytes32 _listId, address _value) external {
// get the number of keys with _listId "balanceKeys"
uint256 size = getUInt256(keccak256(abi.encodePacked(_listId, "KEY_SIZE")));
// the new index (end of the list) is equal to the size of the keys
uint256 index = size;
// store _value as last item in the list
@daniellehrner
daniellehrner / balance_convert.sol
Created April 8, 2019 10:42
Divison in Solidity
_balances[_who] = _balances[_who] / (10 ** 16)
@daniellehrner
daniellehrner / er20_balance.sol
Created April 8, 2019 08:18
Set ERC-20 balance in mapping
function setBalance(address _who, uint256 _value) external {
_balances[_who] = _value;
}
@daniellehrner
daniellehrner / set_balance_eternal_storage.sol
Last active April 5, 2019 15:04
Wrapper function to store a balance in eternal storage
function setBalance(address _who, uint256 _value) external {
setUInt256(
// computes the Ethereum-SHA-3 (Keccak-256) hash
keccak256(
// performs packed encoding of the given arguments,
// as the hash function only accepts one argument
abi.encodePacked(
// we use a hardcoded string to create a different
// hash for the same dynamic value
"balance",
@daniellehrner
daniellehrner / uint256_eternal_storage.sol
Last active April 5, 2019 15:03
Eternal storage for uint256
mapping(bytes32 => uint256) internal uInt256Storage;
function getUInt256(bytes32 _key) external view returns (uint256) {
return uInt256Storage[_key];
}
function setUInt256(bytes32 _key, uint256 _value) external {
uInt256Storage[_key] = _value;
}