-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRWAHubInstantMints.sol
378 lines (331 loc) · 11.9 KB
/
RWAHubInstantMints.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
/**SPDX-License-Identifier: BUSL-1.1
▄▄█████████▄
╓██▀└ ,╓▄▄▄, '▀██▄
██▀ ▄██▀▀╙╙▀▀██▄ └██µ ,, ,, , ,,, ,,,
██ ,██¬ ▄████▄ ▀█▄ ╙█▄ ▄███▀▀███▄ ███▄ ██ ███▀▀▀███▄ ▄███▀▀███,
██ ██ ╒█▀' ╙█▌ ╙█▌ ██ ▐██ ███ █████, ██ ██▌ └██▌ ██▌ └██▌
██ ▐█▌ ██ ╟█ █▌ ╟█ ██▌ ▐██ ██ └███ ██ ██▌ ╟██ j██ ╟██
╟█ ██ ╙██ ▄█▀ ▐█▌ ██ ╙██ ██▌ ██ ╙████ ██▌ ▄██▀ ██▌ ,██▀
██ "██, ╙▀▀███████████⌐ ╙████████▀ ██ ╙██ ███████▀▀ ╙███████▀`
██▄ ╙▀██▄▄▄▄▄,,, ¬─ '─¬
╙▀██▄ '╙╙╙▀▀▀▀▀▀▀▀
╙▀▀██████R⌐
*/
pragma solidity 0.8.16;
import "contracts/interfaces/IRWAHubInstantMints.sol";
import "contracts/RWAHubOffChainRedemptions.sol";
import "contracts/InstantMintTimeBasedRateLimiter.sol";
abstract contract RWAHubInstantMints is
IRWAHubInstantMints,
RWAHubOffChainRedemptions,
InstantMintTimeBasedRateLimiter
{
using SafeERC20 for IERC20;
// Fee collected when instant minting OMMF (in basis points)
uint256 public instantMintFee = 10;
// Fee collected when instant redeeming OMMF (in basis points)
uint256 public instantRedemptionFee = 10;
// priceId associated with instantMints
uint256 public instantMintPriceId;
// priceId associated with instantRedemptions
uint256 public instantRedemptionPriceId;
// Flag whether instantMint is paused
bool public instantMintPaused = true;
// Flag whether instantRedemption is paused
bool public instantRedemptionPaused = true;
// Address to manage instant mints/redemptions
address public instantMintAssetManager;
constructor(
address _collateral,
address _rwa,
address managerAdmin,
address pauser,
address _assetSender,
address _feeRecipient,
uint256 _minimumDepositAmount,
uint256 _minimumRedemptionAmount,
address _instantMintAssetManager
)
RWAHubOffChainRedemptions(
_collateral,
_rwa,
managerAdmin,
pauser,
_assetSender,
_feeRecipient,
_minimumDepositAmount,
_minimumRedemptionAmount
)
InstantMintTimeBasedRateLimiter(0, 0, 0, 0)
{
instantMintAssetManager = _instantMintAssetManager;
}
/*//////////////////////////////////////////////////////////////
Instant Mint/Redemption Functions
//////////////////////////////////////////////////////////////*/
/**
* @notice Instant mints `rwa` token to the caller in exchange for
* `collateral`
*
* @param amount The amount of `collateral` to deposit
*
* @dev All fees are deducted from amount transferred by
* `instantMintAssetManager`
*/
function instantMint(
uint256 amount
) external nonReentrant ifNotPaused(instantMintPaused) {
if (amount < minimumDepositAmount) {
revert DepositTooSmall();
}
if (instantMintPriceId == 0) {
revert PriceIdNotSet();
}
// Calculate fees
uint256 feesInCollateral = _getInstantMintFees(amount);
uint256 depositAmountAfterFee = amount - feesInCollateral;
// Transfer collateral
collateral.safeTransferFrom(msg.sender, instantMintAssetManager, amount);
// Calculate mint amount
uint256 price = pricer.getPrice(instantMintPriceId);
uint256 rwaOwed = _getMintAmountForPrice(depositAmountAfterFee, price);
// Check mint limit
_checkAndUpdateInstantMintLimit(rwaOwed);
// Mint RWA
rwa.mint(msg.sender, rwaOwed);
emit InstantMintCompleted(
msg.sender,
amount,
depositAmountAfterFee,
feesInCollateral,
rwaOwed,
price
);
}
/**
* @notice Instant mints `rwa` token to the caller in exchange for
* `collateral`
*
* @param amount The amount of `collateral` to deposit
*
* @dev All fees are deducted from collateral transferred by
* `instantMintAssetManager`
*/
function instantRedemption(
uint256 amount
) external nonReentrant ifNotPaused(instantRedemptionPaused) {
// Checks
if (amount < minimumRedemptionAmount) {
revert RedemptionTooSmall();
}
if (instantRedemptionPriceId == 0) {
revert PriceIdNotSet();
}
// Update instant redemption limit
_checkAndUpdateInstantRedemptionLimit(amount);
// Calculate collateralDue and fees
uint256 price = pricer.getPrice(instantRedemptionPriceId);
uint256 collateralDue = _getRedemptionAmountForRwa(amount, price);
uint256 feesInCollateral = _getinstantRedemptionFees(collateralDue);
uint256 collateralDuePostFees = collateralDue - feesInCollateral;
// Burn rwa and transfer collateral
rwa.burnFrom(msg.sender, amount);
collateral.safeTransferFrom(
instantMintAssetManager,
msg.sender,
collateralDuePostFees
);
emit InstantRedemptionCompleted(
msg.sender,
amount,
collateralDuePostFees,
feesInCollateral,
price,
instantRedemptionPriceId
);
}
/**
* @notice Guarded function to set the `instantMintAssetManager`
*
* @param _instantMintAssetManager The address to update
* `instantMintAssetManager` to
*/
function setInstantMintAssetManager(
address _instantMintAssetManager
) external onlyRole(MANAGER_ADMIN) {
address oldInstantMintAssetManager = instantMintAssetManager;
instantMintAssetManager = _instantMintAssetManager;
emit InstantMintAssetManagerSet(
oldInstantMintAssetManager,
_instantMintAssetManager
);
}
/*//////////////////////////////////////////////////////////////
Instant Mint/Redeem Fee Utils
//////////////////////////////////////////////////////////////*/
/**
* @notice Sets the instant mint fee
*
* @param _instantMintFee new mint fee specified in basis points
*
* @dev `_instantMintFee` must not exceed 100% (or 10_000 bps)
*/
function setInstantMintFee(
uint256 _instantMintFee
) external onlyRole(MANAGER_ADMIN) {
if (_instantMintFee > BPS_DENOMINATOR) {
revert FeeTooLarge();
}
uint256 oldInstantMintFee = instantMintFee;
instantMintFee = _instantMintFee;
emit InstantMintFeeSet(oldInstantMintFee, _instantMintFee);
}
/**
* @notice Sets instant redemption fee.
*
* @param _instantRedemptionFee new redemption fee specified in basis points
*
* @dev `_instantRedemptionFee` must not exceed 100% (or 10_000 bps)
*/
function setInstantRedemptionFee(
uint256 _instantRedemptionFee
) external onlyRole(MANAGER_ADMIN) {
if (_instantRedemptionFee > BPS_DENOMINATOR) {
revert FeeTooLarge();
}
uint256 oldinstantRedemptionFee = instantRedemptionFee;
instantRedemptionFee = _instantRedemptionFee;
emit InstantRedemptionFeeSet(
oldinstantRedemptionFee,
_instantRedemptionFee
);
}
/**
* @notice Given amount of `collateral`, returns how much in fees
* are owed
*
* @param collateralAmount Amount of `collateral` to calculate fees
* (in decimals of `collateral`)
*/
function _getInstantMintFees(
uint256 collateralAmount
) internal view returns (uint256) {
return (collateralAmount * instantMintFee) / BPS_DENOMINATOR;
}
/**
* @notice Given amount of `collateral`, returns how much in fees
* are owed
*
* @param collateralAmount Amount `collateral` to calculate fees
* (in decimals of `collateral`)
*/
function _getinstantRedemptionFees(
uint256 collateralAmount
) internal view returns (uint256) {
return (collateralAmount * instantRedemptionFee) / BPS_DENOMINATOR;
}
/*//////////////////////////////////////////////////////////////
Instant Mint/Redeem Setters
//////////////////////////////////////////////////////////////*/
/**
* @notice Function to set priceId for instant mints
*/
function setInstantMintPriceId(
uint256 _instantMintPriceId
) external onlyRole(PRICE_ID_SETTER_ROLE) {
instantMintPriceId = _instantMintPriceId;
emit PriceIdSetForInstantMint(_instantMintPriceId);
}
/**
* @notice Function to set priceId for instant redemptions
*/
function setInstantRedemptionPriceId(
uint256 _instantRedemptionPriceId
) external onlyRole(PRICE_ID_SETTER_ROLE) {
instantRedemptionPriceId = _instantRedemptionPriceId;
emit PriceIdSetForInstantRedemption(_instantRedemptionPriceId);
}
/**
* @notice Function to pause instant mints
*/
function pauseInstantMint() external onlyRole(PAUSER_ADMIN) {
instantMintPaused = true;
emit InstantMintPaused(msg.sender);
}
/**
* @notice Function to unpause instant mints
*/
function unpauseInstantMint() external onlyRole(MANAGER_ADMIN) {
instantMintPaused = false;
emit InstantMintUnpaused(msg.sender);
}
/**
* @notice Function to pause instant redemptions
*/
function pauseInstantRedemption() external onlyRole(PAUSER_ADMIN) {
instantRedemptionPaused = true;
emit InstantRedemptionPaused(msg.sender);
}
/**
* @notice Function to unpause instant redemptions
*/
function unpauseInstantRedemption() external onlyRole(MANAGER_ADMIN) {
instantRedemptionPaused = false;
emit InstantRedemptionUnpaused(msg.sender);
}
/*//////////////////////////////////////////////////////////////
Rate Limiter Configuration
//////////////////////////////////////////////////////////////*/
/**
* @notice Set the mintLimit constraint inside the TimeBasedRateLimiter
* base contract
*
* @param newMintLimit New limit that dictates how much RWA can be minted
* in a specified duration
* (in 18 decimals per the RWA contract)
*/
function setInstantMintLimit(
uint256 newMintLimit
) external onlyRole(MANAGER_ADMIN) {
_setInstantMintLimit(newMintLimit);
}
/**
* @notice Set the RedemptionLimit constraint inside the TimeBasedRateLimiter
* base contract
*
* @param newRedemptionLimit New limit that dicates how much RWA
* can be redeemed in a specified duration
* (in 18 decimals per the RWA contract)
*/
function setInstantRedemptionLimit(
uint256 newRedemptionLimit
) external onlyRole(MANAGER_ADMIN) {
_setInstantRedemptionLimit(newRedemptionLimit);
}
/**
* @notice Sets mintLimitDuration constraint inside the TimeBasedRateLimiter
* base contract
*
* @param newMintLimitDuration New limit that specifies the interval
* (in seconds) in which only mintLimit RWA
* can be minted within
*/
function setInstantMintLimitDuration(
uint256 newMintLimitDuration
) external onlyRole(MANAGER_ADMIN) {
_setInstantMintLimitDuration(newMintLimitDuration);
}
/**
* @notice Sets RedemptionLimitDuration inside the TimeBasedRateLimiter
* base contract
*
* @param newRedemptionLimitDuration New limit that specifies the interval
* (in seconds) in which only RedemptionLimit RWA
* can be redeemed within
*/
function setInstantRedemptionLimitDuration(
uint256 newRedemptionLimitDuration
) external onlyRole(MANAGER_ADMIN) {
_setInstantRedemptionLimitDuration(newRedemptionLimitDuration);
}
}