The following contract is quite complex, but showcases a lot of Solidity’s features. It implements a voting contract. Of course, the main problems of electronic voting is how to assign voting rights to the correct persons and how to prevent manipulation. We will not solve all problems here, but at least we will show how delegated voting can be done so that vote counting is automatic and completely transparent at the same time.
The idea is to create one contract per ballot, providing a short name for each option. Then the creator of the contract who serves as chairperson will give the right to vote to each address individually.
The persons behind the addresses can then choose to either vote themselves or to delegate their vote to a person they trust.
At the end of the voting time, winningProposal() will return the proposal with the largest number of votes.
The easiest way is to run the CheckVoter.ts script below to check if the address has a voting right/weight, the address has already voted, etc:
npx ts-node --files ./scripts/CheckVoter.ts [contractAddress] [address1]
Sample contract: https://sepolia.etherscan.io/address/0x329590c91563584091f6f4d8909728eb1050efec
You can also run the PrintResults.ts script below to check the result of votes:
npx ts-node --files ./scripts/PrintResults.ts 0x329590c91563584091f6f4d8909728eb1050efec 3
To deploy a new contract you can run the DeployWithViem.ts script below:
npx ts-node --files ./scripts/DeployWithViem.ts
npx ts-node --files ./scripts/DeployWithViem.ts ramen pizza burger
-copy all files/folders from project2 except artifacts, cache, node_modules
-create new project3 and paste here
-make sure you have the .env file (.gitignore it)
create a scripts folder under the root project4 folder & then create a DeployWithHardhat.ts file under scripts directory
nvm use --lts
node -v
v20.14.0
Unique id | Discord username |
---|---|
RAAzLF | @GRAVER 👾 |
2SyBp0 | @wackozacco |
10exgX | @δαλλασκατ |
r5YSqY | @imchrismayfield |
HhHAQw | @swooz |
Pok9XD | @Timster |
T5zGzt | @Carl Youngblood |
Please check into CONTRACT INTERACTION REPORT
npm init
npm install --save-dev hardhat
npx hardhat init
-> Create a typescript project
npx hardhat compile
npx hardhat test
npx hardhat clean
cretae a .mocharc.json with contents:
{
"require": "hardhat/register",
"timeout": 40000,
"_": ["test*/**/*.ts"]
}
rm ./contracts/*
rm ./ignition/*
rm ./test/*
npx hardhat clean
npm i viem
npm install --save-dev @nomicfoundation/hardhat-chai-matchers