-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCryptoTicket.sol
More file actions
77 lines (63 loc) · 2.12 KB
/
CryptoTicket.sol
File metadata and controls
77 lines (63 loc) · 2.12 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
pragma solidity ^0.4.0;
contract CryptoTicket {
uint public winnerPercentaje = 60;
uint public companyPercentaje = 15;
uint public donationsPercentaje = 25;
address public donationsAddress = 0x0515fA3898931553c36FaCaFc412ac1DA55aC008;
address public owner;
uint256 public lastLotteryTime;
uint256 public timePerLottery = 120;
uint public potAmount = 0;
uint public lastWinnerPotAmount;
address public lastWinner;
Participant[] public participants;
uint public ticketPrice = 1 ether;
event UserStatus(string _msg, address user, uint amount, uint256 time);
struct Participant {
address adr;
uint tickets;
}
function CryptoTicket() {
owner = msg.sender;
lastLotteryTime = block.timestamp;
UserStatus('Loterry Created', msg.sender, 0, block.timestamp);
}
function buyTicket() payable {
bool isParticipant = false;
if(msg.value >= ticketPrice) {
UserStatus('Ticket Bought', msg.sender, msg.value, block.timestamp);
for(uint i = 0; i < participants.length; i++) {
if(participants[i].adr == msg.sender) {
participants[i].tickets += 1;
isParticipant = true;
}
}
if(isParticipant == false) {
participants.push(Participant({
adr: msg.sender,
tickets: 1
}));
potAmount = this.balance;
}
} else {
revert();
}
}
function endLottery() {
if(block.timestamp > (lastLotteryTime + timePerLottery)) {
uint random = uint(block.blockhash(block.number-1))%(participants.length + 1) + 0;
potAmount = this.balance;
lastWinnerPotAmount = (potAmount * winnerPercentaje / 100);
participants[random].adr.transfer(potAmount * winnerPercentaje / 100);
owner.transfer(potAmount * companyPercentaje / 100);
donationsAddress.transfer(potAmount * donationsPercentaje / 100);
lastLotteryTime = block.timestamp;
potAmount = 0;
lastWinner = participants[random].adr;
for (uint i = 0; i<participants.length; i++){
delete participants[i];
}
UserStatus('Loterry Ended, Winner:', lastWinner, lastWinnerPotAmount, block.timestamp);
}
}
}