Last active
November 5, 2016 20:01
-
-
Save karlfloersch/61beaa8700a5494a9cf6fdad2d921603 to your computer and use it in GitHub Desktop.
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.4; | |
contract Lotto { | |
uint constant public maxTickets = 5; | |
uint public currentTickets = 0; | |
mapping(uint=>address) participants; | |
event logString(string x); | |
event logUint(uint x); | |
event logAddress(address x); | |
uint constant public ticketPrice = 100000000000000000; | |
// the cost of each ticket is .1 ether. | |
function rand() returns(uint) { | |
uint decisionBlockHash = uint(block.blockhash(block.number-1)); | |
return decisionBlockHash%maxTickets; | |
} | |
function payout() { | |
// Game is over | |
if (currentTickets != maxTickets) { | |
throw; | |
} | |
// Calulate winner | |
uint winner = rand(); | |
// Set your state BEFORE sending your money! | |
currentTickets = 0; | |
// Send money to the winner | |
bool rv = participants[winner].send(ticketPrice*maxTickets); | |
logString("Winner prize sent:"); | |
logUint(winner); | |
logString("Winner address:"); | |
logAddress(participants[winner]); | |
// Throw if some monkey buisness is going on | |
if (rv == false) { | |
throw; | |
} | |
} | |
function() { | |
if (msg.value != ticketPrice) { | |
throw; | |
} | |
if (currentTickets == maxTickets){ | |
this.payout(); | |
} | |
participants[currentTickets] = msg.sender; | |
currentTickets = currentTickets + 1; | |
logString("A ticket was added"); | |
logUint(currentTickets); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment