Remix Solidity 0.8.x – Unable to Withdraw/Transfer Funds from Contract

remixsolidity-0.8.x

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;
    }
  

}

Best Answer

The reason is that you aren't depositing any amount to the contract. deposit function just takes in an int and updates the balance. You send ether to the contract, you need to make it payable and deduce the amount from msg.value.

function deposit() public payable {
    balances[msg.sender] += msg.value;
}

Now if you try to withdraw, it should work properly.

Related Topic