contract BTCRelay{ function getLastBlockHeight() public returns(int256); function getBlockHash(int256) public returns(int256); } contract Powerball{ mapping(address => uint8[6][]) public tickets; mapping(address => uint) public balances; uint public roundStart; uint public constant roundLength = 3 days; bool roundOver = false; enum Phase {Bet,Draw,Claim} Phase currentPhase = Phase.Bet; uint8[] public balls; BTCRelay relay; uint public constant houseEdge = 1; //percent of each ticket address house; uint houseFunds; int256 firstBlock; function Powerball(address relayAddress){ relay = BTCRelay(relayAddress); roundStart = block.timestamp; house = msg.sender; } function buyTicket(uint8[6] numbers){ if(startDraw()) throw; if(!checkNumbers(numbers)) throw; if(msg.value < 2 ether) throw; for(uint16 i = 0; i < msg.value/2; i++){ tickets[msg.sender].push(numbers); } houseFunds += msg.value/100; } function startDraw() returns(bool){ if(block.timestamp > roundStart + roundLength){ if(currentPhase == Phase.Bet){ currentPhase = Phase.Draw; firstBlock = relay.getLastBlockHeight() + 2; } return false; } return true; } function checkNumbers(uint8[6] numbers) returns (bool){ for(var i = 0; i <6; i++){ if (numbers[i] > 69 || numbers[i] <1) return false; } if(numbers[5]>26) return false; return true; } function getBall(uint8 ball) returns(uint8){ if(balls[ball] != 0) return balls[ball]; if(ball > 5) throw; if (relay.getLastBlockHeight() < 8+ball+firstBlock) throw; int256 hash = relay.getBlockHash(firstBlock + ball); if (hash == 0) throw; else if(ball !=5) balls[ball] = uint8(hash) % 69 +1; else balls[ball] = uint8(hash)%26; } }