-
Notifications
You must be signed in to change notification settings - Fork 6
/
IOSTConversionReceiver.sol
381 lines (334 loc) · 12.3 KB
/
IOSTConversionReceiver.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
pragma solidity 0.4.24;
import "../lib/ownership/Claimable.sol";
import "../lib/lifecycle/Pausable.sol";
import "../lib/math/SafeMath.sol";
import "../lib/asm/ERC20AsmFn.sol";
import "../tokenization/interfaces/ERC20.sol";
contract IOSTConversionReceiver is Claimable, Pausable {
using SafeMath for uint256;
using ERC20AsmFn for ERC20;
ERC20 public token;
ERC20 public discountToken;
uint256 public discountThreshold;
uint256 public conversionFeeBasisPoints;
uint256 public unlockFeeBasisPoints;
uint256 public discountedConversionFeeBasisPoints;
uint256 public discountedUnlockFeeBasisPoints;
uint256 public minimumConversionAmount;
address public feeCollectionWallet;
address public coldStorageWallet;
Conversion[] public conversions;
ConversionUnlock[] public conversionUnlocks;
uint256 public totalLockedTokens;
uint256 public fees;
struct Conversion {
address ethAddress;
string iostAccount;
uint256 amount;
uint256 fee;
uint256 netAmount;
bool discounted;
States state;
}
struct ConversionUnlock {
address ethAddress;
string iostAccount;
string iostMirrorTransactionHash;
uint256 amount;
uint256 fee;
uint256 netAmount;
bool discounted;
States state;
}
enum States {
INVALID,
OPEN,
ACCEPTED,
REJECTED
}
event ConversionRequested(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 requestTimestamp
);
event ConversionRejected(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 rejectTimestamp
);
event ConversionAccepted(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 acceptTimestamp
);
event ConversionUnlockRequested(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
string iostMirrorTransactionHash,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 requestTimestamp
);
event ConversionUnlockAccepted(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
string iostMirrorTransactionHash,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 acceptTimestamp
);
event ConversionUnlockRejected(
uint256 indexID,
address indexed ethAddress,
string iostAccount,
string iostMirrorTransactionHash,
uint256 amount,
uint256 fee,
uint256 netAmount,
bool discounted,
uint256 rejectTimestamp
);
event CollectedFees(address feeCollectionWallet, uint256 amount, uint256 collectionTimestamp);
event SentTokensToColdStorage(address coldStorageWallet, uint256 amount, uint256 storageTimestamp);
modifier onlyOpenConversions(uint256 _index) {
require(conversions[_index].state == States.OPEN, "Conversion should be open");
_;
}
modifier onlyOpenConversionUnlocks(uint256 _index) {
require(conversionUnlocks[_index].state == States.OPEN, "ConversionUnlock should be open");
_;
}
constructor(
ERC20 _token,
ERC20 _discountToken,
uint256 _discountThreshold,
uint256 _conversionFeeBasisPoints,
uint256 _unlockFeeBasisPoints,
uint256 _discountedConversionFeeBasisPoints,
uint256 _discountedUnlockFeeBasisPoints,
uint256 _minimumConversionAmount,
address _feeCollectionWallet,
address _coldStorageWallet
) public {
token = _token;
discountToken = _discountToken;
discountThreshold = _discountThreshold;
conversionFeeBasisPoints = _conversionFeeBasisPoints;
unlockFeeBasisPoints = _unlockFeeBasisPoints;
discountedConversionFeeBasisPoints = _discountedConversionFeeBasisPoints;
discountedUnlockFeeBasisPoints = _discountedUnlockFeeBasisPoints;
minimumConversionAmount = _minimumConversionAmount;
feeCollectionWallet = _feeCollectionWallet;
coldStorageWallet = _coldStorageWallet;
}
// Owner functions, only when contract is paused
function setDiscountToken(ERC20 _discountToken) public onlyOwner whenPaused {
discountToken = _discountToken;
}
function setDiscountThreshold(uint256 _threshold) public onlyOwner whenPaused {
discountThreshold = _threshold;
}
function setConversionFeeBasisPoints(uint256 _bp) public onlyOwner whenPaused {
conversionFeeBasisPoints = _bp;
}
function setUnlockFeeBasisPoints(uint256 _bp) public onlyOwner whenPaused {
unlockFeeBasisPoints = _bp;
}
function setDiscountedConversionFeeBasisPoints(uint256 _bp) public onlyOwner whenPaused {
require(_bp <= conversionFeeBasisPoints, "Discount cannot be higher than non-discounted rate");
discountedConversionFeeBasisPoints = _bp;
}
function setDiscountedUnlockFeeBasisPoints(uint256 _bp) public onlyOwner whenPaused {
require(_bp <= unlockFeeBasisPoints, "Discount cannot be higher than non-discounted rate");
discountedUnlockFeeBasisPoints = _bp;
}
function setMinimumConversionAmount(uint256 _amount) public onlyOwner whenPaused {
minimumConversionAmount = _amount;
}
function setFeeCollectionWallet(address _wallet) public onlyOwner whenPaused {
feeCollectionWallet = _wallet;
}
function setColdStorageWallet(address _wallet) public onlyOwner whenPaused {
coldStorageWallet = _wallet;
}
function requestConversion(
uint256 _amount,
string _iostAccount
)
public
whenNotPaused
{
require(_amount >= minimumConversionAmount, "Should be above minimum conversion amount");
require(_amount <= token.allowance(msg.sender, address(this)), "Allowance should be set");
uint256 index = conversions.length;
uint256 fee = 0;
bool discounted = (discountToken.balanceOf(msg.sender) >= discountThreshold);
// Check whether discounted fee or normal fee.
if (discounted) {
fee = (_amount.mul(discountedConversionFeeBasisPoints)).div(10000);
} else {
fee = (_amount.mul(conversionFeeBasisPoints)).div(10000);
}
uint256 netAmount = _amount.sub(fee);
conversions.push(
Conversion(
msg.sender,
_iostAccount,
_amount,
fee,
netAmount,
discounted,
States.OPEN
)
);
// Transfer total amount to conversion contract.
require(token.asmTransferFrom(msg.sender, address(this), _amount), "Token transfer failed");
emit ConversionRequested(index, msg.sender, _iostAccount, _amount, fee, netAmount, discounted, block.timestamp);
}
function rejectConversion(uint256 _index) public onlyOwner whenNotPaused onlyOpenConversions(_index) {
Conversion storage conversion = conversions[_index];
require(token.asmTransfer(conversion.ethAddress, conversion.amount), "Token transfer failed");
conversion.state = States.REJECTED;
emit ConversionRejected(
_index,
conversion.ethAddress,
conversion.iostAccount,
conversion.amount,
conversion.fee,
conversion.netAmount,
conversion.discounted,
block.timestamp
);
}
function acceptConversion(uint256 _index) public onlyOwner whenNotPaused onlyOpenConversions(_index) {
Conversion storage conversion = conversions[_index];
conversion.state = States.ACCEPTED;
// Add net amount to locked tokens.
totalLockedTokens = totalLockedTokens.add(conversion.netAmount);
// Add fee to total fees collected.
fees = fees.add(conversion.fee);
emit ConversionAccepted(
_index,
conversion.ethAddress,
conversion.iostAccount,
conversion.amount,
conversion.fee,
conversion.netAmount,
conversion.discounted,
block.timestamp
);
}
function requestConversionUnlock(
uint256 _amount,
string _iostAccount,
string _iostMirrorTransactionHash
)
public
whenNotPaused
{
require(_amount >= minimumConversionAmount, "Should be above minimum conversion amount");
require(_amount <= totalLockedTokens, "Not enough tokens to convert");
uint256 index = conversionUnlocks.length;
uint256 fee = 0;
bool discounted = (discountToken.balanceOf(msg.sender) >= discountThreshold);
// Check whether discounted fee or normal fee.
if (discounted) {
fee = (_amount.mul(discountedUnlockFeeBasisPoints)).div(10000);
} else {
fee = (_amount.mul(unlockFeeBasisPoints)).div(10000);
}
uint256 netAmount = _amount.sub(fee);
conversionUnlocks.push(
ConversionUnlock(
msg.sender,
_iostAccount,
_iostMirrorTransactionHash,
_amount,
fee,
netAmount,
discounted,
States.OPEN
)
);
emit ConversionUnlockRequested(
index,
msg.sender,
_iostAccount,
_iostMirrorTransactionHash,
_amount,
fee,
netAmount,
discounted,
block.timestamp
);
}
function rejectConversionUnlock(uint256 _index) public onlyOwner whenNotPaused onlyOpenConversionUnlocks(_index) {
ConversionUnlock storage conversionUnlock = conversionUnlocks[_index];
conversionUnlock.state = States.REJECTED;
emit ConversionUnlockRejected(
_index,
conversionUnlock.ethAddress,
conversionUnlock.iostAccount,
conversionUnlock.iostMirrorTransactionHash,
conversionUnlock.amount,
conversionUnlock.fee,
conversionUnlock.netAmount,
conversionUnlock.discounted,
block.timestamp
);
}
function acceptConversionUnlock(uint256 _index) public onlyOwner whenNotPaused onlyOpenConversionUnlocks(_index) {
ConversionUnlock storage conversionUnlock = conversionUnlocks[_index];
require(conversionUnlock.amount <= totalLockedTokens, "Not enough tokens to convert");
conversionUnlock.state = States.ACCEPTED;
// Remove total amount from locked tokens.
totalLockedTokens = totalLockedTokens.sub(conversionUnlock.amount);
// Add fee to total fees collected.
fees = fees.add(conversionUnlock.fee);
// Transfer (amount - fees) to destination address.
require(token.asmTransfer(conversionUnlock.ethAddress, conversionUnlock.netAmount), "Token transfer failed");
emit ConversionUnlockAccepted(
_index,
conversionUnlock.ethAddress,
conversionUnlock.iostAccount,
conversionUnlock.iostMirrorTransactionHash,
conversionUnlock.amount,
conversionUnlock.fee,
conversionUnlock.netAmount,
conversionUnlock.discounted,
block.timestamp
);
}
function collectFees() public onlyOwner {
uint256 amount = fees;
// Clear fees first *important*;
fees = 0;
require(token.asmTransfer(feeCollectionWallet, amount), "Token transfer failed");
emit CollectedFees(feeCollectionWallet, amount, block.timestamp);
}
function sendTokensToColdStorage(uint256 _amount) public onlyOwner whenPaused {
require(token.asmTransfer(coldStorageWallet, _amount), "Token transfer failed");
emit SentTokensToColdStorage(coldStorageWallet, _amount, block.timestamp);
}
}