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?
7 Answers
- The Remix IDE has a great integrated block-by-block simulator and debugger.
- Ganache allows fast contract testing without the hassle of running a node.
- Once the contract is live, the etherscan.io block explorer can be used to view internal state and EVM execution logs.
-
2
-
1@CodingYourLife updated the answer with the latest resources Nov 23, 2018 at 13:41
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.
-
2Could 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!– EugValJul 7, 2016 at 9:30
-
1I'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 SJul 10, 2016 at 5:14
-
11
-
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 SFeb 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
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.
2-using a transaction hash.
When loading the transaction succeed, the hash, from and to field will show up. Then the vm trace is loaded.
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.
-
Weh I try to debug a transaction, copying transaction hash and clicking the
start debugging
button does nothing except writing0xc09ff6fb32dd6ef66f5691367c11c762c8f730aadeab065d57f8137cd3746bfc is not a contract invokation or contract creation
in the log. May 24, 2018 at 9:03
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
For debugging Truffle Javascript tests in Visual Studio Code, this answer worked for me: https://ethereum.stackexchange.com/a/41096/3708
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
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/