Connect a Smart Contract to a UI

This article is a part of a series called Corporate Governance on Blockchain.

Read this to learn how blockchain could change corporate governance for the better. Here, you will also find how to set up your project to follow this article series.

In Part 1 of the series outlines the potential applications of blockchain in corporate governance and walks through how to set up your project to follow this series. Part 2 outlines the entire business logic of the shareholder voting smart contract and includes best practices for smart contract development.

In this article, we will learn how to wire up a UI to the smart contract created in part 2 of this series.

Step 1: Expose all smart contract functions as a library

On exposing smart contract functions as a library, it becomes very easy to manage the arguments (parameters) and results. This is one of the key steps in connecting the blockchain with our UI.

DappStarter has already generated dapp-lib.js for this purpose. You can find it using:

packages
- dapplib
- src
- lib
-> dapp-lib.js

In this file, you can see that all functions that are available in the contracts.

Let’s create our very own function within dapp-lib.js. You can write them anywhere in the file. I will write them under Examples section.

Dapp-lib.js abstracts all the complex communication with the blockchain. So you, as a developer, don’t need to set up Web3 library for your project from scratch. All the code responsible for managing blockchain (inputs and outputs) can be found in blockchain.js. DappStarter is designed in a way that you wouldn’t really need to tweak blockchain.js. Dapp-Lib.js does that for you.

It’s as simple as that.

Let’s test out the getCandidates() function we just created in Dapp-lib.js. To do so, let’s call getCandidates() function from constructor() function in dapp-page.js and print the result.

This returns a resolved promise. This promise contains all the required data — name and voteCount.

This verifies that our function getCandidates is working properly.

Step 2: Get icons for candidates

Thanks to Alfrey Davilla for creating such cute icons. :)

Our two candidates are Ms. Kitty and Mr. Doggo :

  • You can download the picture of the cat from here.
  • You can download the picture of the dog from here.

As a good practice you should save static assets in:

packages
- client
- src
- dapp
- assets
- img
-> kitty.png
-> doggo.png

Step 3: Import icons

Now let’s import these images in dapp-page.js. You can find dapp-page.js in:

packages
- client
- src
- dapp
- pages
-> dapp-page.js

In the beginning of the file, you can add the code:

Step 4: Set up CSS structure to make a placeholder for candidates

DappStarter uses Tailwind CSS. You can even add your own components in dapp-page.js under render() function.

https://gist.github.com/niharrs/11214ec335a500830361d1150d490979#file-dapp-page-js

Your dapp should look like this now-

In this article, we’ve learned how to expose our functions as a library in dapp-lib.js to communicate with the blockchain. We also learned how to read data from the blockchain and how the basic UI works in DappStarter.

Our dapp is coming together well now. In the next article, we will add voting logic so shareholders can cast their vote to be written to the blockchain. We will also explore how DappStarter manages UI in more depth.

Start building with DappStarter.

Stay tuned for part 4!

--

--

--

Decentralized technology for a better world (formerly TryCrypto). Our DappStarter platform makes it easy for developers to build decentralized apps. https://www.decentology.com

Recommended from Medium

Angular + Azure = Pain

學習使用 Node.js 開API

React v/s Vanilla JS — When to use what?

How To Configure Placeholder Image Fallbacks for PWAs

Drawing of phone screen layout

Importing and exporting JavaScript Gantt chart data from and to Excel®

Procedural Geometry: Trees

Angular extended diagnostics

Event bus in NUXT

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

Niharika Singh

So, can you paradigm?

More from Medium

LevelDB — Invented by Google and used by cryptocurrencies like Bitcoin (₿)

Solana Token Development Company

NEAR Certified Entrepreneur — Week 6

How does the Ethereum blockchain get updates?