2

I am using ganache and truffle to write some tests in solidity (following the example in https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-solidity).

I am getting the usual, not really helpful, message: "Error: Returned error: VM Exception while processing transaction: revert".

I have added events in my code, both in the test and in the actual contract.

Nothing is shown when the error happens.

It does not show the transaction that failed and so I cannot try to start a debug session.

With no breakpoints and no logging. How can I debug this test?

Using the console, I've verified that the contract is working as expected.

1

4 Answers 4

3

Some possible options to test things:

  1. Use Hardhat console.log.

  2. Use Tenderly Explorer (you can use testnet verified contracts or even local contracts using their CLI) or if the contract is on testnet, you can also run it in their simulator.

  3. Start isolating the function calls. And identifying one by one until where the call is reaching and what state changes are it making, etc.

Doing 1 is the easiest method, 2 is hard, and 3 is the hardest.

1
  • I used chai test coverage with ```hardhat`` console.log() during development. When contracts had been deployed I switched on local private ethereum chain and test contracts by step-by-step disabling part of functionality to localise the issue. I came to this post by looking a better version to find out bugs in contracts which has been already deployed, but seems my method so far the best one.
    – Gleichmut
    Commented Dec 19, 2022 at 9:27
1

First, event logs are not saved on transactions failing so you can' use logs to see where your code stopped.

If you are using geth you can use the rpc command debug_traceTransaction with the traceCaller argument, it will return all the calls made to each external contract starting from the contract you are calling.

If you are using ganache, the option above is not available, and your best bet is to create a testing smart contract that would never fail transactions in order to get the event logs. Another option is to use solidity's try and catch on the contract calls you think would revert with no message and in your catch block, you can revert with your own message.

the no message revert could easily be an issue with your own contract code not executing correctly, something like an index out of range, or when trying to loop over a list that changes.

3
  • If I understand correctly, by "a testing smart contract" you mean a proxy that will use raw calls and get a boolean as a result (where true means success). Commented Aug 18, 2021 at 21:58
  • No, what i meant is you create a version of the contract you are calling that will never revert. if you can of course. that way you can use the logs to see if your actions were done correctly or not. it's a very specific way, it worked for me in couple of instances but not always. Commented Aug 19, 2021 at 15:30
  • That's not feasible for me without a proxy because I do not know where and why it's failing, but thanks: it could be useful in other cases. Commented Aug 21, 2021 at 9:54
1

You might wanna check Hardhat when it comes to debugging. It's a dev environment where you can place console.logs ( the same way as you do in javascript ) inside your smart contract and this will give you better overview where exactly is your logic breaking.

1
  • Can I use hardat with solidity tests ? I mean, tests written in solidity instead of javascript or typescript. Commented Aug 19, 2021 at 10:42
1

I second the recommendation to try Hardhat - though tests are written in Javascript/Typescript and not Solidity, hardhat's console.sol is incredibly helpful. What's especially great is that the console.log functions they give you will display even when the transaction revert, UP TO the point when the transaction reverts, allowing you to pinpoint exactly when the failure occurs.

I have not tested this, but in hardhat you should be able to have Solidity tests as well as Javascript/Typescript tests, so long as you include Truffle in your node project (if you're using a node project) then you'll have access to Truffle's Assert and all the other Solidity files. Whether they'll work the same, I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.