Skip to content

Instantly share code, notes, and snippets.

@emmaglorypraise
Last active June 3, 2024 16:58
Show Gist options
  • Save emmaglorypraise/336f5efe5c7563aa99d6a9003d062d32 to your computer and use it in GitHub Desktop.
Save emmaglorypraise/336f5efe5c7563aa99d6a9003d062d32 to your computer and use it in GitHub Desktop.
A voting contract for football clubs
Deployed contract address(polygon mainnet) = 0x4bc4154b03B7fBbE72CBFA33aDe77BB820FbB337
// ABI
"abi": [
{
"inputs": [
{
"internalType": "string[]",
"name": "clubNames",
"type": "string[]"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "clubs",
"outputs": [
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "uint256",
"name": "votes",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "clubId",
"type": "uint256"
}
],
"name": "getClubVotes",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "hasVoted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "clubId",
"type": "uint256"
}
],
"name": "vote",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
// Voting.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
// Uncomment this line to use console.log
// import "hardhat/console.sol";
contract Voting {
struct Club {
string name;
uint256 votes;
}
Club[] public clubs;
mapping(address => bool) public hasVoted;
constructor(string[] memory clubNames) {
for (uint i = 0; i < clubNames.length; i++) {
clubs.push(Club({
name: clubNames[i],
votes: 0
}));
}
}
function vote(uint256 clubId) public {
require(!hasVoted[msg.sender], "You have already voted.");
require(clubId < clubs.length, "Invalid club ID.");
clubs[clubId].votes++;
hasVoted[msg.sender] = true;
}
function getClubVotes(uint256 clubId) public view returns (uint256) {
require(clubId < clubs.length, "Invalid club ID.");
return clubs[clubId].votes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment