This project demonstrates the use of Hardhat to create a VOTING smart contract.
The voting smart contract follows the system in Nigeria, where VOTERS has to bee registered before they can vote and also CANDIDATES has be registered. So, in the contract folder is the Election.sol smart contract.
- Clone the project to you desktop
- cd into the
ElectionSmartContractfolder and runnpm installto install all the dependencies required - Create a
.env - In the
.envfile, addRINKEBY_RPC_KEY:"your api from alchemy"PRIVATE_KEY:"your metamask private key"andETHERSCAN_KEY:"your etherscan key"
- I used
alchemy apiyou can either use alchemy or infura. Which ever one you like - I also used my
metamask private key. So, you have to use yours to sign the transactions Guide to show your metamask private key - I also used
Etherscan APIfor the contract verification. When a contract is verified on etherscan, it has a green check mark that allows you to see and interact with the contract from etherscan - You will need some test ether to be able to deploy contract. Get Rinkeby, Ropston or Kovan Faucets from Chainlink
So, in the .env file, you need to replace the above API KEYS with your's.
The deploy script is in the scripts folder. Since am using Rinkeby (you can use any testnet of your choice). To deploy the smart contract run
npx hardhat run scripts/deploy.js --network rinkeby this will deploy the cntract to etherscan rinkeby testnet
To interact with the smart contact from the command line, I have defined some tasks in the tasks folder
- task to get the total number of registered candidate
get-totalCandidate - task to add a new candidate
add-candidate - task to register a new voter
register-voter
first, run npx hardhat you will see the three custom task that I have defined. Then
- for
get-totalCandidatetask, runnpx hardhat get-totalCandidate --contract contract_address_from_the_deploy_script - for
add-candidatetask, the function requires thename party ageof the candidate. So, you need to pass those values. Runnpx hardhat add-candidate --contract contract_address_from_the_deploy_script --name candidateName --party candidateParty --age candidateAge - for
register-voter, the function requires the voters address as a value. So, runnpx hardhat register-voter --contract contract_address_from_the_deploy_script --address votersAccountAddress
If you follow through the process and you find it difficult to get the smart contract up and running, simply create a new issue I am always whilling to assist.