Skip to content

Solidity #586

Answered by TatyOko28
Mercure28 asked this question in Q&A
Discussion options

You must be logged in to vote

A reentrancy attack occurs when a contract calls another contract before completing its execution, allowing the malicious contract to re-execute certain parts of the code in unexpected ways.

Solution :

Use Checks-Effects-Interactions scheme (modify variables first before sending ETH).
Use ReentrancyGuardOpenZeppelin

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract MyContract is ReentrancyGuard {
    function withdraw(uint amount) external nonReentrant {
        require(balance[msg.sender] >= amount, "Not enough balance");
        balance[msg.sender] -= amount;
        payable(msg.sender).transfer(amount);
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Mercure28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants