Fix collateral price + remove close orders

This commit is contained in:
2025-05-28 12:39:25 +07:00
parent 4e49f03199
commit a31dff3f22
3 changed files with 36 additions and 14 deletions

View File

@@ -4,7 +4,13 @@ import {TokenData, TokensData, TokensRatio} from "../../types/tokens.js";
import {DecreasePositionAmounts, FindSwapPath, SwapAmounts} from "../../types/trade.js";
import {getByKey} from "../../utils/objects.js";
import {createFindSwapPath, findAllSwapPaths, getWrappedAddress} from "../../utils/swap/swapPath.js";
import {convertToUsd, getIsUnwrap, getIsWrap, getTokensRatioByPrice} from "../../utils/tokens.js";
import {
convertToUsd,
getIsEquivalentTokens,
getIsUnwrap,
getIsWrap,
getTokensRatioByPrice
} from "../../utils/tokens.js";
import {getIncreasePositionAmounts} from "../../utils/trade/amounts.js";
import {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js";
@@ -184,6 +190,11 @@ export async function increaseOrderHelper(
maxNegativePriceImpactBps: stopLossAcceptablePriceImpactBps,
});
// Calculate collateral price based on GMX UI logic
const stopLossCollateralPrice = getIsEquivalentTokens(marketInfo.indexToken, collateralToken)
? params.stopLossPrice // Use trigger price if index token equals collateral token
: collateralToken.prices.minPrice; // Otherwise use collateral token's min price
stopLossDecreaseAmounts = {
isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -191,7 +202,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: stopLossCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.stopLossPrice,
collateralPrice: 0n,
collateralPrice: stopLossCollateralPrice,
acceptablePrice: params.isLong ? 0n : ethers.MaxUint256,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps,
recommendedAcceptablePriceDeltaBps: 30n,
@@ -272,6 +283,11 @@ export async function increaseOrderHelper(
maxNegativePriceImpactBps: takeProfitAcceptablePriceImpactBps,
});
// Calculate collateral price based on GMX UI logic
const takeProfitCollateralPrice = getIsEquivalentTokens(marketInfo.indexToken, collateralToken)
? params.takeProfitPrice // Use trigger price if index token equals collateral token
: collateralToken.prices.minPrice; // Otherwise use collateral token's min price
takeProfitDecreaseAmounts = {
isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -279,7 +295,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: takeProfitCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.takeProfitPrice, // Keep original trigger price for indexPrice
collateralPrice: 0n, // Consider if this needs calculation
collateralPrice: takeProfitCollateralPrice,
acceptablePrice: acceptablePriceInfo.acceptablePrice,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps, // Add 0.5% buffer to acceptable price impact
recommendedAcceptablePriceDeltaBps: 30n, // Set recommended buffer to 0.5%