Created
June 20, 2019 23:56
-
-
Save ernestognw/223375d222d2cd4e9d6b040c8526ad23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
pragma solidity ^0.5.9; | |
contract Voting { | |
struct voter { | |
address voterAddress; | |
uint tokensBought; | |
uint[] tokensUsedPerCandidate; | |
} | |
mapping (address => voter) public voterInfo; | |
mapping (bytes32 => uint) public votesReceived; | |
bytes32[] public candidateList; | |
uint public totalTokens; | |
uint public balanceTokens; | |
uint public tokenPrice; | |
//address sContract = this; | |
address payable owner; | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
constructor(uint tokens, uint pricePerToken, bytes32[] memory candidateNames) public { | |
owner = msg.sender; | |
candidateList = candidateNames; | |
totalTokens = tokens; | |
balanceTokens = tokens; | |
tokenPrice = pricePerToken; | |
} | |
function buy() payable public returns (uint) { | |
uint tokensToBuy = msg.value / tokenPrice; | |
require(tokensToBuy <= balanceTokens); | |
voterInfo[msg.sender].voterAddress = msg.sender; | |
voterInfo[msg.sender].tokensBought += tokensToBuy; | |
balanceTokens -= tokensToBuy; | |
return tokensToBuy; | |
} | |
function totalVotesFor(bytes32 candidate) view public returns (uint) { | |
return votesReceived[candidate]; | |
} | |
function voteForCandidate(bytes32 candidate, uint votesInTokens) public { | |
uint index = indexOfCandidate(candidate); | |
require(index != uint(-1)); | |
if (voterInfo[msg.sender].tokensUsedPerCandidate.length == 0) { | |
for(uint i = 0; i < candidateList.length; i++) { | |
voterInfo[msg.sender].tokensUsedPerCandidate.push(0); | |
} | |
} | |
uint availableTokens = voterInfo[msg.sender].tokensBought - totalTokensUsed(voterInfo[msg.sender].tokensUsedPerCandidate); | |
require (availableTokens >= votesInTokens); | |
votesReceived[candidate] += votesInTokens; | |
voterInfo[msg.sender].tokensUsedPerCandidate[index] += votesInTokens; | |
} | |
function totalTokensUsed(uint[] memory _tokensUsedPerCandidate) private pure returns (uint) { | |
uint totalUsedTokens = 0; | |
for(uint i = 0; i < _tokensUsedPerCandidate.length; i++) { | |
totalUsedTokens += _tokensUsedPerCandidate[i]; | |
} | |
return totalUsedTokens; | |
} | |
function indexOfCandidate(bytes32 candidate) view public returns (uint) { | |
for(uint i = 0; i < candidateList.length; i++) { | |
if (candidateList[i] == candidate) { | |
return i; | |
} | |
} | |
return uint(-1); | |
} | |
function tokensSold() view public returns (uint) { | |
return totalTokens - balanceTokens; | |
} | |
function voterDetails(address user) view public returns (uint, uint[] memory) { | |
return (voterInfo[user].tokensBought, voterInfo[user].tokensUsedPerCandidate); | |
} | |
function transferTo() public onlyOwner { | |
owner.transfer(address(this).balance); | |
} | |
function allCandidates() view public returns (bytes32[] memory) { | |
return candidateList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment