From 030134df3da84b902a3f580e8c094fc5d85310b3 Mon Sep 17 00:00:00 2001 From: ckoopmann Date: Wed, 18 Sep 2024 17:39:18 +0200 Subject: [PATCH] test #getChunkRebalanceNotional --- .../morphoLeverageStrategyExtension.spec.ts | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/test/integration/ethereum/morphoLeverageStrategyExtension.spec.ts b/test/integration/ethereum/morphoLeverageStrategyExtension.spec.ts index e2489f72..4c35fb2b 100644 --- a/test/integration/ethereum/morphoLeverageStrategyExtension.spec.ts +++ b/test/integration/ethereum/morphoLeverageStrategyExtension.spec.ts @@ -236,6 +236,18 @@ if (process.env.INTEGRATIONTEST) { .div(totalSharesAdjusted); }; + async function calculateTotalRebalanceNotional( + currentLeverageRatio: BigNumber, + newLeverageRatio: BigNumber, + ): Promise { + 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, @@ -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 }; } @@ -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); }); @@ -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); }); @@ -5071,9 +5083,7 @@ 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, ); @@ -5081,7 +5091,8 @@ if (process.env.INTEGRATIONTEST) { 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)); }); }); @@ -5098,9 +5109,7 @@ 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, ); @@ -5108,7 +5117,9 @@ if (process.env.INTEGRATIONTEST) { 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)); }); }); }); @@ -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)); }); }); @@ -5166,9 +5176,7 @@ if (process.env.INTEGRATIONTEST) { methodology.maxLeverageRatio, methodology.recenteringSpeed, ); - const expectedTotalRebalance = await calculateTotalRebalanceNotionalAaveV3( - setToken, - wsteth, + const expectedTotalRebalance = await calculateTotalRebalanceNotional( currentLeverageRatio, newLeverageRatio, ); @@ -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)); }); }); @@ -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)); }); }); });