Add error handling for GMX positions

This commit is contained in:
2025-10-21 17:56:09 +07:00
parent af08462e59
commit 6ffe9ae9c4
4 changed files with 36 additions and 5 deletions

View File

@@ -32,7 +32,7 @@ namespace Managing.Application.Trading.Commands
if (amountToTrade <= Constants.GMX.Config.MinimumPositionAmount)
{
throw new ArgumentException("Bot trading balance must be greater than zero", nameof(amountToTrade));
throw new ArgumentException("Bot trading balance must be greater than : 5usdc", nameof(amountToTrade));
}
AmountToTrade = amountToTrade;

View File

@@ -148,9 +148,12 @@ export async function callContract(
const txnInstance = { ...txnOpts };
// Get gas limit
const gasLimit = opts.gasLimit
? BigInt(opts.gasLimit)
: await getGasLimit(
let gasLimit: bigint;
if (opts.gasLimit) {
gasLimit = BigInt(opts.gasLimit);
} else {
try {
gasLimit = await getGasLimit(
client,
sdk.config.account as Address,
contractAddress,
@@ -159,6 +162,21 @@ export async function callContract(
params,
opts.value !== undefined ? BigInt(opts.value) : undefined
);
} catch (gasEstimateError: any) {
// Check if the error is related to allowance (common with stale RPC state)
const errorMessage = gasEstimateError?.message?.toLowerCase() || '';
if (errorMessage.includes('allowance') || errorMessage.includes('erc20')) {
console.warn('⚠️ Gas estimation failed due to potential stale RPC state, using fallback gas limit');
console.warn('Gas estimate error:', errorMessage);
// Use a conservative fallback gas limit for GMX multicall transactions
// GMX orders typically use 2-5M gas depending on complexity
gasLimit = 5000000n; // 5M gas limit as fallback
} else {
// Re-throw if it's a different error
throw gasEstimateError;
}
}
}
txnInstance.gas = gasLimit;
// Get gas price

View File

@@ -748,7 +748,7 @@ export const openGmxPositionImpl = async (
marketAddress: marketInfo.marketTokenAddress,
payTokenAddress: collateralToken.address,
collateralTokenAddress: collateralToken.address,
allowedSlippageBps: 50, // 0.5% slippage
allowedSlippageBps: 55, // 0.55% slippage
leverage: leverageBps,
skipSimulation: true,
referralCodeForTxn: encodeReferralCode("kaigen_ai"),

View File

@@ -413,6 +413,7 @@ export async function signPrivyMessage(
* @param spenderAddress The contract address to approve
* @param chainId The chain ID
* @param amount The amount to approve (optional, defaults to max amount)
* @param waitForConfirmation Whether to wait for the transaction to be confirmed on-chain (default: true)
* @returns The transaction hash
*/
export const approveContractImpl = async (
@@ -421,6 +422,7 @@ export const approveContractImpl = async (
spenderAddress: string,
chainId?: number,
amount?: bigint,
waitForConfirmation: boolean = true
): Promise<string> => {
try {
// Create contract interface for ERC20 token
@@ -450,6 +452,17 @@ export const approveContractImpl = async (
},
} as any);
// Wait for the approval transaction to be confirmed to avoid race conditions
if (waitForConfirmation) {
console.log(`⏳ Waiting for approval transaction to be confirmed: ${hash}`);
const sdk = await getClientForAddress(walletAddress);
const receipt = await sdk.publicClient.waitForTransactionReceipt({
hash: hash as `0x${string}`,
confirmations: 1 // Wait for at least 1 confirmation
});
console.log(`✅ Approval transaction confirmed in block ${receipt.blockNumber}`);
}
return hash;
} catch (error) {
console.error('Error approving contract:', error);