/
DebtPreviewer.sol
645 lines (597 loc) · 25.2 KB
/
DebtPreviewer.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.17;
import { FixedPointMathLib } from "solmate/src/utils/FixedPointMathLib.sol";
import { MathUpgradeable as Math } from "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol";
import { Previewer, IPriceFeed, FixedLib } from "./Previewer.sol";
import { ERC20, Market, Auditor, DebtManager } from "./DebtManager.sol";
/// @title DebtPreviewer
/// @notice Contract to be consumed by Exactly's front-end dApp as a helper for `DebtManager`.
contract DebtPreviewer {
using FixedPointMathLib for uint256;
/// @notice DebtManager contract to be used to get Auditor and BalancerVault addresses.
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
DebtManager public immutable debtManager;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor(DebtManager debtManager_) {
debtManager = debtManager_;
}
/// @notice Returns extended data useful to leverage or deleverage an account principal position.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account operating with the `DebtManager`.
/// @param minHealthFactor The minimum health factor that the account must have after the leverage.
/// @return extended leverage data.
function leverage(
Market marketDeposit,
Market marketBorrow,
address account,
uint256 minHealthFactor
) external view returns (Leverage memory) {
assert(marketDeposit == marketBorrow);
(, , uint256 floatingBorrowShares) = marketBorrow.accounts(account);
uint256 deposit = marketDeposit.maxWithdraw(account);
uint256 memMinDeposit = minDeposit(marketDeposit, marketBorrow, account, minHealthFactor);
int256 principal = crossPrincipal(marketDeposit, marketBorrow, account);
uint256 ratio = principal > 0 ? deposit.divWadDown(uint256(principal)) : 0;
return
Leverage({
borrow: marketBorrow.previewRefund(floatingBorrowShares),
deposit: deposit,
principal: principal,
ratio: ratio,
maxRatio: maxRatio(marketDeposit, marketBorrow, account, principal, minHealthFactor),
minDeposit: deposit >= memMinDeposit ? 0 : memMinDeposit - deposit,
maxWithdraw: maxWithdraw(marketDeposit, marketBorrow, account, ratio, minHealthFactor),
availableAssets: balancerAvailableLiquidity()
});
}
/// @notice Returns minimum deposit based on account's current debt and a given health factor.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account operating with the markets.
/// @param minHealthFactor The health factor that the account must have with the minimum deposit, isolated.
function minDeposit(
Market marketDeposit,
Market marketBorrow,
address account,
uint256 minHealthFactor
) internal view returns (uint256) {
MinDepositVars memory vars;
Auditor auditor = debtManager.auditor();
(vars.adjustFactorIn, vars.decimalsIn, , , vars.priceFeedIn) = auditor.markets(marketDeposit);
(vars.adjustFactorOut, vars.decimalsOut, , , vars.priceFeedOut) = auditor.markets(marketBorrow);
return
minHealthFactor
.mulWadDown(floatingBorrowAssets(marketBorrow, account))
.mulDivDown(auditor.assetPrice(vars.priceFeedOut), 10 ** vars.decimalsOut)
.divWadDown(vars.adjustFactorOut.mulWadDown(vars.adjustFactorIn))
.mulDivUp(10 ** vars.decimalsIn, auditor.assetPrice(vars.priceFeedIn));
}
/// @notice Returns the maximum ratio that an account can leverage its principal plus `assets` amount.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account that will be leveraged.
/// @param deposit The amount of assets that will be added to the principal.
/// @param ratio The ratio to be previewed.
/// @param minHealthFactor The minimum health factor that the account must have after the leverage.
function previewLeverage(
Market marketDeposit,
Market marketBorrow,
address account,
uint256 deposit,
uint256 ratio,
uint256 minHealthFactor
) external view returns (Limit memory limit) {
assert(marketDeposit == marketBorrow);
uint256 currentRatio;
(limit.principal, currentRatio, limit.maxRatio) = previewRatio(
marketDeposit,
marketBorrow,
account,
int256(deposit),
minHealthFactor
);
limit.ratio = (ratio < currentRatio || ratio > limit.maxRatio) ? currentRatio : ratio;
if (limit.principal <= 0) {
limit.borrow = floatingBorrowAssets(marketBorrow, account);
limit.deposit = marketDeposit.maxWithdraw(account) + deposit;
return limit;
}
limit.deposit = uint256(limit.principal).mulWadUp(limit.ratio);
limit.maxWithdraw = maxWithdraw(marketDeposit, marketBorrow, account, ratio, minHealthFactor);
limit.borrow = uint256(limit.principal).mulWadDown(limit.ratio - 1e18);
}
/// @notice Returns the maximum ratio that an account can deleverage its principal minus `assets` amount.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account that will be deleveraged.
/// @param withdraw The amount of assets that will be withdrawn from the principal.
/// @param ratio The ratio to be previewed.
/// @param minHealthFactor The minimum health factor that the account must have after the leverage.
function previewDeleverage(
Market marketDeposit,
Market marketBorrow,
address account,
uint256 withdraw,
uint256 ratio,
uint256 minHealthFactor
) external view returns (Limit memory limit) {
assert(marketDeposit == marketBorrow);
if ((limit.principal = crossPrincipal(marketDeposit, marketBorrow, account)) < 0) revert InvalidPreview();
uint256 memMaxWithdraw = maxWithdraw(marketDeposit, marketBorrow, account, ratio, minHealthFactor);
if (withdraw <= uint256(limit.principal)) {
limit.principal -= int256(withdraw);
limit.maxRatio = maxRatio(marketDeposit, marketBorrow, account, limit.principal, minHealthFactor);
} else if (withdraw <= memMaxWithdraw) {
limit.principal = int256(memMaxWithdraw - withdraw);
limit.maxRatio = limit.principal > 0
? maxRatio(marketDeposit, marketBorrow, account, limit.principal, minHealthFactor)
: 1e18;
} else revert InvalidPreview();
limit.ratio = ratio > limit.maxRatio ? limit.maxRatio : ratio;
limit.maxWithdraw = memMaxWithdraw;
uint256 borrowRepay = floatingBorrowAssets(marketBorrow, account) -
previewAssetsOut(marketDeposit, marketBorrow, uint256(limit.principal).mulWadDown(limit.ratio - 1e18));
limit.borrow = floatingBorrowAssets(marketBorrow, account) - borrowRepay;
limit.deposit = marketDeposit.maxWithdraw(account) - withdraw - borrowRepay;
}
/// @notice Returns principal, current ratio and max ratio, considering assets to add or substract.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account to preview the ratio.
/// @param assets The amount of assets that will be added or subtracted to the principal.
/// @param minHealthFactor The minimum health factor that the account should have with the max ratio.
function previewRatio(
Market marketDeposit,
Market marketBorrow,
address account,
int256 assets,
uint256 minHealthFactor
) internal view returns (int256 principal, uint256 current, uint256 max) {
principal = crossPrincipal(marketDeposit, marketBorrow, account) + assets;
max = maxRatio(marketDeposit, marketBorrow, account, principal, minHealthFactor);
if (principal > 0) {
current = uint256(int256(marketDeposit.maxWithdraw(account)) + assets).divWadUp(uint256(principal));
}
}
/// @notice Returns the amount of `marketBorrow` underlying assets considering `amountIn` and assets oracle prices.
/// @param marketDeposit The market of the assets accounted as `amountIn`.
/// @param marketBorrow The market of the assets that will be returned.
/// @param amountIn The amount of `marketDeposit` underlying assets.
function previewAssetsOut(
Market marketDeposit,
Market marketBorrow,
uint256 amountIn
) internal view returns (uint256) {
Auditor auditor = debtManager.auditor();
(, uint256 decimalsOut, , , IPriceFeed priceFeedOut) = auditor.markets(marketBorrow);
(, uint256 decimalsIn, , , IPriceFeed priceFeedIn) = auditor.markets(marketDeposit);
return
amountIn.mulDivDown(auditor.assetPrice(priceFeedIn), 10 ** decimalsIn).mulDivDown(
10 ** decimalsOut,
auditor.assetPrice(priceFeedOut)
);
}
/// @notice Returns the maximum ratio that an account can leverage its principal position.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account that will be leveraged.
/// @param principal The updated principal of the account.
/// @param minHealthFactor The minimum health factor that the account must have after the leverage.
function maxRatio(
Market marketDeposit,
Market marketBorrow,
address account,
int256 principal,
uint256 minHealthFactor
) internal view returns (uint256) {
Auditor auditor = debtManager.auditor();
MaxRatioVars memory mr;
(mr.adjustFactorIn, , , , mr.priceFeedIn) = auditor.markets(marketDeposit);
(mr.adjustFactorOut, , , , ) = auditor.markets(marketBorrow);
uint256 isolatedMaxRatio = minHealthFactor.divWadDown(
minHealthFactor - mr.adjustFactorIn.mulWadDown(mr.adjustFactorOut)
);
if (principal <= 0) return isolatedMaxRatio;
mr.marketMap = auditor.accountMarkets(account);
mr.principalUSD = uint256(principal).mulDivDown(auditor.assetPrice(mr.priceFeedIn), 10 ** marketDeposit.decimals());
for (mr.i = 0; mr.marketMap != 0; mr.marketMap >>= 1) {
if (mr.marketMap & 1 != 0) {
Auditor.MarketData memory md;
Auditor.AccountLiquidity memory vars;
mr.market = auditor.marketList(mr.i);
(md.adjustFactor, md.decimals, , , md.priceFeed) = auditor.markets(mr.market);
(vars.balance, vars.borrowBalance) = mr.market.accountSnapshot(account);
vars.price = auditor.assetPrice(md.priceFeed);
mr.baseUnit = 10 ** md.decimals;
if (mr.market == marketBorrow) {
mr.adjustedDebt += (vars.borrowBalance - floatingBorrowAssets(marketBorrow, account))
.mulDivDown(vars.price, mr.baseUnit)
.divWadUp(md.adjustFactor);
} else {
mr.adjustedDebt += vars.borrowBalance.mulDivUp(vars.price, mr.baseUnit).divWadUp(md.adjustFactor);
}
if (mr.market != marketDeposit) {
mr.adjustedCollateral += vars.balance.mulDivDown(vars.price, mr.baseUnit).mulWadDown(md.adjustFactor);
}
}
unchecked {
++mr.i;
}
}
return
Math.min(
(mr.adjustedCollateral.mulWadDown(mr.adjustFactorOut) +
minHealthFactor.mulWadDown(mr.principalUSD) -
minHealthFactor.mulWadDown(mr.adjustedDebt.mulWadDown(mr.adjustFactorOut))).divWadDown(
mr.principalUSD.mulWadDown(minHealthFactor - mr.adjustFactorIn.mulWadDown(mr.adjustFactorOut))
),
isolatedMaxRatio
);
}
function floatingBorrowAssets(Market market, address account) internal view returns (uint256) {
(, , uint256 floatingBorrowShares) = market.accounts(account);
return market.previewRefund(floatingBorrowShares);
}
/// @notice Returns the maximum amount that an account can withdraw from `marketDeposit` when leveraged.
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account to preview.
/// @param ratio The ratio that the account is willing to deleverage to be able to withdraw more assets.
/// @param minHealthFactor The minimum health factor that the account should have when withdrawing maxWithdraw.
function maxWithdraw(
Market marketDeposit,
Market marketBorrow,
address account,
uint256 ratio,
uint256 minHealthFactor
) internal view returns (uint256) {
MaxWithdrawVars memory mw;
mw.principal = crossPrincipal(marketDeposit, marketBorrow, account);
if (mw.principal <= 0) return 0;
mw.auditor = debtManager.auditor();
Auditor.MarketData memory md;
Auditor.AccountLiquidity memory vars;
mw.marketMap = mw.auditor.accountMarkets(account);
mw.borrowAssets = floatingBorrowAssets(marketBorrow, account);
for (mw.i = 0; mw.marketMap != 0; mw.marketMap >>= 1) {
if (mw.marketMap & 1 != 0) {
mw.market = mw.auditor.marketList(mw.i);
(md.adjustFactor, md.decimals, , , md.priceFeed) = mw.auditor.markets(mw.market);
uint256 baseUnit = 10 ** md.decimals;
(vars.balance, vars.borrowBalance) = mw.market.accountSnapshot(account);
vars.price = mw.auditor.assetPrice(md.priceFeed);
{
mw.memAdjColl = vars.balance.mulDivDown(vars.price, baseUnit).mulWadDown(md.adjustFactor);
mw.memAdjDebt = vars.borrowBalance.mulDivDown(vars.price, baseUnit).divWadDown(md.adjustFactor);
mw.adjustedCollateral += mw.memAdjColl;
mw.adjustedDebt += mw.memAdjDebt;
mw.otherDebt += mw.memAdjDebt;
if (mw.market == marketBorrow) {
mw.adjustedRepay = mw.borrowAssets.mulDivDown(vars.price, baseUnit).divWadDown(md.adjustFactor);
mw.otherDebt -= mw.adjustedRepay;
}
if (marketDeposit != mw.market) {
mw.otherCollateral += mw.memAdjColl;
} else {
mw.adjPrincipalForRepay = mw.borrowAssets.mulDivDown(vars.price, baseUnit).mulWadDown(md.adjustFactor);
mw.adjustedPrincipal =
(mw.market.maxWithdraw(account)).mulDivDown(vars.price, baseUnit).mulWadDown(md.adjustFactor) -
mw.adjPrincipalForRepay;
}
}
}
unchecked {
++mw.i;
}
}
{
(mw.adjustFactorIn, , , , mw.priceFeedIn) = mw.auditor.markets(marketDeposit);
(mw.adjustFactorOut, , , , ) = mw.auditor.markets(marketBorrow);
mw.memOtherDebt = mw.otherDebt.mulWadDown(mw.adjustFactorOut).mulWadDown(minHealthFactor);
mw.memOtherCollateral = (mw.otherCollateral).mulWadDown(mw.adjustFactorOut);
}
if (mw.memOtherDebt <= mw.memOtherCollateral) {
return
Math.min(
Math
.min(
mw.adjustedCollateral + mw.adjustedRepay - mw.adjustedDebt - mw.adjPrincipalForRepay,
mw.adjustedPrincipal
)
.mulDivDown(10 ** marketDeposit.decimals(), mw.auditor.assetPrice(mw.priceFeedIn))
.divWadDown(mw.adjustFactorIn),
uint256(mw.principal)
);
}
return
uint256(mw.principal) -
(mw.memOtherDebt - mw.memOtherCollateral)
.divWadDown(
mw.adjustFactorIn.mulWadDown(ratio).mulWadDown(mw.adjustFactorOut) +
minHealthFactor -
ratio.mulWadDown(minHealthFactor)
)
.mulDivDown(10 ** marketDeposit.decimals(), mw.auditor.assetPrice(mw.priceFeedIn));
}
/// @notice Calculates the crossed principal amount for a given `account` in the input and output markets.
/// @param marketDeposit The Market to withdraw the leveraged position.
/// @param marketBorrow The Market to repay the leveraged position.
/// @param account The account that will be deleveraged.
function crossPrincipal(Market marketDeposit, Market marketBorrow, address account) internal view returns (int256) {
uint256 decimalsIn;
uint256 decimalsOut;
IPriceFeed priceFeedIn;
IPriceFeed priceFeedOut;
Auditor auditor = debtManager.auditor();
(, decimalsIn, , , priceFeedIn) = auditor.markets(marketDeposit);
(, decimalsOut, , , priceFeedOut) = auditor.markets(marketBorrow);
return
int256(marketDeposit.maxWithdraw(account)) -
int256(
floatingBorrowAssets(marketBorrow, account)
.mulDivDown(auditor.assetPrice(priceFeedOut), 10 ** decimalsOut)
.mulDivDown(10 ** decimalsIn, auditor.assetPrice(priceFeedIn))
);
}
/// @notice Returns Balancer Vault's available liquidity of each enabled underlying asset.
function balancerAvailableLiquidity() internal view returns (AvailableAsset[] memory availableAssets) {
Auditor auditor = debtManager.auditor();
uint256 marketsCount = auditor.allMarkets().length;
address balancerVault = address(debtManager.balancerVault());
availableAssets = new AvailableAsset[](marketsCount);
for (uint256 i = 0; i < marketsCount; ) {
ERC20 asset = auditor.marketList(i).asset();
availableAssets[i] = AvailableAsset({ asset: asset, liquidity: asset.balanceOf(balancerVault) });
unchecked {
++i;
}
}
}
/// @notice returns rates based on inputs and leverage ratio impact on the borrow market
/// @param marketDeposit The deposit Market.
/// @param marketBorrow The borrow Market.
/// @param account The account to preview.
/// @param assets The amount of assets that should be added or substracted to the principal.
/// @param targetRatio The target ratio to preview.
/// @param depositRate The current deposit rate of the deposit market.
/// @param nativeRate The current native rate of the deposit market.
/// @param nativeRateBorrow The current native rate of the borrow market.
function leverageRates(
Market marketDeposit,
Market marketBorrow,
address account,
int256 assets,
uint256 targetRatio,
uint256 depositRate,
uint256 nativeRate,
uint256 nativeRateBorrow
) external view returns (Rates memory rates) {
RateVars memory vars;
(vars.principal, vars.ratio, ) = previewRatio(marketDeposit, marketBorrow, account, assets, 1e18);
vars.sameMarket = marketDeposit == marketBorrow;
if (vars.principal <= 0) {
vars.utilization = marketBorrow.totalFloatingBorrowAssets().divWadUp(marketBorrow.totalAssets());
} else if (targetRatio < vars.ratio) {
vars.diff = uint256(vars.principal).mulWadDown(vars.ratio - targetRatio);
vars.utilization = (marketBorrow.totalFloatingBorrowAssets() -
previewAssetsOut(marketDeposit, marketBorrow, vars.diff)).divWadUp(
marketBorrow.totalAssets() - (vars.sameMarket ? vars.diff : 0)
);
} else {
vars.diff = uint256(vars.principal).mulWadDown(targetRatio - vars.ratio);
vars.utilization = (marketBorrow.totalFloatingBorrowAssets() +
previewAssetsOut(marketDeposit, marketBorrow, vars.diff)).divWadUp(
marketBorrow.totalAssets() + (vars.sameMarket ? vars.diff : 0)
);
}
rates.borrow = marketBorrow.interestRateModel().floatingRate(vars.utilization).mulWadDown(targetRatio - 1e18);
rates.deposit = depositRate.mulWadDown(targetRatio);
rates.native = int256(nativeRate.mulWadDown(targetRatio)) - int256(nativeRateBorrow.mulWadDown(targetRatio - 1e18));
rates.rewards = calculateRewards(
rewardRates(marketDeposit),
vars.sameMarket ? new RewardRate[](0) : rewardRates(marketBorrow),
vars.sameMarket,
targetRatio
);
}
function calculateRewards(
RewardRate[] memory depositRewards,
RewardRate[] memory borrowRewards,
bool sameMarket,
uint256 targetRatio
) internal pure returns (RewardRate[] memory result) {
result = new RewardRate[](depositRewards.length + borrowRewards.length);
uint256 i;
for (; i < depositRewards.length; ) {
result[i].deposit = depositRewards[i].deposit.mulWadDown(targetRatio);
if (sameMarket) {
result[i].borrow = depositRewards[i].borrow.mulWadDown(targetRatio - 1e18);
}
result[i].asset = depositRewards[i].asset;
result[i].assetName = depositRewards[i].assetName;
result[i].assetSymbol = depositRewards[i].assetSymbol;
unchecked {
++i;
}
}
if (!sameMarket) {
for (i = 0; i < borrowRewards.length; ) {
result[i + depositRewards.length].borrow = borrowRewards[i].borrow.mulWadDown(targetRatio - 1e18);
result[i + depositRewards.length].asset = borrowRewards[i].asset;
result[i + depositRewards.length].assetName = borrowRewards[i].assetName;
result[i + depositRewards.length].assetSymbol = borrowRewards[i].assetSymbol;
unchecked {
++i;
}
}
}
}
function rewardRates(Market market) internal view returns (RewardRate[] memory rewards) {
Previewer.RewardsVars memory r;
r.controller = market.rewardsController();
Auditor auditor = debtManager.auditor();
if (address(r.controller) != address(0)) {
(, r.underlyingDecimals, , , r.underlyingPriceFeed) = auditor.markets(market);
unchecked {
r.underlyingBaseUnit = 10 ** r.underlyingDecimals;
}
r.deltaTime = 1 hours;
r.rewardList = r.controller.allRewards();
rewards = new RewardRate[](r.rewardList.length);
{
uint256 index;
for (r.i = 0; r.i < r.rewardList.length; ++r.i) {
(r.start, , ) = r.controller.distributionTime(market, r.rewardList[r.i]);
if (r.start == 0) continue;
rewards[index++].asset = r.rewardList[r.i];
}
RewardRate[] memory rewardList = rewards;
rewards = new RewardRate[](index);
for (r.i = 0; r.i < rewards.length; ++r.i) rewards[r.i] = rewardList[r.i];
}
for (r.i = 0; r.i < rewards.length; ) {
r.config = r.controller.rewardConfig(market, rewards[r.i].asset);
(r.borrowIndex, r.depositIndex, ) = r.controller.rewardIndexes(market, rewards[r.i].asset);
(r.projectedBorrowIndex, r.projectedDepositIndex, ) = r.controller.previewAllocation(
market,
rewards[r.i].asset,
block.timestamp > r.config.start ? r.deltaTime : 0
);
r.firstMaturity = r.start - (r.start % FixedLib.INTERVAL) + FixedLib.INTERVAL;
r.maxMaturity =
block.timestamp -
(block.timestamp % FixedLib.INTERVAL) +
(FixedLib.INTERVAL * market.maxFuturePools());
r.maturities = new uint256[]((r.maxMaturity - r.firstMaturity) / FixedLib.INTERVAL + 1);
r.start = 0;
for (r.maturity = r.firstMaturity; r.maturity <= r.maxMaturity; ) {
(uint256 borrowed, ) = market.fixedPoolBalance(r.maturity);
r.fixedDebt += borrowed;
r.maturities[r.start] = r.maturity;
unchecked {
r.maturity += FixedLib.INTERVAL;
++r.start;
}
}
rewards[r.i] = RewardRate({
asset: rewards[r.i].asset,
assetName: rewards[r.i].asset.name(),
assetSymbol: rewards[r.i].asset.symbol(),
borrow: (market.totalFloatingBorrowAssets() + r.fixedDebt) > 0
? (r.projectedBorrowIndex - r.borrowIndex)
.mulDivDown(market.totalFloatingBorrowShares() + market.previewRepay(r.fixedDebt), r.underlyingBaseUnit)
.mulWadDown(auditor.assetPrice(r.config.priceFeed))
.mulDivDown(
r.underlyingBaseUnit,
(market.totalFloatingBorrowAssets() + r.fixedDebt).mulWadDown(auditor.assetPrice(r.underlyingPriceFeed))
)
.mulDivDown(365 days, r.deltaTime)
: 0,
deposit: market.totalAssets() > 0
? (r.projectedDepositIndex - r.depositIndex)
.mulDivDown(market.totalSupply(), r.underlyingBaseUnit)
.mulWadDown(auditor.assetPrice(r.config.priceFeed))
.mulDivDown(
r.underlyingBaseUnit,
market.totalAssets().mulWadDown(auditor.assetPrice(r.underlyingPriceFeed))
)
.mulDivDown(365 days, r.deltaTime)
: 0
});
unchecked {
++r.i;
}
}
}
}
}
error InvalidPreview();
struct Leverage {
uint256 ratio;
uint256 borrow;
uint256 deposit;
int256 principal;
uint256 maxRatio;
uint256 minDeposit;
uint256 maxWithdraw;
AvailableAsset[] availableAssets;
}
struct AvailableAsset {
ERC20 asset;
uint256 liquidity;
}
struct Pool {
address tokenA;
address tokenB;
}
struct Limit {
uint256 ratio;
uint256 borrow;
uint256 deposit;
int256 principal;
uint256 maxRatio;
uint256 maxWithdraw;
}
struct MaxRatioVars {
uint256 i;
uint256 baseUnit;
uint256 marketMap;
uint256 principalUSD;
uint256 adjustedDebt;
uint256 adjustFactorIn;
uint256 adjustFactorOut;
uint256 adjustedCollateral;
IPriceFeed priceFeedIn;
Market market;
}
struct MaxWithdrawVars {
uint256 i;
int256 principal;
uint256 marketMap;
uint256 otherDebt;
uint256 memAdjDebt;
uint256 memAdjColl;
uint256 memOtherDebt;
uint256 adjustedDebt;
uint256 borrowAssets;
uint256 adjustedRepay;
uint256 adjustFactorIn;
uint256 adjustFactorOut;
uint256 otherCollateral;
uint256 adjustedPrincipal;
uint256 memOtherCollateral;
uint256 adjustedCollateral;
uint256 adjPrincipalForRepay;
IPriceFeed priceFeedIn;
Auditor auditor;
Market market;
}
struct MinDepositVars {
uint256 decimalsIn;
uint256 decimalsOut;
uint256 adjustFactorIn;
IPriceFeed priceFeedIn;
uint256 adjustFactorOut;
IPriceFeed priceFeedOut;
}
struct Rates {
int256 native;
uint256 borrow;
uint256 deposit;
RewardRate[] rewards;
}
struct RewardRate {
ERC20 asset;
string assetName;
string assetSymbol;
uint256 borrow;
uint256 deposit;
}
struct RateVars {
uint256 diff;
uint256 ratio;
bool sameMarket;
int256 principal;
uint256 utilization;
}