Skip to content

Truffle Info

andrewgordstewart edited this page May 2, 2018 · 11 revisions

A place to jot notes about truffle, and fill in gaps in the truffle documentation.

General facts

The ethereum network

The network used when running a truffle command depends on the context:

If you specify a network in truffle.js, truffle will run using that network by default. This is useful in case you'd like to run truffle debug on a particular transaction.

Note that truffle debug is broken as of version v4.1.7. Also, you can't debug transactions sent during test runs, since they get rolled back*.

*Empirically, they don't seem to get rolled back. This means tests are not only non-deterministic, but they are also non-idempotent.

Some notes:

  • this network must be running -- if it's a local network, you'll need to start a test network using ganache or ganache-cli
  • truffle will not alter the network's state prior to running the network's state. This results in non-deterministic tests.

If you do not specify a network in truffle.js, by default your tests will run on a freshly created, ephemeral ganache (testRPC?) network.

You can overwrite the default behaviour with the --network option (maybe for only certain commands?):

truffle debug --network ropsten

Local networks come with 10 (hard-coded) accounts

Note that truffle migrate charges the gas fee to the first such account in order to deploy contracts. This results in unpredictable test behaviour if using accounts[0] in a test case.

Develop

truffle develop gets you a generic node (I think?) REPL which comes configured with:

  1. a bunch of modules (eg. web3) pre-loaded
  2. a connection to an ethereum network
  3. a few commands defined by the truffle framework

If you write some migrations and run truffle migrate, the smart contracts will be deployed to the testRPC network of (2.). You can use the repl to interact with these contracts.

The available commands are defined here, though some are excluded.

Testing

truffle test runs test files in the ./tests directory.

By default, transactions are sent to the network from the address found at accounts[0]. In a test file, you can specify the sender with web3.eth.sendTransaction({from: <sender address>, to: <receiver address>, value: <number of wei>}).

Questions

  1. I want to explore a test case. Can I include a debugger in the testing framework so that the debugger; keyword will take effect?
  2. If I specify a network in truffle.js, are the changes made during test cases reverted? What happens if another application is concurrently using this network?
  3. If I want to send a truffle Contract instance some ether, how can I specify the sender? This doesn't seem to work: contractInstance.send({from: <some address>, value: <some wei>})

Dynamically created contracts

Truffle dynamically creates some contracts at test time based on your local environment. For instance, the DeployedAddresses contract, found in the meta-file truffle/DeployedAddresses.sol, gives you access to the address of contracts deployed locally via truffle migrate.