forked from ciaranmcveigh5/ethernaut-x-foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reentrance.sol
33 lines (25 loc) · 814 Bytes
/
Reentrance.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import 'openzeppelin-contracts/contracts/utils/math/SafeMath.sol'; // Path change of openzeppelin contract
contract Reentrance {
using SafeMath for uint256;
mapping(address => uint) public balances;
function donate(address _to) public payable {
balances[_to] = balances[_to].add(msg.value);
}
function balanceOf(address _who) public view returns (uint balance) {
return balances[_who];
}
function withdraw(uint _amount) public {
if(balances[msg.sender] >= _amount) {
(bool result,) = msg.sender.call{value:_amount}("");
if(result) {
_amount;
}
unchecked {
balances[msg.sender] -= _amount; // unchecked to prevent underflow errors
}
}
}
receive() external payable {}
}