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

.to.be.revertedWith only works with callStatic (Rinkeby) #579

Open
cryptobys-rami opened this issue Sep 28, 2021 · 5 comments
Open

.to.be.revertedWith only works with callStatic (Rinkeby) #579

cryptobys-rami opened this issue Sep 28, 2021 · 5 comments

Comments

@cryptobys-rami
Copy link

await expect(marketplaceContractNftAddr1.callStatic.withdraw(numberToWithdraw)) .to.be.revertedWith('Insufficient available balance')

Works as expected. However:

await expect(marketplaceContractNftAddr1.withdraw(numberToWithdraw)) .to.be.revertedWith('Insufficient available balance')

Produces:

AssertionError: Expected transaction to be reverted with Insufficient available balance, but other exception was thrown: Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"name":"ProviderError","code":3,"_isProviderError":true,"data":"0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001e496e73756666696369656e7420617661696c61626c652062616c616e63650000"}, method="estimateGas", transaction={"from":"0xe5B256071adac2Ff3a7a27EC268b59b7f31d1F04","maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x9502f900"},"maxFeePerGas":{"type":"BigNumber","hex":"0x9502f912"},"to":"0xf7C7f2AE52BcE0B785AF7795938DAaB6b2612952","data":"0xdee4dea00000000000000000000000000000000000000000000000000000000000000003","type":2,"accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.4.5)

Even though on Alchemy, I can see that the transaction reverted correctly:

{"jsonrpc": "2.0", "id": 50, "error": {"code": 3, "message": "execution reverted: Insufficient available balance", "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001e496e73756666696369656e7420617661696c61626c652062616c616e63650000"}}

Is this a bug or am I missing something obvious?

@cryptobys-rami
Copy link
Author

cryptobys-rami commented Sep 29, 2021

I've tried it also with Ropsten and I have the same issue. Interestingly, the test works with hardhat locally.

Also interestingly, when I do:

TxReceipt = await (await marketplaceContractNftAddr1.withdraw(numberToWithdraw)).wait()

I get the contract error (after some time waiting). However, the test runs much quicker, and reverts with no error (even though the transaction has failed). Its rather baffling - the matcher think its successful when its not, before even waiting for the receipt.

@cryptobys-rami
Copy link
Author

So I tried to figure out exactly whats causing this - I made the simplest of contracts:

contract TestContract {

    function myRevertFunction() public pure {
        revert("thisworks");
    }
}

And the following test:

it('reverts', async function () {
                await expect(testContract.myRevertFunction()).to.be.revertedWith("thisworks")
            })

It works - great. However, if I make the function payable like this:

`contract TestContract {

function myRevertFunction() public payable {
    revert("thisworks");
}

}`

The test fails:

` AssertionError: Expected transaction to be reverted
+ expected - actual

  -Transaction NOT reverted.
  +Transaction reverted.`

I've tried this with different compiler versons (0.6.0/0.7.2/0.8.7) and its the same problem.

Would appreciate some input - is this not meant to work for functions that could change state? Am I missing something? or is it a bug?

Thank you

@cryptobys-rami
Copy link
Author

Note that this is on testnet (Roptsten)

@cryptobys-rami
Copy link
Author

To those interested - toberevertedwith doesn't work on Ropsten or Rinkeby as they dont return any error strings.

It would be useful to include this information in the documentation as it took me a while to figure it out.

@monkeysjump
Copy link

I'm running into a very similar problem, and I am using the hardhat network with interval mining. I think it only works locally without mining because a state changing call can immediately revert, whereas the call needs to be mined on a testnet for it to revert correctly. I had forgotten about callStatic, and I think that is the best way to go around this so thank you for posting

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