Add error handling for GMX positions
This commit is contained in:
@@ -32,7 +32,7 @@ namespace Managing.Application.Trading.Commands
|
|||||||
|
|
||||||
if (amountToTrade <= Constants.GMX.Config.MinimumPositionAmount)
|
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;
|
AmountToTrade = amountToTrade;
|
||||||
|
|||||||
@@ -148,9 +148,12 @@ export async function callContract(
|
|||||||
const txnInstance = { ...txnOpts };
|
const txnInstance = { ...txnOpts };
|
||||||
|
|
||||||
// Get gas limit
|
// Get gas limit
|
||||||
const gasLimit = opts.gasLimit
|
let gasLimit: bigint;
|
||||||
? BigInt(opts.gasLimit)
|
if (opts.gasLimit) {
|
||||||
: await getGasLimit(
|
gasLimit = BigInt(opts.gasLimit);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
gasLimit = await getGasLimit(
|
||||||
client,
|
client,
|
||||||
sdk.config.account as Address,
|
sdk.config.account as Address,
|
||||||
contractAddress,
|
contractAddress,
|
||||||
@@ -159,6 +162,21 @@ export async function callContract(
|
|||||||
params,
|
params,
|
||||||
opts.value !== undefined ? BigInt(opts.value) : undefined
|
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;
|
txnInstance.gas = gasLimit;
|
||||||
|
|
||||||
// Get gas price
|
// Get gas price
|
||||||
|
|||||||
@@ -748,7 +748,7 @@ export const openGmxPositionImpl = async (
|
|||||||
marketAddress: marketInfo.marketTokenAddress,
|
marketAddress: marketInfo.marketTokenAddress,
|
||||||
payTokenAddress: collateralToken.address,
|
payTokenAddress: collateralToken.address,
|
||||||
collateralTokenAddress: collateralToken.address,
|
collateralTokenAddress: collateralToken.address,
|
||||||
allowedSlippageBps: 50, // 0.5% slippage
|
allowedSlippageBps: 55, // 0.55% slippage
|
||||||
leverage: leverageBps,
|
leverage: leverageBps,
|
||||||
skipSimulation: true,
|
skipSimulation: true,
|
||||||
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
|
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
|
||||||
|
|||||||
@@ -413,6 +413,7 @@ export async function signPrivyMessage(
|
|||||||
* @param spenderAddress The contract address to approve
|
* @param spenderAddress The contract address to approve
|
||||||
* @param chainId The chain ID
|
* @param chainId The chain ID
|
||||||
* @param amount The amount to approve (optional, defaults to max amount)
|
* @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
|
* @returns The transaction hash
|
||||||
*/
|
*/
|
||||||
export const approveContractImpl = async (
|
export const approveContractImpl = async (
|
||||||
@@ -421,6 +422,7 @@ export const approveContractImpl = async (
|
|||||||
spenderAddress: string,
|
spenderAddress: string,
|
||||||
chainId?: number,
|
chainId?: number,
|
||||||
amount?: bigint,
|
amount?: bigint,
|
||||||
|
waitForConfirmation: boolean = true
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
// Create contract interface for ERC20 token
|
// Create contract interface for ERC20 token
|
||||||
@@ -450,6 +452,17 @@ export const approveContractImpl = async (
|
|||||||
},
|
},
|
||||||
} as any);
|
} 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;
|
return hash;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error approving contract:', error);
|
console.error('Error approving contract:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user