Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix fee calculations and swap prices #668

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading