Created
March 27, 2022 11:16
-
-
Save kavania2002/a5c0c219843bda8fa489f23c9e4f057d to your computer and use it in GitHub Desktop.
Contract file for a Decentralized Voting System App - VoteChain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: MIT | |
pragma solidity >=0.7.0 <0.9.0; | |
contract Voting { | |
// struct for each proporsal | |
// struct defines datatype of each proposals | |
struct Info{ | |
string name; | |
string description; | |
address tokenAd; | |
uint leftCount; | |
uint rightCount; | |
address admin; | |
// map to get whether a user selected yes or no | |
mapping(address => bool) userValue; | |
} | |
// array of struct | |
// array of each propasals | |
Info[] Informations; | |
string[] private allName; | |
address[] private allAds; | |
address[] private active; | |
address[] private completed; | |
function startProposal(address ad) public { | |
active.push(ad); | |
} | |
function endProposal(address ad) public { | |
for(uint i=0; i < active.length; i++){ | |
if( ad == active[i]){ | |
active[i] = active[active.length-1]; | |
active.pop(); | |
completed.push(ad); | |
} | |
} | |
} | |
function displayActiveList() public view returns (address[] memory) { | |
return active; | |
} | |
function displayCompletedList() public view returns (address[] memory) { | |
return completed; | |
} | |
// fetch all the proposals for the dashboard | |
function getAll() public returns (string[] memory names, address[] memory tkAds) { | |
for (uint i = 0; i<Informations.length; i++) { | |
allName.push(Informations[i].name); | |
allAds.push(Informations[i].tokenAd); | |
} | |
return (allName, allAds); | |
} | |
// fetch only one propasals for sepecific page's propasals | |
function getOne(address tkAd) public view returns (string memory name, address tkA) { | |
uint n = Informations.length; | |
for (uint i = 0; i<n; i++) { | |
if (Informations[i].tokenAd == tkAd) { | |
return (Informations[i].name, Informations[i].tokenAd); | |
} | |
} | |
} | |
// get user selected value via address | |
function getUserValue(address userAd, address proposalsAd) public view returns (bool value) { | |
for (uint i = 0; i<Informations.length; i++) { | |
if (Informations[i].tokenAd == proposalsAd) { | |
return Informations[i].userValue[userAd]; | |
} | |
} | |
} | |
// set each propasals | |
function setInfo(string memory name, string memory description, address tkAd) public { | |
Info storage info = Informations.push(); | |
info.name=name; | |
info.description = description; | |
info.tokenAd = tkAd; | |
} | |
// set each user's value | |
function setUser(address pAd, address userAd, bool value) public { | |
uint n = Informations.length; | |
for (uint i = 0; i<n; i++) { | |
if (Informations[i].tokenAd == pAd) { | |
Informations[i].userValue[userAd] = value; | |
if (value == true) { | |
Informations[i].leftCount++; | |
} else { | |
Informations[i].rightCount++; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This contract is being included in VoteChain.