1

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?

1

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.

| improve this answer | |
  • 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. – Sowmay Jain Apr 15 at 19:43
  • Yes it's possible to automate. Not trivial, though, as the tools required may not work together very well. – Lauri Peltonen Apr 16 at 5:11
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.

| improve this answer | |
-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.

| improve this answer | |
  • I don't see how this answers the question. This is only useful for reading explicit revert reasons, not for simulating transactions – Lauri Peltonen Apr 16 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. – Markus - soliditydeveloper.com Apr 16 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/… – Lauri Peltonen Apr 16 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. – Markus - soliditydeveloper.com Apr 16 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 – Lauri Peltonen Apr 16 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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