Skip to content

Instantly share code, notes, and snippets.

@bartubozkurt
Created February 2, 2023 11:36
Show Gist options
  • Save bartubozkurt/4f90602b4c442492fa9e2b60ff4e02de to your computer and use it in GitHub Desktop.
Save bartubozkurt/4f90602b4c442492fa9e2b60ff4e02de to your computer and use it in GitHub Desktop.
/* Bad */
uint256 constant private targetEther = 1000 ether;
function join() public payable {
require(msg.value == 5 ether); // each play is 5 ether
...doSomething;
}
function claimReward(address _to) public {
require(this.balance == targetEther);
_to.transfer(targetEther);
}
/* Vulnerable
1. Send ether by selfdestruct
2. the value of this.balance cwill never be a multiples of 5 forever...
*/
/* Better */
uint256 constant private targetEther = 1000 ether;
uint256 private treasury;
function join() public payable {
require(msg.value == 5 ether); // each play is 5 ether
treasury = treasury + 5 ether;
...doSomething;
}
function claimReward(address _to) public {
require(treasury == targetEther); // don't use this.balance
_to.transfer(targetEther);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment