Created
December 20, 2016 22:36
-
-
Save RFV/6fef45cfe7551f77dc48c8e714f81ce1 to your computer and use it in GitHub Desktop.
Double or Nothing betting smart contract with Bitcoin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract Alarm { | |
function deposit(); | |
function scheduleCall(address contractAddress, bytes4 abiSignature, bytes32 | |
dataHash, uint targetBlock, uint8 gracePeriod); | |
} | |
contract BitcoinBridge { | |
function queuePayment(bytes bitcoinAddress) returns(bool successful); | |
} | |
contract Etherdice { | |
function bet(uint8 pick, bool isMirrored, uint8 die) returns (int); | |
} | |
contract DoubleOrNothing { | |
Alarm public alarmClock = Alarm(0x07307d0b136a79bac718f43388aed706389c4588); | |
BitcoinBridge public bitcoinBridge = BitcoinBridge(0x4d6387f3b967da39b11de111158d49754c31985d); | |
Etherdice public etherdice = Etherdice(0x37005accab2a7807d8572902a172c2a325021910); | |
bool public ongoingBet = false; | |
uint public blockNumber; | |
bytes public bitcoinAddress; | |
function () { | |
if (msg.sender == address(etherdice)) { | |
// Etherdice is sending our winnings | |
return; | |
} | |
if (ongoingBet || msg.value < 700 finney) | |
throw; | |
uint betAmount = msg.value - 200 finney; | |
if (etherdice.bet.value(betAmount)(11, true, 1) == -1) | |
throw; | |
alarmClock.deposit.value(200 finney)(); | |
bytes4 sig = bytes4(sha3('alarm()')); | |
bytes32 dataHash = sha3(); | |
uint targetBlock = block.number + 45; | |
uint8 gracePeriod = 64; | |
alarmClock.scheduleCall(this, sig, dataHash, targetBlock, gracePeriod); | |
ongoingBet = true; | |
blockNumber = block.number; | |
bitcoinAddress = msg.data; | |
} | |
function alarm() { | |
if (!ongoingBet) | |
throw; | |
if (block.number - blockNumber < 35) | |
throw; // too early | |
if (this.balance > 0) | |
bitcoinBridge.queuePayment.value(this.balance)(bitcoinAddress); | |
ongoingBet = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment