Waffle icon indicating copy to clipboard operation
Waffle copied to clipboard

Transferring ethers to a mocked contract

Open RomualdH opened this issue 2 years ago • 2 comments

Hello all,

I'm trying to understand if there is an issue with Waffle or it's my lack of knowledge...

When transferring ethers from a contract to a mocked contract, I get the error contract call run out of gas and made the transaction revert even though the caller contract balance has enough. This is the simple Test contract:

pragma solidity ^0.8.0;
import "hardhat/console.sol";

contract Test {
    address payable public receiver;

    constructor(address payable _receiver) {
        receiver = _receiver;
    }

    function transferTest(uint256 amount) external {
        console.log("Contract balance", address(this).balance);
        console.log("Amount to be transfered", amount);
        receiver.transfer(amount);
    }

    receive() external payable {}
}

The reproduction steps are:

  1. Deploy the receiver mocked contract
  2. Deploy the Test contract with receiver's contract address as argument
  3. Send ethers to the Test contract
  4. Call the deployedTest.transferTest function

Thank you in advance.

RomualdH avatar Aug 18 '21 07:08 RomualdH

I suspect there is no way to do what you want.

The mock contract's (payable) fallback function is how it delegates to the different initialized mock functions. Your transfer ultimately ends up invoking this function and since there's nothing for the mock contract to delegate, it will revert. In theory, it will call the receive first, but this is a special function and I don't know how to mock it.

chanhosuh avatar Aug 26 '21 20:08 chanhosuh

Is there no way to transfer some ether into or increase the ether balance of a mocked contract?

sebastiantf avatar Jun 28 '22 18:06 sebastiantf