Skip to content

Commit

Permalink
test #getChunkRebalanceNotional
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Sep 18, 2024
1 parent e809196 commit 030134d
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions test/integration/ethereum/morphoLeverageStrategyExtension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ if (process.env.INTEGRATIONTEST) {
.div(totalSharesAdjusted);
};

async function calculateTotalRebalanceNotional(
currentLeverageRatio: BigNumber,
newLeverageRatio: BigNumber,
): Promise<BigNumber> {
const { collateralTotalBalance } = await getBorrowAndCollateralBalances();
const a = currentLeverageRatio.gt(newLeverageRatio)
? currentLeverageRatio.sub(newLeverageRatio)
: newLeverageRatio.sub(currentLeverageRatio);
const b = preciseMul(a, collateralTotalBalance);
return preciseDiv(b, currentLeverageRatio);
}

function calculateMaxBorrowForDeleverV3(
collateralBalance: BigNumber,
collateralPrice: BigNumber,
Expand All @@ -258,7 +270,7 @@ if (process.env.INTEGRATIONTEST) {
const [, borrowShares, collateral] = await morpho.position(marketId, setToken.address);
const collateralTokenBalance = await wsteth.balanceOf(setToken.address);
const collateralTotalBalance = collateralTokenBalance.add(collateral);
const [, , totalBorrowAssets, totalBorrowShares, ,] = await morpho.market(marketId);
const [, , totalBorrowAssets, totalBorrowShares, , ] = await morpho.market(marketId);
const borrowAssets = sharesToAssetsUp(borrowShares, totalBorrowAssets, totalBorrowShares);
return { collateralTotalBalance, borrowAssets };
}
Expand Down Expand Up @@ -4807,9 +4819,9 @@ if (process.env.INTEGRATIONTEST) {
// Set up new rebalance TWAP
const sendQuantity = BigNumber.from(5 * 10 ** 6);
await usdc.transfer(tradeAdapterMock.address, sendQuantity);
const initialCollateralPrice = ether(1).div(initialCollateralPriceInverted);
const newCollateralPrice = initialCollateralPrice.mul(99).div(100);
await usdcEthOrackeMock.setPrice(ether(1).div(newCollateralPrice));
const initialCollateralPrice = ether(1).div(initialCollateralPriceInverted);
const newCollateralPrice = initialCollateralPrice.mul(99).div(100);
await usdcEthOrackeMock.setPrice(ether(1).div(newCollateralPrice));
await increaseTimeAsync(BigNumber.from(100000));
await leverageStrategyExtension.rebalance(exchangeName);
});
Expand Down Expand Up @@ -5051,9 +5063,9 @@ if (process.env.INTEGRATIONTEST) {
// Set up new rebalance TWAP
const sendQuantity = BigNumber.from(5 * 10 ** 6);
await usdc.transfer(tradeAdapterMock.address, sendQuantity);
const initialCollateralPrice = ether(1).div(initialCollateralPriceInverted);
const newCollateralPrice = initialCollateralPrice.mul(99).div(100);
await usdcEthOrackeMock.setPrice(ether(1).div(newCollateralPrice));
const initialCollateralPrice = ether(1).div(initialCollateralPriceInverted);
const newCollateralPrice = initialCollateralPrice.mul(99).div(100);
await usdcEthOrackeMock.setPrice(ether(1).div(newCollateralPrice));
await increaseTimeAsync(BigNumber.from(100000));
await leverageStrategyExtension.rebalance(exchangeName);
});
Expand All @@ -5071,17 +5083,16 @@ if (process.env.INTEGRATIONTEST) {

const newLeverageRatio = methodology.maxLeverageRatio;
const currentLeverageRatio = await leverageStrategyExtension.getCurrentLeverageRatio();
const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const expectedTotalRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);

expect(sellAsset).to.eq(strategy.collateralAsset);
expect(buyAsset).to.eq(strategy.borrowAsset);
expect(chunkRebalances[0]).to.eq(ether(0.002));
expect(chunkRebalances[1]).to.eq(expectedTotalRebalance);
expect(chunkRebalances[1]).to.gte(expectedTotalRebalance.sub(1));
expect(chunkRebalances[1]).to.lte(expectedTotalRebalance.add(1));
});
});

