mirror of
https://github.com/cerc-io/mars-interface.git
synced 2025-07-07 00:47:59 +00:00
v1.5.3
This commit is contained in:
parent
ee445b1272
commit
44133f72bd
@ -1,8 +1,9 @@
|
|||||||
|
import BigNumber from 'bignumber.js'
|
||||||
import { Action, ActionAmount } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
import { Action, ActionAmount } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
||||||
|
|
||||||
export const getClosePositionActions = (
|
export const getClosePositionActions = (
|
||||||
vault: ActiveVault,
|
vault: ActiveVault,
|
||||||
exchangeRate: number,
|
primaryToSecondaryRate: number,
|
||||||
slippage: number,
|
slippage: number,
|
||||||
): Action[] => {
|
): Action[] => {
|
||||||
const swapMessage: Action[] = []
|
const swapMessage: Action[] = []
|
||||||
@ -12,20 +13,33 @@ export const getClosePositionActions = (
|
|||||||
Math.max(vault.position.amounts.borrowedPrimary, vault.position.amounts.borrowedSecondary) *
|
Math.max(vault.position.amounts.borrowedPrimary, vault.position.amounts.borrowedSecondary) *
|
||||||
1.001,
|
1.001,
|
||||||
)
|
)
|
||||||
const secondaryAmount = vault.position.amounts.lp.secondary
|
const borrowType =
|
||||||
|
vault.position.amounts.borrowedPrimary > vault.position.amounts.borrowedSecondary
|
||||||
|
? 'primary'
|
||||||
|
: 'secondary'
|
||||||
|
|
||||||
|
const availableAmountForRepay = vault.position.amounts.lp[borrowType]
|
||||||
|
|
||||||
|
if (availableAmountForRepay < borrowAmount) {
|
||||||
|
const swapTargetAmount = borrowAmount - availableAmountForRepay
|
||||||
|
const exchangeRate =
|
||||||
|
borrowType === 'secondary'
|
||||||
|
? new BigNumber(1).div(primaryToSecondaryRate)
|
||||||
|
: new BigNumber(primaryToSecondaryRate)
|
||||||
|
const swapAmount = Math.max(
|
||||||
|
exchangeRate.times(swapTargetAmount).integerValue(BigNumber.ROUND_CEIL).toNumber(),
|
||||||
|
10,
|
||||||
|
)
|
||||||
|
|
||||||
if (secondaryAmount < borrowAmount) {
|
|
||||||
const swapTargetAmount = borrowAmount - secondaryAmount
|
|
||||||
const swapAmount = Math.max(Math.ceil(swapTargetAmount / exchangeRate), 10)
|
|
||||||
swapMessage.push({
|
swapMessage.push({
|
||||||
swap_exact_in: {
|
swap_exact_in: {
|
||||||
coin_in: {
|
coin_in: {
|
||||||
amount: {
|
amount: {
|
||||||
exact: swapAmount.toString(),
|
exact: swapAmount.toString(),
|
||||||
},
|
},
|
||||||
denom: vault.denoms.primary,
|
denom: borrowType === 'secondary' ? vault.denoms.primary : vault.denoms.secondary,
|
||||||
},
|
},
|
||||||
denom_out: vault.denoms.secondary,
|
denom_out: vault.denoms[borrowType],
|
||||||
slippage: slippage.toString(),
|
slippage: slippage.toString(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -15,11 +15,11 @@ export const useClosePosition = (props: Props) => {
|
|||||||
|
|
||||||
const actions = useMemo(() => {
|
const actions = useMemo(() => {
|
||||||
if (!props.activeVault) return []
|
if (!props.activeVault) return []
|
||||||
const exchangeRate = getExchangeRate(
|
const primaryToSecondaryRate = getExchangeRate(
|
||||||
props.activeVault.denoms.primary,
|
props.activeVault.denoms.primary,
|
||||||
props.activeVault.denoms.secondary,
|
props.activeVault.denoms.secondary,
|
||||||
)
|
)
|
||||||
return getClosePositionActions(props.activeVault, exchangeRate, slippage)
|
return getClosePositionActions(props.activeVault, primaryToSecondaryRate, slippage)
|
||||||
}, [props.activeVault, getExchangeRate, slippage])
|
}, [props.activeVault, getExchangeRate, slippage])
|
||||||
|
|
||||||
const { data: fee } = useEstimateFarmFee({
|
const { data: fee } = useEstimateFarmFee({
|
||||||
|
Loading…
Reference in New Issue
Block a user