-
Notifications
You must be signed in to change notification settings - Fork 91
/
LibCreditNftManager.sol
484 lines (426 loc) · 17.4 KB
/
LibCreditNftManager.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {CreditNft} from "../../dollar/core/CreditNft.sol";
import {CREDIT_NFT_MANAGER_ROLE} from "./Constants.sol";
import {ICurveStableSwapMetaNG} from "../interfaces/ICurveStableSwapMetaNG.sol";
import {IERC20Ubiquity} from "../../dollar/interfaces/IERC20Ubiquity.sol";
import {IDollarMintExcess} from "../../dollar/interfaces/IDollarMintExcess.sol";
import {LibAppStorage, AppStorage} from "./LibAppStorage.sol";
import {LibCreditRedemptionCalculator} from "./LibCreditRedemptionCalculator.sol";
import {LibCreditNftRedemptionCalculator} from "./LibCreditNftRedemptionCalculator.sol";
import {UbiquityCreditToken} from "../../dollar/core/UbiquityCreditToken.sol";
import {LibAccessControl} from "./LibAccessControl.sol";
import {IDollarMintCalculator} from "../../dollar/interfaces/IDollarMintCalculator.sol";
/**
* @notice Library for basic credit issuing and redemption mechanism for Credit NFT and Credit holders
* @notice Allows users to burn their Dollars in exchange for Credit NFTs or Credits redeemable in the future
* @notice Allows users to:
* - redeem individual Credit NFT or batch redeem Credit NFT on a first-come first-serve basis
* - redeem Credits for Dollars
*/
library LibCreditNftManager {
using SafeERC20 for IERC20Ubiquity;
/// @notice Storage slot used to store data for this library
bytes32 constant CREDIT_NFT_MANAGER_STORAGE_SLOT =
bytes32(
uint256(
keccak256("ubiquity.contracts.credit.nft.manager.storage")
) - 1
) & ~bytes32(uint256(0xff));
/// @notice Emitted when Credit NFT to Governance conversion rate was updated
event ExpiredCreditNftConversionRateChanged(
uint256 newRate,
uint256 previousRate
);
/// @notice Emitted when Credit NFT block expiration length was updated
event CreditNftLengthChanged(
uint256 newCreditNftLengthBlocks,
uint256 previousCreditNftLengthBlocks
);
/// @notice Struct used as a storage for the current library
struct CreditNftManagerData {
//the amount of dollars we minted this cycle, so we can calculate delta.
// should be reset to 0 when cycle ends
uint256 dollarsMintedThisCycle;
uint256 blockHeightDebt;
uint256 creditNftLengthBlocks;
uint256 expiredCreditNftConversionRate;
bool debtCycle;
}
/**
* @notice Returns struct used as a storage for this library
* @return l Struct used as a storage
*/
function creditNftStorage()
internal
pure
returns (CreditNftManagerData storage l)
{
bytes32 slot = CREDIT_NFT_MANAGER_STORAGE_SLOT;
assembly {
l.slot := slot
}
}
/**
* @notice Returns Credit NFT to Governance conversion rate
* @return Conversion rate
*/
function expiredCreditNftConversionRate() internal view returns (uint256) {
return creditNftStorage().expiredCreditNftConversionRate;
}
/**
* @notice Credit NFT to Governance conversion rate
* @notice When Credit NFTs are expired they can be converted to
* Governance tokens using `rate` conversion rate
* @param rate Credit NFT to Governance tokens conversion rate
*/
function setExpiredCreditNftConversionRate(uint256 rate) internal {
emit ExpiredCreditNftConversionRateChanged(
rate,
creditNftStorage().expiredCreditNftConversionRate
);
creditNftStorage().expiredCreditNftConversionRate = rate;
}
/**
* @notice Sets Credit NFT block lifespan
* @param _creditNftLengthBlocks The number of blocks during which Credit NFTs can be
* redeemed for Dollars
*/
function setCreditNftLength(uint256 _creditNftLengthBlocks) internal {
emit CreditNftLengthChanged(
_creditNftLengthBlocks,
creditNftStorage().creditNftLengthBlocks
);
creditNftStorage().creditNftLengthBlocks = _creditNftLengthBlocks;
}
/**
* @notice Returns Credit NFT block lifespan
* @return Number of blocks during which Credit NFTs can be
* redeemed for Dollars
*/
function creditNftLengthBlocks() internal view returns (uint256) {
return creditNftStorage().creditNftLengthBlocks;
}
/**
* @notice Burns Dollars in exchange for Credit NFTs
* @notice Should only be called when Dollar price < 1$
* @param amount Amount of Dollars to exchange for Credit NFTs
* @return Expiry block number when Credit NFTs can no longer be redeemed for Dollars
*/
function exchangeDollarsForCreditNft(
uint256 amount
) internal returns (uint256) {
AppStorage storage store = LibAppStorage.appStorage();
uint256 twapPrice = ICurveStableSwapMetaNG(
store.stableSwapMetaPoolAddress
).price_oracle(0);
require(
twapPrice < 1 ether,
"Price must be below 1 to mint Credit NFT"
);
CreditNft creditNft = CreditNft(
LibAppStorage.appStorage().creditNftAddress
);
creditNft.updateTotalDebt();
CreditNftManagerData storage cs = creditNftStorage();
//we are in a down cycle so reset the cycle counter
// and set the blockHeight Debt
if (!cs.debtCycle) {
cs.debtCycle = true;
cs.blockHeightDebt = block.number;
cs.dollarsMintedThisCycle = 0;
}
uint256 creditNftToMint = LibCreditNftRedemptionCalculator
.getCreditNftAmount(amount);
// we burn user's dollars.
IERC20Ubiquity(LibAppStorage.appStorage().dollarTokenAddress).burnFrom(
msg.sender,
amount
);
uint256 expiryBlockNumber = block.number + (cs.creditNftLengthBlocks);
creditNft.mintCreditNft(msg.sender, creditNftToMint, expiryBlockNumber);
//give the caller the block number of the minted nft
return expiryBlockNumber;
}
/**
* @notice Burns Dollars in exchange for Credit tokens
* @notice Should only be called when Dollar price < 1$
* @param amount Amount of Dollars to burn
* @return Amount of Credits minted
*/
function exchangeDollarsForCredit(
uint256 amount
) internal returns (uint256) {
AppStorage storage store = LibAppStorage.appStorage();
uint256 twapPrice = ICurveStableSwapMetaNG(
store.stableSwapMetaPoolAddress
).price_oracle(0);
require(twapPrice < 1 ether, "Price must be below 1 to mint Credit");
CreditNft creditNft = CreditNft(store.creditNftAddress);
creditNft.updateTotalDebt();
//we are in a down cycle so reset the cycle counter
// and set the blockHeight Debt
if (!creditNftStorage().debtCycle) {
CreditNftManagerData storage cs = creditNftStorage();
cs.debtCycle = true;
cs.blockHeightDebt = block.number;
cs.dollarsMintedThisCycle = 0;
}
uint256 creditToMint = LibCreditRedemptionCalculator.getCreditAmount(
amount,
creditNftStorage().blockHeightDebt
);
// we burn user's dollars.
IERC20Ubiquity(store.dollarTokenAddress).burnFrom(msg.sender, amount);
// mint Credit
UbiquityCreditToken creditToken = UbiquityCreditToken(
store.creditTokenAddress
);
creditToken.mint(msg.sender, creditToMint);
//give minted Credit amount
return creditToMint;
}
/**
* @notice Returns amount of Credit NFTs to be minted for the `amount` of Dollars to burn
* @param amount Amount of Dollars to burn
* @return Amount of Credit NFTs to be minted
*/
function getCreditNftReturnedForDollars(
uint256 amount
) internal view returns (uint256) {
return LibCreditNftRedemptionCalculator.getCreditNftAmount(amount);
}
/**
* @notice Returns the amount of Credit tokens to be minter for the provided `amount` of Dollars to burn
* @param amount Amount of Dollars to burn
* @return Amount of Credits to be minted
*/
function getCreditReturnedForDollars(
uint256 amount
) internal view returns (uint256) {
return
LibCreditRedemptionCalculator.getCreditAmount(
amount,
creditNftStorage().blockHeightDebt
);
}
/**
* @notice Handles the receipt of a single ERC1155 token type. This function is
* called at the end of a `safeTransferFrom` after the balance has been updated.
*
* NOTE: To accept the transfer, this must return
* `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
* (i.e. 0xf23a6e61, or its own function selector).
*
* @param operator The address which initiated the transfer (i.e. msg.sender)
* @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
*/
function onERC1155Received(
address operator,
address,
uint256,
uint256,
bytes calldata
) internal view returns (bytes4) {
if (LibAccessControl.hasRole(CREDIT_NFT_MANAGER_ROLE, operator)) {
//allow the transfer since it originated from this contract
return
bytes4(
keccak256(
"onERC1155Received(address,address,uint256,uint256,bytes)"
)
);
} else {
//reject the transfer
return "";
}
}
/**
* @notice Burns expired Credit NFTs for Governance tokens at `expiredCreditNftConversionRate` rate
* @param id Credit NFT timestamp
* @param amount Amount of Credit NFTs to burn
* @return governanceAmount Amount of Governance tokens minted to Credit NFT holder
*/
function burnExpiredCreditNftForGovernance(
uint256 id,
uint256 amount
) public returns (uint256 governanceAmount) {
// Check whether Credit NFT hasn't expired --> Burn Credit NFT.
CreditNft creditNft = CreditNft(
LibAppStorage.appStorage().creditNftAddress
);
require(id <= block.number, "Credit NFT has not expired");
require(
creditNft.balanceOf(msg.sender, id) >= amount,
"User not enough Credit NFT"
);
creditNft.burnCreditNft(msg.sender, amount, id);
// Mint Governance Token to this contract. Transfer Governance Token to msg.sender i.e. Credit NFT holder
IERC20Ubiquity governanceToken = IERC20Ubiquity(
LibAppStorage.appStorage().governanceTokenAddress
);
governanceAmount =
amount /
creditNftStorage().expiredCreditNftConversionRate;
governanceToken.mint(msg.sender, governanceAmount);
}
/**
* TODO: Should we leave it ?
* @notice Burns Credit NFTs for Credit tokens
* @param id Credit NFT timestamp
* @param amount Amount of Credit NFTs to burn
* @return Credit tokens balance of `msg.sender`
*/
function burnCreditNftForCredit(
uint256 id,
uint256 amount
) public returns (uint256) {
// Check whether Credit NFT hasn't expired --> Burn Credit NFT.
CreditNft creditNft = CreditNft(
LibAppStorage.appStorage().creditNftAddress
);
require(id > block.timestamp, "Credit NFT has expired");
require(
creditNft.balanceOf(msg.sender, id) >= amount,
"User not enough Credit NFT"
);
creditNft.burnCreditNft(msg.sender, amount, id);
// Mint LP tokens to this contract. Transfer LP tokens to msg.sender i.e. Credit NFT holder
UbiquityCreditToken creditToken = UbiquityCreditToken(
LibAppStorage.appStorage().creditTokenAddress
);
creditToken.mint(address(this), amount);
creditToken.transfer(msg.sender, amount);
return creditToken.balanceOf(msg.sender);
}
/**
* @notice Burns Credit tokens for Dollars when Dollar price > 1$
* @param amount Amount of Credits to burn
* @return Amount of unredeemed Credits
*/
function burnCreditTokensForDollars(
uint256 amount
) public returns (uint256) {
AppStorage storage store = LibAppStorage.appStorage();
uint256 twapPrice = ICurveStableSwapMetaNG(
store.stableSwapMetaPoolAddress
).price_oracle(0);
require(twapPrice > 1 ether, "Price must be above 1");
if (creditNftStorage().debtCycle) {
creditNftStorage().debtCycle = false;
}
UbiquityCreditToken creditToken = UbiquityCreditToken(
LibAppStorage.appStorage().creditTokenAddress
);
require(
creditToken.balanceOf(msg.sender) >= amount,
"User doesn't have enough Credit pool tokens."
);
IERC20Ubiquity dollar = IERC20Ubiquity(
LibAppStorage.appStorage().dollarTokenAddress
);
uint256 maxRedeemableCredit = dollar.balanceOf(address(this));
if (maxRedeemableCredit <= 0) {
mintClaimableDollars();
maxRedeemableCredit = dollar.balanceOf(address(this));
}
uint256 creditToRedeem = amount;
if (amount > maxRedeemableCredit) {
creditToRedeem = maxRedeemableCredit;
}
creditToken.burnFrom(msg.sender, creditToRedeem);
dollar.transfer(msg.sender, creditToRedeem);
return amount - creditToRedeem;
}
/**
* @notice Burns Credit NFTs for Dollars when Dollar price > 1$
* @param id Credit NFT expiry block number
* @param amount Amount of Credit NFTs to burn
* @return Amount of unredeemed Credit NFTs
*/
function redeemCreditNft(
uint256 id,
uint256 amount
) public returns (uint256) {
AppStorage storage store = LibAppStorage.appStorage();
uint256 twapPrice = ICurveStableSwapMetaNG(
store.stableSwapMetaPoolAddress
).price_oracle(0);
require(
twapPrice > 1 ether,
"Price must be above 1 to redeem Credit NFT"
);
if (creditNftStorage().debtCycle) {
creditNftStorage().debtCycle = false;
}
CreditNft creditNft = CreditNft(store.creditNftAddress);
require(id > block.number, "Credit NFT has expired");
require(
creditNft.balanceOf(msg.sender, id) >= amount,
"User not enough Credit NFT"
);
mintClaimableDollars();
UbiquityCreditToken creditToken = UbiquityCreditToken(
store.creditTokenAddress
);
IERC20Ubiquity dollar = IERC20Ubiquity(store.dollarTokenAddress);
// Credit have a priority on Credit NFT holder
require(
creditToken.totalSupply() <= dollar.balanceOf(address(this)),
"There aren't enough Dollar to redeem currently"
);
uint256 maxRedeemableCreditNft = dollar.balanceOf(address(this)) -
creditToken.totalSupply();
uint256 creditNftToRedeem = amount;
if (amount > maxRedeemableCreditNft) {
creditNftToRedeem = maxRedeemableCreditNft;
}
require(
dollar.balanceOf(address(this)) > 0,
"There aren't any Dollar to redeem currently"
);
// creditNftManager must be an operator to transfer on behalf of msg.sender
creditNft.burnCreditNft(msg.sender, creditNftToRedeem, id);
dollar.transfer(msg.sender, creditNftToRedeem);
return amount - (creditNftToRedeem);
}
/**
* @notice Mints Dollars when Dollar price > 1$
* @notice Distributes excess Dollars this way:
* - 50% goes to the treasury address
* - 10% goes for burning Dollar-Governance LP tokens in a DEX pool
* - 40% goes to the Staking contract
*/
function mintClaimableDollars() public {
AppStorage storage store = LibAppStorage.appStorage();
CreditNft creditNft = CreditNft(store.creditNftAddress);
creditNft.updateTotalDebt();
uint256 totalMintableDollars = IDollarMintCalculator(address(this))
.getDollarsToMint();
uint256 dollarsToMint = totalMintableDollars -
(creditNftStorage().dollarsMintedThisCycle);
//update the dollars for this cycle
creditNftStorage().dollarsMintedThisCycle = totalMintableDollars;
// Dollar should be minted to address(this)
IERC20Ubiquity dollar = IERC20Ubiquity(store.dollarTokenAddress);
dollar.mint(address(this), dollarsToMint);
UbiquityCreditToken creditToken = UbiquityCreditToken(
store.creditTokenAddress
);
uint256 currentRedeemableBalance = dollar.balanceOf(address(this));
uint256 totalOutstandingDebt = creditNft.getTotalOutstandingDebt() +
creditToken.totalSupply();
if (currentRedeemableBalance > totalOutstandingDebt) {
uint256 excessDollars = currentRedeemableBalance -
(totalOutstandingDebt);
IDollarMintExcess dollarsDistributor = IDollarMintExcess(
address(this)
);
// transfer excess dollars to the distributor and tell it to distribute
dollar.transfer(address(this), excessDollars);
dollarsDistributor.distributeDollars();
}
}
}