Last active
April 1, 2021 04:09
-
-
Save brianfakhoury/c8c6969e3c659350d727a06ac3027f1b to your computer and use it in GitHub Desktop.
Incentivize external caller for a contract job using incremental allocation.
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.8.1; | |
contract TimedCallbacks | |
{ | |
struct Job { uint time; address addr; bytes data; uint reward; address creator; } | |
Job[] jobs; | |
function register(uint _time, address _addr, bytes calldata _data) external payable returns (uint index) { | |
index = jobs.length; | |
Job memory newJob = Job(_time, _addr, _data, msg.value, msg.sender); | |
jobs.push(newJob); | |
} | |
function invoke(uint index) external returns (bytes memory ret) { | |
if (jobs[index].time > 0 && block.timestamp >= jobs[index].time) { | |
payable(msg.sender).transfer(jobs[index].reward); | |
( , ret) = jobs[index].addr.call(jobs[index].data); | |
delete jobs[index]; | |
} | |
} | |
function cancel(uint index) external returns (bool res) { | |
if (msg.sender == jobs[index].creator) { | |
payable(msg.sender).transfer(jobs[index].reward); | |
delete jobs[index]; | |
res = true; | |
} | |
} | |
function getNumJobs() external view returns (uint count) { | |
count = jobs.length; | |
} | |
function getJob(uint index) external view returns (Job memory j) { | |
j = jobs[index]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment