Skip to content

Commit

Permalink
fix: close to collateral logic when fee is collected from from token
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotucks committed Aug 18, 2023
1 parent b22c1c5 commit abc8f1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/dma-common/utils/swap/calculate-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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,
Expand Down

0 comments on commit abc8f1a

Please sign in to comment.