Fix price impact

This commit is contained in:
2025-05-28 01:04:58 +07:00
parent f743436634
commit 4e49f03199
6 changed files with 45 additions and 31 deletions

View File

@@ -6,7 +6,7 @@ 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 {getIncreasePositionAmounts} from "../../utils/trade/amounts.js";
import {getAcceptablePriceInfo, getOrderThresholdType} from "../../utils/prices.js";
import {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js";
import type {GmxSdk} from "../..";
import {createSwapEstimator, getMarketsGraph} from "../../utils/swap/swapRouting.js";
@@ -164,13 +164,24 @@ export async function increaseOrderHelper(
if (params.stopLossPrice) {
const stopLossCollateralDeltaUsd = convertToUsd(increaseAmounts.collateralDeltaAmount, collateralToken.decimals, params.stopLossPrice);
// Use a higher acceptable price impact buffer for stop loss orders to ensure execution
const stopLossAcceptablePriceImpactBps = params.fixedAcceptablePriceImpactBps ??
getDefaultAcceptablePriceImpactBps({
isIncrease: false,
isLong: params.isLong,
indexPrice: params.stopLossPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
priceImpactDeltaUsd: 0n, // We'll calculate this based on current market conditions
acceptablePriceImapctBuffer: params.acceptablePriceImpactBuffer ?? 100, // Default 1% buffer for SL orders
});
const acceptablePriceInfo = getAcceptablePriceInfo({
marketInfo,
isIncrease: false,
isLong: params.isLong,
indexPrice: params.stopLossPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
maxNegativePriceImpactBps: marketInfo.maxPositionImpactFactorForLiquidations,
maxNegativePriceImpactBps: stopLossAcceptablePriceImpactBps,
});
stopLossDecreaseAmounts = {
@@ -241,13 +252,24 @@ export async function increaseOrderHelper(
if (params.takeProfitPrice) {
const takeProfitCollateralDeltaUsd = convertToUsd(increaseAmounts.collateralDeltaAmount, collateralToken.decimals, params.takeProfitPrice);
// Use a higher acceptable price impact buffer for take profit orders to ensure execution
const takeProfitAcceptablePriceImpactBps = params.fixedAcceptablePriceImpactBps ??
getDefaultAcceptablePriceImpactBps({
isIncrease: false,
isLong: params.isLong,
indexPrice: params.takeProfitPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
priceImpactDeltaUsd: 0n, // We'll calculate this based on current market conditions
acceptablePriceImapctBuffer: params.acceptablePriceImpactBuffer ?? 100, // Default 1% buffer for TP orders
});
const acceptablePriceInfo = getAcceptablePriceInfo({
marketInfo,
isIncrease: false,
isLong: params.isLong,
indexPrice: params.takeProfitPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
maxNegativePriceImpactBps: marketInfo.maxPositionImpactFactorForLiquidations,
maxNegativePriceImpactBps: takeProfitAcceptablePriceImpactBps,
});
takeProfitDecreaseAmounts = {

View File

@@ -212,12 +212,13 @@ export const openGmxPositionImpl = async (
marketAddress: marketInfo.marketTokenAddress,
payTokenAddress: collateralToken.address,
collateralTokenAddress: collateralToken.address,
allowedSlippageBps: 50, // 0.5% slippage
allowedSlippageBps: 100, // 0.5% slippage
leverage: leverageBps,
skipSimulation: true,
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined,
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined,
acceptablePriceImpactBuffer: 150,
};
if (direction === TradeDirection.Long) {