-
Notifications
You must be signed in to change notification settings - Fork 1
/
DepositVault.sol
441 lines (375 loc) · 12.4 KB
/
DepositVault.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import {IERC20Upgradeable as IERC20} from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import {IERC20MetadataUpgradeable as IERC20Metadata} from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";
import {Counters} from "@openzeppelin/contracts/utils/Counters.sol";
import "./interfaces/IDepositVault.sol";
import "./interfaces/IMTbill.sol";
import "./interfaces/IDataFeed.sol";
import "./abstract/ManageableVault.sol";
/**
* @title DepositVault
* @notice Smart contract that handles mTBILL minting
* @author RedDuck Software
*/
contract DepositVault is ManageableVault, IDepositVault {
using Counters for Counters.Counter;
/**
* @notice minimal USD amount for first user`s deposit
*/
uint256 public minMTokenAmountForFirstDeposit;
/**
* @notice mapping, requestId => request data
*/
mapping(uint256 => Request) public mintRequests;
/**
* @dev depositor address => amount minted
*/
mapping(address => uint256) public totalMinted;
/**
* @dev leaving a storage gap for futures updates
*/
uint256[50] private __gap;
/**
* @notice upgradeable pattern contract`s initializer
* @param _ac address of MidasAccessControll contract
* @param _mTokenInitParams init params for mToken
* @param _receiversInitParams init params for receivers
* @param _instantInitParams init params for instant operations
* @param _sanctionsList address of sanctionsList contract
* @param _variationTolerance percent of prices diviation 1% = 100
* @param _minAmount basic min amount for operations in mToken
* @param _minMTokenAmountForFirstDeposit min amount for first deposit in mToken
*/
function initialize(
address _ac,
MTokenInitParams calldata _mTokenInitParams,
ReceiversInitParams calldata _receiversInitParams,
InstantInitParams calldata _instantInitParams,
address _sanctionsList,
uint256 _variationTolerance,
uint256 _minAmount,
uint256 _minMTokenAmountForFirstDeposit
) external initializer {
__ManageableVault_init(
_ac,
_mTokenInitParams,
_receiversInitParams,
_instantInitParams,
_sanctionsList,
_variationTolerance,
_minAmount
);
minMTokenAmountForFirstDeposit = _minMTokenAmountForFirstDeposit;
}
/**
* @inheritdoc IDepositVault
*/
function depositInstant(
address tokenIn,
uint256 amountToken,
uint256 minReceiveAmount,
bytes32 referrerId
)
external
whenFnNotPaused(this.depositInstant.selector)
onlyGreenlisted(msg.sender)
onlyNotBlacklisted(msg.sender)
onlyNotSanctioned(msg.sender)
{
address user = msg.sender;
address tokenInCopy = tokenIn;
uint256 amountTokenCopy = amountToken;
(
uint256 tokenAmountInUsd,
uint256 feeTokenAmount,
uint256 amountTokenWithoutFee,
uint256 mintAmount,
,
,
uint256 tokenDecimals
) = _calcAndValidateDeposit(user, tokenInCopy, amountTokenCopy, true);
require(
mintAmount >= minReceiveAmount,
"DV: minReceiveAmount > actual"
);
totalMinted[user] += mintAmount;
_requireAndUpdateLimit(mintAmount);
_tokenTransferFromUser(
tokenInCopy,
tokensReceiver,
amountTokenWithoutFee,
tokenDecimals
);
if (feeTokenAmount > 0)
_tokenTransferFromUser(
tokenInCopy,
feeReceiver,
feeTokenAmount,
tokenDecimals
);
mToken.mint(user, mintAmount);
bytes32 referrerIdCopy = referrerId;
emit DepositInstant(
user,
tokenInCopy,
tokenAmountInUsd,
amountTokenCopy,
feeTokenAmount,
mintAmount,
referrerIdCopy
);
}
/**
* @inheritdoc IDepositVault
*/
function depositRequest(
address tokenIn,
uint256 amountToken,
bytes32 referrerId
)
external
whenFnNotPaused(this.depositRequest.selector)
onlyGreenlisted(msg.sender)
onlyNotBlacklisted(msg.sender)
onlyNotSanctioned(msg.sender)
returns (uint256 requestId)
{
address user = msg.sender;
address tokenInCopy = tokenIn;
uint256 amountTokenCopy = amountToken;
bytes32 referrerIdCopy = referrerId;
uint256 currentId = currentRequestId.current();
requestId = currentId;
currentRequestId.increment();
(
uint256 tokenAmountInUsd,
uint256 feeAmount,
uint256 amountTokenWithoutFee,
,
uint256 tokenInRate,
uint256 tokenOutRate,
uint256 tokenDecimals
) = _calcAndValidateDeposit(user, tokenInCopy, amountTokenCopy, false);
_tokenTransferFromUser(
tokenInCopy,
tokensReceiver,
amountTokenWithoutFee,
tokenDecimals
);
if (feeAmount > 0)
_tokenTransferFromUser(
tokenInCopy,
feeReceiver,
feeAmount,
tokenDecimals
);
mintRequests[currentId] = Request({
sender: user,
tokenIn: tokenInCopy,
status: RequestStatus.Pending,
depositedUsdAmount: tokenAmountInUsd,
usdAmountWithoutFees: (amountTokenWithoutFee * tokenInRate) /
10**18,
tokenOutRate: tokenOutRate
});
emit DepositRequest(
currentId,
user,
tokenInCopy,
tokenAmountInUsd,
feeAmount,
tokenOutRate,
referrerIdCopy
);
}
/**
* @inheritdoc IDepositVault
*/
function safeApproveRequest(uint256 requestId, uint256 newOutRate)
external
onlyVaultAdmin
{
_approveRequest(requestId, newOutRate, true);
emit SafeApproveRequest(requestId, newOutRate);
}
/**
* @inheritdoc IDepositVault
*/
function approveRequest(uint256 requestId, uint256 newOutRate)
external
onlyVaultAdmin
{
_approveRequest(requestId, newOutRate, false);
emit ApproveRequest(requestId, newOutRate);
}
/**
* @inheritdoc IDepositVault
*/
function rejectRequest(uint256 requestId) external onlyVaultAdmin {
Request memory request = mintRequests[requestId];
require(request.sender != address(0), "DV: request not exist");
require(
request.status == RequestStatus.Pending,
"DV: request not pending"
);
mintRequests[requestId].status = RequestStatus.Canceled;
emit RejectRequest(requestId, request.sender);
}
/**
* @inheritdoc IDepositVault
*/
function setMinMTokenAmountForFirstDeposit(uint256 newValue)
external
onlyVaultAdmin
{
minMTokenAmountForFirstDeposit = newValue;
emit SetMinMTokenAmountForFirstDeposit(msg.sender, newValue);
}
/**
* @inheritdoc ManageableVault
*/
function vaultRole() public pure virtual override returns (bytes32) {
return DEPOSIT_VAULT_ADMIN_ROLE;
}
/**
* @dev validates that inputted USD amount >= minAmountToDepositInUsd()
* and amount >= minAmount()
* @param user user address
* @param amountMTokenWithoutFee amount of mToken without fee (decimals 18)
*/
function _validateMinAmount(address user, uint256 amountMTokenWithoutFee)
internal
view
{
require(amountMTokenWithoutFee >= minAmount, "DV: mToken amount < min");
if (totalMinted[user] != 0) return;
require(
amountMTokenWithoutFee >= minMTokenAmountForFirstDeposit,
"DV: mint amount < min"
);
}
/**
* @dev approving request
* Checks price diviation if safe
* Mints mTokens to user
* @param requestId request id
* @param newOutRate mToken rate
*/
function _approveRequest(
uint256 requestId,
uint256 newOutRate,
bool isSafe
) private {
Request memory request = mintRequests[requestId];
require(request.sender != address(0), "DV: request not exist");
require(
request.status == RequestStatus.Pending,
"DV: request not pending"
);
if (isSafe)
_requireVariationTolerance(request.tokenOutRate, newOutRate);
uint256 amountMToken = (request.usdAmountWithoutFees * (10**18)) /
newOutRate;
mToken.mint(request.sender, amountMToken);
totalMinted[request.sender] += amountMToken;
request.status = RequestStatus.Processed;
request.tokenOutRate = newOutRate;
mintRequests[requestId] = request;
}
/**
* @dev validate deposit and calculate mint amount
* @param user user address
* @param tokenIn tokenIn address
* @param amountToken tokenIn amount (decimals 18)
* @param isInstant is instant operation
*
* @return tokenAmountInUsd tokenIn amount converted to USD
* @return feeTokenAmount fee amount in tokenIn
* @return amountTokenWithoutFee tokenIn amount without fee
* @return mintAmount mToken amount for mint
* @return tokenInRate tokenIn rate
* @return tokenOutRate mToken rate
* @return tokenDecimals tokenIn decimals
*/
function _calcAndValidateDeposit(
address user,
address tokenIn,
uint256 amountToken,
bool isInstant
)
internal
returns (
uint256 tokenAmountInUsd,
uint256 feeTokenAmount,
uint256 amountTokenWithoutFee,
uint256 mintAmount,
uint256 tokenInRate,
uint256 tokenOutRate,
uint256 tokenDecimals
)
{
require(amountToken > 0, "DV: invalid amount");
tokenDecimals = _tokenDecimals(tokenIn);
_requireTokenExists(tokenIn);
(uint256 amountInUsd, uint256 tokenInUSDRate) = _convertTokenToUsd(
tokenIn,
amountToken
);
tokenAmountInUsd = amountInUsd;
tokenInRate = tokenInUSDRate;
address userCopy = user;
_requireAndUpdateAllowance(tokenIn, amountToken);
feeTokenAmount = _truncate(
_getFeeAmount(userCopy, tokenIn, amountToken, isInstant, 0),
tokenDecimals
);
amountTokenWithoutFee = amountToken - feeTokenAmount;
uint256 feeInUsd = (feeTokenAmount * tokenInRate) / 10**18;
(uint256 mTokenAmount, uint256 mTokenRate) = _convertUsdToMToken(
tokenAmountInUsd - feeInUsd
);
mintAmount = mTokenAmount;
tokenOutRate = mTokenRate;
if (!isFreeFromMinAmount[userCopy]) {
_validateMinAmount(userCopy, mintAmount);
}
require(mintAmount > 0, "DV: invalid mint amount");
}
/**
* @dev calculates USD amount from tokenIn amount
* @param tokenIn tokenIn address
* @param amount amount of tokenIn (decimals 18)
*
* @return amountInUsd converted amount to USD
* @return rate conversion rate
*/
function _convertTokenToUsd(address tokenIn, uint256 amount)
internal
view
returns (uint256 amountInUsd, uint256 rate)
{
require(amount > 0, "DV: amount zero");
TokenConfig storage tokenConfig = tokensConfig[tokenIn];
rate = _getTokenRate(tokenConfig.dataFeed, tokenConfig.stable);
require(rate > 0, "DV: rate zero");
amountInUsd = (amount * rate) / (10**18);
}
/**
* @dev calculates mToken amount from USD amount
* @param amountUsd amount of USD (decimals 18)
*
* @return amountMToken converted USD to mToken
* @return mTokenRate conversion rate
*/
function _convertUsdToMToken(uint256 amountUsd)
internal
view
returns (uint256 amountMToken, uint256 mTokenRate)
{
require(amountUsd > 0, "DV: amount zero");
mTokenRate = mTokenDataFeed.getDataInBase18();
require(mTokenRate > 0, "DV: rate zero");
amountMToken = (amountUsd * (10**18)) / mTokenRate;
}
}