defi-wonderland / smock

The Solidity mocking library
MIT License
319 stars 40 forks source link

Sending and receiving of ether to/from fakes #148

Open sebastianst opened 2 years ago

sebastianst commented 2 years ago

Description When a function receives ether, the whenCalledWith filter cannot be set to an expect amount of ether being received by the function. Likewise, a function as argument to returns(input => { // do smth with input }) doesn't receive the amount of ether received with the function call.

On the other hand, I also cannot set a function to send some amount of ether when called.

If any of this is possible, it is not documented.

Solution

I guess the handling of ether could also be applied to other functions not mentioned in this issue.

sebastianst commented 2 years ago

Furthermore, more transaction context like the msg.sender could additionally be added to the optional input parameter of whenCalledWith and the returns-function.

aharvet commented 2 years ago

To complete the use cases mentioned, it would be very useful to be able to check the amount of ether sent with a call to a fake. Just like you can check the arguments with "calledWith".

sebastianst commented 2 years ago

Yes that would also be helpful, like

expect(contract.func).to.have.receivedEther(amount)

Even though hardhat chai matchers already allow for testing this on the fake's address with

expect(tx).to.changeEtherBalance(fake.address, amount)

this would still be nice, if possible.

sebastianst commented 2 years ago

I tried to achieve sending ether from a fake with the following trick

    contract.func.returns(async () => {
      await contract.wallet.sendTransaction({
        to: receiver,
        value: amount,
      })
    })

but this didn't work either, as returns doesn't seem to accept promises. The TS type of returns's parameter is just any?, so it compiled though.