Expand All @@ -5098,17 +5109,17 @@ if (process.env.INTEGRATIONTEST) {

const currentLeverageRatio = await leverageStrategyExtension.getCurrentLeverageRatio();
const newLeverageRatio = await leverageStrategyExtension.twapLeverageRatio();
const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const expectedTotalRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);

expect(sellAsset).to.eq(strategy.collateralAsset);
expect(buyAsset).to.eq(strategy.borrowAsset);
expect(chunkRebalances[0]).to.eq(ether(0.001));
expect(chunkRebalances[1]).to.eq(expectedTotalRebalance);
// TODO: Had to add this rounding error tolerance, understand why
expect(chunkRebalances[1]).to.gte(expectedTotalRebalance.sub(1));
expect(chunkRebalances[1]).to.lte(expectedTotalRebalance.add(1));
});
});
});
Expand All @@ -5134,16 +5145,15 @@ if (process.env.INTEGRATIONTEST) {

const newLeverageRatio = methodology.maxLeverageRatio;
const currentLeverageRatio = await leverageStrategyExtension.getCurrentLeverageRatio();
const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const expectedTotalRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);

expect(sellAsset).to.eq(strategy.collateralAsset);
expect(buyAsset).to.eq(strategy.borrowAsset);
expect(chunkRebalances[0]).to.eq(expectedTotalRebalance);
expect(chunkRebalances[0]).to.lte(expectedTotalRebalance.add(1));
expect(chunkRebalances[0]).to.gte(expectedTotalRebalance.sub(1));
expect(chunkRebalances[1]).to.eq(ether(0.002));
});
});
Expand All @@ -5166,9 +5176,7 @@ if (process.env.INTEGRATIONTEST) {
methodology.maxLeverageRatio,
methodology.recenteringSpeed,
);
const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const expectedTotalRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);
Expand Down Expand Up @@ -5198,16 +5206,15 @@ if (process.env.INTEGRATIONTEST) {
methodology.maxLeverageRatio,
methodology.recenteringSpeed,
);
const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const expectedTotalRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);

expect(sellAsset).to.eq(strategy.collateralAsset);
expect(buyAsset).to.eq(strategy.borrowAsset);
expect(chunkRebalances[0]).to.eq(expectedTotalRebalance);
expect(chunkRebalances[0]).to.gte(expectedTotalRebalance.sub(1));
expect(chunkRebalances[0]).to.lte(expectedTotalRebalance.add(1));
expect(chunkRebalances[1]).to.eq(ether(0.001));
});
});
Expand All @@ -5230,26 +5237,21 @@ if (process.env.INTEGRATIONTEST) {
methodology.maxLeverageRatio,
methodology.recenteringSpeed,
);
const totalCollateralRebalance = await calculateTotalRebalanceNotionalAaveV3(
setToken,
wsteth,
const totalCollateralRebalance = await calculateTotalRebalanceNotional(
currentLeverageRatio,
newLeverageRatio,
);
// Multiply collateral by conversion rate
const currentCollateralPrice = (await chainlinkCollateralPriceMock.latestAnswer()).mul(
10 ** 10,
);
const currentBorrowPrice = (await chainlinkBorrowPriceMock.latestAnswer()).mul(
10 ** 10,
const currentCollateralPrice = (await morphoOracle.price()).div(ether(1));
const expectedTotalRebalance = preciseMul(
totalCollateralRebalance,
currentCollateralPrice,
);
const priceRatio = preciseDiv(currentCollateralPrice, currentBorrowPrice);
const expectedTotalRebalance = preciseMul(totalCollateralRebalance, priceRatio);

expect(sellAsset).to.eq(strategy.borrowAsset);
expect(buyAsset).to.eq(strategy.collateralAsset);
expect(chunkRebalances[0]).to.eq(expectedTotalRebalance);
expect(chunkRebalances[1]).to.eq(preciseMul(ether(0.001), priceRatio));
expect(chunkRebalances[1]).to.eq(preciseMul(ether(0.001), currentCollateralPrice));
});
});
});
Expand Down

0 comments on commit 030134d

Please sign in to comment.