Skip to content

Instantly share code, notes, and snippets.

@bibekblockchain
Created May 8, 2019 17:21
Show Gist options
  • Save bibekblockchain/aaf53aa297b2af6cf375a0363d359fd7 to your computer and use it in GitHub Desktop.
Save bibekblockchain/aaf53aa297b2af6cf375a0363d359fd7 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.5.1+commit.c8a2cb62.js&optimize=false&gist=
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;
}
}
}
pragma solidity ^0.4.23;
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract Polling{
using strings for *;
struct Event{
uint id;
string name;
string description;
mapping(uint => Options) options;
uint optionsCount;
mapping(address => bool) hasVoted;
}
mapping(uint => Event) public events;
uint public eventCount;
struct Options{
string name;
uint vote;
uint eventId;
}
constructor() public{
addEvent("testEvent","test", "options-abc");
addEvent("testEvent2","this is a test", "op1-op2");
}
function addEvent(string _name,string _description, string _options) public{
eventCount++;
events[eventCount].id=eventCount;
events[eventCount].name=_name;
events[eventCount].description=_description;
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].options[i].eventId=eventCount;
events[eventCount].optionsCount++;
}
}
function getOptions(uint id) public view 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 view returns (uint num){
num = events[eventId].options[optionId].vote;
}
function flashResult(uint _eventId) public view 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;
}
}
}
}
pragma solidity >=0.4.23;
contract Election{
//definition of candidates
struct Candidate{
uint id;
string name;
uint noOfVotes;
}
//mapping of candidates
mapping(uint => Candidate) public candidates;
//mappping of voters
mapping(address => bool) public hasVoted;
uint public candidatesCount;
//constructor function
constructor() public{
addCandidate("Messi");
addCandidate("Ronaldo");
}
//function to add candidate
function addCandidate(string memory _name) private{
candidatesCount++;
candidates[candidatesCount]=Candidate(candidatesCount,_name,0);
}
//function to vote
function vote(uint _candidateId) public{
require(!hasVoted[msg.sender]);
require(_candidateId>0 && _candidateId<=candidatesCount);
hasVoted[msg.sender]=true;
candidates[_candidateId].noOfVotes++;
}
}
pragma solidity ^0.4.23;
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract Polling{
using strings for *;
struct Event{
string name;
string description;
mapping(uint => Options) options;
uint optionsCount;
mapping(address => bool) hasVoted;
address owner;
}
mapping(uint => Event) public events;
uint public eventCount;
struct Options{
string name;
uint vote;
}
function addEvent(string _name,string _description, string _options) public{
eventCount++;
events[eventCount].name=_name;
events[eventCount].description=_description;
events[eventCount].owner=msg.sender;
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 view 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 view returns (uint num){
num = events[eventId].options[optionId].vote;
}
function flashResult(uint _eventId) public view 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;
}
}
}
}
//compiler version of 0.4.18 or higher could be user
pragma solidity >=0.4.18;
contract GOTPoll{
/*
declaration, definition and modeling of the entities
*/
//modeling the character of game of thrones
struct Character{
string characterName;
uint vote;
}
//mapping of characters against an id
mapping(uint => Character) public characters;
//mapping of users against if they have voted
mapping(address => bool) public hasVoted;
//declaration of a variable to keep track of total number of characters
uint public characterCount;
/*
necessary functions to carry out voting
*/
//adding a constructor
constructor() public{
addCharacters("Tiriyon Lannister");
addCharacters("John Snow");
addCharacters("Arya Stark");
addCharacters("Sansa Stark");
addCharacters("Cersi Lannister");
addCharacters("Night King");
}
//functions to add newCharacters to the poll
function addCharacters(string memory _characterName) public{
characterCount++;
//assigning value to the characters mapping
characters[characterCount].characterName=_characterName;
characters[characterCount].vote=0;
}
//function to carry out the voting
function vote(uint _id) public{
//check if the user has voted or not
require(!hasVoted[msg.sender]);
//check if the id of the user is below the characterCount
require(_id<=characterCount);
//increasing the number of votes by 1
characters[_id].vote++;
hasVoted[msg.sender]=true;
}
}
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);
}
}
pragma solidity >=0.4.23;
// written for Solidity version 0.4.18 and above that doesnt break functionality
contract Election{
struct Candidate{
uint id;
string name;
uint voteCount;
}
mapping(address => bool) public voters;
mapping(uint => Candidate) public candidates;
uint public candidatesCount;
constructor() public{
addCandidate("Candidate 1");
addCandidate("Candidate 2");
}
function addCandidate(string memory _name) private{
candidatesCount++;
candidates[candidatesCount]= Candidate(candidatesCount, _name, 0);
}
function vote (uint _candidateId) public {
// require that they haven't voted before
require(!voters[msg.sender]);
// require a valid candidate
require(_candidateId > 0 && _candidateId <= candidatesCount);
// record that voter has voted
voters[msg.sender] = true;
// update candidate vote Count
candidates[_candidateId].voteCount ++;
}
}
pragma solidity >=0.4.18;
contract StructArrayInit {
event OnCreateRoom(address indexed _from, uint256 _value);
struct Room {
address[] players;
uint256 whosTurnId;
uint256 roomState;
}
Room[] public rooms;
function createRoom() public {
Room memory room = Room(new address[](0), 0, 0);
rooms.push(room);
rooms[rooms.length-1].players.push(msg.sender);
emit OnCreateRoom(msg.sender, 0);
}
function getRoomPlayers(uint i) public view returns (address[] memory){
return rooms[i].players;
}
}
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);
}}
pragma solidity ^0.4.23;
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract Polling{
using strings for *;
struct Event{
uint id;
string name;
string description;
mapping(uint => Options) options;
uint optionsCount;
mapping(address => bool) hasVoted;
}
bool public hasEnded;
bool public hasStarted;
mapping(uint => Event) public events;
uint public eventCount;
address public owner;
mapping(address => bool) public canVote;
struct Options{
string name;
uint vote;
uint eventId;
}
constructor() public {
owner=msg.sender;
}
function addEvent(string _name,string _description, string _options) public{
require(msg.sender == owner);
require(!hasStarted);
require(!hasEnded);
require(msg.sender == owner);
eventCount++;
events[eventCount].id=eventCount;
events[eventCount].name=_name;
events[eventCount].description=_description;
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();
addOptions(eventCount,parts[i]);
}
}
function addOptions(uint eventId,string optionName) public{
uint i=events[eventId].optionsCount;
events[eventId].options[i].name=optionName;
events[eventId].options[i].eventId=eventId;
events[eventId].optionsCount++;
}
function addVoters(address voter) public{
require(msg.sender==owner);
require(!hasStarted);
require(!hasEnded);
require(msg.sender == owner);
canVote[voter]=true;
}
function getOptions(uint id) public view 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(hasStarted);
require(!hasEnded);
require(!events[_eventId].hasVoted[msg.sender]);
require(canVote[msg.sender]);
events[_eventId].options[_optionId].vote++;
events[_eventId].hasVoted[msg.sender]=true;
}
function seeVotes(uint eventId) public view returns (uint[]){
require(msg.sender==owner);
require(hasStarted);
require(hasEnded);
uint eventOptionCount= events[eventId].optionsCount;
uint[] memory num= new uint[](eventOptionCount);
for(uint i=0;i<eventOptionCount;i++){
num[i] =events[eventId].options[i].vote;
}
return num;
}
function startPoll() public{
require(msg.sender==owner);
hasStarted = true;
}
function endPoll() public{
require(msg.sender==owner);
require(hasStarted);
hasEnded = true;
}
function kill() public {
require(msg.sender==owner);
selfdestruct(owner);
}
}
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;
}
}
pragma solidity >=0.4.22 <0.6.0;
contract HelloName{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment