From 052ee48d5b351a0ae6555d994f85067d1a739494 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Fri, 27 Oct 2017 13:43:21 -0700 Subject: [PATCH] Include Solidity contract. --- contracts/the-button.sol | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 contracts/the-button.sol diff --git a/contracts/the-button.sol b/contracts/the-button.sol new file mode 100644 index 0000000..4591739 --- /dev/null +++ b/contracts/the-button.sol @@ -0,0 +1,38 @@ +pragma solidity ^0.4.0; + +contract TheButton { + uint public timeLimit; + uint public lastPress; + mapping(address => uint) public pressTimes; + + event ButtonPress(address pressedBy, uint time); + + function TheButton(uint timeLimit_) public { + timeLimit = timeLimit_; + lastPress = now; + } + + function press(uint expectLastPress) public returns (bool) { + // If the time limit expires, the game is over. + if (lastPress + timeLimit < now) { + return false; + } + + // Don't allow users to press multiple times. + if (pressTimes[msg.sender] != 0) { + return false; + } + + // Check the lastPress is the expected one so that if another user + // presses first, this one is rejected. The user should be notified + // that they should try again. + if (lastPress != expectLastPress) { + return false; + } + + pressTimes[msg.sender] = now - lastPress; + lastPress = now; + ButtonPress(msg.sender, now); + return true; + } +}