50

When developing my smart contract, how can I debug it? Are there any debugging, testing tools or simulators that I can use to examine my code?

1

7 Answers 7

28
  1. The Remix IDE has a great integrated block-by-block simulator and debugger.
  2. Ganache allows fast contract testing without the hassle of running a node.
  3. Once the contract is live, the etherscan.io block explorer can be used to view internal state and EVM execution logs.
2
  • 2
    Mix IDE has been discontinued... Sep 19, 2018 at 15:31
  • 1
    @CodingYourLife updated the answer with the latest resources Nov 23, 2018 at 13:41
18

Those are all good tools, but I have found the logging events is much easier method for debugging, plus if you are doing anything with money you want to keep a record of what happened.

In Solidity, you log with Events. You can then listen to all events on your contract with web3.js "allEvents" callback

This is how I debug my (currently) 780 lines of solidity.

6
  • 2
    Could you please elaborate a bit more on how you go about debugging with events? Using a javascript console in order to connect with the contract manually and watch for events or are you using a script somehow to automate the process? This sounds like a good technique and I am trying to apply it by console.log event results in truffle unit tests but have not managed to make it work yet!
    – EugVal
    Jul 7, 2016 at 9:30
  • 1
    I'm using nodejs scripts, which work with web3.js just fine. Here's a hacked up version of ether pudding that includes logging (but from one contract only for now), see unit tests for how it works. github.com/barkthins/ether-pudding
    – Paul S
    Jul 10, 2016 at 5:14
  • 11
    But when execution throws no events are logged in Solidity Feb 13, 2017 at 11:19
  • good point. I try to avoid exceptions if at all possible because they eat up all your gas, amongst other things. I would love it if someone added an answer on how to post-hoc debug an exception being thrown. AFAICT you have to examine a node's logs. I hope someone is working on a 'soft' exception feature in the VM, the 'hard exception' is basically the equivalent of a BSOD...
    – Paul S
    Feb 13, 2017 at 19:11
  • @PaulS And if no events are written even in the simplest case like dumping variables without prior condition, how can I know what is wrong? (and in a case were no exception is logged) May 24, 2018 at 9:13
12

to debug a contract you could use the solidity browser

the debugger provides you the ability to debug any transaction (creation or call) step by step and to visualize the memory/storage space. it helps you to trace the gas cost by opcode and the remaining gas after each step.

To debug transactions, there's two way of doing that:

1-using a block number and a transaction index. enter image description here

2-using a transaction hash.

enter image description here

When loading the transaction succeed, the hash, from and to field will show up. Then the vm trace is loaded. enter image description here

The debugger itself contains several controls that allow stepping over the trace and seing the current state of a selected step.

Slider and Stepping action:

The slider allows to move quickly from a state to another. Stepping actions are:

Step Into Back Step Over Back Step Over Forward Step Into Forward Jump Next Call (this will select the next state that refers to a context changes - CALL, CALLCODE, DELEGATECALL, CREATE) State Viewer:

The upper right panel contains basic informations about the current step:

VMTraceStep: the index in the trace of the current step. Step Add memory Gas: gas used by this step Remaining gas: gas left Loaded address: the current code loaded, refers to the executing code. The other 6 panels describe the current selected state:

Instructions list: list of all the instruction that defines the current executing code. Stack Storage Changes Memory Call Data$ Call Stack

breakpoints in remix Breakpoints can be added and removed by clicking the left margin of the first line of code. You can add as many breakpoints as you want. enter image description here

1
  • Weh I try to debug a transaction, copying transaction hash and clicking the start debugging button does nothing except writing 0xc09ff6fb32dd6ef66f5691367c11c762c8f730aadeab065d57f8137cd3746bfc is not a contract invokation or contract creation in the log. May 24, 2018 at 9:03
8

You can use truffle debug command: http://truffleframework.com/blog/announcing-full-portable-solidity-debugger

It supports:

  • Code stepping (over, into, out, next, instruction, etc.)
  • Current code location, including the address of the running contract
  • Breakpoints
  • Watch expressions
  • Variable inspection (stack, memory and storage)
  • Custom expression evaluation using Solidity variables
1

For debugging Truffle Javascript tests in Visual Studio Code, this answer worked for me: https://ethereum.stackexchange.com/a/41096/3708

1

latest version truffle has an inbuilt debugger that can be used to debug state of a failed transaction.

$ truffle debug <transaction-id>

where <transaction-id> is id of the errored transaction,

once debugger is launched follow the instruction to walkthrough and print variable state etc

find out more here : https://www.trufflesuite.com/blog/announcing-full-portable-solidity-debugger

1

I recently released a public beta of my Solidity Debugger, which is a Visual Studio Code extension. It integrates with the Hardhat framework and allows you to step through solidity smart contracts. Check it out here: https://soliditydebugger.org/

Your Answer

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

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