Smart contracts have revolutionized the core sectors of our economy and consequently our lives. These includes trade finance,  securities, data recording, insurance, property ownership, mortgage and the list goes on. It is not only the number of areas but also the market size is significantly large. Smart contracts are self-executing contracts with the terms of the agreement between two or more parties being directly written into lines of code. In this blog, I am going to illustrate how to develop, deploy and test Ethereum smart contracts written in Solidity.

Prerequisites

Every delicious recipe starts with collecting and preparing the ingredients before we start cooking them. Today, we are also going to use multiple tools and technologies for our smart contract recipe. Let’s begin with them first.

Node

Install Node js version 12.18.1. I installed it using nvm.

[code language=”bash”]curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
nvm install 12.18.1[/code]

Truffle

Truffle is a development environment, testing framework and asset pipeline for Ethereum blockchains. It contains all the tools to quickly start a personal blockchain, test and deploy smart contracts.

[code language=”bash”]npm install -g truffle[/code]

Ganache cli

Ganache will be used to create personal blockchain for Ethereum development.

[code language=”bash”]npm install -g ganache-cli[/code]

How to Write Smart Contract?

Solidity is the most popular language to write Ethereum smart contracts. It is an object-oriented and high level language. Below, you find a simple contract called MyStatus written in the Solidity.

[code language=”js”]// SPDX-License-Identifier: Apache-2.0
pragma solidity >0.5.0;

contract MyStatus {
string status;

constructor() {
status = “Busy”;
}

function getStatus() external view returns(string memory) {
return status;
}

function setStarus(string calldata _status ) external {
status = _status;
}
}[/code]

The contract has a variable named status which stores the current status. The constructor set the status to Busy when the contract is deployed. The functions getStatus and setStatus can be utilized to get and change current status respectively. I have uploaded the smart contract and configuration scripts in the github repository. For the following tasks, clone the repository. The commands are following.

[code language=”bash”]git clone https://github.com/b-rohit/hello-world-smart-contract.git
cd dapp-hello-world[/code]

How to Run Dev Blockchain Network?

Ganache CLI is utilized to simulate a fully functional blockchain network. It is very simple and fast tracks the development of smart contracts and ethereum decentralized applications (DApp). Use the following command to spin up a personal blockchain network.

[code language=”bash”]ganache-cli[/code]

It shows the output similar to following.

[code language=”bash” gutter=”false”]Ganache CLI v6.9.1 (ganache-core: 2.10.2)

Available Accounts
==================
(0) 0x271a1C629Fc6e3A6Ee731de1199CB87d18cB13bC (100 ETH)
(1) 0x668164fF71Df9Abf7Fe6Ea3E39f568c3dd040d2d (100 ETH)
(2) 0x030864BedD2AD5e531B75D0283d7B1DF893a03f2 (100 ETH)
(3) 0xADaEfdFFaA3F2025b3bbc3aa05D86c0AC98E17Df (100 ETH)

Private Keys
==================
(0) 0x1aa9f96a3f06aa05d4d2b69198e4cfa2744cb72fe054c6bc7ef5d63e77540ed6
(1) 0xae252d496d96b833c612d5280250cd5055f96767ca97c274b4afea2ace966b86
(2) 0xc5e176af1ed18225573a625f61dc101ee3ad5dc07af318904094c96a286bc81c
(3) 0x71921bc67cc6f5ea500c80af67cc2949cf5c8f2b160b66a56604875e1a951834

HD Wallet
==================
Mnemonic: person pony render what upgrade glide name have kangaroo grunt warrior simple
Base HD Path: m/44’/60’/0’/0/{account_index}[/code]

It generates 10 accounts for developments at the start and all accounts are loaded with 100 ETH. Here, in the output only 4 are shown. Now, you have your own blockchain network running on your machine. The command also makes a HTTP RPC server on port 8545. You can interact with it on http:127.0.0.1:8545, for an instance following request returns 10 accounts associated with it. You can fine list of supported methods in the ganache-cli repository.

[code language=”bash”]curl -X POST –data ‘{“jsonrpc”:”2.0″,”method”:”eth_accounts”,”params”:[],”id”:53}’ http://127.0.0.1:8545[/code]

There is one more way to do the same. I find it simpler. You could use truffle to spawn a blockchain network. The command is following.

[code language=”bash”]truffle develop[/code]

It also creates 10 accounts and HTTP JSON RPC server but the server runs on port 9545.  Furthermore, it opens a truffle console where other truffle commands can be run to interact with the blockchain network.

How to Test the Smart Contract?

The tests can be written in Solidity or Javascript. I have written the tests in the Solidity. You can find them in test directory. You have to take care of port in the truffle.js file while executing the tests. As I mentioned earlier the ganache-cli and truffle runs the HTTP server on port 8545 and 9545 respectively. Make the change accordingly before executing the following command

[code language=”bash”]truffle test[/code]

It will output similar to shown below:

How to Compile and Deploy Smart Contract?

We are going to use the truffle to compile and deploy the smart contract. In the repository, the contracts directory holds the 2 smart contracts, MyStatus and Migrations. The former is our main smart contract and the latter is used to deploy the MyStatus contract. The truffle uses the migrations scripts stored in migrations directory to deploy the smart contracts. Currently, it has 2 scripts prefixed with 1 and 2.  These scripts migrate the Migrations and MyStatus contracts respectively. The configuration of blockchain network such as host, port, network_id are done in truffle.js. The commands are following to compile and deploy.

[code language=”bash”]truffle compile
truffle migrate[/code]

The deployment logs will be similar to following:

Later, if you want to make changes in the MyStatus contract and deploy the updated version then copy/paste the 2_deploy_contracts.js script and rename it to 3_deploy_contracts.js. After that run the above mentioned commands to redeploy. The truffle will automatically check and deploy only the contract from 3_deploy_contracts.js not the previous ones. Keep adding 4, 5 and so on for other migrations.

If you have used truffle develop to start the blockchain network then you could execute compile and migrate commands without truffle directly in the truffle console as shown below.

[code language=”bash”]truffle(develop)compile
truffle(develop)migrate[/code]

How to Interact With the Smart Contract?

You can utilize truffle console to call the functions defined in the smart contract. If you have used ganache-cli to spawn the blockchain network then execute following command to open the console. In the case of truffle develop, it automatically opens the console.

[code language=”bash”]truffle console[/code]

The truffle console is the easiest method to interact with your smart contract. Use the following commands to call the functions of MyStatus smart contract.

[code language=”bash”]let instance = await MyStatus.deployed()
await MyStatus.getStatus()
await MyStatus.setStatus(“Available”)[/code]

The output from the above commands will be similar to following.

You could find the complete truffle console logs in the repository for the reference. With this, our recipe is complete.