/
SmartlandsCrowdsale.sol
204 lines (180 loc) · 6.5 KB
/
SmartlandsCrowdsale.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
pragma solidity ^0.4.16;
import "./Ownable.sol";
import "./SmartlandsToken.sol";
contract SmartlandsCrowdsale is Ownable {
SmartlandsToken public tokenReward;
mapping(address => uint256) public balanceOf;
uint public fundingGoal = 2 * 1 ether;
uint public price = 1 szabo; // 0.001 ether / 1000 (3 decimalUnits of token)
bool public isFundsLocked = true;
event GoalReached(address _beneficiary, uint _amountRaised);
event FundTransfer(address _backer, uint _amount, bool _isContribution);
// StateMachine <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
enum Stages {
NotStarted,
FirstIteration,
SecondIteration,
Finished,
FinishedAheadOfTime
}
uint public startTime = 0;
Stages public stage = Stages.NotStarted;
modifier atStage(Stages _stage) {
require(stage == _stage);
_;
}
modifier atStages(Stages _first, Stages _second) {
require(stage == _first || stage == _second);
_;
}
modifier transitionNext() {
_;
stage = Stages(uint(stage) + 1);
}
modifier transition(Stages _stage) {
_;
stage = _stage;
}
modifier timedTransitions () {
// if (stage == Stages.FirstIteration && now >= startTime + 30 days)
// if (stage == Stages.SecondIteration && now >= startTime + 45 days)
require(stage > Stages.NotStarted);
if (stage < Stages.FinishedAheadOfTime) {
uint diff = now - startTime;
if (diff >= 5 minutes && diff < 10 minutes && stage != Stages.SecondIteration) {
stage = Stages.SecondIteration;
} else if (diff >= 10 minutes && stage != Stages.Finished) {
stage = Stages.Finished;
}
}
_;
}
// StateMachine >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**
* Constrctor function
*
* Setup the owner
*/
function SmartlandsCrowdsale (address _addressOfTokenUsedAsReward)
{
tokenReward = SmartlandsToken(_addressOfTokenUsedAsReward);
}
/**
* Fallback function
*
* The function without name is the default function that is called whenever anyone sends funds to a contract
*/
function () payable
timedTransitions
atStages(Stages.FirstIteration, Stages.SecondIteration)
{
uint amount = msg.value;
// require(amount >= 5 ether);
balanceOf[msg.sender] += amount;
tokenReward.transfer(msg.sender, calculateBonus(amount) / price);
FundTransfer(msg.sender, amount, true);
}
function calculateFirstBonus (uint _amount) internal constant returns (uint amount) {
if (_amount >= 5 ether && _amount < 10 ether) {
return _amount * 115 / 100;
} else if (_amount >= 10 ether && _amount < 20 ether) {
return _amount * 120 / 100;
} else if (_amount >= 20 ether && _amount < 50 ether) {
return _amount * 125 / 100;
} else if (_amount >= 50 ether && _amount < 100 ether) {
return _amount * 130 / 100;
} else if (_amount >= 100 ether && _amount < 300 ether) {
return _amount * 140 / 100;
} else if (_amount >= 300 ether && _amount < 1000 ether) {
return _amount * 150 / 100;
} else if (_amount >= 1000 ether && _amount < 2000 ether) {
return _amount * 160 / 100;
} else if (_amount >= 2000 ether && _amount < 3000 ether) {
return _amount * 175 / 100;
} else if (_amount >= 3000 ether) {
return _amount * 2;
} else {
return _amount;
}
}
function calculateSecondBonus (uint _amount) internal constant returns (uint amount) {
require(startTime > 0);
uint diff = now - startTime - 30;
if (diff < 1 days) {
return _amount * 110 / 100;
} else if (diff >= 1 days && diff < 5 days) {
return _amount * 106 / 100;
} else if (diff >= 5 days && diff < 10 days) {
return _amount * 103 / 100;
} else if (diff >= 10 days && diff < 15 days) {
return _amount;
} else {
return _amount;
}
}
function calculateBonus (uint _amount) constant returns (uint amount) {
if (stage == Stages.FirstIteration) {
return calculateFirstBonus(_amount);
} else if(stage == Stages.SecondIteration) {
return calculateSecondBonus(_amount);
} else {
return _amount;
}
}
function startCrowdsale ()
onlyOwner
atStage(Stages.NotStarted)
transitionNext
{
startTime = now;
}
function unlockFunds ()
onlyOwner
{
isFundsLocked = false;
}
function finishAheadOfTime () // move to payable?
onlyOwner
transition(Stages.FinishedAheadOfTime)
{
require(isGoalReached());
GoalReached(owner, this.balance);
}
/**
* Check if goal was reached
*
* Checks if the goal has been reached
*/
function isGoalReached() constant returns (bool isReached) {
return this.balance >= fundingGoal;
}
function timeToEnd() constant returns (uint time) {
require(stage > Stages.NotStarted && stage < Stages.Finished);
return (startTime + 10 minutes - now) / 1 minutes;
}
/**
* Withdraw the funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
* the amount they contributed.
*/
function safeWithdrawal()
timedTransitions
atStages(Stages.Finished, Stages.FinishedAheadOfTime)
{
if (isGoalReached() && owner == msg.sender) {
uint amountRaised = this.balance;
if (owner.send(amountRaised)) {
FundTransfer(owner, amountRaised, false);
}
}
if (!isGoalReached() || !isFundsLocked) {
uint amount = balanceOf[msg.sender];
if (amount > 0 && msg.sender.send(amount)) {
balanceOf[msg.sender] = 0;
FundTransfer(msg.sender, amount, false);
}
}
}
}