Created
January 30, 2016 16:31
-
-
Save qwtel/18763f2c5ee83b8cc632 to your computer and use it in GitHub Desktop.
ETH Pyramid Contract
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 Pyramid { | |
struct Participant { | |
bytes desc; | |
address etherAddress; | |
bytes bitcoinAddress; | |
} | |
Participant[] public participants; | |
uint public payoutIdx = 0; | |
uint public collectedFees; | |
address public owner; | |
address public bitcoinBridge; | |
// used later to restrict some methods | |
modifier onlyowner { if (msg.sender == owner) _ } | |
// events make it easier to interface with the contract | |
event NewParticipant(uint indexed idx); | |
function Pyramid() { | |
owner = msg.sender; | |
} | |
// fallback function - simple transactions trigger this | |
function() { | |
enter(msg.data); | |
} | |
function enter(bytes desc) { | |
if (msg.value < 1 ether) { | |
msg.sender.send(msg.value); | |
return; | |
} | |
if (desc.length > 16) { | |
msg.sender.send(msg.value); | |
return; | |
} | |
if (msg.value > 1 ether) { | |
msg.sender.send(msg.value - 1 ether); | |
} | |
uint idx = participants.length; | |
participants.length += 1; | |
participants[idx].desc = desc; | |
participants[idx].etherAddress = msg.sender; | |
NewParticipant(idx); | |
if (idx != 0) { | |
collectedFees += 100 finney; | |
} else { | |
// first participant has no one above them, | |
// so it goes all to fees | |
collectedFees += 1 ether; | |
} | |
// for every three new participants we can | |
// pay out to an earlier participant | |
if (idx != 0 && idx % 3 == 0) { | |
// payout is triple, minus 10 % fee | |
uint amount = 3 ether - 300 finney; | |
participants[payoutIdx].etherAddress.send(amount); | |
payoutIdx += 1; | |
} | |
} | |
function getNumberOfParticipants() constant returns (uint n) { | |
return participants.length; | |
} | |
function collectFees(address recipient) onlyowner { | |
if (collectedFees == 0) return; | |
recipient.send(collectedFees); | |
collectedFees = 0; | |
} | |
function setOwner(address _owner) onlyowner { | |
owner = _owner; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment