Created
July 18, 2018 10:03
-
-
Save bibekblockchain/582bf31f46b64e4240f6d16994486b54 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
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
pragma solidity ^0.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function giveRightToVote(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
} | |
/// Delegate your vote to the voter $(to). | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; // assigns reference | |
if (sender.voted) return; | |
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) | |
to = voters[to].delegate; | |
if (to == msg.sender) return; | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegateTo = voters[to]; | |
if (delegateTo.voted) | |
proposals[delegateTo.vote].voteCount += sender.weight; | |
else | |
delegateTo.weight += sender.weight; | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
} | |
function winningProposal() public constant returns (uint8 _winningProposal) { | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} |
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
pragma solidity ^0.4.23; | |
import "github.com/Arachnid/solidity-stringutils/strings.sol"; | |
contract Polling{ | |
using strings for *; | |
struct Event{ | |
string name; | |
mapping(uint => Options) options; | |
uint optionsCount; | |
mapping(address => bool) hasVoted; | |
} | |
mapping(uint => Event) public events; | |
uint public eventCount; | |
struct Options{ | |
string name; | |
uint vote; | |
} | |
constructor() public{ | |
} | |
function addEvent(string _name, string _options) public{ | |
eventCount++; | |
events[eventCount].name=_name; | |
strings.slice memory s = _options.toSlice(); | |
strings.slice memory delim = "-".toSlice(); | |
string[] memory parts = new string[](s.count(delim)+1); | |
for(uint i = 0; i < parts.length; i++) { | |
parts[i] = s.split(delim).toString(); | |
events[eventCount].options[i].name=parts[i]; | |
events[eventCount].optionsCount++; | |
} | |
} | |
function getOptions(uint id) public returns(string categories){ | |
categories=string(abi.encodePacked(events[id].options[0].name)); | |
for(uint i=1;i<events[id].optionsCount;i++){ | |
categories=string(abi.encodePacked(categories,",",events[id].options[i].name)); | |
} | |
} | |
function vote(uint _eventId,uint _optionId) public{ | |
require(!events[_eventId].hasVoted[msg.sender]); | |
events[_eventId].options[_optionId].vote++; | |
events[_eventId].hasVoted[msg.sender]=true; | |
} | |
function seeVotes(uint eventId,uint optionId) public returns (uint num){ | |
num = events[eventId].options[optionId].vote; | |
} | |
function flashResult(uint _eventId) public returns(string winnerName,uint value){ | |
winnerName=events[_eventId].options[0].name; | |
value=events[_eventId].options[0].vote; | |
for(uint i=1;i<events[_eventId].optionsCount;i++){ | |
if(events[_eventId].options[i].vote>value){ | |
value=events[_eventId].options[i].vote; | |
winnerName=events[_eventId].options[i].name; | |
} | |
} | |
} | |
} |
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
pragma solidity ^0.4.23; | |
contract Paradarshi{ | |
struct Event{ | |
uint id; | |
string name; | |
uint donateCount; | |
address owner; | |
bool completed; | |
} | |
struct Donor{ | |
uint eventIndex; | |
uint value_to_donate; | |
address donor; | |
} | |
uint public eventCount; | |
uint public donorCount; | |
mapping( uint =>Event ) public events; | |
mapping(uint => Donor) public donors; | |
event donateEvent ( | |
uint indexed _eventId | |
); | |
event Transfer(string from, string to, uint amountTransferred); | |
event CollectionCompleted(uint total); | |
constructor() public{ | |
addEvent("Event 1"); | |
} | |
function addEvent (string _name) public{ | |
events[eventCount] = Event(eventCount, _name, 0, msg.sender, false); | |
eventCount++; | |
} | |
function donate (uint _eventId, uint _asset) public { | |
// require a valid event | |
require(_eventId > 0 && _eventId <= eventCount); | |
//require a running event | |
require(events[_eventId].completed == false); | |
donorCount++; | |
donors[donorCount].donor = msg.sender; | |
donors[donorCount].value_to_donate = _asset; | |
donors[donorCount].eventIndex = _eventId; | |
// update candidate vote Count | |
events[_eventId].donateCount += _asset; | |
emit donateEvent(_eventId); | |
} | |
function endEvent(uint _eventId) public{ | |
require(msg.sender == events[_eventId].owner); | |
events[_eventId].completed = true; | |
emit CollectionCompleted(events[_eventId].donateCount); | |
} | |
function transfer(uint fromEvent, uint toEvent, uint value_to_transfer) public{ | |
require(msg.sender == events[fromEvent].owner); | |
events[fromEvent].donateCount -= value_to_transfer; | |
events[toEvent].donateCount += value_to_transfer; | |
emit Transfer(events[fromEvent].name, events[toEvent].name, value_to_transfer); | |
} | |
} |
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
pragma solidity ^0.4.23; | |
// written for Solidity version 0.4.18 and above that doesnt break functionality | |
contract Voting { | |
// an event that is called whenever a Candidate is added so the frontend could | |
// appropriately display the candidate with the right element id (it is used | |
// to vote for the candidate, since it is one of arguments for the function "vote") | |
event AddedCandidate(uint candidateID); | |
// describes a Voter, which has an id and the ID of the candidate they voted for | |
struct Voter { | |
bytes32 uid; // bytes32 type are basically strings | |
uint candidateIDVote; | |
} | |
// describes a Candidate | |
struct Candidate { | |
bytes32 name; | |
bytes32 party; | |
// "bool doesExist" is to check if this Struct exists | |
// This is so we can keep track of the candidates | |
bool doesExist; | |
} | |
// These state variables are used keep track of the number of Candidates/Voters | |
// and used to as a way to index them | |
uint numCandidates; // declares a state variable - number Of Candidates | |
uint numVoters; | |
// Think of these as a hash table, with the key as a uint and value of | |
// the struct Candidate/Voter. These mappings will be used in the majority | |
// of our transactions/calls | |
// These mappings will hold all the candidates and Voters respectively | |
mapping (uint => Candidate) candidates; | |
mapping (uint => Voter) voters; | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* These functions perform transactions, editing the mappings * | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
function addCandidate(bytes32 name, bytes32 party) public { | |
// candidateID is the return variable | |
uint candidateID = numCandidates++; | |
// Create new Candidate Struct with name and saves it to storage. | |
candidates[candidateID] = Candidate(name,party,true); | |
AddedCandidate(candidateID); | |
} | |
function vote(bytes32 uid, uint candidateID) public { | |
// checks if the struct exists for that candidate | |
if (candidates[candidateID].doesExist == true) { | |
uint voterID = numVoters++; //voterID is the return variable | |
voters[voterID] = Voter(uid,candidateID); | |
} | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * | |
* Getter Functions, marked by the key word "view" * | |
* * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// finds the total amount of votes for a specific candidate by looping | |
// through voters | |
function totalVotes(uint candidateID) view public returns (uint) { | |
uint numOfVotes = 0; // we will return this | |
for (uint i = 0; i < numVoters; i++) { | |
// if the voter votes for this specific candidate, we increment the number | |
if (voters[i].candidateIDVote == candidateID) { | |
numOfVotes++; | |
} | |
} | |
return numOfVotes; | |
} | |
function getNumOfCandidates() public view returns(uint) { | |
return numCandidates; | |
} | |
function getNumOfVoters() public view returns(uint) { | |
return numVoters; | |
} | |
// returns candidate information, including its ID, name, and party | |
function getCandidate(uint candidateID) public view returns (uint,bytes32, bytes32) { | |
return (candidateID,candidates[candidateID].name,candidates[candidateID].party); | |
} | |
} |
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
pragma solidity ^0.4.23; | |
contract Paradarshi{ | |
struct Event{ | |
uint id; | |
string name; | |
uint donateCount; | |
address owner; | |
} | |
struct Donor{ | |
uint eventIndex; | |
uint value_to_donate; | |
address donor; | |
} | |
uint public eventCount; | |
uint public donorCount; | |
mapping( uint =>Event ) public events; | |
mapping(uint => Donor) public donors; | |
event donateEvent ( | |
uint indexed _eventId | |
); | |
event Transfer(string from, string to, uint amountTransferred); | |
constructor() public{ | |
addEvent("Event 1"); | |
} | |
function addEvent (string _name) public{ | |
eventCount++; | |
events[eventCount] = Event(eventCount, _name, 0, msg.sender); | |
} | |
function donate (uint _eventId, uint _asset) public { | |
// require a valid event | |
require(_eventId > 0 && _eventId <= eventCount); | |
//require a running event | |
donorCount++; | |
donors[donorCount].donor = msg.sender; | |
donors[donorCount].value_to_donate = _asset; | |
donors[donorCount].eventIndex = _eventId; | |
// update candidate vote Count | |
events[_eventId].donateCount += _asset; | |
emit donateEvent(_eventId); | |
} | |
function transfer(uint fromEvent, uint toEvent, uint value_to_transfer) public{ | |
require(msg.sender == events[fromEvent].owner); | |
require(events[fromEvent].donateCount >= value_to_transfer); | |
events[fromEvent].donateCount -= value_to_transfer; | |
events[toEvent].donateCount += value_to_transfer; | |
emit Transfer(events[fromEvent].name, events[toEvent].name, value_to_transfer); | |
}} |
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
pragma solidity ^0.4.23; | |
contract Polling{ | |
struct Event{ | |
uint id; | |
string name; | |
address owner; | |
} | |
mapping(uint => Event) public events; | |
uint public eventCount; | |
struct Options{ | |
uint id; | |
string name; | |
uint vote; | |
uint event_id; | |
} | |
mapping(uint => Options) public options; | |
uint public optionCount; | |
mapping(address => bool) public not_eligible; | |
constructor(){ | |
eventCount=0; | |
optionCount=0; | |
} | |
function createEvent(string _name) public{ | |
eventCount++; | |
events[eventCount].id=eventCount; | |
events[eventCount].name=_name; | |
events[eventCount].owner=msg.sender; | |
} | |
function createOptions(string _name,uint event_id) public{ | |
optionCount++; | |
options[optionCount].id=optionCount; | |
options[optionCount].name=_name; | |
options[optionCount].vote=0; | |
options[optionCount].event_id=event_id; | |
} | |
function vote(uint _id){ | |
require(!not_eligible[msg.sender]); | |
options[_id].vote++; | |
not_eligible[msg.sender]=true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment