Skip to content

Commit

Permalink
Merge pull request #668 from OasisDEX/kk/sc-15055/bug-morpho-blue-ajn…
Browse files Browse the repository at this point in the history
…a-susde-dai-closing-to

fix: fix fee calculations and swap prices
  • Loading branch information
halaprix authored Apr 5, 2024
2 parents a6d80ef + 3da288e commit de4187a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/dma-library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oasisdex/dma-library",
"version": "0.6.50",
"version": "0.6.51",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ async function getAaveSwapDataToCloseToCollateral(
debtPrice: debtTokenPrice,
outstandingDebt: dependencies.currentPosition.debt.amount,
slippage,
// Needs to be WETH for isETH comparison
ETHAddress: addresses.tokens.WETH,
getSwapData: dependencies.getSwapData,
})
}
Expand Down
1 change: 0 additions & 1 deletion packages/dma-library/src/strategies/ajna/multiply/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ async function getAjnaSwapDataToCloseToCollateral(
debtPrice,
slippage: args.slippage,
outstandingDebt,
ETHAddress: dependencies.WETH,
getSwapData: dependencies.getSwapData,
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Address } from '@deploy-configurations/types/address'
import { FEE_BASE, ONE, TEN, ZERO } from '@dma-common/constants'
import { areAddressesEqual } from '@dma-common/utils/addresses'
import { calculateFee } from '@dma-common/utils/swap'
import { SAFETY_MARGIN } from '@dma-library/strategies/aave-like/multiply/close/constants'
import { GetSwapData } from '@dma-library/types/common'
Expand All @@ -22,7 +21,6 @@ interface GetSwapDataToCloseToCollateralArgs {
debtPrice: BigNumber
outstandingDebt: BigNumber
slippage: BigNumber
ETHAddress: Address
getSwapData: GetSwapData
__feeOverride?: BigNumber
}
Expand All @@ -34,7 +32,6 @@ export async function getSwapDataForCloseToCollateral({
debtPrice,
outstandingDebt,
slippage,
ETHAddress,
getSwapData,
__feeOverride,
}: GetSwapDataToCloseToCollateralArgs) {
Expand Down Expand Up @@ -65,7 +62,7 @@ export async function getSwapDataForCloseToCollateral({
colPrice,
collateralTokenPrecision,
_outstandingDebt,
fee,
fee.div(new BigNumber(FEE_BASE).plus(fee)),
slippage,
)

Expand All @@ -74,47 +71,21 @@ export async function getSwapDataForCloseToCollateral({
// there is a deviation threshold value that shows how much the prices on/off chain might differ
// When there is a 1inch swap, we use real-time market price. To calculate that,
// A preflight request is sent to calculate the existing market price.
const debtIsEth = areAddressesEqual(debtToken.address, ETHAddress)
const collateralIsEth = areAddressesEqual(collateralToken.address, ETHAddress)

if (debtIsEth) {
debtPrice = ONE.times(TEN.pow(debtTokenPrecision))
} else {
const debtPricePreflightSwapData = await getSwapData(
debtToken.address,
ETHAddress,
_outstandingDebt,
slippage,
undefined,
true, // inverts swap mock in tests ignored in prod
)
debtPrice = new BigNumber(
debtPricePreflightSwapData.toTokenAmount
.div(debtPricePreflightSwapData.fromTokenAmount)
.times(TEN.pow(debtTokenPrecision))
.toFixed(0),
)
}
debtPrice = ONE.times(TEN.pow(debtTokenPrecision))

if (collateralIsEth) {
colPrice = ONE.times(TEN.pow(collateralTokenPrecision))
} else {
const colPricePreflightSwapData =
!collateralIsEth &&
(await getSwapData(
collateralToken.address,
ETHAddress,
collateralNeeded.integerValue(BigNumber.ROUND_DOWN),
slippage,
))
const colPricePreflightSwapData = await getSwapData(
collateralToken.address,
debtToken.address,
collateralNeeded.integerValue(BigNumber.ROUND_DOWN),
slippage,
)

colPrice = new BigNumber(
colPricePreflightSwapData.toTokenAmount
.div(colPricePreflightSwapData.fromTokenAmount)
.times(TEN.pow(collateralTokenPrecision))
.toFixed(0),
)
}
colPrice = new BigNumber(
colPricePreflightSwapData.toTokenAmount
.div(colPricePreflightSwapData.fromTokenAmount)
.times(TEN.pow(collateralTokenPrecision))
.toFixed(0),
)

// 4. Get Swap Data
// This is the actual swap data that will be used in the transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ async function getMorphoSwapDataToCloseToCollateral(
debtPrice,
slippage: args.slippage,
outstandingDebt,
ETHAddress: dependencies.addresses.ETH,
getSwapData: dependencies.getSwapData,
})
}
Expand Down

0 comments on commit de4187a

Please sign in to comment.