Guest User

Lotteresque

a guest
Aug 9th, 2015
925
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Lotteresque: reimbursement + lottery + dividend.
  2.  
  3.  
  4. Cost to participate: one ether.
  5.  
  6. Begins with zero participants. First participant fills up level one. Next two participants fill up level two. Next three participants fill up level three, and so on.
  7.  
  8. When a level fills up, three things happen:
  9.  
  10. 1. Every participant in the previous two levels receives 0.5 ether (the "reimbursement").
  11.  
  12. 2. Every participant in every level participates in a one-ether lottery.
  13.  
  14. 3. Any remaining balance is divided equally amongst all participants in all levels (the "dividend").
  15.  
  16. Beginning with level two, the remaining balance is a minimum of 0.5 ether. It might also be higher; see "Errata", below.
  17.  
  18.  
  19. To participate:
  20.  
  21. eth.sendTransaction({from:eth.accounts[0],to:"0xfbc128067a2fe13c11bbc6cc55f29aa1f27630da",value:web3.toWei(1,"ether"),gas:500000});
  22.  
  23.  
  24. To monitor:
  25.  
  26. var lotteresqueContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"next","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"participants","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"balance","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"inputs":[],"type":"constructor"}]);
  27.  
  28. var lotteresque = lotteresqueContract.at("0xfbc128067a2fe13c11bbc6cc55f29aa1f27630da");
  29.  
  30. lotteresque.balance();
  31.  
  32. lotteresque.participants();
  33.  
  34. lotteresque.next();
  35.  
  36.  
  37. Errata:
  38.  
  39. If less than one ether is sent, the sender does not participate and the entire amount is added to the contract balance for distribution to the participants.
  40.  
  41. If more than one ether is sent, the sender does participate and the surplus amount is added to the contract balance for distribution to the participants.
  42.  
  43.  
  44. Code:
  45.  
  46. contract lotteresque {
  47.  
  48. uint public balance;
  49. uint depth;
  50. uint public participants;
  51. address[] tree;
  52.  
  53. function lotteresque() {
  54. tree.length = 1;
  55. }
  56.  
  57. function () {
  58. uint dividend;
  59. uint i;
  60. uint lower;
  61. uint upper;
  62.  
  63. balance += msg.value;
  64.  
  65. if ( msg.value >= 1000000000000000000 ) {
  66. tree[ participants++ ] = msg.sender;
  67.  
  68. if ( participants == tree.length ) {
  69. depth++;
  70.  
  71. // reimbursement
  72.  
  73. if ( depth > 1 ) {
  74. lower = participants - 3 * ( depth - 1 );
  75. upper = participants - depth;
  76.  
  77. for ( i = lower; i < upper; i++ ) {
  78. tree[ i ].send( 500000000000000000 );
  79. }
  80.  
  81. balance -= ( upper - lower ) * 500000000000000000;
  82. }
  83.  
  84. // lottery
  85.  
  86. tree[ uint( block.blockhash( block.number - 1 ) ) % participants ].send( 1000000000000000000 );
  87.  
  88. balance -= 1000000000000000000;
  89.  
  90. // dividend
  91.  
  92. if ( balance > 0 ) {
  93. dividend = balance / participants;
  94.  
  95. for ( i = 0; i < participants; i++ ) {
  96. tree[ i ].send( dividend );
  97. }
  98.  
  99. balance -= participants * dividend;
  100. }
  101.  
  102. tree.length += depth + 1;
  103. }
  104. }
  105. }
  106.  
  107. function next() constant returns ( uint ) {
  108. return tree.length;
  109. }
  110.  
  111. }
RAW Paste Data