This repo contains a smart contract that allows users to vote from a predefined list. The voting is open for a specified duration, after which the votes are counted to determine the winner. The contract provides functions to create a poll, start voting, cast votes and tally the results. Each poll is associated with the user who created it.
- Create Polls: Users can create a new poll with a list and specified voting duration.
- Start Voting: The creator of a poll can start the voting process, allowing participants to cast their votes.
- Cast Votes: Participants can vote...duhh =))
- Count Votes: At the end of the voting period, votes are tallied automatically and the winner is announced.
- Ownership and Permissions: Each poll is associated with the user who created it and only the creator has certain administrative rights over the poll.
The contract includes several gas optimizations and security features to enhance its performance and safety:
-
Memory Optimization for Arrays:
- What was done: The
endVotingfunction uses thememorykeyword for themoviesarray instead ofstorage. - Why it’s important: Accessing data in memory is cheaper in terms of gas compared to storage. Storage operations are significantly more expensive in Solidity, so minimizing their use can lead to substantial gas savings.
- How it improves gas usage: By copying the array to memory once and working with it from there, the contract reduces the number of expensive storage reads, optimizing gas usage during execution.
- What was done: The
-
Use of Custom Errors:
- What was done: The contract defines custom errors such as
PollNotFound,AlreadyVotedandInvalidMovie. - Why it’s important: Custom errors consume less gas than traditional
requirestatements with string messages because they save the bytecode space needed to store the error messages. - How it improves gas usage: This approach reduces the gas cost when a transaction is reverted, especially if the error messages are long or if the reverts are frequent.
- What was done: The contract defines custom errors such as
-
Gas-Efficient Existence Check Using a Mapping:
- What was done: The contract includes a
mapping(string => bool) movieExistsin thePollstruct to quickly verify if a movie exists in the poll. - Why it’s important: Directly checking the existence of an item in a mapping is much cheaper than iterating through an array, especially for large data sets.
- How it improves gas usage: This reduces the computational cost associated with vote validation, particularly as the number of movies in the poll increases.
- What was done: The contract includes a
MovieVoting.sol: The smart contract file containing all the logic for voting.- Test files: Tests to ensure the contract functions as expected and meets the required coverage.
- Deployment scripts: Scripts to deploy the contract to the Ethereum network.
To get started with the Smart Contract Voting, follow these steps:
Clone the repository:
git clone https://github.com/yourusername/SmartContractVoting.git
cd SmartContractVoting
Make sure you have Node.js installed and then install the necessary packages:
npm install
Use the Hardhat framework to compile the contract:
npx hardhat compile
To ensure everything is working correctly and to see the test coverage, run:
npx hardhat test
Before deploying, configure your deployment settings (network, private keys, etc.) in hardhat.config.js, then deploy:
npx hardhat run scripts/deploy.js --network yourNetwork
