-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank - Reentrancy Guard Solution.sol
40 lines (31 loc) · 1.28 KB
/
Bank - Reentrancy Guard Solution.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
34
35
36
37
38
39
40
// REENTRANCY GUARD SOLUTION TO VULNERABLE BANK SMART CONTRACT
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "hardhat/console.sol";
// IMPORTING AND USING REENTRANCY GUARD
contract EtherBank is ReentrancyGuard{
using Address for address payable;
// keeps track of all savings account balances
mapping(address => uint) public balances;
// deposit funds into the sender's account
function deposit() external payable {
balances[msg.sender] += msg.value;
}
// withdraw all funds from the user's account
function withdraw() external nonReentrant{
require(balances[msg.sender] > 0, "Withdrawl amount exceeds available balance.");
console.log("");
console.log("EtherBank balance: ", address(this).balance);
console.log("Attacker balance: ", balances[msg.sender]);
console.log("");
// WE DON'T USE THE LOGICAL SOLUTION HERE
payable(msg.sender).sendValue(balances[msg.sender]);
balances[msg.sender] = 0;
}
// check the total balance of the EtherBank contract
function getBalance() external view returns (uint) {
return address(this).balance;
}
}