Skip to content

Instantly share code, notes, and snippets.

@chriseth
Last active July 29, 2020 11:37
Show Gist options
  • Save chriseth/a183d858bb4184197d2d0dc4a7023faa to your computer and use it in GitHub Desktop.
Save chriseth/a183d858bb4184197d2d0dc4a7023faa to your computer and use it in GitHub Desktop.
Oracle
struct Query
{
uint id;
function(uint, bytes memory) external callback;
}
contract Oracle
{
event Queried(bytes,function(uint, bytes memory) external);
Query[] public queries;
uint public price;
address immutable owner = msg.sender;
modifier onlyowner { require(msg.sender == owner); _; }
function query(bytes calldata q, function(uint, bytes memory) external callback) public payable returns (uint id) {
require(msg.value == price);
id = queries.length;
queries.push(Query(id, callback));
emit Queried(q, callback);
}
function resolve(uint id, bytes calldata response) onlyowner public {
Query memory q = queries[id];
delete queries[id];
q.callback(id, response);
}
function withdraw() onlyowner public {
msg.sender.transfer(address(this).balance);
}
function setPrice(uint _price) onlyowner public {
price = _price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment