Created
June 3, 2018 04:37
-
-
Save DrAlbedo/0a381d377278c0b8f0536272898da0e5 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=false&gist=
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
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
Proposal[] proposals; | |
mapping(address => Voter) voter; | |
address chairperson; | |
function Ballot(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; | |
proposals.length = _numProposals; | |
} | |
function register(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
} | |
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) | |
} | |
} |
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.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. | |
function Ballot(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 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.4.0; | |
contract Bidder { | |
string name = "PKU"; | |
uint bidAmount = 20000; | |
bool eligible = false; | |
uint minBid = 1000; | |
function setName(string name_) public { | |
name = name_; | |
} | |
function setBidAmount(uint amount) public { | |
bidAmount = amount; | |
} | |
function determineEligibility() public constant returns (bool) { | |
if (bidAmount > minBid) return true; | |
else return false; | |
} | |
} |
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.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
enum Stage {Init,Reg, Vote, Done} | |
Stage public stage = Stage.Init; | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
event votingCompleted(); | |
uint startTime; | |
//modifiers | |
modifier validStage(Stage reqStage) | |
{ require(stage == reqStage); | |
_; | |
} | |
/// Create a new ballot with $(_numProposals) different proposals. | |
function Ballot(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; // weight is 2 for testing purposes | |
proposals.length = _numProposals; | |
stage = Stage.Reg; | |
startTime = now; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public validStage(Stage.Reg) { | |
//if (stage != Stage.Reg) {return;} | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
if (now > (startTime+ 30 seconds)) {stage = Stage.Vote; } | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public validStage(Stage.Vote) { | |
// if (stage != Stage.Vote) {return;} | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
if (now > (startTime+ 30 seconds)) {stage = Stage.Done; votingCompleted();} | |
} | |
function winningProposal() public validStage(Stage.Done) constant returns (uint8 _winningProposal) { | |
//if(stage != Stage.Done) {return;} | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
assert (winningVoteCount > 0); | |
} | |
} | |
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.4.0; | |
contract Coin { | |
address public minter; | |
mapping (address => uint) public balances; | |
event Sent(address from, address to, uint amount); | |
function Coin() public { | |
minter = msg.sender; | |
} | |
function mint (address receiver, uint amount) public { | |
if (msg.sender != minter) return; | |
balances[receiver] += amount; | |
} | |
function send(address receiver, uint amount) public { | |
if (balances[msg.sender] < amount) return; | |
balances[msg.sender] -= amount; | |
balances[receiver] += amount; | |
Sent(msg.sender, receiver, amount); | |
} | |
} |
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.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
// address delegate; | |
} | |
struct Proposal { | |
uint voteCount; // could add other data about proposal | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
function Ballot(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
} | |
/// 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 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.4.0; | |
contract Coin { | |
// The keyword "public" makes those variables | |
// readable from outside. | |
address public minter; | |
mapping (address => uint) public balances; | |
// Events allow light clients to react on | |
// changes efficiently. | |
event Sent(address from, address to, uint amount); | |
// This is the constructor whose code is | |
// run only when the contract is created. | |
function Coin() public{ | |
minter = msg.sender; | |
} | |
function mint(address receiver, uint amount) public { | |
if (msg.sender != minter) return; | |
balances[receiver] += amount; | |
} | |
function send(address receiver, uint amount) public { | |
if (balances[msg.sender] < amount) return; | |
balances[msg.sender] -= amount; | |
balances[receiver] += amount; | |
Sent(msg.sender, receiver, amount); | |
} | |
} |
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.4.0; | |
// Imagine a big integer that the whole world could share | |
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) public { | |
storedData = x; | |
} | |
function get() constant public returns (uint) { | |
return storedData; | |
} | |
function increment (uint n) public { | |
storedData = storedData + n; | |
return; | |
} | |
function decrement (uint n) public { | |
storedData = storedData - n; | |
return; | |
} | |
} | |
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.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
enum Stage {Init,Reg, Vote, Done} | |
Stage public stage = Stage.Init; | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
uint startTime; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
function Ballot(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; // weight is 2 for testing purposes | |
proposals.length = _numProposals; | |
stage = Stage.Reg; | |
startTime = now; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public { | |
if (stage != Stage.Reg) {return;} | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
if (now > (startTime+ 10 seconds)) {stage = Stage.Vote; startTime = now;} | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
if (stage != Stage.Vote) {return;} | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
if (now > (startTime+ 10 seconds)) {stage = Stage.Done;} | |
} | |
function winningProposal() public constant returns (uint8 _winningProposal) { | |
if(stage != Stage.Done) {return;} | |
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 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.4.0; | |
contract Greeter { | |
string public yourName; // data | |
/* This runs when the contract is executed */ | |
function Greeter() public { | |
yourName = "World"; | |
} | |
function set(string name)public { | |
yourName = name; | |
} | |
function hello() constant public returns (string) { | |
return yourName; | |
} | |
} |
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.4.0; | |
contract Bidder { | |
string public name; | |
uint public bidAmount = 20000; | |
bool public eligible; | |
uint constant minBid = 1000; | |
} |
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
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) public { | |
storedData = x; | |
} | |
function get() constant public returns (uint) { | |
return storedData; | |
} | |
function increment(uint n) public { | |
storedData += n; | |
return; | |
} | |
function decrement(uint n) public { | |
storedData -= n; | |
return; | |
} | |
} |
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
contract Test { | |
int public number; | |
function Test(int num) public { | |
number = num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment