Truffle - cogeorg/teaching GitHub Wiki

Truffle is a world class development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get:

  • Built-in smart contract compilation, linking, deployment and binary management.
  • Automated contract testing for rapid development.
  • Scriptable, extensible deployment & migrations framework.
  • Network management for deploying to any number of public & private networks.
  • Package management with EthPM & NPM, using the ERC190 standard.
  • Interactive console for direct contract communication.
  • Configurable build pipeline with support for tight integration.
  • External script runner that executes scripts within a Truffle environment.
Source: Truffle 2019-03-28

Thus, Truffle is the (almost) all-in-one solution for Ethereum development.

Installation

Truffle is installed as an NPM package. If you already have an older version of Truffle, make sure to uninstall that first by running

$ npm uninstall -g truffle

Then install the newest version of Truffle:

$ npm install -g truffle

If your are running this on Ubuntu, make sure to run it with super-user rights, i.e. sudo npm ...

Creating a Project

If you are already and experienced Ethereum developer, you can start a fresh project by calling

$ truffle init

in the desired directory. It will create the following files and subdirectories

├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
└── truffle-config.js

The Migrations.sol contract is used by truffle to check which deployment scripts have already run on each network and makes sure that deployed contracts are not lost accidentally.

The migrations directory contains the logic used during the deployment process. The scripts in this directory are sequentially prefixed and run in sequence when we trigger a migration. 1_initial_migration.js deploys the utility contract Migrations.sol. This script should always run first.

The test directory should contain the test scripts written by you to test your code. You should always test your code, especially before deploying your contracts to the main network. We will discuss testing in section Test and Deploy with Truffle. Truffle makes it possible to test your code using JavaScript or Solidity.

The truffle-config.js file configures your environment and more specifically the network where you want to deploy your contracts. By default, truffle-config.js is empty because truffle is bundled with an in-memory node powered by the same core, called Ganache core, as the one used by Ganache.

Using a boilerplate project

For those just getting started, Truffle offers many boilerplates and example applications available through Truffle Boxes. To use, e.g., the MetaCoin box, run the folloing in the command line

$ mkdir MetaCoin
$ cd MetaCoin
$ truffle unbox metacoin

It will download the following content

├── contracts
│   ├── ConvertLib.sol
│   ├── MetaCoin.sol
│   └── Migrations.sol
├── LICENSE
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
│   ├── metacoin.js
│   └── TestMetacoin.sol
└── truffle-config.js

The example smart contract is in MetaCoin.sol and a helper function is in ConvertLib.sol. The deployment of the contract is handled in 2_deploy_contract.js and tests are provided written in JavaScript and Solidity in the test folder.

Deploying the smart contract

You need a running node which can be either your private node or a development blockchain. The latter can be launched on port 9545 by running

$ truffle develop

in the command line, which will also start an interactive JavaScript console. Alternatively, you can start Ganache, which will use port 7545 by default.

Next, you need to configure your truffle-config.js file to include

module.exports = {
   networks: {
     development: {
       host: "localhost",
       port: 9545,
       network_id: "*" // Match any network id
    }
  }
};

It allows truffle to listen to localhost:9545. Finally, you can deploy the contract by calling

$ truffle migrate

from the command line or alternatively

> migrate

in the truffle development console. To reset already existing contracts, provide the command with the flags --compile-all and --reset. The --compile-all flag forces a recompilation of all solidity files and the --reset flag resets the states of the migrations contract and forces a rerun of all the migrations scripts.

Testing the smart contract

It is best practice to test your contracts before deploying them. These tests can be either written in Solidity or in JavaScript. Truffle can run both kinds by calling

$ truffle test
⚠️ **GitHub.com Fallback** ⚠️