How To Implement A Voting Smart Contract

Part 1 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.

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.

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.

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

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.

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.

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.

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

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.

--

--

--

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

Blockchain Interoperability: Comparing cross-chain solutions

Delivering the most compliant blockchain

Will Starfish OS be a dark horse for GameFi?

Mission and Purpose of SELENE.🔎

Connext Network — create dApps with no boundaries.

Free Republic of LIBERLAND opens its first virtual embassy inside Somnium Space

Aletheia: The Decentralised, Distributed Database

Stratos Bi-Weekly Progress Report: February 11, 2022 — March 8, 2022

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

Why should you use Infura for your web3 projects?

Infura logo

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

Significant Features Of Decentralized Apps To Strengthen Your Business

Provision Of Services (Business Model 2) / 提供服务(商业模式 2)