DEV Community

Cover image for How can we connect our smart contract to a UI
Niharika Singh ⛓
Niharika Singh ⛓

Posted on • Updated on

How can we connect our 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
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

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 :

Alt Text

Alt Text

  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Your dapp should look like this now-

Alt Text


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!

Top comments (0)