Expose Rest APIs to interact with Block Chain Smart contracts

In My previous article I wrote about creating smart contracts, deploying in local ganache test block chain network.

This article would give an brief about the tools available and how to use it to develop a web api/front end to interact with block chain network, to support creating dapps.

I have created a web app using NodeJs and React Nextjs.

To interact with the block chain network using the smart contracts, created in previous article we need web3js package.

Assuming prior knowledge on nodejs. Start with initializing a node application, using npm init command. And install express using npm install express.(Ensure you have node version above 12.9.1, you can manage via nvm)

Try out a simple node express API with below code and access http://localhost:8080 to see whether it works.

Now lets install webjs client using npm install web3.

Detailed instructions in web3 here.

Once installed, load the smart contract we created in previous article using JSON. In Remix ide, under artifacts folder find smartcontract.json, copy the json file to node project folder.

The below code snippet will load the contract and expose contract opeartions via javascript interface.

The example I created has Rest APIs to get balance of the account, withdraw from account and transfer wei/ether to other account.

SmartContract operations can be invoked via call() or send() method in web3js, the difference is that former is doesnt neither alter state of the contract or create transaction, where later will affect the state of contract. The APIs are designed to get input from query parameter and execute the smart contract operations.

You are now set with 3 APIs as below

http://localhost:8000/getBalance?account_id=<ACCOUNT_ID>

http://localhost:8000/withdraw?account_id=<ACCOUNT_ID>&amount=<AMOUNT

http://localhost:8000/transfer?account_id=<FROM_ACCOUNT>&amount=<AMOUNT>&to_account=<ACCOUNT_TRANSFER_TO>.

Any front end UI app can use this APIs , I have tried with NextJs and react hooks to demonstrate the operations.

A simple UI with login, based on which it will choose from account number and pass to backed API for interacting with smart contracts.

The source code can be found in GitHub. Modify the below piece of code in index.js and home.js within blockchain-ui folder. The value should reflect account ids in you local block chain network or Ganache UI

const allowedState = [

{ id: ‘John’, value: ‘0xA37493Fa3c493fa3A1153D7F8D35973d1a8D3b10’ },

{ id: ‘Peter’, value: “0x732006f00873908201B828CC5B6418E48437f150” },

{ id: ‘Supplier1’, value: “0xC988E219e9f73d8f675d28794a9f307E87a57188” },

{ id: ‘Supplier2’, value: “0x19507b7f8999ec19Ec83Df8d09294c7Ed01dFEB7” }

];

In next article we will see how to set private block chain network using geth.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Meiyappan Kannappa

Meiyappan Kannappa

Technical enthusiast, daydreamer. Design of involute software architecture for digital transformation, sustainability and cost optimization.