Created
August 5, 2018 15:16
-
-
Save michielmulders/104f719c4236e355a4c3d3004833ccb7 to your computer and use it in GitHub Desktop.
Speed bump delay contract actions Solidity Ethereum
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
struct RequestedWithdrawal { | |
uint amount; | |
uint time; | |
} | |
mapping (address => uint) private balances; | |
mapping (address => RequestedWithdrawal) private requestedWithdrawals; | |
uint constant withdrawalWaitPeriod = 28 days; // 4 weeks | |
function requestWithdrawal() public { | |
if (balances[msg.sender] > 0) { | |
uint amountToWithdraw = balances[msg.sender]; | |
balances[msg.sender] = 0; // for simplicity, we withdraw everything; | |
// presumably, the deposit function prevents new deposits when withdrawals are in progress | |
requestedWithdrawals[msg.sender] = RequestedWithdrawal({ | |
amount: amountToWithdraw, | |
time: now | |
}); | |
} | |
} | |
function withdraw() public { | |
if(requestedWithdrawals[msg.sender].amount > 0 && now > requestedWithdrawals[msg.sender].time + withdrawalWaitPeriod) { | |
uint amountToWithdraw = requestedWithdrawals[msg.sender].amount; | |
requestedWithdrawals[msg.sender].amount = 0; | |
if(!msg.sender.send(amountToWithdraw)) { | |
throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment