What is Truffle Suite? Features, How to Install, How to Run Smart Contracts

In the early days, when Blockchain just began its journey into the mainstream tech domain, you had to develop Smart Contracts using Solidity programming language and deploy them by calling the geth or solc functions.

To make this process a tad easier, you had to write bash scripts that would first compile and then deploy the contract. Now, although the latter approach was better than the former, it was pretty rudimentary, given that bash scripting often entails a lack of standardization.

This need for creating a seamless and optimal development and testing framework for Blockchain Smart Contracts is what gave birth to Truffle Etheruem.

What is the Truffle Suite?

According to Truffle Suite, Truffle is a “world-class development environment, testing framework, and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.”

In simple words, Truffle is a development environment, testing framework, and asset pipeline all rolled into one. It is based on Ethereum Blockchain and is designed to facilitate the smooth and seamless development of DApps (Distributed Applications). With Truffle, you can compile and deploy Smart Contracts, inject them into web apps, and also develop front-end for DApps. Today, Truffle is one of the most widely used IDEs for Ethereum Blockchain.

The Truffle Suite comprises of three core elements: 

  • Truffle – The actual development environment that integrates compilation, testing, and deployment of Smart Contracts.
  • Ganache – It is a locally deployed Blockchain simulator. Ganache features a graphical user interface that can simulate Blockchain networks and live-test Smart Contracts without requiring you to set up real test networks or using to a remote network.   
  • Drizzle – It is an assortment of front-end libraries that offer useful components for developing web applications that can seamlessly connect with the Smart Contracts. 

Read: How To Become A Blockchain Developer – A Beginners Guide

What are the features of Truffle?

The features that make Truffle Etheruem one of the most widely used IDEs for Ethereum Blockchain are:

  • It has built-in support for compiling, deploying and linking Smart Contracts.
  • It allows for automated contract testing with Mocha and Chai.
  • The Truffle Console allows you to work with your compiled contracts in a hassle-free manner.
  • It has a configurable build pipeline that supports both console apps and web apps.
  • It comes with built-in support for JavaScript, CoffeeScript, SASS, ES6, and JSX.
  • It has generators that help in the creation of new contracts and tests (for example, rails generate).
  • It has a script runner that allows you to run JS/Coffee files, including your Smart Contracts.
  • It allows for the instant rebuilding of assets during the development stage.
  • It enables contract compilation and deployment using your preferred choice of RPC client.
  • It supports both network and package management.

How to install Truffle Ethereum?

You can install Truffle Ethereum using the Node Package Manager (npm). First, you need to set up NPM on your computer, and then install Truffle. To do so, you need to open the terminal and type the following:

npm install -g truffle

In Linux machines, however, you might need to add “sudo” prefix to the above statement. Once you’ve installed the Truffle framework, make sure to download and install the Ganache blockchain simulator. You can visit the official Ganache website – it contains installers for most popular platforms.

Now that you’ve successfully installed Truffle, you can start a Truffle project in two ways. To create a bare project, you can type the following statement:

truffle init

However, you can also use the Truffle unbox command to create a project with existing code. For instance, you can create a token like so:

truffle unbox metacoin

On running this command, a project will be created from a Truffle box. You can find many Truffle boxes on the official website. They contain boilerplate code and all the necessary configurations for building projects and launch them quickly.

The structure of a Truffle Project

The Truffle folder you create will look something like this:

├── contracts

│ ├── ConvertLib.sol

│ ├── MetaCoin.sol

│ └── Migrations.sol

├── migrations

│ ├── 1_initial_migration.js

│ └── 2_deploy_contracts.js

├── test

│ ├── TestMetacoin.sol

│ └── metacoin.js

├── truffle-config.js

└── truffle.js

Now, we’ll discuss in detail about the different sub-folders contained in your Truffle folder.

Contracts folder

The Contracts folder is one where all your Smart Contracts are stored. In this folder, there’s also a special file known as the Migrations.sol file. Usually, when Truffle compiles your project, it will browse through the Contracts folder to compile all the compatible files.

Migrations folder

Truffle Migration refers to a script that defines how your Smart Contracts will be deployed to the Ethereum Blockchain. Migrations are essential because as your project keeps becoming more complex, the complexity of your deployments also increases. Thanks to Truffle Migrations, you can automate the process of the deployment of Smart Contracts in a sequential manner.

If you look back at the Truffle folder structure, you will see a file called 1_initial_migration.js. This file deploys the Migrations.sol contract to the Ethereum Blockchain.

Migrations allow you to:

  • Set the maximum gas limit for deployments
  • Modify or change the from the address of deployments.
  • Call arbitrary contract functions.
  • Deploy libraries

Test folder

When it comes to Smart Contracts, testing is a must! Thankfully, Truffle has a built-in testing framework that allows you to write tests in Solidity or JavaScript. If you write your tests in Solidity, you have to import the Smart Contracts into the tests with the Solidity import directive, like so:

import “../contracts/MetaCoin.sol”;

However, if you are writing tests in JavaScript, you have to import them using the artifacts.require() helper function, like so:

var MetaCoin = artifacts.require(“./MetaCoin.sol”);

Configuration file

The configuration file, also known as, truffle.js or truffle-config.js, defines how Truffle can connect to Ethereum networks along with the following things:

  • Environments – It allows you to can define the address of the geth note, the network_id, the max gas limit for deployment, and the gas price you are willing to pay.
  • Project structure – You can change the location where the files are built and stored.
  • Compiler version and settings – You can fix the solc version and set -O (optimization) parameters.
  • Package management – Truffle is compatible with EthPM (Ethereum Package Manager). You can set up dependencies for EthPM for your Truffle projects.
  • Project description – It states who developed the project, specifies the project name, contact addresses, and so on.

Also read: Blockchain Project Ideas for Beginners

How to run Smart Contracts?

Here are the different ways to run Smart Contracts – 

  • For compiling Smart Contracts, you have to run the following code:

truffle compile

  • For running migrations, you have to use the following code:

truffle migrate

You can also do it by mentioning a specific environment:

truffle migrate –network live

  • For testing Smart Contracts, you can run the following code:

truffle test

You can also run a specific test by using the following code:

truffle test ./path/to/FileTest.sol

In this post, we’ve aimed to give you the basic idea of Truffle Etheruem and how it functions. Truffle is a powerful tool that makes the development of DApps and Smart Contracts much more convenient by bringing the standard development practices into the Blockchain picture.

Wrapping up

There is a rise in careers in blockchain technology and blockchain has tremendously changed the very face of the technology industry forever. If you’re interested to become a blockchain developer and build smart contracts and chaincodes, checkout IIIT-B & upGrad’s PG Certification in Blockchain Technology.

Become a Blockchain Developer

Apply Now @ UPGRAD

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Blockchain Course