Skip to content

Instantly share code, notes, and snippets.

@brianfakhoury
Last active April 1, 2021 04:09
Show Gist options
  • Save brianfakhoury/c8c6969e3c659350d727a06ac3027f1b to your computer and use it in GitHub Desktop.
Save brianfakhoury/c8c6969e3c659350d727a06ac3027f1b to your computer and use it in GitHub Desktop.
Incentivize external caller for a contract job using incremental allocation.
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