forked from magonicolas/Ethereum-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewAlea.sol
More file actions
105 lines (87 loc) · 3.06 KB
/
NewAlea.sol
File metadata and controls
105 lines (87 loc) · 3.06 KB
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
pragma solidity ^0.4.0;
contract Alea {
uint public refferalSentAmount = 0;
uint public potAmount = 0;
uint public ticketsSold = 0;
uint public winnerPercentaje = 80;
uint public companyPercentaje = 15;
uint public referralPercentaje = 5;
address public owner;
uint256 public cratedTime;
uint256 public timePerLottery = 80400;
uint public lastWinnerPotAmount;
address public lastWinner;
address[] public participants;
uint public ticketPrice = 0.01 ether;
address creator1 = 0x5baa8cf9c87ea0f0c8d1a1d4d4f9d6cfa1eac083;
address creator2 = 0x5baa8cf9c87ea0f0c8d1a1d4d4f9d6cfa1eac083;
event UserStatus(string _msg, address user, uint amount, uint256 time);
struct Participant {
address adr;
uint tickets;
address referral;
}
function Alea() public {
owner = msg.sender;
cratedTime = block.timestamp;
UserStatus('Loterry Created', msg.sender, 0, block.timestamp);
}
function buyTicket(uint amount) public payable {
if(msg.value >= ticketPrice * amount && block.timestamp < (cratedTime + timePerLottery)) {
bool isParticipant = false;
ticketsSold += amount;
participants.push(msg.address);
UserStatus('Ticket Bought', msg.sender, msg.value, block.timestamp);
potAmount = this.balance;
} else {
revert();
}
}
function buyTicketReferral(address referral, uint amount) public payable {
if(msg.value >= ticketPrice * amount && block.timestamp < (cratedTime + timePerLottery)) {
bool isParticipant = false;
ticketsSold += amount;
for(uint i = 0; i < participants.length; i++) {
if(participants[i].adr == msg.sender) {
participants[i].tickets += amount;
isParticipant = true;
}
}
if(isParticipant == false) {
participants.push(Participant({
adr: msg.sender,
tickets: amount,
referral: referral
}));
UserStatus('Ticket Bought', msg.sender, msg.value, block.timestamp);
potAmount = this.balance;
referral.transfer(msg.value * referralPercentaje / 100);
refferalSentAmount = refferalSentAmount + (msg.value * referralPercentaje / 100);
}
} else {
revert();
}
}
function endLottery() public {
if(block.timestamp > (cratedTime + timePerLottery)) {
uint random = uint(block.blockhash(block.number-1))%(ticketsSold + 1) + 0;
for(uint i = 0; i < participants.length; i++) {
for(uint j = 0; j < participants[i].tickets + 1; j++) {
UserStatus('Checking', participants[i].adr, random, block.timestamp);
if(random == 0) {
lastWinner = participants[i].adr;
participants[i].adr.transfer(potAmount * winnerPercentaje / 100);
break;
}
random = random - 1;
}
}
lastWinnerPotAmount = (potAmount * winnerPercentaje / 100);
creator1.transfer(this.balance / 2);
creator2.transfer(this.balance);
UserStatus('Loterry Ended, Winner:', lastWinner, lastWinnerPotAmount, block.timestamp);
} else {
UserStatus('Loterry NOT Ended:', lastWinner, lastWinnerPotAmount, block.timestamp);
}
}
}