I wanted to create a contract where wallets can deposit and withdraw funds, to test the re-entrancy attack.
However, on deploying this contract in Remix VM (London), I'm unable to withdraw using the withdrawAll() and withdraw() functions even when my indicated amount is < deposited funds.
The error is:
transact to Victim.withdraw errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Here's the code, please let me know where I did wrong?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Victim {
mapping(address => uint256) public balances;
function deposit(uint256 _amount) public {
balances[msg.sender] += _amount;
}
function withdrawAll() public payable {
uint256 amount = balances[msg.sender];
require(amount <= balances[msg.sender], "too much!");
payable(msg.sender).transfer(amount);
// uint256 new_balance = balances[msg.sender] - amount;
balances[msg.sender] = 0;
}
function withdraw(uint256 amount) public payable {
require(amount <= balances[msg.sender], "too much!");
payable(msg.sender).transfer(amount);
balances[msg.sender] -= amount;
}
}