-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenLongevity.sol
412 lines (349 loc) · 14.3 KB
/
OpenLongevity.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
This file is part of the Open Longevity Contract.
The Open Longevity Contract is free software: you can redistribute it and/or
modify it under the terms of the GNU lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Open Longevity Contract is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU lesser General Public License for more details.
You should have received a copy of the GNU lesser General Public License
along with the Open Longevity Contract. If not, see <http://www.gnu.org/licenses/>.
@author Ilya Svirin <[email protected]>
*/
pragma solidity ^0.4.0;
contract owned {
address public owner;
address public newOwner;
function owned() payable {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender);
_;
}
function changeOwner(address _owner) onlyOwner public {
require(_owner != 0);
newOwner = _owner;
}
function confirmOwner() public {
require(newOwner == msg.sender);
owner = newOwner;
delete newOwner;
}
}
contract Crowdsale is owned {
uint256 public totalSupply;
mapping (address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
uint public etherPrice;
address public crowdsaleOwner;
uint public totalLimitUSD;
uint public minimalSuccessUSD;
uint public collectedUSD;
enum State { Disabled, PreICO, CompletePreICO, Crowdsale, Enabled, Migration }
event NewState(State state);
State public state = State.Disabled;
uint public crowdsaleStartTime;
uint public crowdsaleFinishTime;
modifier enabledState {
require(state == State.Enabled);
_;
}
modifier enabledOrMigrationState {
require(state == State.Enabled || state == State.Migration);
_;
}
struct Investor {
uint256 amountTokens;
uint amountWei;
}
mapping (address => Investor) public investors;
mapping (uint => address) public investorsIter;
uint public numberOfInvestors;
function () payable {
require(state == State.PreICO || state == State.Crowdsale);
uint256 tokensPer10USD = 100;
uint valueWei = msg.value;
uint valueUSD = valueWei * etherPrice / 1000000000000000000;
if (state == State.PreICO) {
if (valueUSD >= 100000) {
tokensPer10USD = 200;
} else {
tokensPer10USD = 175;
}
} else if (state == State.Crowdsale) {
if (now < crowdsaleStartTime + 1 days || valueUSD >= 100000) {
tokensPer10USD = 150;
} else if (now < crowdsaleStartTime + 1 weeks) {
tokensPer10USD = 125;
}
}
if (collectedUSD + valueUSD > totalLimitUSD) { // don't need so much ether
valueUSD = totalLimitUSD - collectedUSD;
valueWei = valueUSD * 1000000000000000000 / etherPrice;
msg.sender.transfer(msg.value - valueWei);
collectedUSD = totalLimitUSD; // to be sure!
} else {
collectedUSD += valueUSD;
}
uint256 tokens = tokensPer10USD * valueUSD / 10;
require(balanceOf[msg.sender] + tokens > balanceOf[msg.sender]); // overflow
require(tokens > 0);
Investor storage inv = investors[msg.sender];
if (inv.amountWei == 0) { // new investor
investorsIter[numberOfInvestors++] = msg.sender;
}
inv.amountTokens += tokens;
inv.amountWei += valueWei;
balanceOf[msg.sender] += tokens;
totalSupply += tokens;
Transfer(this, msg.sender, tokens);
}
function startTokensSale(address _crowdsaleOwner, uint _etherPrice) public onlyOwner {
require(state == State.Disabled || state == State.CompletePreICO);
crowdsaleStartTime = now;
crowdsaleOwner = _crowdsaleOwner;
etherPrice = _etherPrice;
delete numberOfInvestors;
delete collectedUSD;
if (state == State.Disabled) {
crowdsaleFinishTime = now + 14 days;
state = State.PreICO;
totalLimitUSD = 1000000;
minimalSuccessUSD = 0;
} else {
crowdsaleFinishTime = now + 30 days;
state = State.Crowdsale;
totalLimitUSD = 99750000;
minimalSuccessUSD = 7000000;
}
NewState(state);
}
function timeToFinishTokensSale() public constant returns(uint t) {
require(state == State.PreICO || state == State.Crowdsale);
if (now > crowdsaleFinishTime) {
t = 0;
} else {
t = crowdsaleFinishTime - now;
}
}
function finishTokensSale(uint _investorsToProcess) public {
require(state == State.PreICO || state == State.Crowdsale);
require(now >= crowdsaleFinishTime || collectedUSD == totalLimitUSD);
if (collectedUSD < minimalSuccessUSD) {
// Investors can get their ether calling withdrawBack() function
while (_investorsToProcess > 0 && numberOfInvestors > 0) {
address addr = investorsIter[--numberOfInvestors];
Investor memory inv = investors[addr];
balanceOf[addr] -= inv.amountTokens;
totalSupply -= inv.amountTokens;
Transfer(addr, this, inv.amountTokens);
--_investorsToProcess;
delete investorsIter[numberOfInvestors];
}
if (numberOfInvestors > 0) {
return;
}
if (state == State.PreICO) {
state = State.Disabled;
} else {
state = State.CompletePreICO;
}
} else {
while (_investorsToProcess > 0 && numberOfInvestors > 0) {
--numberOfInvestors;
--_investorsToProcess;
delete investors[investorsIter[numberOfInvestors]];
delete investorsIter[numberOfInvestors];
}
if (numberOfInvestors > 0) {
return;
}
crowdsaleOwner.transfer(this.balance);
if (state == State.PreICO) {
state = State.CompletePreICO;
} else {
uint256 extraEmission = 2 * totalSupply / 5;
balanceOf[crowdsaleOwner] = extraEmission;
Transfer(this, crowdsaleOwner, extraEmission);
extraEmission = 3 * totalSupply / 5;
balanceOf[this] = extraEmission;
Transfer(this, this, extraEmission);
totalSupply *= 2;
state = State.Enabled;
}
}
NewState(state);
}
// This function must be called by token holder in case of crowdsale failed
function withdrawBack() public {
require(state == State.Disabled || state == State.CompletePreICO);
uint value = investors[msg.sender].amountWei;
if (value > 0) {
delete investors[msg.sender];
msg.sender.transfer(value);
}
}
}
contract Token is Crowdsale {
string public standard = 'Token 0.1';
string public name = 'OpenLongevity';
string public symbol = "YEAR";
uint8 public decimals = 0;
modifier onlyTokenHolders {
require(balanceOf[msg.sender] != 0);
_;
}
mapping (address => mapping (address => uint256)) public allowed;
event Approval(address indexed owner, address indexed spender, uint256 value);
function Token() payable Crowdsale() {}
function transfer(address _to, uint256 _value) public enabledState {
require(balanceOf[msg.sender] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]); // overflow
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
Transfer(msg.sender, _to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) public enabledState {
require(balanceOf[_from] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]); // overflow
require(allowed[_from][msg.sender] >= _value);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowed[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
}
function approve(address _spender, uint256 _value) public enabledState {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
}
function allowance(address _owner, address _spender) public constant enabledState
returns (uint256 remaining) {
return allowed[_owner][_spender];
}
}
contract MigrationAgent {
function migrateFrom(address _from, uint256 _value);
}
contract TokenMigration is Token {
address public migrationAgent;
uint256 public totalMigrated;
event Migrate(address indexed from, address indexed to, uint256 value);
function TokenMigration() payable Token() {}
// Migrate _value of tokens to the new token contract
function migrate(uint256 _value) external {
require(state == State.Migration);
require(migrationAgent != 0);
require(_value != 0);
require(_value <= balanceOf[msg.sender]);
balanceOf[msg.sender] -= _value;
totalSupply -= _value;
totalMigrated += _value;
MigrationAgent(migrationAgent).migrateFrom(msg.sender, _value);
Migrate(msg.sender, migrationAgent, _value);
}
function setMigrationAgent(address _agent) external onlyOwner {
require(migrationAgent == 0);
migrationAgent = _agent;
state = State.Migration;
}
}
contract OpenLongevity is TokenMigration {
enum Vote { NoVote, VoteYea, VoteNay }
uint public deploymentPriceYear = 1;
function OpenLongevity() payable TokenMigration() {}
event Deployed(address indexed projectOwner, uint proofReqFund, string urlInfo);
event Voted(address indexed projectOwner, address indexed voter, bool inSupport);
event VotingFinished(address indexed projectOwner, bool inSupport);
event VotingRejected(address indexed projectOwner);
struct Project {
uint yearReqFund;
string urlInfo;
uint votingDeadline;
uint numberOfVotes;
uint yea;
uint nay;
mapping (address => Vote) votes;
mapping (uint => address) votesIter;
}
mapping (address => Project) public projects;
function setDeploymentPriceYear(uint _price) public onlyOwner {
deploymentPriceYear = _price;
}
function deployProject(uint _yearReqFund, string _urlInfo) public
onlyTokenHolders enabledOrMigrationState {
require(_yearReqFund > 0 && _yearReqFund <= balanceOf[this]);
require(projects[msg.sender].yearReqFund == 0);
transfer(this, deploymentPriceYear);
projects[msg.sender].yearReqFund = _yearReqFund;
projects[msg.sender].urlInfo = _urlInfo;
projects[msg.sender].votingDeadline = now + 7 days;
Deployed(msg.sender, _yearReqFund, _urlInfo);
}
function projectInfo(address _projectOwner) enabledOrMigrationState constant public
returns(uint _yearReqFund, string _urlInfo, uint _timeToFinish) {
_yearReqFund = projects[_projectOwner].yearReqFund;
_urlInfo = projects[_projectOwner].urlInfo;
if (projects[_projectOwner].votingDeadline <= now) {
_timeToFinish = 0;
} else {
_timeToFinish = projects[_projectOwner].votingDeadline - now;
}
}
function vote(address _projectOwner, bool _inSupport) public
onlyTokenHolders enabledOrMigrationState returns (uint voteId) {
Project storage p = projects[_projectOwner];
require(p.yearReqFund > 0);
require(p.votes[msg.sender] == Vote.NoVote);
require(p.votingDeadline > now);
voteId = p.numberOfVotes++;
p.votesIter[voteId] = msg.sender;
if (_inSupport) {
p.votes[msg.sender] = Vote.VoteYea;
} else {
p.votes[msg.sender] = Vote.VoteNay;
}
Voted(_projectOwner, msg.sender, _inSupport);
return voteId;
}
function rejectProject(address _projectOwner) public onlyOwner {
require(projects[_projectOwner].yearReqFund > 0);
delete projects[_projectOwner];
VotingRejected(_projectOwner);
}
function finishVoting(address _projectOwner, uint _votesToProcess) public
enabledOrMigrationState returns (bool _inSupport) {
Project storage p = projects[_projectOwner];
require(p.yearReqFund > 0);
require(now >= p.votingDeadline && p.yearReqFund <= balanceOf[this]);
while (_votesToProcess > 0 && p.numberOfVotes > 0) {
address voter = p.votesIter[--p.numberOfVotes];
Vote v = p.votes[voter];
uint voteWeight = balanceOf[voter];
if (v == Vote.VoteYea) {
p.yea += voteWeight;
} else if (v == Vote.VoteNay) {
p.nay += voteWeight;
}
delete p.votesIter[p.numberOfVotes];
delete p.votes[voter];
--_votesToProcess;
}
if (p.numberOfVotes > 0) {
_inSupport = false;
return;
}
_inSupport = (p.yea > p.nay);
uint yearReqFund = p.yearReqFund;
delete projects[_projectOwner];
if (_inSupport) {
require(balanceOf[_projectOwner] + yearReqFund >= balanceOf[_projectOwner]); // overflow
balanceOf[this] -= yearReqFund;
balanceOf[_projectOwner] += yearReqFund;
Transfer(this, _projectOwner, yearReqFund);
}
VotingFinished(_projectOwner, _inSupport);
}
}