Update slippage handling in GMX position management to ensure valid ranges and improve logging. Refactor slippage calculations in openGmxPositionImpl and swapGmxTokensImpl, introducing clamping for slippage percentages and detailed output for swap calculations. Adjust tests to reflect changes in expected parameters for position opening.

This commit is contained in:
2025-12-30 20:14:11 +07:00
parent 21d87efeee
commit d102459b27
8 changed files with 50 additions and 9 deletions

BIN
src/.DS_Store vendored

Binary file not shown.

Binary file not shown.

View File

@@ -856,9 +856,11 @@ export const openGmxPositionImpl = async (
// Use provided slippage or default to 0.5% (50 basis points)
// Convert percentage to basis points (e.g., 0.5% = 50 bps)
const slippageBps = allowedSlippage != null
? Math.floor(allowedSlippage * 100) // Convert percentage to basis points
: 50; // Default 0.5% = 50 basis points
// Ensure slippage is within valid range (0-100%)
const slippagePercentage = allowedSlippage != null
? Math.max(0, Math.min(100, allowedSlippage)) // Clamp between 0-100%
: 0.5; // Default 0.5%
const slippageBps = Math.floor(slippagePercentage * 100); // Convert percentage to basis points
const params: PositionIncreaseParams = {
payAmount: collateralAmount,
@@ -884,7 +886,9 @@ export const openGmxPositionImpl = async (
payTokenAddress: collateralToken.address,
collateralTokenAddress: collateralToken.address,
leverage: leverageBps.toString(),
direction: direction
direction: direction,
allowedSlippagePercentage: slippagePercentage,
allowedSlippageBps: slippageBps
});
// Check and handle token allowance for GMX contracts
@@ -3098,9 +3102,16 @@ export const swapGmxTokensImpl = async (
triggerPrice = BigInt(Math.floor(triggerRatio * Math.pow(10, 30)));
}
// Convert slippage percentage to basis points
const allowedSlippageBps = Math.floor(allowedSlippage * 100); // Convert percentage to basis points
// Ensure slippage is within valid range (0-100%) and convert to basis points
const slippagePercentage = Math.max(0, Math.min(100, allowedSlippage)); // Clamp between 0-100%
const allowedSlippageBps = Math.floor(slippagePercentage * 100); // Convert percentage to basis points
console.log('💱 Swap slippage settings:', {
slippagePercentage: slippagePercentage,
slippageBps: allowedSlippageBps,
fromTicker,
toTicker
});
// Try using the SDK's built-in swap method first
try {
@@ -3116,7 +3127,10 @@ export const swapGmxTokensImpl = async (
swapParams.marketsInfoData = marketsInfoData;
swapParams.tokensData = tokensData;
console.log('🔄 Attempting SDK swap...');
console.log('🔄 Attempting SDK swap with slippage:', {
allowedSlippageBps: allowedSlippageBps,
slippagePercentage: slippagePercentage
});
await sdk.orders.swap(swapParams);
console.log('✅ SDK swap successful!');
} catch (sdkError) {
@@ -3160,13 +3174,40 @@ export const swapGmxTokensImpl = async (
console.log(`💰 Calculated execution fee: ${(Number(executionFee.feeTokenAmount) / 1e18).toFixed(6)} ETH`);
// Calculate minOutputAmount based on current market price and slippage
// Get the current price ratio from token data
const fromTokenPrice = fromTokenData.prices?.minPrice
? Number(fromTokenData.prices.minPrice) / 1e30
: 1;
const toTokenPrice = toTokenData.prices?.minPrice
? Number(toTokenData.prices.minPrice) / 1e30
: 1;
// Calculate expected output amount (simplified - assumes 1:1 price ratio if prices unavailable)
const fromAmountInUsd = (Number(verifiedFromTokenAmount) / Math.pow(10, fromTokenData.decimals)) * fromTokenPrice;
const expectedOutputInUsd = fromAmountInUsd * (toTokenPrice / fromTokenPrice);
const expectedOutputInTokens = expectedOutputInUsd / toTokenPrice;
// Apply slippage tolerance to minimum output
const minOutputAmount = BigInt(Math.floor(
expectedOutputInTokens * Math.pow(10, toTokenData.decimals) * (1 - slippagePercentage / 100)
));
console.log('💱 Swap output calculation:', {
fromAmountUsd: fromAmountInUsd.toFixed(2),
expectedOutputUsd: expectedOutputInUsd.toFixed(2),
expectedOutputTokens: expectedOutputInTokens.toFixed(6),
minOutputAmount: minOutputAmount.toString(),
slippagePercentage: slippagePercentage
});
await createSwapOrderTxn(sdk, {
fromTokenAddress: fromTokenData.address,
fromTokenAmount: verifiedFromTokenAmount,
toTokenAddress: toTokenData.address,
swapPath: swapPath,
orderType: OrderType.MarketSwap,
minOutputAmount: BigInt(Math.floor(amount * Math.pow(10, toTokenData.decimals) * (1 - allowedSlippage / 100))),
minOutputAmount: minOutputAmount,
referralCode: encodeReferralCode("kaigen_ai"),
executionFee: executionFee.feeTokenAmount,
allowedSlippage: allowedSlippageBps,

View File

@@ -13,7 +13,7 @@ test('GMX Position Opening', async (t) => {
TradeDirection.Long,
0.00012, // ~5.3 USDC collateral with 2x leverage (fits available balance of 5.69 USDC)
2,
87856,
87880,
75000,
100000
)