Truffle - Getting Started

Truffle

Truffle is a development environment for blockchains.

  • You can build, compile, test and deploy smart contracts.

  • You can connect to public or private blockchain networks.

  • You can manage packages with EthPM and NPM.

Install Truffle

Requirements

  • NodeJS v8.9.4 or later

  • A running Ethereum client (for example Ganache or Truffle Develop)

npm install truffle -g

Truffle Commands

Build

Execute build pipeline (if configuration present).

This command is deprecated. Please use third-party build tools like webpack or grunt, or see our Truffle Boxes for an example.

Compile

Compile contract source files.

truffle compile [--list <filter>] [--all] [--network <name>] [--quiet]

Options:

  • --list <filter>: List all recent stable releases from solc-bin. The filter parameter must be prereleases, releases, latestRelease or docker.

  • --all: Compile ‘all’ contracts, not only the contracts changed since last compile.

  • --network <name>: Specify the network to use, saving artifacts specific to that network. Network name must exist in the configuration.

  • --quiet: Suppress all compilation output.

Config

Display whether analytics are enabled or disabled.

truffle config [--enable-analytics|--disable-analytics]

Options:

  • --enable-analytics|--disable-analytics: Enable or disable analytics.

Console

Run a console with contract abstractions and commands available.

truffle console [--network <name>] [--verbose-rpc]

Requires an external Ethereum client, such as Ganache or geth. For a console that creates a development and test environment, use truffle develop.

Options:

  • --network <name>: Specify the network to use. Network name must exist in the configuration.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

Create

Creates new contracts, migrations and tests.

truffle create <artifact_type> <ArtifactName>

Options:

  • <artifact_type>(required): Create a new artifact where artifact_type is: contract, migration or test. The new artifact is created along with one of the following files: contracts/ArtifactName.sol, migrations/####_artifact_name.js or tests/artifact_name.js.

  • <ArtifactName> (required): Name of new artifact.

Debug

Interactively debug any transaction on the blockchain.

This command is considered experimental.

truffle debug <transaction_hash>

Option:

  • <transaction_hash>(required): Transaction ID to use for debugging.

Develop

Spawns a local development blockchain, and allows you to interact with contracts via the command line.

truffle develop [--log]

Option:

  • --log: Start/Connect to a Truffle develop session and log all RPC activity. See the Log RPC Activity docs for more information about using this option.

Exec

Execute a JS module (like web3) within the Truffle environment.

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

Options:

  • <script.js>(required): JavaScript file to be executed. Can include path information if the script does not exist in the current directory.

  • --network <name>: Specify the network to use, using artifacts specific to that network. Network name must exist in the configuration.

  • --compile: Compile contracts before executing the script.

Help

Display a list of all commands or information about a specific command.

truffle help [<command>]

Option:

  • <command>: Display usage information about the specified command.

Init

Initializes new and empty Truffle project within the current working directory.

truffle init [--force]

Option:

  • --force: Initialize project regardless of the current working directory's state. Be careful, this could overwrite existing files that have name conflicts.

Install

Install a package from the Ethereum Package Registry.

truffle install <package_name>[@<version>]

Options:

  • <package_name>(required): Name of the package as listed in the Ethereum Package Registry.

  • @<version>: When specified, will install a specific version of the package, otherwise will install the latest version.

Migrate

Run migrations to deploy contracts.

truffle migrate [--reset] [--f <number>] [--to <number>] [--network <name>] [--compile-all] [--verbose-rpc] [--dry-run] [--interactive]

Options:

  • --reset: Run all migrations from the beginning, instead of running from the last completed migration.

  • --f <number>: Run contracts from a specific migration. The number refers to the prefix of the migration file.

  • --to <number>: Run contracts to a specific migration. The number refers to the prefix of the migration file.

  • --network <name>: Specify the network to use, saving artifacts specific to that network. Network name must exist in the configuration.

  • --compile-all: Compile all contracts instead of intelligently choosing which contracts need to be compiled.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

  • --dry-run: Fork the network specified and only perform a test migration.

  • --interactive: Prompt to confirm that the user wants to proceed after the dry run.

Networks

Show addresses for deployed contracts on each network.

truffle networks [--clean]

Option:

  • --clean: Remove all network artifacts that aren't associated with a named network.

Opcode

Print the compiled opcodes for a given contract.

truffle opcode <contract_name>

Option:

  • <contract_name>(required): Name of the contract to print opcodes for. Must be a contract name, not a file name.

Publish

Publish a package to the Ethereum Package Registry.

truffle publish

Run

Note: This feature is new and still in a beta state.

Run a third-party plugin command

truffle run <command>

Option:

  • <command> (required): Name of a command defined by an installed plugin.

Test

Run JavaScript and Solidity tests.

truffle test [<test_file>] [--compile-all] [--network <name>] [--verbose-rpc] [--show-events]

Options:

  • <test_file>: Name of the test file to be run. Can include path information if the file does not exist in the current directory.

  • --compile-all: Compile all contracts instead of intelligently choosing which contracts need to be compiled.

  • --network <name>: Specify the network to use, using artifacts specific to that network. Network name must exist in the configuration.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

  • --show-events: Log all contract events.

Unbox

Download Truffle Boxes which are pre-built Truffle project.

truffle unbox <box_name>

Options:

  • <box_name> (required): Name of the Truffle Box.

  • --force: Unbox project in the current directory regardless of its state. Be careful, this will potentially overwrite files that exist in the directory.

Version

Shows version number.

truffle version