Skip to content

Instantly share code, notes, and snippets.

@christoph2806
Created June 23, 2016 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christoph2806/858df56093267fe8b2d468421a7f21f8 to your computer and use it in GitHub Desktop.
Save christoph2806/858df56093267fe8b2d468421a7f21f8 to your computer and use it in GitHub Desktop.
contract Safe_Send {
bool reentrant = false;
event Attack();
event LOG_Send(uint amount, address recipient, bool success);
modifier noReentrant {
if (reentrant) {
Attack();
return;
}
reentrant = true;
_
reentrant = false;
}
mapping (address => uint) balances;
uint totalSupply;
function () noReentrant {
balances[msg.sender] += msg.value;
totalSupply += msg.value;
}
function payOut(uint amount, address recipient) noReentrant {
if (balances[msg.sender] > amount) {
uint balance = balances[msg.sender];
balances[msg.sender] -= amount;
uint oldSupply = totalSupply;
totalSupply -= amount;
if(!recipient.send(amount)) {
totalSupply = oldSupply;
balances[msg.sender] = balance;
LOG_Send(amount, recipient, false);
return;
}
LOG_Send(amount, recipient, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment