This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redeemer.sol
586 lines (493 loc) · 21.4 KB
/
Redeemer.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;
import 'src/MarketPlace.sol';
import 'src/lib/Safe.sol';
import 'src/errors/Exception.sol';
import 'src/interfaces/IERC5095.sol';
import 'src/interfaces/IERC20.sol';
import 'src/interfaces/ITempus.sol';
import 'src/interfaces/ITempusPool.sol';
import 'src/interfaces/ITempusToken.sol';
import 'src/interfaces/IAPWine.sol';
import 'src/interfaces/IAPWineController.sol';
import 'src/interfaces/IAPWineFutureVault.sol';
import 'src/interfaces/IAPWineToken.sol';
import 'src/interfaces/IAPWineAMMPool.sol';
import 'src/interfaces/ISwivel.sol';
import 'src/interfaces/ISwivelToken.sol';
import 'src/interfaces/IElementToken.sol';
import 'src/interfaces/IYieldToken.sol';
import 'src/interfaces/INotional.sol';
import 'src/interfaces/IPendle.sol';
import 'src/interfaces/IPendleForge.sol';
import 'src/interfaces/IPendleToken.sol';
import 'src/interfaces/ISenseDivider.sol';
import 'src/interfaces/ISenseAdapter.sol';
import 'src/interfaces/IYield.sol';
import 'src/interfaces/IConverter.sol';
import 'forge-std/Test.sol';
/// @title Redeemer
/// @author Sourabh Marathe, Julian Traversa, Rob Robbins
/// @notice The Redeemer contract is used to redeem the underlying lent capital of a loan.
/// @notice Users may redeem their ERC-5095 tokens for the underlying asset represented by that token after maturity.
contract Redeemer {
/// @notice address that is allowed to set fees and contracts, etc. It is commonly used in the authorized modifier.
address public admin;
/// @notice address of the MarketPlace contract, used to access the markets mapping
address public marketPlace;
/// @notice address that custodies principal tokens for all markets
address public lender;
/// @notice address that converts compounding tokens to their underlying
address public converter;
/// @notice third party contract needed to redeem Swivel PTs
address public immutable swivelAddr;
/// @notice third party contract needed to redeem Pendle PTs
address public immutable pendleAddr;
/// @notice third party contract needed to redeem Tempus PTs
address public immutable tempusAddr;
/// @notice this value determines the amount of fees paid on auto redemptions
uint256 public feenominator;
/// @notice represents a point in time where the feenominator may change
uint256 public feeChange;
/// @notice represents a minimum that the feenominator must exceed
uint256 public MIN_FEENOMINATOR = 500;
/// @notice mapping that indicates how much underlying has been redeemed by a market
mapping(address => mapping(uint256 => uint256)) public holdings;
/// @notice mapping that determines if a market's iPT can be redeemed
mapping(address => mapping(uint256 => bool)) public paused;
/// @notice emitted upon redemption of a loan
event Redeem(
uint8 principal,
address indexed underlying,
uint256 indexed maturity,
uint256 amount,
address sender
);
/// @notice emitted upon changing the admin
event SetAdmin(address indexed admin);
/// @notice emitted upon changing the converter
event SetConverter(address indexed converter);
/// @notice emitted upon setting the fee rate
event SetFee(uint256 indexed fee);
/// @notice ensures that only a certain address can call the function
/// @param a address that msg.sender must be to be authorized
modifier authorized(address a) {
if (msg.sender != a) {
revert Exception(0, 0, 0, msg.sender, a);
}
_;
}
/// @notice reverts on all markets where the paused mapping returns true
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
modifier unpaused(address u, uint256 m) {
if (paused[u][m]) {
revert Exception(17, m, 0, u, address(0));
}
_;
}
/// @notice Initializes the Redeemer contract
/// @param l the lender contract
/// @param s the Swivel contract
/// @param p the Pendle contract
/// @param t the Tempus contract
constructor(
address l,
address s,
address p,
address t
) {
admin = msg.sender;
lender = l;
swivelAddr = s;
pendleAddr = p;
tempusAddr = t;
feenominator = 4000;
}
/// @notice sets the admin address
/// @param a Address of a new admin
/// @return bool true if successful
function setAdmin(address a) external authorized(admin) returns (bool) {
admin = a;
emit SetAdmin(a);
return true;
}
/// @notice sets the address of the marketplace contract which contains the addresses of all the fixed rate markets
/// @param m the address of the marketplace contract
/// @return bool true if the address was set
function setMarketPlace(address m)
external
authorized(admin)
returns (bool)
{
if (marketPlace != address(0)) {
revert Exception(5, 0, 0, marketPlace, address(0));
}
marketPlace = m;
return true;
}
/// @notice sets the converter address
/// @param c address of the new converter
/// @return bool true if successful
function setConverter(address c) external authorized(admin) returns (bool) {
converter = c;
emit SetConverter(c);
return true;
}
/// @notice sets the address of the lender contract which contains the addresses of all the fixed rate markets
/// @param l the address of the lender contract
/// @return bool true if the address was set
function setLender(address l) external authorized(admin) returns (bool) {
if (lender != address(0)) {
revert Exception(8, 0, 0, address(lender), address(0));
}
lender = l;
return true;
}
/// @notice sets the feenominator to the given value
/// @param f the new value of the feenominator, fees are not collected when the feenominator is 0
/// @return bool true if successful
function setFee(uint256 f) external authorized(admin) returns (bool) {
uint256 feeTime = feeChange;
if (feeTime == 0) {
revert Exception(23, 0, 0, address(0), address(0));
} else if (feeTime < block.timestamp) {
revert Exception(
24,
block.timestamp,
feeTime,
address(0),
address(0)
);
} else if (f < MIN_FEENOMINATOR) {
revert Exception(25, 0, 0, address(0), address(0));
}
feenominator = f;
delete feeChange;
emit SetFee(f);
return true;
}
/// @notice allows admin to stop redemptions of Illuminate PTs for a given market
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
/// @param b true to pause, false to unpause
function pauseRedemptions(
address u,
uint256 m,
bool b
) external authorized(admin) {
paused[u][m] = b;
}
/// @notice approves the converter to spend the compounding asset
/// @param i an interest bearing token that must be approved for conversion
function approve(address i) external authorized(marketPlace) {
if (i != address(0)) {
Safe.approve(IERC20(i), address(converter), type(uint256).max);
}
}
/// @notice redeem method for Swivel, Yield, Element, Pendle, APWine, Tempus and Notional protocols
/// @param p principal value according to the MarketPlace's Principals Enum
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
/// @return bool true if the redemption was successful
function redeem(
uint8 p,
address u,
uint256 m
) external returns (bool) {
// Get the principal token that is being redeemed by the user
address principal = IMarketPlace(marketPlace).token(u, m, p);
// Get the maturity for the given principal token
uint256 maturity;
if (p == uint8(MarketPlace.Principals.Swivel)) {
maturity = ISwivelToken(principal).maturity();
} else if (p == uint8(MarketPlace.Principals.Yield)) {
maturity = IYieldToken(principal).maturity();
} else if (p == uint8(MarketPlace.Principals.Element)) {
maturity = IElementToken(principal).unlockTimestamp();
} else if (p == uint8(MarketPlace.Principals.Pendle)) {
maturity = IPendleToken(principal).expiry();
} else if (p == uint8(MarketPlace.Principals.Tempus)) {
maturity = ITempusPool(ITempusToken(principal).pool())
.maturityTime();
} else if (p == uint8(MarketPlace.Principals.Apwine)) {
// APWine's maturity is retrieved indirectly via the PT's
// futureVault and Controller
address futureVault = IAPWineToken(principal).futureVault();
address controller = IAPWineFutureVault(futureVault)
.getControllerAddress();
uint256 duration = IAPWineFutureVault(futureVault)
.PERIOD_DURATION();
maturity = IAPWineController(controller).getNextPeriodStart(
duration
);
} else if (p == uint8(MarketPlace.Principals.Notional)) {
maturity = INotional(principal).getMaturity();
} else {
revert Exception(6, p, 0, address(0), address(0));
}
// Verify that the token has matured
if (maturity > block.timestamp) {
revert Exception(7, maturity, 0, address(0), address(0));
}
// Cache the lender to save gas on sload
address cachedLender = lender;
// Get the amount to be redeemed
uint256 amount = IERC20(principal).balanceOf(cachedLender);
// Receive the principal token from the lender contract
Safe.transferFrom(
IERC20(principal),
cachedLender,
address(this),
amount
);
// Get the starting balance of the underlying held by the redeemer
uint256 starting = IERC20(u).balanceOf(address(this));
if (p == uint8(MarketPlace.Principals.Swivel)) {
// Redeems principal tokens from Swivel
if (!ISwivel(swivelAddr).redeemZcToken(u, maturity, amount)) {
revert Exception(15, 0, 0, address(0), address(0));
}
} else if (p == uint8(MarketPlace.Principals.Yield)) {
// Redeems principal tokens from Yield
IYieldToken(principal).redeem(address(this), amount);
} else if (p == uint8(MarketPlace.Principals.Element)) {
// Redeems principal tokens from Element
IElementToken(principal).withdrawPrincipal(amount, address(this));
} else if (p == uint8(MarketPlace.Principals.Pendle)) {
// Get the forge contract for the principal token
address forge = IPendleToken(principal).forge();
// Get the forge ID of the principal token
bytes32 forgeId = IPendleForge(forge).forgeId();
// Redeem the tokens from the Pendle contract
IPendle(pendleAddr).redeemAfterExpiry(forgeId, u, maturity);
// Get the compounding asset for this market
address compounding = IPendleToken(principal)
.underlyingYieldToken();
// Redeem the compounding to token to the underlying
IConverter(converter).convert(
compounding,
u,
IERC20(compounding).balanceOf(address(this))
);
} else if (p == uint8(MarketPlace.Principals.Tempus)) {
// Retrieve the pool for the principal token
address pool = ITempusToken(principal).pool();
// Redeems principal tokens from Tempus
ITempus(tempusAddr).redeemToBacking(pool, amount, 0, address(this));
} else if (p == uint8(MarketPlace.Principals.Apwine)) {
apwineWithdraw(principal, u, amount);
} else if (p == uint8(MarketPlace.Principals.Notional)) {
// Redeems principal tokens from Notional
INotional(principal).redeem(
INotional(principal).maxRedeem(address(this)),
address(this),
address(this)
);
}
// Calculate how much underlying was redeemed
uint256 redeemed = IERC20(u).balanceOf(address(this)) - starting;
// Update the holding for this market
holdings[u][m] = holdings[u][m] + redeemed;
emit Redeem(p, u, m, redeemed, msg.sender);
return true;
}
/// @notice redeem method signature for Sense
/// @param p principal value according to the MarketPlace's Principals Enum
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
/// @param s Sense's maturity is needed to extract the pt address
/// @param a Sense's adapter for this market
/// @return bool true if the redemption was successful
function redeem(
uint8 p,
address u,
uint256 m,
uint256 s,
address a
) external returns (bool) {
// Check the principal is Sense
if (p != uint8(MarketPlace.Principals.Sense)) {
revert Exception(6, p, 0, address(0), address(0));
}
// Get Sense's principal token for this market
IERC20 token = IERC20(IMarketPlace(marketPlace).token(u, m, p));
// Cache the lender to save on SLOAD operations
address cachedLender = lender;
// Get the balance of tokens to be redeemed by the user
uint256 amount = token.balanceOf(cachedLender);
// Transfer the user's tokens to the redeem contract
Safe.transferFrom(token, cachedLender, address(this), amount);
// Get the starting balance to verify the amount received afterwards
uint256 starting = IERC20(u).balanceOf(address(this));
// Get the divider from the adapter
ISenseDivider divider = ISenseDivider(ISenseAdapter(a).divider());
// Redeem the tokens from the Sense contract
ISenseDivider(divider).redeem(a, s, amount);
// Get the compounding token that is redeemed by Sense
address compounding = ISenseAdapter(a).target();
// Redeem the compounding token back to the underlying
IConverter(converter).convert(
compounding,
u,
IERC20(compounding).balanceOf(address(this))
);
// Get the amount received
uint256 redeemed = IERC20(u).balanceOf(address(this)) - starting;
// Verify that underlying are received 1:1 - cannot trust the adapter
if (redeemed < amount) {
revert Exception(13, 0, 0, address(0), address(0));
}
// Update the holdings for this market
holdings[u][m] = holdings[u][m] + redeemed;
emit Redeem(p, u, m, redeemed, msg.sender);
return true;
}
/// @notice burns Illuminate principal tokens and sends underlying to user
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
function redeem(address u, uint256 m) external unpaused(u, m) {
// Get Illuminate's principal token for this market
IERC5095 token = IERC5095(
IMarketPlace(marketPlace).token(
u,
m,
uint8(MarketPlace.Principals.Illuminate)
)
);
// Verify the token has matured
if (block.timestamp < token.maturity()) {
revert Exception(7, block.timestamp, m, address(0), address(0));
}
// Get the amount of tokens to be redeemed from the sender
uint256 amount = token.balanceOf(msg.sender);
// Calculate how many tokens the user should receive
uint256 redeemed = (amount * holdings[u][m]) / token.totalSupply();
// Update holdings of underlying
holdings[u][m] = holdings[u][m] - redeemed;
// Burn the user's principal tokens
token.authBurn(msg.sender, amount);
// Transfer the original underlying token back to the user
Safe.transfer(IERC20(u), msg.sender, redeemed);
emit Redeem(0, u, m, redeemed, msg.sender);
}
/// @notice implements the redeem method for the contract to fulfill the ERC-5095 interface
/// @param u address of an underlying asset
/// @param m maturity (timestamp) of the market
/// @param f address from where the underlying asset will be burned
/// @param t address to where the underlying asset will be transferred
/// @param a amount of the Illuminate PT to be burned and redeemed
/// @return uint256 amount of the underlying asset that was burned
function authRedeem(
address u,
uint256 m,
address f,
address t,
uint256 a
)
external
authorized(IMarketPlace(marketPlace).token(u, m, 0))
returns (uint256)
{
// Get the principal token for the given market
IERC5095 pt = IERC5095(IMarketPlace(marketPlace).token(u, m, 0));
// Make sure the market has matured
uint256 maturity = pt.maturity();
if (block.timestamp < maturity) {
revert Exception(7, maturity, 0, address(0), address(0));
}
// Calculate the amount redeemed
uint256 redeemed = (a * holdings[u][m]) / pt.totalSupply();
// Update holdings of underlying
holdings[u][m] = holdings[u][m] - redeemed;
// Burn the user's principal tokens
pt.authBurn(f, a);
// Transfer the original underlying token back to the user
Safe.transfer(IERC20(u), t, redeemed);
emit Redeem(0, u, m, redeemed, msg.sender);
return a;
}
/// @notice implements a redeem method to enable third-party redemptions
/// @dev expects approvals from owners to redeemer
/// @param u address of the underlying asset
/// @param m maturity of the market
/// @param f address from where the principal token will be burned
/// @return uint256 amount of underlying yielded as a fee
function autoRedeem(
address u,
uint256 m,
address[] calldata f
) external returns (uint256) {
// Get the principal token for the given market
IERC5095 pt = IERC5095(IMarketPlace(marketPlace).token(u, m, 0));
// Make sure the market has matured
uint256 maturity = pt.maturity();
if (block.timestamp < maturity) {
revert Exception(7, maturity, 0, address(0), address(0));
}
// Retrieve the underlying
IERC20 uToken = IERC20(u);
// Sum up the fees received by the caller
uint256 incentiveFee;
// Get the number of owners to loop through
uint256 length = f.length;
// Loop through the provided arrays and mature each individual position
for (uint256 i; i != length; ) {
// Fetch the allowance set by the holder of the principal tokens
uint256 allowance = uToken.allowance(f[i], address(this));
// Get the amount of tokens held by the owner
uint256 amount = pt.balanceOf(f[i]);
// Calculate how many tokens the user should receive
uint256 redeemed = (amount * holdings[u][m]) / pt.totalSupply();
// Calculate the fees to be received (currently .025%)
uint256 fee = redeemed / feenominator;
// Verify allowance
if (allowance < amount) {
revert Exception(20, allowance, amount, address(0), address(0));
}
// Burn the tokens from the user
pt.authBurn(f[i], amount);
// Update the holdings for this market
holdings[u][m] = holdings[u][m] - redeemed;
// Transfer the underlying to the user
Safe.transfer(uToken, f[i], redeemed - fee);
unchecked {
// Track the fees gained by the caller
incentiveFee += fee;
++i;
}
}
// Transfer the fee to the caller
Safe.transfer(uToken, msg.sender, incentiveFee);
return incentiveFee;
}
/// @notice Execute the business logic for conducting an APWine redemption
function apwineWithdraw(
address p,
address u,
uint256 a
) internal {
// Retrieve the vault which executes the redemption in APWine
address futureVault = IAPWineToken(p).futureVault();
// Retrieve the controller that will execute the withdrawal
address controller = IAPWineFutureVault(futureVault)
.getControllerAddress();
// Retrieve the next period index
uint256 index = IAPWineFutureVault(futureVault).getCurrentPeriodIndex();
// Get the FYT address for the current period
address fyt = IAPWineFutureVault(futureVault).getFYTofPeriod(index);
// Trigger claim to FYTs by executing transfer
// Safe.transferFrom(IERC20(fyt), address(lender), address(this), a);
ILender(lender).transferFYTs(fyt, a);
// Redeem the underlying token from APWine to Illuminate
IAPWineController(controller).withdraw(futureVault, a);
// Retrieve the interest bearing token
address ibt = IAPWineFutureVault(futureVault).getIBTAddress();
// Convert the interest bearing token to underlying
IConverter(converter).convert(
IAPWineFutureVault(futureVault).getIBTAddress(),
u,
IERC20(ibt).balanceOf(address(this))
);
}
}