Absentee Voting on Blockchain

Timmy chien
2 min readAug 31, 2021

Some countries has implement absentee voting for years, but there’s several countries haven’t because of lots of doubts.As the blockchain smart contract being mature ,maybe we can try using smart contract to implement an absentee voting?

There’re some token standard provided, ERC20,ERC721,ERC777,ERC1155 and so on.ERC20 is for currency transaction,ERC721 for collection,ERC777 as an operator managed ERC20, how about ERC1155?ERC1155 is now most for games, but I think that ERC1155 may be a great token standard for ballots.

The standard Mint function is like:

function _mint(address account, uint256 id, uint256 amount, bytes memory data)

We input account(mint to which address),id(the type of ERC1155)and amount.It has the feature of ERC20(the amount of each token can be not only 1) and ERC721(each token has its tokenId),the id can be ballot for president ,ballot for mayor or parliamentary.Each token balance can be more than 1, it’s just like ballots.

ERC20 also have burn function:

function _burn(address account, uint256 id, uint256 amount)

As the voter votes, the ballot will be burnt to ensure that nobody vote for the same candidate twice.

The ballot contract totally follow the ERC1155 standard.And next is the voting contract.The voting contract is to :

  1. Add the candidate
  2. Voter vote for candidate
  3. get the votes balance after the voting ended

So,it may looks like:

pragma solidity ^0.6.0;
contract ERC1155{
function burn(address account, uint256 id, uint256 amount) external virtual {
}
function balanceOf(address account, uint256 id) public view virtual returns (uint256){

}
}
contract Voting{
struct Candidate{
bytes District;
uint8 CandidateId;
string Name;
uint8 Age;
string FromParty;

}
uint public expireTime;
mapping(address=>Candidate)public _candidate;
mapping(bytes=>mapping(uint8=>uint))private getVotes;
ERC1155 public ballot;
constructor(address erc1155addr,uint time)public{
ballot=ERC1155(erc1155addr);
expireTime=time;
}
function addCandidate(address candidateAddr,bytes memory dist,uint8 Cid,string memory name,uint8 age,string memory party)public returns(bool){
Candidate storage __candidate=_candidate[candidateAddr];
__candidate.District=dist;
__candidate.CandidateId=Cid;
__candidate.Name=name;
__candidate.Age=age;
__candidate.FromParty=party;
return true;
}
function vote(uint8 id,bytes memory dist,uint8 cid)public returns(bool){
require(now<expireTime);
require(ballot.balanceOf(msg.sender,id)==1);
ballot.burn(msg.sender,id,1);
getVotes[dist][cid]+=1;
return true;
}
function VoteBalance(bytes memory dist,uint8 cid)public view returns(uint){
require(now>expireTime);
return getVotes[dist][cid];
}
}

In the constructor, we declared the ballot address and the endtime of the voting,and there’s 3 main function,including:

addCandidate : Register the candidate and his/her info

vote:Voters have to input the id(the type of candidate), dist(your voting district) and cid(the id of the candidate) to finish the vote.

VoteBalance:We can view the total votes of each candidates only after the voting ended.

By this simple voting contract ,maybe an absentee voting is not that hard and hope oneday most countries in the world will implement the absentee voting.

For the entire contract ,you can visit:https://github.com/timmychien/Absentee-Voting

--

--