Skip to content

Instantly share code, notes, and snippets.

@michielmulders
Created August 5, 2018 15:16
Show Gist options
  • Save michielmulders/104f719c4236e355a4c3d3004833ccb7 to your computer and use it in GitHub Desktop.
Save michielmulders/104f719c4236e355a4c3d3004833ccb7 to your computer and use it in GitHub Desktop.
Speed bump delay contract actions Solidity Ethereum
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