Smart Contract for Voting with Binance Smart ChianIDE

In this smart contract, we will implement a voting process along with delegation. We’ll use Solidity as a programming language, and Metamask for the deployment and testing. All this will be done using Binance Smart ChainIDE.

1. Preliminaries

In this section we will briefly explain the background knowledge to make you better understand the smart contract.

1.1 Tools used in the project

  1. Binance Smart ChainIDE
  2. Metamask

1.2 Contract Programming Language

A voting process along with delegation smart contract is written in solidity language as it is an object-oriented, high-level programming language for implementing smart contracts. It was introduced in 2014. Smart contracts are programs that govern the behavior of accounts within the Ethereum state. Solidity was influenced by C++, Python, and JavaScript and is designed to target the Ethereum Virtual Machine (EVM). Now, it is the most commonly used programming language for Blockchain.

1.3 Data types and Definitions

Before moving on, we will briefly explain the basic terms, data types, and definitions used in this smart contract for the better understanding.

  • Smart Contract- smart contracts are the lines of the code that are stored on Blockchain and execute when predetermined terms and conditions are met. A contract is the fundamental building block of Ethereum applications — all variables and functions belong to a contract, and this will be the starting point of all your projects.
  • All the variables and functions are defined inside the body of contract. The smart contract for the voting uses the data types such as uint, bool, address, byte32. And function, struct, and constructor.
  • uint- The uint data type is an unsigned integer, meaning its value must be non-negative. There’s also an int data type for signed integers. In Solidity, uint is actually an alias for uint256, a 256-bit unsigned integer.
  • bool- The possible values are constants true and false. Operators for bool are following ! (logical negation) && (logical conjunction, “and”) || (logical disjunction, “or”) == (equality) != (inequality).
  • constructor- Constructor is a special method which gets invoked whenever an instance of a class is created — that is correct for object-oriented programming languages. However, in Solidity, it’s different; Solidity supports declaring constructor inside a smart contract and it is invoked-only once-while deploying it.
  • byte32- byte32 is a fixed size byte array.
  • struct- Sometimes you need a more complex data type. For this, Solidity provides structs. Structs allow you to create more complicated data types that have multiple properties such as uint, bool, address, string, etc.
  • Array- When you want a collection of something, you can use an array. There are two types of arrays in Solidity: fixed arrays and dynamic arrays: Array with a fixed length of 2 elements: uint[2] fixedArray. A dynamic Array — has no fixed size, can keep growing: uint[] dynamicArray;
  • function- Like other programming languages, solidity also uses function. A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
  • address- Holds a 20 byte value (size of an Ethereum address). Address types also have members and serve as a base for all contracts. operators for the address are followings: <=, <, ==, !=, >= and >.
  • All solidity source code should start with a “version pragma” — a declaration of the version of the Solidity compiler this code should use. This is to prevent issues with future compiler versions potentially introducing changes that would break your code. For the scope of this smart contract, we’ll compile our smart contracts with any compiler version in the range of 0.4.22 (inclusive) to 0.7.0 (exclusive). It looks like this: pragma solidity >=0.4.22 <0.7.0;

2. Practical Implementation of Smart Contract for Voting

In this section, we will implement and discuss in detail, a smart contract for the voting named as Ballot. The figure below shows the functions and structs used in this contract for different purposes such as to gives right to vote, summon winner name etc.

The function for to give rights to vote is declared as:

function giveRightToVote(address voter) public { require( msg.sender == chairperson, "Only chairperson can give right to vote." ); require( !voters[voter].voted, "The voter already voted." ); require(voters[voter].weight == 0); voters[voter].weight = 1; }

Please click here to see the detailed implementation of this smart contract Once you have written down the code, you can check it for the errors by compiling from the most right side panel. If it does not have any error, you can deploy it and practice to write more contracts according to your choice of interest using Binance Smart ChainIDE.

Conclusion

The tutorial on using ChainIDE to develop a smart contract for voting. Students who have questions can leave a message in the comment area. Different platforms bring more possibilities due to different architectures, and hope you can try more , make progress together.

Social Media Coordinator at White Matrix.