Truffle: Is there a way to debug a smart contract deployment/migration when using Truffle develop?

Created on 1 Jun 2018  路  6Comments  路  Source: trufflesuite/truffle

Currently when you execute a debug session within the Truffle develop console using debug <tx id>, it automatically compiles and migrates the current project before entering a debug session, which is heralded by the appearance of the debug command summary.

How can we debug the construction of a smart contract during a deployment/migration? If this is not possible yet, I would like to make this a feature suggestion.

All 6 comments

@roschler This is possible within truffle develop (using MetaCoin) as an example:

$ truffle develop
> truffle(develop)> migrate
... etc ...
Running migration: 2_deploy_contracts.js
  Deploying ConvertLib...
  ... 0xb806956cbc1c9101b7487a399ef5ec0772796d5677659638ae60fe05ea34dc12
  ConvertLib: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
  Linking ConvertLib to MetaCoin
  Deploying MetaCoin...
  ... 0x3a277e87522c77df758e9344ef4f51f17234fab241693afe72b769ebcba9c19a
  MetaCoin: 0xf25186b5081ff5ce73482ad761db0eb0d25abfbf
Saving successful migration to network...
  ... 0x059cf1bbc372b9348ce487de910358801bbbd1c89182853439bec0afaee6c7db
Saving artifacts...
truffle(develop)> debug 0x3a277e87522c77df758e9344ef4f51f17234fab241693afe72b769ebcba9c19a
# Debugger launches ...

It's also possible outside develop by launching ganache-cli (or Geth, which also support the debug_traceTransaction RPC method) in a separate terminal window and setting up a configuration in truffle.js:

truffle.js

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" 
    },
    ... etc ...

ganache-cli

(In a separate terminal window)

$ ganache cli

Migrate

(I've introduced a require(false) statement at the top of the MetaCoin contract constructor to simulate a failure)

$ truffle migrate --network development
# ... etc ...
  Deploying MetaCoin...
  ... 0x021602751ca588d6109a614a68e0886fff7c4f1ce4fa5653edf39516f691ec0b
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.
    at Object.callback (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/contract.js:147:1)

$ truffle debug 0x021602751ca588d6109a614a68e0886fff7c4f1ce4fa5653edf39516f691ec0b --network development
# Debugger launches successfully

@cgewecke A big thank you for that!

Another note: it's not currently possible to debug transactions over Infura. For more on that subject as well as discussion about some constraints with debugging over a local Geth node, see issue #890.

Closing for house-keeping, but please ping if the info above doesn't work for your use case or you run into further problems with this.

@cgewecke Thanks. Pardon the drive-by question, but I find myself entering similar queries into truffle develop repeatedly, as I'm sure many programmers do; many of them complex Javascript queries that are lengthy. If I want to write my own editor/wrapper that executed queries against whatever engine is driving the Truffle develop environment when you launch it in a Terminal window, what repo would I fork to do this?

@roschler No worries!

truffle-develop is located in truffle-core and much of its logic is here.

Another command you might want to check out is truffle exec which lets you run an arbitrary script in a context that has web3 and the artifacts module injected into it. It's not interactive but it can be good for writing little routines and seeing the outcome.

truffle exec <script.js> [--network <name>]

Thanks Christopher, will do.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ysfAskri picture ysfAskri  路  4Comments

ferittuncer picture ferittuncer  路  3Comments

tcurdt picture tcurdt  路  3Comments

hefgi picture hefgi  路  3Comments

EmanHerawy picture EmanHerawy  路  3Comments