Advertisement
tjade273

EthGambler Source

Aug 9th, 2015
3,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. contract Lotto {
  2. uint constant ticketPrice = 100 finney;
  3. uint constant waitTime = 6;
  4. uint constant bettingTime = 40;
  5. uint constant randSubmitTime =30;
  6. uint constant payoutTime = 30;
  7. uint constant totalTime = waitTime + bettingTime+ randSubmitTime + payoutTime;
  8. uint public lastBlock;
  9. uint public balance;
  10. uint public submitted;
  11. address[] accounts;
  12. address[] public tickets;
  13. uint256 XOR;
  14. mapping(address => uint256) secretHash;
  15. mapping(address => uint) balances;
  16. function Lotto(){ //Runs only at creation
  17. lastBlock = block.number;
  18. }
  19.  
  20. function tStep() constant public returns (uint) {
  21. return block.number%totalTime;
  22. }
  23. function sha(uint256 a) public returns(uint256){ //DO NOT USE VIA BLOCKCHAIN!
  24. return uint256(sha3(a));
  25. }
  26.  
  27.  
  28.  
  29. function payoutReady() public constant returns (bool){
  30.  
  31. return (block.number % totalTime > totalTime- payoutTime && block.number - lastBlock > 68 ) ;
  32.  
  33. }
  34.  
  35. function buyTicket(uint256 secrethash) public {
  36. if (block.number % totalTime < bettingTime && msg.value > ticketPrice){
  37. secretHash[msg.sender] = secrethash;
  38. balances[msg.sender] += msg.value;
  39. balance += msg.value;
  40. if(balances[msg.sender] == 0){
  41. accounts.length = accounts.length + 1;
  42. accounts[accounts.length-1] = msg.sender;
  43. }
  44. }
  45. }
  46.  
  47. function submitSecret(uint256 secret) public {
  48. if(balances[msg.sender] != 0 && block.number% totalTime < totalTime - payoutTime && block.number% totalTime > bettingTime + waitTime){
  49. if (uint256(sha3(secret)) == secretHash[msg.sender]){
  50. XOR = XOR ^ secret ^ uint(msg.sender);
  51. submitted++;
  52. for(uint32 i=0; i < balances[msg.sender] / ticketPrice; i++ ){
  53. tickets.length = tickets.length +1;
  54. tickets[tickets.length -1 ] = msg.sender; //append one copy of sender address
  55.  
  56. }
  57. balances[msg.sender]=0;
  58. }
  59. }
  60.  
  61. }
  62.  
  63. function payout() public returns (address) {
  64. if (payoutReady()) {
  65. if(submitted==accounts.length){
  66. address winner = tickets[XOR%tickets.length];
  67. winner.send(balance - 25000*tx.gasprice);
  68. }
  69. else{
  70. for(uint j =0; i<tickets.length; i++){
  71. tickets[i].send(balance/tickets.length);
  72. }
  73. }
  74. lastBlock = block.number;
  75. delete balance;
  76. for (uint i=0; i< accounts.length; i++){
  77. delete secretHash[accounts[i]];
  78. delete balances[accounts[i]];
  79. }
  80. delete tickets;
  81.  
  82. return winner;
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement