11

Is there any way to simulate a transaction without sending it to the blockchain and find the exact error (or line of code), in case it fails if its mined?

4 Answers 4

4

I am a software engineer at Tenderly and our Transaction Simulator is pretty much the solution for the problem you described above and our most used feature. You can:

  • Test out bug fixes and improvements for historical transactions
  • Change the source code of a smart contract before simulation
  • Analyze each transaction using our Visual Debugger and instantly find the line your transaction reverted on

One more thing worth mentioning - you can create a Tenderly Fork and chain multiple transactions to be simulated in succession to test out complex scenarios with live on-chain data.

I hope this answer is useful and don’t hesitate to reach out to me if you have any questions. :)

2

Yes. Depends on what you need.

If it's enough for you to test using a contract you can just deploy the same contract to your local blockchain (Ganache, for example). It's easy to set up. But that blockchain will of course not contain the same state as the real blockchain.

If you require the exact same state as the real blockchain then you have to download the blockchain state and run it locally (without connection to the real blockchain anymore, Ganache) - offline for example. That way you can issue transactions to it for free and you can just roll back the blockchain whenever you need to retry something.

2
  • 1
    That's helpful. Is it possible to automate the whole process programmatically? For example, I run a dapp and whenever a txn feels like failing, we can run the txn locally with the latest state of blockchain and find the exact line of error. Apr 15, 2020 at 19:43
  • Yes it's possible to automate. Not trivial, though, as the tools required may not work together very well. Apr 16, 2020 at 5:11
1

If you are using web3, they now support revert reason strings. You will have to activate the handleRevert option: web3.eth.handleRevert = true.

TestContract.methods.myMethod(myParam).call({
        from,
        value,
}).then(result => { console.log('no error happened' })
  .catch(revertReason => console.log({ revertReason }))

See https://soliditydeveloper.com/web3-1-2-5-revert-reason-strings for more details.

6
  • I don't see how this answers the question. This is only useful for reading explicit revert reasons, not for simulating transactions Apr 16, 2020 at 5:12
  • @LauriPeltonen How do you define 'simulating the transactions'? If you connect to the main blockchain via Web3 and run the TestContract.methods.myMethod(myParam).call, it runs the transaction as it would when you send it, but without actually sending it. Apr 16, 2020 at 5:28
  • No call does not run a transaction and it does not even create a transaction. It's used only for access constant data in the blockchain and its data is read directly from your own node. web3js.readthedocs.io/en/v1.2.0/… Apr 16, 2020 at 5:42
  • @LauriPeltonen Exactly, but you can call any state modifying function. I understand now what you mean by simulating it as in actually sending it to a local blockchain. However, I don't know what that might be useful for. And the original poster asked for 'the exact error (or line of code)' which you can perfectly get the way I described. Apr 16, 2020 at 5:48
  • To me it sounds like a good idea to simulate a tx in advance. If for example you want to issue a tx which either costs a lot or might cause unwanted side effects then you can safely test the consequences locally in ganache Apr 16, 2020 at 5:56
0

I use the Ganache personal blockchain. Then use the Truffle Framework to test all the transactions and contracts. With ganache-cli you can view the transactions in real time on the console.

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.