Deploying Smart Contract with Truffle

Apoorv Maheshwari
Blockchain Research Lab
4 min readDec 21, 2020

Truffle, the most popular development tooling for Ethereum programmers. Easily deploy smart contracts and communicate with their underlying state without heavy client side programming. An especially useful library for the testing and iteration of Ethereum smart contracts.

Truffle is a development environment (providing a command-line tool to compile, deploy, test, and build), framework (providing various packages to make it easy to write tests, deployment code, build clients, and so on) and asset pipeline (publishing packages and using packages published by others) to build ethereum-based DApps.

The most popular development tooling for Ethereum programmers
Deploying Smart Contract with Truffle

With Truffle, you get:

● Built-in smart contract compilation, linking, deployment and binary management.

● Automated contract testing with Mocha and Chai.

● Configurable build pipeline with support for custom build processes.

● Scriptable deployment & migrations framework.

● Network management for deploying to many public & private networks.

● Interactive console for direct contract communication.

● Instant rebuilding of assets during development.

● External script runner that executes scripts within a Truffle environment.

To use most Truffle commands, you need to run them against an existing Truffle project. So the first step is to create a Truffle project.

Let’s start by installing truffle:

$ npm install -g truffle

Ensure that it is installed:

$ truffle 
Truffle v3.2.1 - a development framework for Ethereum
Usage: truffle [options] Commands:
init Initialize new Ethereum project with example contracts and tests
...

Then create the project:

$ mkdir storage_smart_contract_example $ cd storage_smart_contract_example $ truffle init

From there, you can run truffle compile, truffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests.

Truffle comes bundled with a local development blockchain server that launches automatically when you invoke the commands above. If you’d like to configure a more advanced development environment we recommend you install the blockchain server separately by running npm install -g ganache-cli at the command line.

ganache-cli: a command-line version of Truffle’s blockchain server.

ganache: A GUI for the server that displays your transaction history and chain state.

The structure of a Truffle Project

The Truffle folder you create will look something like this:

  1. contract

● ConvertLib.sol

● MetaCoin.sol

● Migrations.so

2. migrations

● 1_initial_migration.js

● 2_deploy_contracts.js

3. test

● TestMetacoin.sol

● metacoin.js

4. truffle-config.js

5. truffle.js

A useful library for the testing and iteration of Ethereum smart contracts.
Truffle Logo

Now moving further to write the code, you will see that truffle created the file structure to us. Go to the contracts folder and create Storage.sol file and then write the code for the smart contract in it.

pragma solidity ^0.4.8; contract Storage {  
uint256 storedData;
function set(uint256 data) {
storedData = data;
}
function get() constant returns (uint256) {
return storedData;
}
}

Now go to the migrations/2_deploy_contracts.js and modify it to look like as follows:

var Storage = artifacts.require("./Storage.sol"); module.exports = function(deployer) {  
deployer.deploy(Storage);
};

Now that we got the basic set up on we need to deploy it to a blockchain. For simplistic sake, let’s use testrpc which does the job well for testing development purposes.

On a separate tab, type these commands:

$ npm install -g ethereumjs-testrpc $ testrpcEthereumJS TestRPC v3.0.3 Available Accounts 
==================

Then back on the tab where you have the Truffle project run:

$ truffle compile $ truffle migrate

And thus we are done with deploying the contract.

Let’s check if we are able to call the contract functions.

$ truffle console truffle(development)> 
Storage.deployed().then(instance => instance.get.call()).then(result => storeData = result)
{ [String: '0'] s: 1, e: 0, c: [ 0 ] } truffle(development)> storeData.toString()
'0'

Alright, let’s see now we if can set storeData to be value 99.

truffle(development)> 
Storage.deployed().then(instance => instance.set.sendTransaction(99)).then(result => newStorageData = result) '0xc5e2f9c9da4cf9f563c8e59073d5b6ca9458f112a6dcfc14aaea7c16a99422d4'
truffle(development)> Storage.deployed().then(instance => instance.get.call()).then(result => storeData = result) { [String: '99'] s: 1, e: 1, c: [ 99 ] }
truffle(development)> storeData.toString() '99'

That it is.

What are Truffle Teams?

Truffle Teams streamlines a team’s workflow, giving them a shared compilation and testing environment for continuous integration. Truffle Teams enables a team to monitor deployed smart contracts’ transaction, state, and events. It also enables for visual presentation of useful data and configurable notifications of errors provide critical insight for the team.

Truffle Team Dashboard
Truffle Team Statistics

Benefits:

The key benefit of Truffle Teams is the ability to enable a team, no matter the size, to view where your project is going during the enter product life cycle. From the development perspective, teams can watch where things are failing and succeeding to respond accordingly. From the operations side, teams are monitoring what’s been deployed and how people are interacting with your contracts with as little configuration as possible.

What is TruffleCon?

TruffleCon is a virtual community gathering for Truffle users, fans, developers, and those who want to build world-changing applications powered by decentralized technologies.

TruffleCon’s goal is to inspire you to build your dapps and smart contracts to bring your ideas to the world! This year we’ve created eight tracks for you to build your own conference experience and engage with the topics that interest you the most!

Key Takeaways:

● Truffle is a NodeJS Framework for Ethereum, used to compile and deploy smart contracts.

● The contract abstractions provided by Truffle contain a wealth of utilities for making interacting with your contracts easy.

● Truffle Teams removes environmental inconsistencies by providing a unified environment for continuous smart contract testing.

● TruffleCon is a way to connect with other passionate developers in the blockchain space to share projects, exchange tips and tricks, and discuss challenges and successes.

--

--

Apoorv Maheshwari
Blockchain Research Lab

Blockchain | Flutter Developer | Artificial Intelligence | Freelance Content Writer | Contributor at GeeksforGeeks