diff --git a/packages/dma-common/utils/swap/calculate-fee.ts b/packages/dma-common/utils/swap/calculate-fee.ts index 41ab06ab3..6b81e659d 100644 --- a/packages/dma-common/utils/swap/calculate-fee.ts +++ b/packages/dma-common/utils/swap/calculate-fee.ts @@ -4,7 +4,7 @@ import BigNumber from 'bignumber.js' export function calculateFee(amountWei: BigNumber, fee: number = DEFAULT_FEE): BigNumber { return amountWei .times(fee) - .div(new BigNumber(fee).plus(FEE_BASE)) + .div(new BigNumber(fee).plus(new BigNumber(FEE_BASE))) .abs() .integerValue(BigNumber.ROUND_DOWN) } diff --git a/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts b/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts index 2777dbc16..c25dfd47f 100644 --- a/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts +++ b/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts @@ -102,20 +102,15 @@ export async function getSwapDataForCloseToCollateral({ // 4. Get Swap Data // This is the actual swap data that will be used in the transaction. + // We're inflating the needed collateral by the fee amount + // This is for when fees is collected on the other end of the swap const amountNeededToEnsureRemainingDebtIsRepaid = calculateNeededCollateralToPaybackDebt( debtPrice, debtTokenPrecision, colPrice, collateralTokenPrecision, outstandingDebt, - fee.div(FEE_BASE), - slippage, - ) - - const swapData = await getSwapData( - collateralToken.address, - debtToken.address, - amountNeededToEnsureRemainingDebtIsRepaid, + fee.div(new BigNumber(FEE_BASE).plus(fee)), slippage, ) @@ -129,6 +124,15 @@ export async function getSwapDataForCloseToCollateral({ ? calculateFee(amountNeededToEnsureRemainingDebtIsRepaid, fee.toNumber()) : ZERO + // 5. Get Swap Data + // The swap amount needs to be the collateral needed minus the preSwapFee + const swapData = await getSwapData( + collateralToken.address, + debtToken.address, + amountNeededToEnsureRemainingDebtIsRepaid.minus(preSwapFee), + slippage, + ) + return { swapData, collectFeeFrom,