forked from crytic/not-so-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeRun.sol
173 lines (130 loc) · 5.8 KB
/
theRun.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
contract theRun {
uint private Balance = 0;
uint private Payout_id = 0;
uint private Last_Payout = 0;
uint private WinningPot = 0;
uint private Min_multiplier = 1100; //110%
//Fees are necessary and set very low, to maintain the website. The fees will decrease each time they are collected.
//Fees are just here to maintain the website at beginning, and will progressively go to 0% :)
uint private fees = 0;
uint private feeFrac = 20; //Fraction for fees in per"thousand", not percent, so 20 is 2%
uint private PotFrac = 30; //For the WinningPot ,30=> 3% are collected. This is fixed.
address private admin;
function theRun() {
admin = msg.sender;
}
modifier onlyowner {if (msg.sender == admin) _; }
struct Player {
address addr;
uint payout;
bool paid;
}
Player[] private players;
//--Fallback function
function() {
init();
}
//--initiated function
function init() private {
uint deposit=msg.value;
if (msg.value < 500 finney) { //only participation with >1 ether accepted
msg.sender.send(msg.value);
return;
}
if (msg.value > 20 ether) { //only participation with <20 ether accepted
msg.sender.send(msg.value- (20 ether));
deposit=20 ether;
}
Participate(deposit);
}
//------- Core of the game----------
function Participate(uint deposit) private {
//calculate the multiplier to apply to the future payout
uint total_multiplier=Min_multiplier; //initiate total_multiplier
if(Balance < 1 ether && players.length>1){
total_multiplier+=100; // + 10 %
}
if( (players.length % 10)==0 && players.length>1 ){ //Every 10th participant gets a 10% bonus, play smart !
total_multiplier+=100; // + 10 %
}
//add new player in the queue !
players.push(Player(msg.sender, (deposit * total_multiplier) / 1000, false));
//--- UPDATING CONTRACT STATS ----
WinningPot += (deposit * PotFrac) / 1000; // take some 3% to add for the winning pot !
fees += (deposit * feeFrac) / 1000; // collect maintenance fees 2%
Balance += (deposit * (1000 - ( feeFrac + PotFrac ))) / 1000; // update balance
// Winning the Pot :) Condition : paying at least 1 people with deposit > 2 ether and having luck !
if( ( deposit > 1 ether ) && (deposit > players[Payout_id].payout) ){
uint roll = random(100); //take a random number between 1 & 100
if( roll % 10 == 0 ){ //if lucky : Chances : 1 out of 10 !
msg.sender.send(WinningPot); // Bravo !
WinningPot=0;
}
}
//Classic payout for the participants
while ( Balance > players[Payout_id].payout ) {
Last_Payout = players[Payout_id].payout;
players[Payout_id].addr.send(Last_Payout); //pay the man, please !
Balance -= players[Payout_id].payout; //update the balance
players[Payout_id].paid=true;
Payout_id += 1;
}
}
uint256 constant private salt = block.timestamp;
function random(uint Max) constant private returns (uint256 result){
//get the best seed for randomness
uint256 x = salt * 100 / Max;
uint256 y = salt * block.number / (salt % 5) ;
uint256 seed = block.number/3 + (salt % 300) + Last_Payout +y;
uint256 h = uint256(block.blockhash(seed));
return uint256((h / x)) % Max + 1; //random number between 1 and Max
}
//---Contract management functions
function ChangeOwnership(address _owner) onlyowner {
admin = _owner;
}
function WatchBalance() constant returns(uint TotalBalance) {
TotalBalance = Balance / 1 wei;
}
function WatchBalanceInEther() constant returns(uint TotalBalanceInEther) {
TotalBalanceInEther = Balance / 1 ether;
}
//Fee functions for creator
function CollectAllFees() onlyowner {
if (fees == 0) throw;
admin.send(fees);
feeFrac-=1;
fees = 0;
}
function GetAndReduceFeesByFraction(uint p) onlyowner {
if (fees == 0) feeFrac-=1; //Reduce fees.
admin.send(fees / 1000 * p);//send a percent of fees
fees -= fees / 1000 * p;
}
//---Contract informations
function NextPayout() constant returns(uint NextPayout) {
NextPayout = players[Payout_id].payout / 1 wei;
}
function WatchFees() constant returns(uint CollectedFees) {
CollectedFees = fees / 1 wei;
}
function WatchWinningPot() constant returns(uint WinningPot) {
WinningPot = WinningPot / 1 wei;
}
function WatchLastPayout() constant returns(uint payout) {
payout = Last_Payout;
}
function Total_of_Players() constant returns(uint NumberOfPlayers) {
NumberOfPlayers = players.length;
}
function PlayerInfo(uint id) constant returns(address Address, uint Payout, bool UserPaid) {
if (id <= players.length) {
Address = players[id].addr;
Payout = players[id].payout / 1 wei;
UserPaid=players[id].paid;
}
}
function PayoutQueueSize() constant returns(uint QueueSize) {
QueueSize = players.length - Payout_id;
}
}