Skip to content

Commit

Permalink
🚑️ debt-previewer: fix different reward lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
santichez authored and cruzdanilo committed Jul 26, 2023
1 parent 6d2abe1 commit b6fd0a7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-pants-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/protocol": patch
---

🚑️ debt-previewer: fix different reward lengths
12 changes: 6 additions & 6 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ DebtManagerTest:testPermitAndDeleverage() (gas: 836428)
DebtManagerTest:testPermitAndLeverage() (gas: 1454722)
DebtManagerTest:testPermitAndRollFloatingToFixed() (gas: 884049)
DebtManagerTest:testPermitAndTransferLeverage() (gas: 778529)
DebtPreviewerTest:testDeleverageRatesCrossAsset() (gas: 1457689)
DebtPreviewerTest:testDeleverageRatesCrossAssetWithWithdraw() (gas: 1457668)
DebtPreviewerTest:testDeleverageRatesSameAsset() (gas: 945044)
DebtPreviewerTest:testLeverageRatesCrossAsset() (gas: 806098)
DebtPreviewerTest:testLeverageRatesCrossAssetAlreadyLeveraged() (gas: 1834563)
DebtPreviewerTest:testLeverageRatesSameAsset() (gas: 509952)
DebtPreviewerTest:testDeleverageRatesCrossAsset() (gas: 1459925)
DebtPreviewerTest:testDeleverageRatesCrossAssetWithWithdraw() (gas: 1459904)
DebtPreviewerTest:testDeleverageRatesSameAsset() (gas: 946157)
DebtPreviewerTest:testLeverageRatesCrossAsset() (gas: 808333)
DebtPreviewerTest:testLeverageRatesCrossAssetAlreadyLeveraged() (gas: 1836799)
DebtPreviewerTest:testLeverageRatesSameAsset() (gas: 511065)
DebtPreviewerTest:testMaxRatioLowerThanOne() (gas: 1614697)
DebtPreviewerTest:testMaxRatioWithNewDeposit() (gas: 1721869)
DebtPreviewerTest:testMinDepositCrossAsset() (gas: 1436904)
Expand Down
28 changes: 19 additions & 9 deletions contracts/periphery/DebtPreviewer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,25 @@ contract DebtPreviewer is Initializable {
r.deltaTime = 1 hours;
r.rewardList = r.controller.allRewards();
rewards = new RewardRate[](r.rewardList.length);
for (r.i = 0; r.i < r.rewardList.length; ) {
r.config = r.controller.rewardConfig(market, r.rewardList[r.i]);
(r.borrowIndex, r.depositIndex, ) = r.controller.rewardIndexes(market, r.rewardList[r.i]);
{
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,
r.rewardList[r.i],
rewards[r.i].asset,
block.timestamp > r.config.start ? r.deltaTime : 0
);
(r.start, , ) = r.controller.distributionTime(market, r.rewardList[r.i]);
r.firstMaturity = r.start - (r.start % FixedLib.INTERVAL) + FixedLib.INTERVAL;
r.maxMaturity =
block.timestamp -
Expand All @@ -581,9 +591,9 @@ contract DebtPreviewer is Initializable {
}
}
rewards[r.i] = RewardRate({
asset: address(r.rewardList[r.i]),
assetName: r.rewardList[r.i].name(),
assetSymbol: r.rewardList[r.i].symbol(),
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)
Expand Down Expand Up @@ -684,7 +694,7 @@ struct Rates {
}

struct RewardRate {
address asset;
ERC20 asset;
string assetName;
string assetSymbol;
uint256 borrow;
Expand Down
20 changes: 10 additions & 10 deletions test/solidity/DebtPreviewer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,8 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.borrow, 71562762755180610);
assertEq(rates.native, 0);
assertEq(rates.rewards.length, 2);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[1].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[1].asset), 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[1].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
Expand Down Expand Up @@ -1076,8 +1076,8 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.borrow, 53668550133510202);
assertEq(rates.native, 0);
assertEq(rates.rewards.length, 2);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[1].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[1].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[1].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
Expand All @@ -1104,7 +1104,7 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.borrow, 50832100630765815);
assertEq(rates.native, 0);
assertEq(rates.rewards.length, 1);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
assertEq(rates.rewards[0].borrow, 26229302711998560);
Expand All @@ -1121,8 +1121,8 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.native, 0);

assertEq(rates.rewards.length, 2);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[1].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[1].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[1].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
Expand Down Expand Up @@ -1151,8 +1151,8 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.native, 0);

assertEq(rates.rewards.length, 2);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[1].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[1].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[1].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
Expand All @@ -1173,7 +1173,7 @@ contract DebtPreviewerTest is ForkTest {
assertEq(rates.native, 0);

assertEq(rates.rewards.length, 1);
assertEq(rates.rewards[0].asset, 0x4200000000000000000000000000000000000042);
assertEq(address(rates.rewards[0].asset), 0x4200000000000000000000000000000000000042);
assertEq(rates.rewards[0].assetSymbol, "OP");
assertEq(rates.rewards[0].assetName, "Optimism");
assertEq(rates.rewards[0].borrow, 17475604202887560);
Expand Down

0 comments on commit b6fd0a7

Please sign in to comment.