0

I am trying to test the deposit() function of the WETH9 contract in Remix. I compiled and deployed a simplified WETH9 contract:

//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.1;

contract WETH9 {   
    
    event Deposit(address indexed dst, uint wad);

    mapping (address => uint) public balanceOf; //get balance of WETH held by an address?

    function deposit() public payable {
        balanceOf[msg.sender] += msg.value;
        emit Deposit(msg.sender, msg.value);
    }

    function totalSupply() public view returns (uint) {
        return address(this).balance;
    }
}

I then compiled and deployed the below Caller contract by passing in the WETH9 contract address.

//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.1;

import "./WETH9.sol";

interface IWETH9 {
    function deposit() external payable;
}

contract Caller {
    IWETH9 public weth9;

    constructor(IWETH9 _weth9){
        weth9 = _weth9;
    }

    function call_deposit() public payable {     
        weth9.deposit{ value: 50 }();
        
    }
}

I then called the call_deposit() function. This reverts at weth9.deposit{ value: 50 }(); and results in the below error. I am not sure what the issue is. Maybe I'm not sending the right value? There's 99.9+ ETH in the vm account.

[vm]from: 0x5B3...eddC4 to: Caller.call_deposit() 0xE5f...78e22 value: 0 wei data: 0x374...22832 logs: 0 hash: 0x66d...ea411 transact to Caller.call_deposit 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. Debug the transaction to get more information.

1 Answer 1

1

Instead of passing IWETH9 _weth9 into constructor, you should pass the address of new created WEHT9 address to constructor param, and create new IWETH in it:

 constructor(address weth9Address){
    weth9 = IWETH9(weth9Address);
 }

Additionally, the Caller contract should have ether beacause weth9.deposit{ value: 50 }(); use ether of contract itself.

6
  • I rewrote the constructor as you suggested but I'm still getting the same error. Were you able to get it working in Remix (see the account balance go down? see contract balance increase? etc.?)
    – gioro
    Jul 16, 2022 at 23:47
  • @gioro this line weth9.deposit{ value: 50 }(); mean that you use 50wei of Caller contract to deposit and it seems that your Caller contract has no ether. You should transfer to address of Caller contract some Eth and retry. Jul 16, 2022 at 23:53
  • Thank you, that was indeed the problem. Can you edit your solution so I can accept it. For reference: I deployed the Caller contract on Remix > Added 50 wei to the Caller contract via the "Value" field and "Transact" button in the IDE > Called call_deposit() again which worked.
    – gioro
    Jul 17, 2022 at 0:12
  • Oh and I also had to add receive() external payable {} to Caller as fallback.
    – gioro
    Jul 17, 2022 at 0:19
  • instead of hardcoding 50wei you could change to weth9.deposit{ value: msg.sender }();. that way the function just passes on the eth that was passed to it, and you don't have to pre-load the contract with eth before calling the function.
    – sola24
    Jul 17, 2022 at 8:25

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.