Created
April 14, 2019 17:27
-
-
Save lildata/cd18478b3fd7044a7af9754a0f8f066d 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.7+commit.6da8b019.js&optimize=false&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.22 <0.6.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 view 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
# Voting with delegation. | |
# Information about voters | |
struct Voter: | |
# weight is accumulated by delegation | |
weight: int128 | |
# if true, that person already voted (which includes voting by delegating) | |
voted: bool | |
# person delegated to | |
delegate: address | |
# index of the voted proposal, which is not meaningful unless 'voted' is True. | |
vote: int128 | |
# Users can create proposals | |
struct Proposal: | |
# short name (up to 32 bytes) | |
name: bytes32 | |
# number of accumulated votes | |
voteCount: int128 | |
voters: public(map(address, Voter)) | |
proposals: public(map(int128, Proposal)) | |
voterCount: public(int128) | |
chairperson: public(address) | |
int128Proposals: public(int128) | |
@public | |
@constant | |
def delegated(addr: address) -> bool: | |
return self.voters[addr].delegate != ZERO_ADDRESS | |
@public | |
@constant | |
def directlyVoted(addr: address) -> bool: | |
return self.voters[addr].voted and (self.voters[addr].delegate == ZERO_ADDRESS) | |
# Setup global variables | |
@public | |
def __init__(_proposalNames: bytes32[2]): | |
self.chairperson = msg.sender | |
self.voterCount = 0 | |
for i in range(2): | |
self.proposals[i] = Proposal({ | |
name: _proposalNames[i], | |
voteCount: 0 | |
}) | |
self.int128Proposals += 1 | |
# Give a 'voter' the right to vote on this ballot. | |
# This may only be called by the 'chairperson'. | |
@public | |
def giveRightToVote(voter: address): | |
# Throws if the sender is not the chairperson. | |
assert msg.sender == self.chairperson | |
# Throws if the voter has already voted. | |
assert not self.voters[voter].voted | |
# Throws if the voter's voting weight isn't 0. | |
assert self.voters[voter].weight == 0 | |
self.voters[voter].weight = 1 | |
self.voterCount += 1 | |
# Used by 'delegate' below, and can be called by anyone. | |
@public | |
def forwardWeight(delegate_with_weight_to_forward: address): | |
assert self.delegated(delegate_with_weight_to_forward) | |
# Throw if there is nothing to do: | |
assert self.voters[delegate_with_weight_to_forward].weight > 0 | |
target: address = self.voters[delegate_with_weight_to_forward].delegate | |
for i in range(4): | |
if self.delegated(target): | |
target = self.voters[target].delegate | |
# The following effectively detects cycles of length <= 5, | |
# in which the delegation is given back to the delegator. | |
# This could be done for any int128ber of loops, | |
# or even infinitely with a while loop. | |
# However, cycles aren't actually problematic for correctness; | |
# they just result in spoiled votes. | |
# So, in the production version, this should instead be | |
# the responsibility of the contract's client, and this | |
# check should be removed. | |
assert target != delegate_with_weight_to_forward | |
else: | |
# Weight will be moved to someone who directly voted or | |
# hasn't voted. | |
break | |
weight_to_forward: int128 = self.voters[delegate_with_weight_to_forward].weight | |
self.voters[delegate_with_weight_to_forward].weight = 0 | |
self.voters[target].weight += weight_to_forward | |
if self.directlyVoted(target): | |
self.proposals[self.voters[target].vote].voteCount += weight_to_forward | |
self.voters[target].weight = 0 | |
# To reiterate: if target is also a delegate, this function will need | |
# to be called again, similarly to as above. | |
# Delegate your vote to the voter 'to'. | |
@public | |
def delegate(to: address): | |
# Throws if the sender has already voted | |
assert not self.voters[msg.sender].voted | |
# Throws if the sender tries to delegate their vote to themselves or to | |
# the default address value of 0x0000000000000000000000000000000000000000 | |
# (the latter might not be problematic, but I don't want to think about it). | |
assert to != msg.sender | |
assert to != ZERO_ADDRESS | |
self.voters[msg.sender].voted = True | |
self.voters[msg.sender].delegate = to | |
# This call will throw if and only if this delegation would cause a loop | |
# of length <= 5 that ends up delegating back to the delegator. | |
self.forwardWeight(msg.sender) | |
# Give your vote (including votes delegated to you) | |
# to proposal 'proposals[proposal].name'. | |
@public | |
def vote(proposal: int128): | |
# can't vote twice | |
assert not self.voters[msg.sender].voted | |
# can only vote on legitimate proposals | |
assert proposal < self.int128Proposals | |
self.voters[msg.sender].vote = proposal | |
self.voters[msg.sender].voted = True | |
# transfer msg.sender's weight to proposal | |
self.proposals[proposal].voteCount += self.voters[msg.sender].weight | |
self.voters[msg.sender].weight = 0 | |
# Computes the winning proposal taking all | |
# previous votes into account. | |
@public | |
@constant | |
def winningProposal() -> int128: | |
winning_vote_count: int128 = 0 | |
winning_proposal: int128 = 0 | |
for i in range(2): | |
if self.proposals[i].voteCount > winning_vote_count: | |
winning_vote_count = self.proposals[i].voteCount | |
winning_proposal = i | |
return winning_proposal | |
# Calls winningProposal() function to get the index | |
# of the winner contained in the proposals array and then | |
# returns the name of the winner | |
@public | |
@constant | |
def winnerName() -> bytes32: | |
return self.proposals[self.winningProposal()].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
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
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 Courses { | |
string fName; | |
uint age; | |
function setInstructor(string _fName, uint _age) public { | |
fName = _fName; | |
age = _age; | |
} | |
function getInstructor() public constant returns (string, uint) { | |
return (fName, age); | |
} | |
} |
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.5.6; | |
contract owned { | |
address payable owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner, "only the owner can call this fn"); | |
_; | |
} | |
} | |
contract mortal is owned { | |
function destroy() public onlyOwner { | |
selfdestruct(owner); | |
} | |
} | |
contract Courses is mortal { | |
string fName; | |
uint age; | |
function setInstructor(string memory _fName, uint _age) public { | |
fName = _fName; | |
age = _age; | |
} | |
function getInstructor() public view returns (string memory, uint) { | |
return (fName, age); | |
} | |
} |
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
fName: public(string[20]) | |
age: public(int128) | |
@public | |
def setInstructor(_fName: string[20], _age: int128): | |
self.fName = _fName | |
self.age = _age | |
@public | |
def getInstructor() -> string[20]: | |
return self.fName |
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.5.6; | |
contract owned { | |
address payable owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner, "only the owner can call this fn"); | |
_; | |
} | |
} | |
contract mortal is owned { | |
function destroy() public onlyOwner { | |
selfdestruct(owner); | |
} | |
} | |
contract Faucet is mortal { | |
event Withdrawal(address indexed to, uint amount); | |
event Deposit(address indexed from, uint amount); | |
function withdraw(uint withdraw_amount) public { | |
require(withdraw_amount <= 0.01 ether); | |
require(address(this).balance >= withdraw_amount, "not enought eth in the faucet"); | |
msg.sender.transfer(withdraw_amount); | |
emit Withdrawal(msg.sender, withdraw_amount); | |
} | |
function () external payable { | |
emit Deposit(msg.sender, msg.value); | |
} | |
} |
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 <0.6.0; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
// file name has to end with '_test.sol' | |
contract test_1 { | |
function beforeAll() public { | |
// here should instantiate tested contract | |
Assert.equal(uint(4), uint(3), "error in before all function"); | |
} | |
function check1() public { | |
// use 'Assert' to test the contract | |
Assert.equal(uint(2), uint(1), "error message"); | |
Assert.equal(uint(2), uint(2), "error message"); | |
} | |
function check2() public view returns (bool) { | |
// use the return value (true or false) to test the contract | |
return true; | |
} | |
} | |
contract test_2 { | |
function beforeAll() public { | |
// here should instantiate tested contract | |
Assert.equal(uint(4), uint(3), "error in before all function"); | |
} | |
function check1() public { | |
// use 'Assert' to test the contract | |
Assert.equal(uint(2), uint(1), "error message"); | |
Assert.equal(uint(2), uint(2), "error message"); | |
} | |
function check2() public view returns (bool) { | |
// use the return value (true or false) to test the contract | |
return true; | |
} | |
} |
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.24; | |
contract e0x { | |
event Log(string message); | |
event LinkAdded(uint linkId, string url); | |
struct LinkTemplate { | |
address userAddress; | |
string url; | |
} | |
uint lastLinkId; | |
mapping (uint => LinkTemplate) public linkMapping; | |
constructor() public { | |
lastLinkId = 0; | |
} | |
function createNewLink(string url) public returns (uint) { | |
lastLinkId++; | |
linkMapping[lastLinkId] = LinkTemplate(msg.sender, url); | |
emit LinkAdded(lastLinkId, url); | |
return lastLinkId; | |
} | |
modifier linkExists(uint linkId) { | |
//link with the given hash does not exist | |
if(linkMapping[linkId].userAddress == 0x0000000000000000000000000000000000000000) { | |
revert(); | |
} | |
_; | |
} | |
function getLink(uint linkId) linkExists(linkId) public constant | |
returns( | |
address, | |
string | |
) { | |
LinkTemplate memory link = linkMapping[linkId]; | |
return( | |
link.userAddress, | |
link.url | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment