Skip to content

Commit 44133f7

Browse files
committed
v1.5.3
1 parent ee445b1 commit 44133f7

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/functions/fields/getClosePositionActions.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import BigNumber from 'bignumber.js'
12
import { Action, ActionAmount } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
23

34
export const getClosePositionActions = (
45
vault: ActiveVault,
5-
exchangeRate: number,
6+
primaryToSecondaryRate: number,
67
slippage: number,
78
): Action[] => {
89
const swapMessage: Action[] = []
@@ -12,20 +13,33 @@ export const getClosePositionActions = (
1213
Math.max(vault.position.amounts.borrowedPrimary, vault.position.amounts.borrowedSecondary) *
1314
1.001,
1415
)
15-
const secondaryAmount = vault.position.amounts.lp.secondary
16+
const borrowType =
17+
vault.position.amounts.borrowedPrimary > vault.position.amounts.borrowedSecondary
18+
? 'primary'
19+
: 'secondary'
20+
21+
const availableAmountForRepay = vault.position.amounts.lp[borrowType]
22+
23+
if (availableAmountForRepay < borrowAmount) {
24+
const swapTargetAmount = borrowAmount - availableAmountForRepay
25+
const exchangeRate =
26+
borrowType === 'secondary'
27+
? new BigNumber(1).div(primaryToSecondaryRate)
28+
: new BigNumber(primaryToSecondaryRate)
29+
const swapAmount = Math.max(
30+
exchangeRate.times(swapTargetAmount).integerValue(BigNumber.ROUND_CEIL).toNumber(),
31+
10,
32+
)
1633

17-
if (secondaryAmount < borrowAmount) {
18-
const swapTargetAmount = borrowAmount - secondaryAmount
19-
const swapAmount = Math.max(Math.ceil(swapTargetAmount / exchangeRate), 10)
2034
swapMessage.push({
2135
swap_exact_in: {
2236
coin_in: {
2337
amount: {
2438
exact: swapAmount.toString(),
2539
},
26-
denom: vault.denoms.primary,
40+
denom: borrowType === 'secondary' ? vault.denoms.primary : vault.denoms.secondary,
2741
},
28-
denom_out: vault.denoms.secondary,
42+
denom_out: vault.denoms[borrowType],
2943
slippage: slippage.toString(),
3044
},
3145
})

src/hooks/queries/useClosePosition.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export const useClosePosition = (props: Props) => {
1515

1616
const actions = useMemo(() => {
1717
if (!props.activeVault) return []
18-
const exchangeRate = getExchangeRate(
18+
const primaryToSecondaryRate = getExchangeRate(
1919
props.activeVault.denoms.primary,
2020
props.activeVault.denoms.secondary,
2121
)
22-
return getClosePositionActions(props.activeVault, exchangeRate, slippage)
22+
return getClosePositionActions(props.activeVault, primaryToSecondaryRate, slippage)
2323
}, [props.activeVault, getExchangeRate, slippage])
2424

2525
const { data: fee } = useEstimateFarmFee({

0 commit comments

Comments
 (0)