Skip to content

Instantly share code, notes, and snippets.

@henryjuan
Created August 4, 2020 14:09
Show Gist options
  • Save henryjuan/8eaf79c8f26f27276ad5220bb0a4ee47 to your computer and use it in GitHub Desktop.
Save henryjuan/8eaf79c8f26f27276ad5220bb0a4ee47 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=undefined&optimize=true&gist=
pragma solidity >=0.4.22 <0.7.0;
/**
* @title Config
* @dev Set and get settings
*/
contract Config {
address private owner;
modifier isOwner() {
require(msg.sender == owner, "Caller is not owner");
_;
}
struct Setting {
string key;
string value;
}
Setting[] settings;
constructor() public {
owner = msg.sender;
}
function set(string memory _key, string memory _value) public isOwner {
//uint i = find(_key);
//settings[i].value = _value;
settings.push(Setting(_key, _value));
}
function unset(string memory _key) public isOwner {
uint i = find(_key);
if (i != 0) {
delete settings[i];
}
}
function get(string memory _key) public view returns (string memory, string memory) {
uint i = find(_key);
return (settings[i].key, settings[i].value);
}
function find(string memory _key) internal view returns (uint) {
for(uint i = 0; i < settings.length; i++) {
if (isEqual(settings[i].key, _key)) {
return i;
}
}
revert('key not found');
}
function isEqual(string memory a, string memory b) internal pure returns (bool) {
if (bytes(a).length != bytes(b).length) {
return false;
} else {
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
}
/*function all() public view returns (Setting[] memory) {
return settings;
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment