Created
May 31, 2018 00:23
-
-
Save scastiel/a0a1d6dab01f8c82e9c5003541348d91 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=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
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.4.0; | |
contract WakeMeUp { | |
event Print(string _name, uint _value); | |
event PrintString(string _name, bytes32 _value); | |
struct Alarm { | |
uint id; | |
address owner; | |
uint createdAtBlock; | |
uint minBlock; | |
uint maxBlock; | |
} | |
mapping(address => mapping(uint => Alarm)) alarmsByOwner; | |
uint nextAlarmId = 1; | |
function createAlarm(uint minBlock, uint maxBlock) public returns (uint alarmId) { | |
emit Print("block number", block.number); | |
require(minBlock < maxBlock, "minBlock must be smaller than maxBlock."); | |
require(block.number < minBlock, "The alarm must be set in the future."); | |
alarmId = nextAlarmId++; | |
Alarm storage alarm = alarmsByOwner[msg.sender][alarmId]; | |
alarm.id = alarmId; | |
alarm.owner = msg.sender; | |
alarm.createdAtBlock = block.number; | |
alarm.minBlock = minBlock; | |
alarm.maxBlock = maxBlock; | |
} | |
function cancelAlarm(uint alarmId) public { | |
Alarm storage alarm = alarmsByOwner[msg.sender][alarmId]; | |
require(alarm.id > 0, "Wrong alarm ID"); | |
require(block.number <= alarm.minBlock, "Too late!"); | |
delete alarmsByOwner[msg.sender][alarmId]; | |
} | |
function sha(bytes32 value) private pure returns (bytes32) { | |
return keccak256(abi.encodePacked(value)); | |
} | |
function getCalculusForAlarm(uint alarmId) public view returns (uint n1, uint n2, uint n3) { | |
Alarm memory alarm = alarmsByOwner[msg.sender][alarmId]; | |
require(alarm.id > 0, "Wrong alarm ID"); | |
require(block.number > alarm.minBlock, "Too soon!"); | |
require(block.number <= alarm.maxBlock, "Too late :("); | |
bytes32 hash = blockhash(alarm.minBlock); | |
n1 = uint(sha(hash)) % 1000; | |
n2 = uint(sha(sha(hash))) % 2 + 2; // will be 2 or 3 | |
n3 = uint(sha(sha(sha(hash)))) % 1000; | |
} | |
function turnOffAlarm(uint alarmId, uint calculusResult) public { | |
(uint n1, uint n2, uint n3) = getCalculusForAlarm(alarmId); | |
require(calculusResult == n1 * n2 + n3, "Wrong calculus result :("); | |
delete alarmsByOwner[msg.sender][alarmId]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment