Skip to content

Instantly share code, notes, and snippets.

@tjade273
Last active February 5, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjade273/63bc7fdce703b1a79cce to your computer and use it in GitHub Desktop.
Save tjade273/63bc7fdce703b1a79cce to your computer and use it in GitHub Desktop.
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[6] 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+1;
}
function getPayout() public returns(uint){
if (currentPhase == Phase.Draw){
for(uint8 i; i<6; i++){
if(balls[i] == 0) throw;
}
currentPhase = Phase.Claim;
}
else if (currentPhase == Phase.Bet) throw;
uint payout;
for(uint ticket; ticket<tickets[msg.sender].length; ticket++){
uint8 matches;
bool powerBall = false;
for(uint8 ball; ball < 5; ball++){
for(uint8 draw; draw < 5; draw++){
if(tickets[msg.sender][ticket][ball] == balls[draw]){
matches++;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment