Smart Contract Development Using Truffle

Kowsik Gelli
7 min readApr 18, 2021

In our previous blog, we have seen how to use remix for smart contract development.

check out our previous blog.

writing your first smart contract

In this blog, we will use truffle framework for developing, testing, and deploying smart contracts. We use ganache a local blockchain to deploy our smart contracts.

Truffle is a development environment and a testing framework for smart contract development. The framework can build Distributed Apps, compile Smart Contracts, deploy Smart Contracts, inject Smart contracts, and DApps into a web app, and can create front-end for DApps and test them.

You should have node js installed in your system.

Type the commands in your terminal to see the version of node js and npm.

node -vnpm -v

Now type the command to install truffle on your computer.

npm install -g truffle

Now check the version of the truffle by typing the following command.

truffle version

Ganache is a local Blockchain that runs on our computer for developing smart contracts. Install it by going to the below website.

After installing run the application to launch.

Click on the quickstart button. Now the local blockchain will be running in the port 7545. It gives you 10 accounts with 100 ether (remember these accounts are only for development).

You can check blocks, transactions, and logs of every transaction here.

Now we have our local Blockchain running let's initiate a truffle project. By typing the following commands.

Note: We will use our lottery smart contract which we have written in our previous blog

mkdir lottery
cd lottery
truffle init

It will start downloading some files for you.

Now open the lottery folder with any text editor. I will be using vs code editor in this tutorial. To open the project in vs code just type the following command in terminal.

code .

You will see the project structure. It contains three folders and one configuration file.

In the contracts folder, we write our smart contracts. In the migrations folder, we write a migration script to deploy our smart contracts. In the test folder, we will be writing tests for our smart contract. Before starting development make sure the port number in the truffle-config.js file is 7545 because we are using ganache for our development which is running in port 7545.

In the contracts folder and migrations folder, there will be some migration files, ignore them they are just for migration purposes.

Now create a file called lottery.sol in the contract folder and paste the lottery smart contract from our previous blog in it.

now type the following command to compile the smart contract.

truffle compile

It will tell that artifacts are written to build/contracts folder. You should see JSON files in the build folder which is created after compiling.

In this Lottery.json file, all the abi, bytecode, and metadata of our smart contract exists.

Now we should write a migration script for our Lottery contract in the migrations folder. You can see the 1_initial_migration.js file in the migrations folder. This script is to migrate the migrations.sol contract in our contracts folder. We will write a similar script for our lottery contract.

Note: make sure the file name starts with ‘2_’ otherwise truffle will not be able to realize. Lets say the file name is ‘2_deploy_contracts.js’

The first line in the file will get all artifacts of our Lottery smart contract.

Now enter the command in the terminal to deploy our smart contract.

Make sure your ganache is running.

truffle migrate --reset

You will get all the logs of the transaction. You can see the deployed contract address.

Now let's interact with our contract from truffle console. To open truffle console type the following command in the terminal.

truffle console

It will open a console like this.

Now let's get the deployed contract instance by typing the following command.

let instance = await Lottery.deployed()

it will return undefined no need to worry, type instance in the terminal you can see the whole instance in your terminal. Let's get the contract address by typing the following command.

instance.address

you will get the contract address which is deployed to the ganache local Blockchain. You can see the all addresses by typing the following command in the terminal.

let accounts = await web3.eth.getAccounts()

You can see all the methods by typing the following command.

instance.methods

Now let's get the manager of our contract, it should be the first address in ganache which is used to deploy the smart contract.

let manager = await instance.manager()
manager == accounts[0]//it should return true.

Now let's call the enter function and enter one player into the lottery. Lets store it in the result variable.

let result = await instance.enter({from:accounts[0],value:web3.utils.toWei(‘1’,’ether’))

here from represents who is calling the function and value represents the ether sending to the contract. Since our enter function is payable and whoever enters the lottery should pay at least 0.1 ether, so accounts[0] has entered into the lottery with 1 ether.

You can check the transaction hash by typing the following command.

result.tx

You can get whole transaction details by typing the following command.

result.receipt

Now let's write a test to our smart contract.

Truffle uses the Mocha testing framework and Chai for assertions to provide you with a solid framework from which to write your JavaScript tests.

We will write all our test files in our test folder created by truffle. Let's create a file Lottery.test.js in the test folder. Let’s write a simple test to check the enter function.

In the first line, we will get the artifacts of our contract(same as migration script). The contract function works like describe function in mocha if you are unfamiliar with mocha check this Mocha Documentation. The contract() function provides a list of accounts made available by your Ethereum client which you can use to write tests.

In it block the first argument describes the description of the test and the second argument is the callback function. We will use async/await syntax to handle asynchronous calls to the blockchain, so make our callback function async.

let instance = await Lottery.deployed();

This line gives the deployed instance of our contract.

Now let’s call the enter function by giving 1 ether.

await instance.enter({from:accounts[0],value: web3.utils.toWei(‘1’,’ether’)})

the from value describes the caller of function (msg.sender). value describes (msg.value). In truffle tests web3 is available, we use web3 to convert ether to Wei.

web3.utils.toWei(‘1’,’ether’)

this converts 1 ether to Wei. Since in solidity the value passed should be in Wei we should convert it to Wei.

Now let’s check the balance in the contract by calling the function balanceInPool.

let balance = await instance.balanceInPool({from:accounts[0]})

This gives the balance in the smart contract. Since we entered with one ether the balance in the pool should be one ether. This function returns a big number(BN) we convert it to string when comparing.

To check whether the balance in the pool is one ether we write an assertion.

assert.equal(‘1000000000000000000’,balance.toString())

Note: 1 ether = 10 ^18 Wei

Now to run the test type the following command in the terminal.

truffle test

This command runs all test files in the project. If you want to test a single test file you can give a path like this.

truffle test ./test/Lottery.test.js

You should see your test should pass.

So you can write multiple it blocks and with different tests.

So we have seen how to compile, deploy, and test the smart contracts using the truffle framework. You have seen how easy truffle makes smart contract development. In our next blog, we will create a frontend with React to our Lottery smart contract.

--

--