From acea43ec717a21f3be7091ddef4716cc6458ceeb Mon Sep 17 00:00:00 2001 From: cryptooda Date: Fri, 17 Oct 2025 14:45:21 +0700 Subject: [PATCH] Change Insufficient Allowance message to High network fee --- .../Exceptions/CustomExceptions.cs | 13 +++++-- .../src/plugins/custom/gmx.ts | 39 +++++++++++++++++++ .../Trading/TradeChart/TradeChart.tsx | 1 - 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Managing.Core/Exceptions/CustomExceptions.cs b/src/Managing.Core/Exceptions/CustomExceptions.cs index 9062041f..697d406a 100644 --- a/src/Managing.Core/Exceptions/CustomExceptions.cs +++ b/src/Managing.Core/Exceptions/CustomExceptions.cs @@ -109,11 +109,16 @@ public class InsufficientFundsException : Exception "Your wallet doesn't have enough ETH to pay for transaction gas fees.\n" + "Please add ETH to your wallet and try again.", - InsufficientFundsType.InsufficientAllowance => - "❌ Insufficient Token Allowance\n" + - "The trading contract doesn't have permission to spend your tokens.\n" + - "Please approve token spending in your wallet and try again.", + // InsufficientFundsType.InsufficientAllowance => + // "❌ Insufficient Token Allowance\n" + + // "The trading contract doesn't have permission to spend your tokens.\n" + + // "Please approve token spending in your wallet and try again.", + InsufficientFundsType.InsufficientAllowance => + "❌ High Network Fee\n" + + "The gas fee for this position is too high.\n" + + "Position opening rejected.", + InsufficientFundsType.InsufficientBalance => "❌ Insufficient Token Balance\n" + "Your wallet doesn't have enough tokens for this trade.\n" + diff --git a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts index 0c7e05cc..02677f45 100644 --- a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts +++ b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts @@ -770,6 +770,34 @@ export const openGmxPositionImpl = async ( direction: direction }); + // Check and handle token allowance for GMX contracts + console.log('🔐 Checking token allowances for position opening...'); + await approveTokenForContract( + sdk, + "USDC", // Using USDC as collateral + collateralToken, + collateralAmount, + "OrderVault" + ); + + await approveTokenForContract( + sdk, + "USDC", // Using USDC as collateral + collateralToken, + collateralAmount, + "ExchangeRouter" + ); + + await approveTokenForContract( + sdk, + "USDC", // Using USDC as collateral + collateralToken, + collateralAmount, + "SyntheticsRouter" + ); + + console.log('✅ Token allowances verified, proceeding with position opening...'); + // Check gas fees before opening position console.log('⛽ Checking gas fees before opening position...'); const estimatedGasFee = await estimatePositionGasFee(sdk); @@ -881,6 +909,17 @@ export async function openGmxPosition( }; } + // Handle approval-specific errors with better messaging + if (error instanceof Error && error.message.includes('Failed to handle token allowance')) { + reply.status(400); + return { + success: false, + error: error.message, + errorType: 'APPROVAL_FAILED', + suggestion: 'Token approval failed. Please ensure you have sufficient funds and try again, or manually approve the GMX contracts to spend your USDC tokens.' + }; + } + return handleError(this, reply, error, 'gmx/open-position'); } } diff --git a/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx b/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx index ef237ec1..2751b628 100644 --- a/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx +++ b/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx @@ -588,7 +588,6 @@ const TradeChart = ({ paneCount++ } - console.log(indicatorsValues) if (indicatorsValues?.MacdCross != null) { console.log(indicatorsValues.MacdCross) const histogramSeries = chart.current.addHistogramSeries({