QA InfoTech » Blockchain » TRUFFLE – A Framework for Solidity Smart Contracts
TRUFFLE – A Framework for Solidity Smart Contracts
20 Aug, 2019

TRUFFLE – A Framework for Solidity Smart Contracts

It’s very tempting to work on emerging technologies and going insane to explore more on that. This article includes details about one such framework used for blockchain development. Considering you have a basic idea about the smart contracts development, we move forward to learn about the truffle framework for the deployment and testing of smart contracts. 

Let’s face it, it’s very difficult to manage the lifecycle and processing of a smart contract. To do so, we have to first write the contract and compile it using solc (the solidity compiler). After successful compilation, we get the contract artifacts such as ABI, bytecode, etc which is then used to deploy the contract on the connected network. Once the contract is deployed, we then use the deployed contract’s address to perform the testing. 

This procedure requires extra time and attention from the smart contract developer. To enhance the effortless processing of the smart contract, the Truffle suite is developed. Truffle framework is designed for the smart contract developer to deploy and test their contracts using some basic commands. 

Truffle provides the following feature set to the developers:

  • 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.

After knowing the feature set, smart contract heroes must be excited to learn more about the technical implementation and usage of the Truffle framework. Let’s get started with a practical example.

Installation: Install truffle in your system using npm:

$ npm install -g truffle

Skip this step if the truffle is already installed. 

Network Setup: To deploy the contracts, we will be requiring a blockchain network. One can create their private network using geth or connect to any testnet (Network configurations need to be defined in the truffle configuration file). In this article, we are going to use ganache-cli, which is the ethereum testrpc having 10 accounts with 100 ether balance. In a separate terminal, install and run the ganache-cli using npm:

$ npm install -g ganache-cli
$ ganache-cli

Project Creation: Create a project directory and navigate inside that directory:

$ mkdir truffle_app
$ cd truffle_app

Project Structuring: Initialize the project with truffle by running either of the following commands:

$ truffle init
$ truffle unbox <box_name>

Truffle init is used to initialize the truffle project with no smart contract included. Truffle unbox command is used to download any project from the truffle box. After successful execution, the project structure is created with the following directories or files inside the project directory:

  1. contracts/: This directory includes all the solidity smart contracts.
  2. migrations/: This directory includes the scriptable migration files.
  3. test/: This directory includes the test files written in javascript and/or solidity.
  4. truffle-config.js: This file includes the truffle configurations for the compiler, network, plugins, test (mocha), etc.

Network Configuration: As we are using ganache-cli, we need to configure networks for development in the truffle-config file by setting host to “127.0.0.1”, port to 8545 (default port for ganache-cli) and network_id to “*” (any network).

Contract Compilation: Once the project structure is created, define all your solidity smart contracts in the contracts/ directory. Truffle uses the default solc compiler to compile the contracts or it can use the external compiler if defined in the truffle configuration file. 

$ truffle compile

This command will create the build/ directory inside the project directory including the contracts/ directory for storing the contract artifacts in the .json file. Each contract has its own .json file irrespective of the contracts file. (Suppose there is one contract file in the contracts/ directory which includes two contracts, then after compilation, the build/contracts/ directory have two .json files for each one).

For more details, refer to contract compilation truffle documentation.

Contract Migration: Migration files can be written in javascript which includes the contract artifacts using the artifacts.require() as shown below. 

let SampleContract = artifacts.require(‘SampleContract’);

Migration files have module.exports function defined as below:

Module.exports = function(deployer, network, accounts) {
// Deploy your contracts as shown below
deployer.deploy(SampleContract, arg1, arg2, { from: accounts[1] });
}

File name starts wth a number which is used to keep track of the sequence and is followed by the basic description. Once the migration files are defined, one can deploy or link the contracts using the following command:

$ truffle migrate

This will generate the addresses of the contracts deployed. 

For more details, refer to running migration truffle documentation.

Contract Testing: Contracts can be tested by writing the test cases in the following: Javascript and typescript, for exercising your contracts from the outside world, just like your application.

Solidity, for exercising your contracts in advanced, bare-to-the-metal scenarios.

To run the automated test cases defined in the test/ directory: 

$ truffle test

To know how to write test cases using truffle, follow the truffle testing documentation.

As now you have the basic idea about the Truffle framework, start working around with it and enjoy the ease of smart contract deployment and testing with just a few commands. 

To obtain a deeper knowledge and a better understanding of Smart Contract Audit and how it sustains the back-end functioning of delocalized systems just talk to our experts.

About the Author