-
Notifications
You must be signed in to change notification settings - Fork 2
/
lukeContract211.sol
170 lines (133 loc) · 4.06 KB
/
lukeContract211.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
pragma solidity ^0.5.1;
contract Lottery
{
struct Ticket
{
address associatedUser;
uint price;
uint id;
}
struct Charity
{
string name;
string description;
address charityAddress;
}
uint nextTicketIdToAssign;
Ticket[] tickets;
mapping(address => bool) users;
address payable winner;
address[] usersAddresses;
Charity[] charities;
uint totalBalance;
// those three variables are relative to the winner.
uint notificationTime;
uint timeToAnswer;
uint winnerDeadline;
// those three variables are relative to the lottery.
uint lotteryDuration;
uint lotteryOpeningTime;
uint lotteryClosingTime;
modifier lotteryOpen() {
require(now >= lotteryOpeningTime && now < lotteryClosingTime );
_;
}
modifier sufficientAmount(uint n)
{
require(msg.value >n);
_;
}
modifier winnerAnswer()
{
require(now >= notificationTime && now < winnerDeadline);
_;
}
// times to signal
event lotteryStart()
event betPlaced(address better, uint betValue);
event lotteryEnd(address winner, uint payout);
constructor() public
{
lotteryDuration = 3 weeks;
timeToAnswer = 48 hours;
winnerDeadline = notificationTime + timeToAnswer;
lotteryClosingTime = lotteryOpeningTime + lotteryDuration;
nextTicketIdToAssign = 0;
}
function getBalance() public view returns(uint)
{
return address(this).balance; // or totalBalance
}
function getNumberOfUsers() public view returns (uint)
{
return usersAddresses.length;
}
function bet(uint charityAddress) lotteryOpen sufficientAmount(pricePerTicket) public payable //allows for donation to any address, good and bad thing. not sure if want lotteryOpen
{
uint extraEther = msg.value % pricePerTicket;
if(msg.value / pricePerTicket < 1){
//return ether to user, fallback function?
}
if(extraEther != 0) {
msg.sender.send(extraEther)
}
if(getBalance() == 0) {
emit lotteryStart()
}
uint numberOfTickets = msg.value / pricePerTicket - extraEther;
if(!userInLottery(msg.sender)) usersAddresses.push(msg.sender);
for(uint i=1; i<=numberOfTickets; i++)
{
Ticket memory t = Ticket(msg.sender,pricePerTicket,nextTicketIdToAssign++);
tickets.push(t);
}
uint charityShare = msg.value * .9;
uint lotteryShare = msg.value * .1;
charityAddress.send(lotteryShare)
totalBalance+=lotteryShare;
}
emit betPlaced(msg.sender,msg.value) //front end can show lottery and charity cut
}
}
function addCharity(string memory charityName, string memory charityDescription , address charityAddress) public {
charities.push(Charity(charityName,charityDescription,charityAddress));
}
function removeCharity(address charityToDelete) public {
uint i = indexOfElement(charityToDelete);
charities[i] = charities[charities.length-1];
delete charities[charities.length-1];
charities.length--;
}
function indexOfElement(address ca) public returns (uint)
{
for(uint index=0; index<charities.length; index++)
{
Charity memory c = charities[index];
if(ca == c.charityAddress)
{
return index;
}
}
return 0; // need to think about error value.
}
function userInLottery(address u) public view returns (bool)
{
return users[u];
}
// function to distribute money to charity => transferMoney with appropriate address and amount.
function transferMoney(address payable a, uint amount) payable public
{
a.transfer(amount);
}
function dealWithWinner() public
{
}
function giveMoneyToWinner(uint amount) public
{
transferMoney(winner,amount);
}
function getDeadline() view public returns (uint)
{
return lotteryDuration;
}
}