33

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?

19
  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.
  • Mix IDE has been discontinued... – CodingYourLife Sep 19 '18 at 15:31
  • @CodingYourLife updated the answer with the latest resources – Paul Berg Nov 23 '18 at 13:41
14

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.

  • 1
    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 '16 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 '16 at 5:14
  • 6
    But when execution throws no events are logged in Solidity – Kirill Slatin Feb 13 '17 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 '17 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) – user2284570 May 24 '18 at 9:13
8

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

  • 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. – user2284570 May 24 '18 at 9:03
4

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
0

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

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.