This page looks best with JavaScript enabled

Deploy Your First Smart Contract With Truffle

 ·  ☕ 5 min read  ·  ✍️ Adesh

In this tutorial, we are going to create and deploy a simple smart contract Truffle. We will also learn how to use it with the Truffle-CLI tool.

What is Truffle?

Truffle is a 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 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.

Install Truffle

In order to install Truffle, you must have Node.JS and NPM installed in your machine.

Truffle requirements

  • NodeJS v8.9.4 or later
  • Windows, Linux or Mac OS X

Installation

1
npm install -g truffle
Note: Before creating a Solidity smart contract, we should install Solidity extension for Visual Studio Code, if you are going to use it.

Solidity - Ethereum Solidity Language for Visual Studio Code

Solidity extension for Visual Studio Code

Solidity Extension for Visual Studio Code

Create a Solidity Smart Contract Project

Let’s create our project folder, which we will call HelloTruffle. You can do this using the same terminal/cmd, and executing these commands:

1
mkdir HelloTruffle
1
cd HelloTruffle

With Truffle, you can create a bare project template, or use Truffle Boxes, which are example applications and project templates. For this tutorial, we will be starting from scratch, so we execute this command:

1
truffle init

This command creates a bare Truffle project without anything else included.

1
truffle init command

Once this command runs successfully, you can check the default project structure using ls command.

1
ls

It will create the following files and folders in your project folder.

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment
  • test/: Directory for test files for testing your application and contracts
  • truffle-config.js: Truffle configuration file

Running truffle develop command

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

1
truffle develop

This command will show you the url of local development blockchain with 10 default accounts and private keys.

Open a new tab in your terminal window and run a log command.

1
truffle develop --log

--log: Start/Connect to a Truffle develop session and log all RPC activity.

Creating a Solidity Smart Contract

Open this project in your Visual Studio Code editor.

Let’s create a solidity smart contract named as Hello.sol in your /contracts folder. Here, I am assuming that you are aware of creating solidity smart contracts. This tutorial doesn’t focus on creating a smart contract. For more details about Solidity, click the below link.

Solidity

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
pragma solidity >=0.4.21 <0.7.0;

contract Hello {
    string greeting;

    constructor() public {
        greeting = "hello";
    }

    function getGreeting() public view returns(string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;
    }
}

Compiling Smart Contract

Once you have created your first smart contract Hello.sol. It’s time to compile it using truffle-cli which is now showing in your terminal window. Run below command to compile your smart contract class file.

1
compile

If there is no error, it will successfully compile your smart contract and show the above message.

Migrating Smart Contract

Once you compiled your smart contract class, it’s time to create a migration file. Create a migration file 2_deploy_contracts.js in /migrations folder.

1
2
3
4
5
var Hello = artifacts.require("./Hello.sol");

module.exports = function(deployer) {
  deployer.deploy(Hello);
};

Once you created the contract migration file, it is time to migrate it using truffle development cli. Let’s get back to the terminal window again and type this command there.

1
migrate

It will start from migrating 1_initial_migration.js file and it will show various migration details like transaction hash, contract address, and total cost, etc.

Then it will migrate our contract file 2_deploy_contract.js and all migration details. Now, you can get your contract’s address by the following command.

1
Hello.address

Reading Smart Contract

Once our smart contract deployed to a blockchain, now it’s time to interact with it. We are going to use our terminal to interact with it.

Let’s first create an instance of our smart contract class using the below code.

1
Hello.deployed().then(function(instance){ app = instance; })

Once you have created an app instance of our smart contract class, you can get complete details of Hello smart contract.

1
truffle(develop)> app

Now, let’s access our smart contract function getGreeting() and get the message in the terminal.

1
app.getGreeting()

It will display hello in the terminal.

Writing to Smart Contract

Before going to write to our smart contract. Let me tell you briefly about web3.eth package. Click the below link to know more about web3.eth

web3.eth

The web3-eth package allows you to interact with an Ethereum blockchain and Ethereum smart contracts.

There is a function called setGreeting() which is used to write data in smart contract block. In order to call this function through the terminal, we have to pass the sender account’s address.

To get all available accounts, use this command.

1
web3.eth.getAccounts().then(console.log)

Let’s assign all these available accounts to a variable.

1
web3.eth.getAccounts().then(function(result){ accounts = result })

Now, you can check this accounts array variable. Just type accounts in your terminal window.

Now, write our next command to call the setGreeting() function in the terminal.

1
app.setGreeting("Hello ZeptoBook", { from: accounts[0] })

Once this command executes successfully, it will show the transaction details.

To check whether our message is updated in our smart contract or not, call getGreeting() function again. It will show you the latest message now.

1
app.getGreeting()

Exiting the truffle cli

Just type .exit to exit from truffle develop window.

1
.exit

Further Reading

How to build lists and navigation in SwiftUI

Learn about module design pattern in JavaScript

Understand Shallow And Deep Copy In JavaScript

Share on

Adesh
WRITTEN BY
Adesh
Technical Architect