How To Implement A Voting Smart Contract
Tags: blockchain, javascript, webdev, node
Published: 2020-06-16 16:56:13

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

[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. It also provides an overview of the technical architecture of the shareholder voting solution to be implemented in this series and walks through how to set up your project to follow this series. Read part 1 [here](https://www.trycrypto.com/blog/how-blockchain-could-change-corporate-governance-for-the-better). 

In this article, we will write a shareholder voting smart contract using Solidity.

Here is the overview of the idea we want to capture in the smart contract. We will write a function to:
* Add candidates
* Get candidates
* Cast a vote


We will assume that each 'Annual General Meeting (AGM)', the annual meeting where shareholders vote on agendas to make key decisions for the company, will be different, so a different smart contract would be developed for each one.

Assuming that you've set up your project. You can find `Dapp.sol`

```
packages
  - dapplib
    - contracts
      -> Dapp.sol
```

Let's start by defining what our candidate will look like. For sake of simplicity, let's assume it will have an `id`, `name` and, `voteCount`.

{% gist https://gist.github.com/niharrs/3f4e4398c33264a7f19383381f80da1e %}

Now, let's define the `addCandidate` function. This function's job is to create a candidate listing on the blockchain. Instead of a candidate, you can also vote on agendas. The choice is yours.

The initial `voteCount` for each candidate will be zero. 

{% gist https://gist.github.com/niharrs/2c149e1677daaef722f518fd70085ea8 %}

We want to invoke the `addCandidate` function as soon as the smart contract is deployed, so we will call it from the `Constructor()` function.

{% gist https://gist.github.com/niharrs/6f9c3caaf3613e08f706f30a008ab146 %}

The next step would be to fetch all candidates. There are two approaches you could choose from:
1. __Fetch candidates by ID:__ This means that if there are 50 candidates, each user will have to query the smart contracts 50 times. Let's say there are 20,000 voters, this means that your smart contract will be invoked 20,000 * 50 times (1,000,000 times). I don't think this is wise, you'd end up wasting a lot of bandwidth. Nonetheless, I'll still show you what this code might look like.

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

2. __Fetch all candidates at once.__ You will return ALL candidates at once. This means your smart contract will be invoked 20,000 times (# of users) instead of a million times. This is the approach we will go with.

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

Here we are returning an array of `names` and an array of `voteCounts`. We can manipulate these arrays later using JavaScript to fit our needs.

Note 1: Since `getCandidates()` is NOT called from within the smart contract but will be called from the outside, we will use `external` keyword here.

Note 2: `getCandidates()` is a read only function. Reading from the blockchain is free. So we use the keyword `view` here.

Finally we need to add voting logic in our smart contract. Let's call this function vote. Since this function will be called from outside of the smart contract, we will use `external` keyword here. We will NOT use `view` keyword because this function writes your vote to the blockchain and writing to the blockchain is not free.

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

At the end, your smart contract should look like this-

{% gist https://gist.github.com/niharrs/30dc19eacf18bd890e9038e2f1085b06 %}

In this article, we explored the various approaches to writing a resource-efficient smart contract. We also covered the important keywords `(external, view)` to understand and use properly when developing a smart contract. In part 3, we’ll outline how to connect your smart contract to a UI to make it simple for shareholders to interact with the smart contract functions we’ve written here.

Start building with [DappStarter](https://dappstarter.trycrypto.com/).