Guest User

Untitled

a guest
Jul 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. contract Lotto {
  2.  
  3. uint constant public maxTickets = 5;
  4. uint public currentTickets = 0;
  5.  
  6. // mapping(key=>value)
  7. mapping(uint=>address) participants;
  8.  
  9. event logString(string x);
  10. event logUint(uint x);
  11. event logAddress(address x);
  12.  
  13. // 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.
  14.  
  15. uint constant public ticketPrice = 100000000000000000;
  16. // the cost of each ticket is .1 ether.
  17.  
  18. function rand() returns(uint) {
  19. uint decisionBlockHash = uint(block.blockhash(block.number));
  20. return decisionBlockHash%maxTickets;
  21. }
  22.  
  23. function payout() {
  24. // Game is over
  25. if (currentTickets != maxTickets) {
  26. throw;
  27. }
  28. // Calulate winner
  29. uint winner = rand(); // BUG: rand() always returns zero
  30.  
  31. // Set your state BEFORE sending your money!
  32. currentTickets = 0;
  33.  
  34. // Send money to the winner
  35. bool rv = participants[winner].send(ticketPrice*maxTickets);
  36.  
  37. logString("Winner prize sent:");
  38. logUint(winner);
  39. logString("Winner address:");
  40. logAddress(participants[winner]);
  41.  
  42. // Throw if some monkey buisness is going on
  43. if (rv == false) {
  44. throw;
  45. }
  46.  
  47. }
  48.  
  49. function() {
  50. // msg.sender == address of the sender
  51. // msg.value == money that was sent
  52. // send() is async
  53. if (msg.value != ticketPrice) {
  54. throw;
  55. }
  56. if (currentTickets == maxTickets){
  57. this.payout();
  58. }
  59. participants[currentTickets] = msg.sender;
  60. currentTickets = currentTickets + 1;
  61. logString("A ticket was added");
  62. logUint(currentTickets);
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment