diff --git a/.DS_Store b/.DS_Store index e9601c7b..bfc6d5bb 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.cursor/.DS_Store b/.cursor/.DS_Store new file mode 100644 index 00000000..f56d4302 Binary files /dev/null and b/.cursor/.DS_Store differ diff --git a/assets/.DS_Store b/assets/.DS_Store new file mode 100644 index 00000000..d194e99a Binary files /dev/null and b/assets/.DS_Store differ diff --git a/scripts/.DS_Store b/scripts/.DS_Store index e52549c7..39ecf65d 100644 Binary files a/scripts/.DS_Store and b/scripts/.DS_Store differ diff --git a/src/.DS_Store b/src/.DS_Store index e50394b8..62c7156f 100644 Binary files a/src/.DS_Store and b/src/.DS_Store differ diff --git a/src/Managing.Web3Proxy/.DS_Store b/src/Managing.Web3Proxy/.DS_Store index c526cd96..58278181 100644 Binary files a/src/Managing.Web3Proxy/.DS_Store and b/src/Managing.Web3Proxy/.DS_Store differ diff --git a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts index 01acb37b..ac199eb6 100644 --- a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts +++ b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts @@ -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, diff --git a/src/Managing.Web3Proxy/test/plugins/open-position.test.ts b/src/Managing.Web3Proxy/test/plugins/open-position.test.ts index 3b335ea2..3b29b688 100644 --- a/src/Managing.Web3Proxy/test/plugins/open-position.test.ts +++ b/src/Managing.Web3Proxy/test/plugins/open-position.test.ts @@ -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 )