Created
January 17, 2018 10:56
-
-
Save anonymous/337a7cbb356ae4d898e891b206c1dc27 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.19+commit.c4cbbb05.js&optimize=false&gist=
This file contains 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
pragma solidity ^0.4.0; | |
contract Lottery { | |
address owner = 0xf94e9009C8edC5FD7c408f944836860744cb009E; | |
uint public pot; | |
uint public winningsLimit; | |
uint public participantLimit; | |
address[] public participants; | |
event Payout(address target, uint amount, uint nrOfParticipants); | |
modifier onlyBy(address _account) | |
{ | |
require(msg.sender == _account); | |
_; | |
} | |
function Lottery(uint _winningsLimit, uint _participantLimit) public onlyBy(owner) { | |
owner = msg.sender; | |
winningsLimit = _winningsLimit; | |
participantLimit = _participantLimit; | |
} | |
function () payable public { | |
participants.push(msg.sender); | |
pot += msg.value; | |
if (this.balance > winningsLimit || participants.length > participantLimit) { | |
terminate(); | |
} | |
} | |
function terminate() private { | |
uint totalPayout = pot; | |
pot = 0; | |
// Take 5% for the owner (rounded down by int-division) | |
uint ownerFee = totalPayout / 20; | |
// Pay the rest to the winner | |
uint payoutToWinner = totalPayout - ownerFee; | |
uint winnerIndex = uint(block.blockhash(block.number-1)) % participants.length; | |
address winner = participants[winnerIndex]; | |
winner.transfer(payoutToWinner); | |
owner.transfer(ownerFee); | |
Payout(winner, pot, participants.length); | |
delete participants; | |
} | |
function murder() public onlyBy(owner) { | |
if (msg.sender == owner) { | |
terminate(); | |
selfdestruct(owner); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment