-
Notifications
You must be signed in to change notification settings - Fork 0
/
KriptoLottery.sol
176 lines (143 loc) · 5.61 KB
/
KriptoLottery.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pragma solidity ^0.4.18;
import '../zeppelin-solidity/contracts/ownership/Ownable.sol';
import '../zeppelin-solidity/contracts/lifecycle/Pausable.sol';
contract KriptoLottery is Ownable, Pausable {
// wait for approx. 1 week.
uint constant NEXT_LOTTERY_WAIT_TIME_IN_BLOCKS = 38117;
event LotteryRunFinished(address winner, uint256 charityAmount, uint256 affiliateAmount, uint256 jackpot);
event ApplicationDone(uint applicationNumber);
uint128 public maxParticipant;
uint256 public lotteryAmount;
address public charityAddress;
address public affiliateAddress;
uint256 public totalGivenAmount;
uint256 public totalDonation;
uint256 public totalAffiliateAmount;
uint16 public donationRatio;
uint16 public affiliateRatio;
mapping(uint => LotteryPlay) public lotteries;
uint16 public currentLottery;
struct LotteryPlay {
uint endBlock;
uint startBlock;
bytes32 blockHash;
address winner;
Participant[] participants;
}
struct Participant {
address addr;
}
function KriptoLottery(uint128 _maxParticipant, uint256 _lotteryAmount) public {
if (_maxParticipant == 0) {
maxParticipant = 30;
} else {
maxParticipant = _maxParticipant;
}
if (_lotteryAmount == 0) {
lotteryAmount = 0.02 ether;
} else {
lotteryAmount = _lotteryAmount;
}
paused = true;
initialise();
paused = false;
affiliateAddress = msg.sender;
charityAddress = msg.sender;
donationRatio = 100;
affiliateRatio = 0;
}
function() public payable {
apply();
}
function apply() public payable whenNotPaused returns (uint256) {
// Anyone can apply as much as they want.
require(lotteries[currentLottery].participants.length + 1 <= maxParticipant);
require(msg.value == lotteryAmount);
lotteries[currentLottery].participants.push(Participant(msg.sender));
ApplicationDone(lotteries[currentLottery].participants.length - 1);
return lotteries[currentLottery].participants.length - 1;
}
function getCurrentCount() public constant returns (uint256) {
return lotteries[currentLottery].participants.length;
}
function getCurrentLottery() public constant returns(uint endBlock, uint startBlock, bytes32 blockHash, address winner, uint participants) {
LotteryPlay storage lottery = lotteries[currentLottery];
return (lottery.endBlock, lottery.startBlock, lottery.blockHash, lottery.winner, lottery.participants.length);
}
// Admin tools
function initialise() public whenPaused onlyOwner {
// Balance should be 0 in order to start a new lottery.
// otherwise you might end up **stealing** others money.
require(this.balance == 0);
currentLottery++;
lotteries[currentLottery].startBlock = block.number;
lotteries[currentLottery].blockHash = block.blockhash(lotteries[currentLottery].startBlock);
// Set the next waiting time to apprx. 1 week.
// This is not working since I couldn't find a good way to unit test this.
lotteries[currentLottery].endBlock = block.number + NEXT_LOTTERY_WAIT_TIME_IN_BLOCKS;
}
function setParticipantsNumber(uint128 newNumber) public onlyOwner {
maxParticipant = newNumber;
}
function setAffiliateRatio(uint16 newRatio) public onlyOwner {
require(newRatio < 101);
require(newRatio + donationRatio < 101);
affiliateRatio = newRatio;
}
function setAffiliateAddress(address _newAffiliate) public onlyOwner {
require(_newAffiliate != address(0));
affiliateAddress = _newAffiliate;
}
function setCharityAddress(address _newCharityAddress) public onlyOwner {
require(_newCharityAddress != address(0));
charityAddress = _newCharityAddress;
}
function setDonationRatio(uint16 newRatio) public onlyOwner {
require(newRatio < 101);
require(newRatio + affiliateRatio < 101);
donationRatio = newRatio;
}
function runLottery() public onlyOwner returns (uint256, address) {
require(charityAddress != address(0));
require(lotteries[currentLottery].participants.length >= 2);
// Uncomment the line below, *if* you can find a way to unit test
// this logic.
// require(lotteries[currentLottery].endBlock < block.number);
paused = true;
// send money to charity account.
uint256 charityAmount = (this.balance * donationRatio) / 100;
charityAddress.transfer(charityAmount);
totalDonation += charityAmount;
// send money to an affiliate address to cover the costs.
uint256 affiliateAmount = (this.balance * affiliateRatio) / 100;
affiliateAddress.transfer(affiliateAmount);
totalAffiliateAmount += affiliateAmount;
// random winner.
uint256 randomValue = random();
address winner = lotteries[currentLottery].participants[randomValue].addr;
// send the rest of the funds to the winner if anything is left.
uint256 winningPrice = this.balance;
if (winningPrice > 0) {
winner.transfer(winningPrice);
}
lotteries[currentLottery].winner = winner;
totalGivenAmount += winningPrice;
// initialise a new one.
initialise();
paused = false;
LotteryRunFinished(winner, charityAmount, affiliateAmount, winningPrice);
return (randomValue, winner);
}
// Helper functions
function random() internal view returns(uint256) {
// I know, I know. I should have use a proper off the chain random generator.
// Why not implement oraclize and send a pull request?
uint256 r1 = uint256(block.blockhash(block.number-1));
uint256 r2 = uint256(block.blockhash(lotteries[currentLottery].startBlock));
uint256 val;
assembly {
val := xor(r1, r2)
}
return val % lotteries[currentLottery].participants.length;
}
}