/
Immla.sol
188 lines (153 loc) · 5.25 KB
/
Immla.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
pragma solidity ^0.4.4;
// ERC20 token interface is implemented only partially
// (no SafeMath is used because contract code is very simple)
//
// Some functions left undefined:
// - transfer, transferFrom,
// - approve, allowance.
contract PresaleToken
{
/// Fields:
string public constant name = "IMMLA Presale Token";
string public constant symbol = "IML";
uint public constant decimals = 18;
uint public constant PRICE = 5200; // per 1 Ether
// price
// Cap is 2747 ETH
// 1 eth = 5200; presale IMMLA tokens
// 1 IML = 0,000192 ETH
// ETH price ~220$ - 10.07.2017
uint public constant TOKEN_SUPPLY_LIMIT = PRICE * 2747 * (1 ether / 1 wei);
enum State{
Init,
Running,
Paused,
Migrating,
Migrated
}
State public currentState = State.Init;
uint public totalSupply = 0; // amount of tokens already sold
// Gathered funds can be withdrawn only to escrow's address.
address public escrow = 0;
// Token manager has exclusive priveleges to call administrative
// functions on this contract.
address public tokenManager = 0;
// Crowdsale manager has exclusive priveleges to burn presale tokens.
address public crowdsaleManager = 0;
mapping (address => uint256) private balance;
/// Modifiers:
modifier onlyTokenManager() { if(msg.sender != tokenManager) throw; _; }
modifier onlyCrowdsaleManager() { if(msg.sender != crowdsaleManager) throw; _; }
modifier onlyInState(State state){ if(state != currentState) throw; _; }
/// Events:
event LogBuy(address indexed owner, uint value);
event LogBurn(address indexed owner, uint value);
event LogStateSwitch(State newState);
/// Functions:
/// @dev Constructor
/// @param _tokenManager Token manager address.
function PresaleToken(address _tokenManager, address _escrow)
{
if(_tokenManager==0) throw;
if(_escrow==0) throw;
tokenManager = _tokenManager;
escrow = _escrow;
}
function buyTokens(address _buyer) public payable onlyInState(State.Running)
{
if(msg.value == 0) throw;
uint newTokens = msg.value * PRICE;
if (totalSupply + newTokens > TOKEN_SUPPLY_LIMIT) throw;
balance[_buyer] += newTokens;
totalSupply += newTokens;
LogBuy(_buyer, newTokens);
}
/// @dev Returns number of tokens owned by given address.
/// @param _owner Address of token owner.
function burnTokens(address _owner) public onlyCrowdsaleManager onlyInState(State.Migrating)
{
uint tokens = balance[_owner];
if(tokens == 0) throw;
balance[_owner] = 0;
totalSupply -= tokens;
LogBurn(_owner, tokens);
// Automatically switch phase when migration is done.
if(totalSupply == 0)
{
currentState = State.Migrated;
LogStateSwitch(State.Migrated);
}
}
/// @dev Returns number of tokens owned by given address.
/// @param _owner Address of token owner.
function balanceOf(address _owner) constant returns (uint256)
{
return balance[_owner];
}
function setPresaleState(State _nextState) public onlyTokenManager
{
// Init -> Running
// Running -> Paused
// Running -> Migrating
// Paused -> Running
// Paused -> Migrating
// Migrating -> Migrated
bool canSwitchState
= (currentState == State.Init && _nextState == State.Running)
|| (currentState == State.Running && _nextState == State.Paused)
// switch to migration phase only if crowdsale manager is set
|| ((currentState == State.Running || currentState == State.Paused)
&& _nextState == State.Migrating
&& crowdsaleManager != 0x0)
|| (currentState == State.Paused && _nextState == State.Running)
// switch to migrated only if everyting is migrated
|| (currentState == State.Migrating && _nextState == State.Migrated
&& totalSupply == 0);
if(!canSwitchState) throw;
currentState = _nextState;
LogStateSwitch(_nextState);
}
function withdrawEther() public onlyTokenManager
{
if(this.balance > 0)
{
if(!escrow.send(this.balance)) throw;
}
}
/// Setters/getters
function setTokenManager(address _mgr) public onlyTokenManager
{
tokenManager = _mgr;
}
function setCrowdsaleManager(address _mgr) public onlyTokenManager
{
// You can't change crowdsale contract when migration is in progress.
if(currentState == State.Migrating) throw;
crowdsaleManager = _mgr;
}
function getTokenManager()constant returns(address)
{
return tokenManager;
}
function getCrowdsaleManager()constant returns(address)
{
return crowdsaleManager;
}
function getCurrentState()constant returns(State)
{
return currentState;
}
function getPrice()constant returns(uint)
{
return PRICE;
}
function getTotalSupply()constant returns(uint)
{
return totalSupply;
}
// Default fallback function
function() payable
{
buyTokens(msg.sender);
}
}