Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending and receiving of ether to/from fakes #148

Open
sebastianst opened this issue Aug 19, 2022 · 4 comments
Open

Sending and receiving of ether to/from fakes #148

sebastianst opened this issue Aug 19, 2022 · 4 comments

Comments

@sebastianst
Copy link

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

  • whenCalledWith should accept an optional last argument {value: BigNumberish} with which I can set an expected amount of ether to receive.
  • returns should accept functions that have an optional last argument {value: BigNumberish} to which the amount of ether is passed that this call received. The function should also be able to return a dynamic amount of ether to send, e.g. as an object {value: BigNumberish, to: string}
  • returns should have an optional last argument {value: BigNumberish, to: string} which, if present, lets the function send an amount of value ether to address to when called.

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

@sebastianst
Copy link
Author

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

@aharvet
Copy link

aharvet commented Aug 19, 2022

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
Copy link
Author

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants