From 0fe073e30c959fe27fa5b959f6d9ff0e3b664090 Mon Sep 17 00:00:00 2001 From: asdmiaodh <14949942+asdmiaodh@user.noreply.gitee.com> Date: Sun, 7 Jun 2026 14:21:00 +0800 Subject: [PATCH] feat: show liquidation price in margin modal --- src/components/modals/EditMargin.svelte | 49 +++++++++++++++---------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/components/modals/EditMargin.svelte b/src/components/modals/EditMargin.svelte index bca6bf5..3bb8546 100644 --- a/src/components/modals/EditMargin.svelte +++ b/src/components/modals/EditMargin.svelte @@ -7,12 +7,12 @@ import Button from '@components/layout/Button.svelte' import LabelValue from '@components/layout/LabelValue.svelte' - import { ADDRESS_ZERO } from '@lib/config' + import { ADDRESS_ZERO, BPS_DIVIDER } from '@lib/config' import { formatForDisplay } from '@lib/formatters' import { approveAsset, getAllowance } from '@api/assets' import { addMargin, removeMargin } from '@api/positions' import { focusInput, hideModal } from '@lib/ui' - import { allowances, selectedMarketInfo } from '@lib/stores' + import { allowances, marketInfos } from '@lib/stores' export let data; @@ -55,38 +55,43 @@ let funding = data.funding || 0; let newLiqPrice = data.position.liqprice; let newMargin = 0; + function getLiquidationPrice(nextMargin) { + const liqThreshold = ($marketInfos[data.position.market]?.liqThreshold || BPS_DIVIDER) / BPS_DIVIDER; + const basePrice = data.position.price * 1; + const positionSize = data.position.size * 1; + if (!positionSize) return data.position.liqprice; + + let liqPrice; + if (data.position.isLong) { + liqPrice = basePrice - liqThreshold * nextMargin * basePrice / positionSize; + } else { + liqPrice = basePrice + liqThreshold * nextMargin * basePrice / positionSize; + } + return liqPrice < 0 ? 0 : liqPrice; + } + function calculateNewLiquidationPrice(marginDelta, mode) { - if (!marginDelta) marginDelta = 0; + marginDelta = marginDelta * 1 || 0; + const currentMargin = data.position.margin * 1 + funding * 1; if (mode == 'Add') { - newMargin = ((data.position.margin*1 + funding*1) + marginDelta); - - if (data.position.isLong) { - newLiqPrice = data.position.price * 1 - newMargin * data.position.price / data.position.size; - } else { - newLiqPrice = data.position.price * 1 + newMargin * data.position.price / data.position.size; - } - if (newLiqPrice < 0) newLiqPrice = 0; + newMargin = currentMargin + marginDelta; } else { - if (marginDelta >= (data.position.margin*1 + funding*1)) { + if (marginDelta >= currentMargin) { newMargin = 0; - margin = (data.position.margin*1 + funding*1); - } else { - newMargin = ((data.position.margin*1 + funding*1) - marginDelta); - } - if (data.position.isLong) { - newLiqPrice = data.position.price * 1 - ((data.position.margin*1 + funding*1) - marginDelta) * data.position.price / data.position.size; + margin = currentMargin; } else { - newLiqPrice = data.position.price * 1 + ((data.position.margin*1 + funding*1 - marginDelta)) * data.position.price / data.position.size; + newMargin = currentMargin - marginDelta; } } + newLiqPrice = getLiquidationPrice(newMargin); calculateNewLeverage(); } let newLeverage = 0; function calculateNewLeverage() { - newLeverage = data.position.size / newMargin; + newLeverage = newMargin > 0 ? data.position.size / newMargin : 0; } $: calculateNewLiquidationPrice(margin, selected) @@ -151,6 +156,10 @@ +