Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Remove incorrect balance reset in claimReward function inside Self Destruct Forced Prevention Contract #284

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from

Conversation

vedant-asati
Copy link

Description

In the Self Destruct Preventative Techniques section, the claimReward function contains a logical error where it sets balance = 0 before attempting to transfer funds. This causes the transfer to fail since it tries to send 0 ETH instead of the actual contract balance.

Current code:

function claimReward() public {
    require(msg.sender == winner, "Not winner");
    balance = 0;  // <-- Problem: Sets balance to 0 before transfer
    (bool sent,) = msg.sender.call{value: balance}("");  // Will always try to send 0 ETH
    require(sent, "Failed to send Ether");
}

Proposed fix:

function claimReward() public {
    require(msg.sender == winner, "Not winner");
    uint256 amount = balance;
    balance = 0;
    (bool sent,) = msg.sender.call{value: amount}("");
    require(sent, "Failed to send Ether");
}

Why this change is necessary

  • The current implementation prevents winners from claiming their reward since it tries to transfer 0 ETH
  • The fix ensures the proper amount is transferred before resetting the balance
  • This aligns with the intended behavior of the game where winners should receive their reward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant