Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- contract Lotto {
- uint constant public maxTickets = 5;
- uint public currentTickets = 0;
- // mapping(key=>value)
- mapping(uint=>address) participants;
- event logString(string x);
- event logUint(uint x);
- event logAddress(address x);
- // there are an infinite number of rounds (just like a real lottery that takes place every week). `blocksPerRound` decides how many blocks each round will last. 6800 is around a day.
- uint constant public ticketPrice = 100000000000000000;
- // the cost of each ticket is .1 ether.
- function rand() returns(uint) {
- uint decisionBlockHash = uint(block.blockhash(block.number));
- return decisionBlockHash%maxTickets;
- }
- function payout() {
- // Game is over
- if (currentTickets != maxTickets) {
- throw;
- }
- // Calulate winner
- uint winner = rand(); // BUG: rand() always returns zero
- // 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() {
- // msg.sender == address of the sender
- // msg.value == money that was sent
- // send() is async
- if (msg.value != ticketPrice) {
- throw;
- }
- if (currentTickets == maxTickets){
- this.payout();
- }
- participants[currentTickets] = msg.sender;
- currentTickets = currentTickets + 1;
- logString("A ticket was added");
- logUint(currentTickets);
- }
- }
Add Comment
Please, Sign In to add comment