Debugging Solidity
When we first started working with solidity, we didn't know there were debugging tools available. We were basically trying to debug by commenting out lines and occasionally trying to emit debugging events, all of which was incredibly painful. Don't do this - use a debugger!
Using the truffle debugger
To use the truffle debugger, you'll want a long-running local network.
truffle develop
truffle develop
The simplest way to achieve this is through truffle develop
,
This will start a local development network at
localhost:9545
, or attach to one if already running on this port.It will then open a node console, in which you can run javascript, with a web3 instance made available to you, connected to this network.
Call this console
RUN
.If you go this way, you should run
truffle develop --log
in another terminal window, which will attach to the network started in the previous step.Call this console
LOG
.You can then run eg.
migrate --reset
(or justmigrate
) in consoleRUN
.In console
LOG
, you'll see a list of ethereum operations.These are called anytime you run
truffle migrate
from the command line.You can then run eg
In console
LOG
, you'll see something likeGrab the transaction hash, and in console
RUN
:This will put you into a debug session.
You're on your own now.
Good luck.
truffle debug
truffle debug
If you wish to debug a transaction from your development network, you can run
directly from the command line. You have to run this from a folder containing truffle.js
, and it will look for the specified transaction on the network labeled development
in this configuration file.
You might wish to do this, for instance, if you're running some tests against a long-running development network. I this case, use it.only
to filter down to one test, then grab and log the transaction you want to debug
Then run truffle debug $TX_HASH
from the command line.
Notes
You can only debug transactions, which means you can't debug external calls to pure or constant functions. As a workaround, you can mark these functions as not pure. This will cause a compiler error in solidity 0.5.x, but we're still on 0.4.x, where it is just a compiler warning.
An alternative method:
@andrewgordstewart you can't debug eth_call, but you can use truffle-contract to send calls as transactions, doing something like myContract.myViewFunction.sendTransaction(...args)
the other option, @andrewgordstewart, is to petition the Ethereum community to standardize a debug_traceCall JSON-RPC method. whatever's easier.
References
Debugging your contracts | Truffle Suite
Ethereum Development On Windows — Web3, and Truffle Debugging -- maybe there's something different in windows?
Last updated