Getting Started with ethereum blockchain development Part -2

 

In the first blog Getting Started with ethereum blockchain development: Part-1, I have demonstrated how to create simple smart contract and get it deployed on ganache-cli. I have also demonstrated, how to write truffle tests for the contract. You can find code in Github.

In this article, I will demonstrate how to write interaction layer to interact with smart contract. We will use web3 npm package to write interaction layer. This article assumes that you already have setup your local machine for blockchain development by steps mentioned in part-1 of this article.

Let’s write interaction layer to interact with ‘Countercontract.

Let’s perform some more setup before we move ahead:

  1. Setup Codebase: If you have followed part-1, you can skip this step. Otherwise checkout code from github.
 git clone https://github.com/sarveshgs/firstBlockchainApp.git
 git checkout tutorial-part-1
  1. Initialise node project: Use npm to initialise node project.
 npm init

3.Web3: Web3 is a library which helps you to interact with a local or remote ethereum node, using a HTTP or IPC connection. You can use npm to install it.

npm install web3 — save

Let’s get started with some coding

  1. Deploy Contract: Let’s deploy Counter.sol contract which will create a contract address. Contract address is needed by Web3 to make interactions.

I. Run ganache-cli: Setup ethereum node by running ganache-cli.

ganache-cli

II. Unlock Account: Unlock first account of created by ganache-cli.a.

Open truffle console:

  truffle console

b. Unlock Account: Use first account address and private key printed on console when ganache-cli is ran to unlock account. This unlock account is used to deploy contract.

web3.personal.unlockAccount(firstAccountAdress,firstPrivateKey,15000)

III. Deploy contract: You can use truffle to deploy contract. Note down contract address underlined with red line in the image.

 truffle migrate

2. Writing Js script to interact with contract: After completing first step successfully, you should now have contract address. We will pass this address to web3 to identify contract deployed on ethereum node.

I. Create a folder in your project structure named as ‘interaction’ and ‘interaction.js’ in interaction folder.

II. Make web3 connection with local ethereum node: ganache-cli is running at http://localhost:8545

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

III. Initialise contract: To initialise contract, we need two things

  • Contract address: You must be having a contract address after deploying contract successfully in step-1
  • ABI: ABI stands for application binary interface. Basically it’s metadata of your contract in json format. You can find abi in ‘build folder of your project. It is generated after compilation of contracts.
const metadata = require('../build/contracts/Counter');
const abi = metadata.abi;
const contractAddress = "0x42f18096c12db0b85ce999dbe5cceb4316f1e0be";
const counter = new web3.eth.Contract(abi, contractAddress);

IV. Calling contract methods: Last thing, which needs to be done is to call contract method. Calling a contract method will form a transaction in ethereum blockchain. Once the transaction is executed, it will return a transaction receipt. This receipt can be used to retrieve return value from method and events.

In below code snippet, we are calling ‘incrementCounter’ method and listening for ‘on receipt receive event’

V. Run interaction.js: Run interaction.js using node.

node interaction/interaction.js

Great you now know, how to write smart contract and interaction layer to interact with contract methods using web3.

Full source code is available on Github:sarveshgs/firstBlockchainApp

Thank you for reading both blogs, I hope it gave you some basic understanding of how to write the dapps.

In next few blogs, I have explained about gas consumption and effective memory management.

Stay tuned, Write back or post comments for queries and suggestion.

Happy Coding 🙂

linkedin.com/in/jainsarvesh
medium.com/@sarvesh.sgsits

Leave a Reply

Your email address will not be published. Required fields are marked *