Created
August 14, 2019 13:37
-
-
Save mhe/17dde00dcefccc0428bd4ca15d5989c1 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=
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
pragma solidity >=0.4.22 <0.6.0; | |
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.5.0; | |
// ---------------------------------------------------------------------------- | |
// Safe maths | |
// ---------------------------------------------------------------------------- | |
library SafeMath { | |
function add(uint a, uint b) internal pure returns (uint c) { | |
c = a + b; | |
require(c >= a); | |
} | |
function sub(uint a, uint b) internal pure returns (uint c) { | |
require(b <= a); | |
c = a - b; | |
} | |
function mul(uint a, uint b) internal pure returns (uint c) { | |
c = a * b; | |
require(a == 0 || c / a == b); | |
} | |
function div(uint a, uint b) internal pure returns (uint c) { | |
require(b > 0); | |
c = a / b; | |
} | |
} | |
contract CuriousUHackathonVoting { | |
// struct Team { | |
// string name; | |
// members; | |
// } | |
// mapping(uint => ) | |
mapping(address => uint) member2team; | |
Team[] teams; | |
mapping(address => uint) donations; | |
// A function to create a team | |
function createTeam(string memory name) public { | |
Team storage t; | |
t.name = name; | |
t.members[msg.sender] = true; | |
teams.push(t); | |
} | |
function addMemberToTeam(uint team, address newMember) { | |
require(teams[team].members[msg.sender]); | |
teams[team].members[newMember] = true; | |
} | |
function () payable{ | |
donations[msg.sender] += msg.value; | |
} | |
function startVoting() { | |
} | |
// What team is voter member of? | |
// Has the voter voted? | |
// Is ... a valid voter? => Has voter send ether? | |
// Which team won the vote? | |
// Starting and stopping the vote | |
// Teams big enough? | |
// End time (is it in the future?) | |
// Minimum amount of ether to send (set/get?) | |
// What are the members of a team? | |
// How big is the team? | |
// Create teams? | |
// Members decided by teacher | |
// | |
} |
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.0; | |
// Based on https://github.com/bokkypoobah/Tokens/blob/master/contracts/FixedSupplyToken.sol | |
// ---------------------------------------------------------------------------- | |
// Safe maths | |
// ---------------------------------------------------------------------------- | |
library SafeMath { | |
function add(uint a, uint b) internal pure returns (uint c) { | |
c = a + b; | |
require(c >= a); | |
} | |
function sub(uint a, uint b) internal pure returns (uint c) { | |
require(b <= a); | |
c = a - b; | |
} | |
function mul(uint a, uint b) internal pure returns (uint c) { | |
c = a * b; | |
require(a == 0 || c / a == b); | |
} | |
function div(uint a, uint b) internal pure returns (uint c) { | |
require(b > 0); | |
c = a / b; | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC Token Standard #20 Interface | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md | |
// ---------------------------------------------------------------------------- | |
contract ERC20Interface { | |
function totalSupply() public view returns (uint); | |
function balanceOf(address tokenOwner) public view returns (uint balance); | |
function allowance(address tokenOwner, address spender) public view returns (uint remaining); | |
function transfer(address to, uint tokens) public returns (bool success); | |
function approve(address spender, uint tokens) public returns (bool success); | |
function transferFrom(address from, address to, uint tokens) public returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
} | |
// ---------------------------------------------------------------------------- | |
// Contract function to receive approval and execute function in one call | |
// | |
// Borrowed from MiniMeToken | |
// ---------------------------------------------------------------------------- | |
contract ApproveAndCallFallBack { | |
function receiveApproval(address from, uint256 tokens, address token, bytes memory data) public; | |
} | |
// ---------------------------------------------------------------------------- | |
// Owned contract | |
// ---------------------------------------------------------------------------- | |
contract Owned { | |
address public owner; | |
address public newOwner; | |
event OwnershipTransferred(address indexed _from, address indexed _to); | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address _newOwner) public onlyOwner { | |
newOwner = _newOwner; | |
} | |
function acceptOwnership() public { | |
require(msg.sender == newOwner); | |
emit OwnershipTransferred(owner, newOwner); | |
owner = newOwner; | |
newOwner = address(0); | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC20 Token, with the addition of symbol, name and decimals and a | |
// fixed supply | |
// ---------------------------------------------------------------------------- | |
contract CuriousUToken is ERC20Interface, Owned { | |
using SafeMath for uint; | |
string public symbol; | |
string public name; | |
uint8 public decimals; | |
uint _totalSupply; | |
mapping(address => uint) balances; | |
mapping(address => mapping(address => uint)) allowed; | |
// ------------------------------------------------------------------------ | |
// Constructor | |
// ------------------------------------------------------------------------ | |
constructor() public { | |
symbol = "CU"; | |
name = "CuriousU Token"; | |
decimals = 2; | |
_totalSupply = 1000 * 10**uint(decimals); | |
balances[owner] = _totalSupply; | |
emit Transfer(address(0), owner, _totalSupply); | |
} | |
// ------------------------------------------------------------------------ | |
// Total supply | |
// ------------------------------------------------------------------------ | |
function totalSupply() public view returns (uint) { | |
return _totalSupply.sub(balances[address(0)]); | |
} | |
// ------------------------------------------------------------------------ | |
// Get the token balance for account `tokenOwner` | |
// ------------------------------------------------------------------------ | |
function balanceOf(address tokenOwner) public view returns (uint balance) { | |
return balances[tokenOwner]; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer the balance from token owner's account to `to` account | |
// - Owner's account must have sufficient balance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transfer(address to, uint tokens) public returns (bool success) { | |
balances[msg.sender] = balances[msg.sender].sub(tokens); | |
balances[to] = balances[to].add(tokens); | |
emit Transfer(msg.sender, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for `spender` to transferFrom(...) `tokens` | |
// from the token owner's account | |
// | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// recommends that there are no checks for the approval double-spend attack | |
// as this should be implemented in user interfaces | |
// ------------------------------------------------------------------------ | |
function approve(address spender, uint tokens) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
emit Approval(msg.sender, spender, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer `tokens` from the `from` account to the `to` account | |
// | |
// The calling account must already have sufficient tokens approve(...)-d | |
// for spending from the `from` account and | |
// - From account must have sufficient balance to transfer | |
// - Spender must have sufficient allowance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transferFrom(address from, address to, uint tokens) public returns (bool success) { | |
balances[from] = balances[from].sub(tokens); | |
allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens); | |
balances[to] = balances[to].add(tokens); | |
emit Transfer(from, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Returns the amount of tokens approved by the owner that can be | |
// transferred to the spender's account | |
// ------------------------------------------------------------------------ | |
function allowance(address tokenOwner, address spender) public view returns (uint remaining) { | |
return allowed[tokenOwner][spender]; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for `spender` to transferFrom(...) `tokens` | |
// from the token owner's account. The `spender` contract function | |
// `receiveApproval(...)` is then executed | |
// ------------------------------------------------------------------------ | |
function approveAndCall(address spender, uint tokens, bytes memory data) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
emit Approval(msg.sender, spender, tokens); | |
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, address(this), data); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Don't accept ETH | |
// ------------------------------------------------------------------------ | |
function () external payable { | |
revert(); | |
} | |
// ------------------------------------------------------------------------ | |
// Owner can transfer out any accidentally sent ERC20 tokens | |
// ------------------------------------------------------------------------ | |
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) { | |
return ERC20Interface(tokenAddress).transfer(owner, tokens); | |
} | |
} |
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.7.0; | |
contract CuriousUToken { | |
string public constant name = "CuriousU"; | |
string public constant symbol = "CU"; | |
uint8 public constant decimals = 18; // 18 is the most common number of decimal places | |
// The balance for each account | |
mapping(address => uint256) balances; | |
uint totalAmount; | |
constructor(uint amount) public { | |
totalAmount = amount; | |
} | |
function totalSupply() public view returns (uint) { | |
return totalAmount; | |
} | |
// Get the token balance for account 'tokenOwner' | |
function balanceOf(address tokenOwner) public view returns (uint balance) { | |
return balances[tokenOwner]; | |
} | |
function allowance(address tokenOwner, address spender) public view returns (uint remaining); | |
function transfer(address to, uint tokens) public returns (bool success) { | |
require(balances[msg.sender] >= tokens); | |
balances[msg.sender] = balances[msg.sender] - tokens; | |
balances[to] = balances[to] + tokens; | |
return true; | |
} | |
function approve(address spender, uint tokens) public returns (bool success); | |
function transferFrom(address from, address to, uint tokens) public returns (bool success) { | |
require(balances[from] >= tokens && msg.sender == from); | |
balances[from] = balances[from] - tokens; | |
balances[to] = balances[to] + tokens; | |
return true; | |
} | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
} |
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.7.0; | |
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) public { | |
storedData = x; | |
} | |
function get() public view returns (uint) { | |
return storedData; | |
} | |
} |
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.26; | |
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) public { | |
storedData = x; | |
} | |
function get() public view returns (uint) { | |
return storedData; | |
} | |
} |
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.7.0; | |
/// @title Voting with delegation. | |
contract HackathonVoting { | |
// This declares a new complex type which will | |
// be used for variables later. | |
// It will represent a single voter. | |
struct TeamMember { | |
bool canVote; | |
bool voted; // if true, that person already voted | |
uint vote; // index of the voted proposal | |
uint teamId; | |
} | |
// This is a type for a single proposal. | |
struct Team { | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
mapping(address => bool) members; | |
} | |
address public chairperson; | |
// This declares a state variable that | |
// stores a `Voter` struct for each possible address. | |
mapping(address => TeamMember) public voters; | |
// A dynamically-sized array of `Proposal` structs. | |
Team[] public proposals; | |
/// Create a new ballot to choose one of `proposalNames`. | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].canVote = true; | |
// For each of the provided proposal names, | |
// create a new proposal object and add it | |
// to the end of the array. | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// `Proposal({...})` creates a temporary | |
// Proposal object and `proposals.push(...)` | |
// appends it to the end of `proposals`. | |
proposals.push(Team({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
// A function to create a team | |
function createTeam(string memory name) public { | |
} | |
function () payable{ | |
donations[msg.sender] += msg.value; | |
} | |
function startVoting() { | |
} | |
function payoutWinners() { | |
} | |
/// Give your vote (including votes delegated to you) | |
/// to proposal `proposals[proposal].name`. | |
function vote(uint teamId) public { | |
TeamMember storage sender = voters[msg.sender]; | |
require(sender.canVote, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
require(sender.teamId != teamId); | |
sender.voted = true; | |
sender.vote = teamId; | |
// If `proposal` is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[teamId].voteCount += 1; | |
} | |
/// @dev Computes the winning proposal taking all | |
/// previous votes into account. | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
// Calls winningProposal() function to get the index | |
// of the winner contained in the proposals array and then | |
// returns the name of the winner | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment