How can we connect our smart contract to a UI
Tags: blockchain, javascript, webdev, beginners
Published: 2020-06-23 19:07:32

*This article is a part of a series called __Corporate Governance on Blockchain__.*

Read [this](https://medium.com/@Niharika3297/how-blockchain-could-change-corporate-governance-for-the-better-fb2353ff5b6a) 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](https://www.trycrypto.com/blog/how-blockchain-could-change-corporate-governance-for-the-better) 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](https://www.trycrypto.com/blog/part-2-voting-smart-contract) 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](https://www.trycrypto.com/blog/part-2-voting-smart-contract) 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.

{% gist https://gist.github.com/niharrs/90b38f47140009f4ac89b78874f2da56 %}

`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.

{% gist https://gist.github.com/niharrs/8e1eba06f570f1ea0f3820c132d0f926 %}

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uvinsl7peffzao6s1w34.png)

This verifies that our function getCandidates is working properly.

### Step 2: Get icons for candidates
Thanks to [Alfrey Davilla](https://dribbble.com/vaneltia) for creating such cute icons. :)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8ur5rng12q7brn6tm7a7.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xkectp4se4h4tyaubl3n.jpg)

- You can download the picture of the cat from [here](https://dribbble.com/shots/10836458-Cat-Playing-Slime).
- You can download the picture of the dog from [here](https://medium.com/@grifer163/alfrey-davilla-vaneltia-54eaf3ba6162).

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:

{% gist https://gist.github.com/niharrs/c4a1a4125cf839d1e841c57e507e3fc3 %}

### Step 4: Set up CSS structure to make a placeholder for candidates
DappStarter uses [Tailwind CSS](https://tailwindcss.com/).  You can even add your own components in `dapp-page.js` under `render()` function.

{% gist https://gist.github.com/niharrs/11214ec335a500830361d1150d490979 %}

Your dapp should look like this now-

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u81aep46af9aw900vlub.png)

* * * * *

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!