Skip to content

Instantly share code, notes, and snippets.

@pingiun
Last active August 2, 2020 11:15
Show Gist options
  • Save pingiun/ae3cd2798cbee95ea189a3c904e20886 to your computer and use it in GitHub Desktop.
Save pingiun/ae3cd2798cbee95ea189a3c904e20886 to your computer and use it in GitHub Desktop.
Ethereum contract which only pays back if you continue to activate it
pragma solidity ^0.4.0;
contract Ownable {
/// @dev `owner` is the only address that can call a function with this
/// modifier
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
address public owner;
/// @notice The Constructor assigns the message sender to be `owner`
function Ownable() public {
owner = msg.sender;
}
address public newOwner;
/// @notice `owner` can step down and assign some other address to this role
/// @param _newOwner The address of the new owner.
function changeOwner(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
if (msg.sender == newOwner) {
owner = newOwner;
}
}
}
contract DeadMansSwitch is Ownable {
uint lastTime;
uint timeunit;
uint nOfUnits;
uint gracePeriod;
uint initGracePeriod;
bool graceReset;
address payout;
function DeadMansSwitch(uint _timeunit,
uint _nOfUnits,
uint _gracePeriod,
bool _graceReset,
address _payout) public payable {
require(msg.value != 0);
timeunit = _timeunit;
nOfUnits = _nOfUnits;
initGracePeriod = gracePeriod = _gracePeriod;
payout = _payout;
lastTime = block.timestamp;
graceReset = _graceReset;
}
function activate() public onlyOwner {
if (lastTime + timeunit < block.timestamp) {
if (lastTime + timeunit + timeunit * gracePeriod > block.timestamp) {
gracePeriod -= 1;
} else {
// Grace period ended and owner was not on time
selfdestruct(0xB365dBcA3863b2EfDFCA90990043455a9171E2c1);
}
} else if (graceReset) {
gracePeriod = initGracePeriod;
}
if (nOfUnits == 1) {
// Contract is done, return remaining funds to `payout`
selfdestruct(payout);
}
lastTime = block.timestamp;
nOfUnits -= 1;
}
}
@pingiun
Copy link
Author

pingiun commented Apr 9, 2018

This gist is licensed under the MIT license

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment