June 23, 2020
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.
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.
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:
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 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.
Thanks to Alfrey Davilla for creating beautiful icons. :)
Our two candidates are Ms. Kitty and Mr. Doggo :
As a good practice you should save static assets in:
Now let's import these images in dapp-page.js. You can find dapp-page.js in:
In the beginning of the file, you can add the code:
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:
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 so stay tuned!
Start building with DappStarter.
Part 4 of this series is now available!