-
How to avoid security vulnerabilities like Reentrancy Attack in a Solidity smart contract? |
Beta Was this translation helpful? Give feedback.
Answered by
TatyOko28
Feb 8, 2025
Replies: 1 comment
-
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). 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);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Mercure28
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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