2

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

}

1 Answer 1

2

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.

1
  • It works, you are right! Thank you! I see my mistake now :)
    – shonendumm
    Jan 23, 2022 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.