Deploy Your First Smart Contract With Truffle
Photo: Google

Deploy Your First Smart Contract With Truffle

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

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:

mkdir HelloTruffle

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:

truffle init

This command creates a bare Truffle project without anything else included. This command will show the following message in your console.

truffle init
truffle init command

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

ls

truffle project structure

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.

truffle develop

truffle develop command

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.

truffle develop --log

truffle develop --log command

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

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.

compile

truffle compile command

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.

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.

migrate

truffle migrate command

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.

truffle migration

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.

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.

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.

truffle(develop)> app

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

app.getGreeting()

smart contract function call

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.

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

smart contract address

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

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

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

smart contract addresses

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

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

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

writing to smart contract

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.

app.getGreeting()

truffle command

Exiting the truffle cli

Just type .exit to exit from truffle develop window.

.exit

truffle exit command

Further Reading

How to build lists and navigation in SwiftUI

Learn about module design pattern in JavaScript

Understand Shallow And Deep Copy In JavaScript

Adesh

Hi, I am Adesh. I am a senior software engineer having more than 12 years of software development experience. I am a full stack developer and interested in writing the technical post on programming. I am currently working in New York City area.

This Post Has 3 Comments

  1. Good people, already the third day, the money has not arrived yet. They stole my money. They clarified everything and proved that the robber is Blockchain.com. One of the company’s employees created a fake Wallet bitcoin and steals money through this wallet. In my example, it is wallet: “1BU3EMX9r1kqeeCSr38139bDvWHcqJ9dad” Given time, this wallet is running and doing operations. Everything is proved that money was stolen for this cough. But blockchain.com does not take any measure to temporarily block wallet. We just found out this wallet is owned by company employees. Help me what to do????? I trusted them but they cheated.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu
%d bloggers like this: