Open sebastianst opened 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.
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".
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.
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.
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 toreturns(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 ofvalue
ether to addressto
when called.I guess the handling of ether could also be applied to other functions not mentioned in this issue.