In this tutorial, we will guide you through the process of developing a voting smart contract using the Solidity programming language. We will then deploy the contract on a blockchain network and test it to ensure its functionality. By the end of this tutorial, you will have a good understanding of how to create and deploy a smart contract for voting purposes.
Step 1: Cloning the Tutorial Repo
1.1 Forking the Tutorial Repository:
1.2 Cloning the repository locally:
git clone <paste the URL you copied>
cd <Name of the cloned Repo>, then move to the voting folder use this command
Step 2: Developing Election Smart Contract
Below is the code for our smart contract. You can get it from here Election. sol.
Electioncontract. Details include
Candidatethe structure would consist of data like
name(candidate's name), and
voteCount(number of times they are voted).
votersis a mapping between the address of the voter and a boolean. In Solidity, the default boolean value is
false, so if the returned value of
falsewe can understand that the voters are voting for the first time in this election, and vice-versa for
constructor()function. Whatever we want to initialize in a smart contract, we do it inside the
constructor()function. Like here, we will be adding a name, description, and candidates to the election. Here,
addCandidate()is a private function, so that, it cannot be called publicly. This function takes
descriptionas a single array named
_ndain the first argument and candidates' names as an array in the second argument.
vote()function. It takes
candidateIdas an argument and increments the vote of the respective candidate. It requires two things, viz. voters should not have voted in the particular election by checking the boolean across the
candidateIdshould be a valid one, i.e.
0 <= candidateId < candiatesCount.
MainContract.sol is the main entry point of our e-voting DApp. It will maintain the number of election contracts deployed, and their address on the network and will also help in deploying them. We have also imported
Election.sol a contract, for using it in the
electionIdis used for assigning IDs to each election that a user creates and is incremented for using it while creating the next election. Also,
Electionsis a public mapping between
electionIdand the address of the deployed election contract.
createElection()the function which will be used to deploy our
Electionsmart contract. This function takes
descriptionas a single array named
_ndain the first argument and candidates’ names as an array in the second argument.
Here you can see that a new
Election contract is deployed on the network using the
new keyword. And the address for the deployed smart contract is stored in the
Elections mapping. Once the election contract is deployed successfully,
electionId is incremented.
3.1. Visit the BuildBear App (why BuildBear: Read article here).
3.2. Create your Private Testnet forking from the Ethereum Mainnet, feel free to fork any Chain.
3.3. Add your Private Testnet to your MetaMask wallet by using the “Add to Metamask” button:
3.4. Click on Open Faucet you will be redirected to Faucet Pages, and then Mint native Tokens to pay for gas for the transactions.
For installing the necessary packages, please execute the following command:
npm install .
4.1 Update the
maincontract.solthe smart contract runs the deploy command. This will run the deployment script in
deploy/00_deploy_mainContract.jsand save the deployment details along with ABI and Contract address.
npx hardhat deploy
Click on the link provided in the terminal to see the Contract on Explore page.
WOOT WOOT 🎉 🎉
4.2. Creating an Election
The scripts for invoking the createElection function and verifying the Election contract have already been configured in the
scripts/deployElectionContract.js file, as demonstrated above. To execute the scripts, please run the following command.
npx hardhat run scripts/deployElectionContract.js
Our new election has been successfully deployed, and the contract has been verified. To view the contract on Explore, simply click on the link provided in the terminal. This will take you directly to the Contract page, where we can read and write directly from the contract.
4.3 Casting our Vote
Enter the candidate id to see the there name and Total votes cast to them.
Let’s move to the write the contract section and cast our vote.
Now if we go back to the read and check the total votes received by Candidate 1 it will be increased by 1.
We have successfully built an Election smart contract, deployed it, created an election, and cast our vote !!! 🎉🎉🎉
Share your project on Twitter and LinkedIn and tag BuildBear.
If you appreciate what we are doing, please follow us on Twitter, and LinkedIn and Join the Telegram group if you haven’t done yet.
And please give us a clap 👏 if you like our work.
BuildBear is a platform for testing dApps at scale, for teams. It provides users with their own private Testnet to test their smart contracts and dApps, which can be forked from any EVM chain. It also provides a Faucet, Explorer, and RPC for testing purposes.
BuildBear aims to build an ecosystem of tools for testing dApps at scale for the teams.
Read our past articles and keep learning :