Skip to content

Instantly share code, notes, and snippets.

@vasa-develop
Created July 21, 2018 18:30
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 vasa-develop/4edb6734ee9d1c214af21c6ef833f3e0 to your computer and use it in GitHub Desktop.
Save vasa-develop/4edb6734ee9d1c214af21c6ef833f3e0 to your computer and use it in GitHub Desktop.
THIS CODE IS SAFE.
contract EtherGame {
uint public payoutMileStone1 = 3 ether;
uint public mileStone1Reward = 2 ether;
uint public payoutMileStone2 = 5 ether;
uint public mileStone2Reward = 3 ether;
uint public finalMileStone = 10 ether;
uint public finalReward = 5 ether;
uint public depositedWei;
mapping (address => uint) redeemableEther;
function play() public payable {
require(msg.value == 0.5 ether);
uint currentBalance = depositedWei + msg.value;
// ensure no players after the game as finished
require(currentBalance <= finalMileStone);
if (currentBalance == payoutMileStone1) {
redeemableEther[msg.sender] += mileStone1Reward;
}
else if (currentBalance == payoutMileStone2) {
redeemableEther[msg.sender] += mileStone2Reward;
}
else if (currentBalance == finalMileStone ) {
redeemableEther[msg.sender] += finalReward;
}
depositedWei += msg.value;
return;
}
function claimReward() public {
// ensure the game is complete
require(depositedWei == finalMileStone);
// ensure there is a reward to give
require(redeemableEther[msg.sender] > 0);
redeemableEther[msg.sender] = 0;
msg.sender.transfer(redeemableEther[msg.sender]);
}
}
@Ty-Sir
Copy link

Ty-Sir commented Apr 8, 2022

line 36 clears the value being transferred before it gets transferred.

Should be something like:

function claimReward() public {
  // ensure the game is complete
  require(depositedWei == finalMileStone);
  // ensure there is a reward to give
  require(redeemableEther[msg.sender] > 0); 
  unit256 prize = redeemableEther[msg.sender];
  redeemableEther[msg.sender] = 0;
  payable(msg.sender).transfer(prize);
} 

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