|
|
|
@@ -17,14 +17,14 @@ import {PositionIncreaseParams, SwapParams} from '../../generated/gmxsdk/modules
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
basisPointsToFloat,
|
|
|
|
basisPointsToFloat,
|
|
|
|
bigintToNumber,
|
|
|
|
bigintToNumber,
|
|
|
|
|
|
|
|
calculateDisplayDecimals,
|
|
|
|
|
|
|
|
formatUsd,
|
|
|
|
numberToBigint,
|
|
|
|
numberToBigint,
|
|
|
|
PRECISION_DECIMALS
|
|
|
|
PRECISION_DECIMALS
|
|
|
|
} from '../../generated/gmxsdk/utils/numbers.js';
|
|
|
|
} from '../../generated/gmxsdk/utils/numbers.js';
|
|
|
|
import {DecreasePositionSwapType, OrderType, PositionOrderInfo} from '../../generated/gmxsdk/types/orders.js';
|
|
|
|
import {DecreasePositionSwapType, OrderType, PositionOrderInfo} from '../../generated/gmxsdk/types/orders.js';
|
|
|
|
import {DecreasePositionAmounts} from '../../generated/gmxsdk/types/trade.js';
|
|
|
|
import {DecreasePositionAmounts} from '../../generated/gmxsdk/types/trade.js';
|
|
|
|
import {decodeReferralCode, encodeReferralCode} from '../../generated/gmxsdk/utils/referrals.js';
|
|
|
|
import {decodeReferralCode, encodeReferralCode} from '../../generated/gmxsdk/utils/referrals.js';
|
|
|
|
import {formatUsd} from '../../generated/gmxsdk/utils/numbers.js';
|
|
|
|
|
|
|
|
import {calculateDisplayDecimals} from '../../generated/gmxsdk/utils/numbers.js';
|
|
|
|
|
|
|
|
import {handleError} from '../../utils/errorHandler.js';
|
|
|
|
import {handleError} from '../../utils/errorHandler.js';
|
|
|
|
import {Abi, formatEther, parseEther, zeroHash} from 'viem';
|
|
|
|
import {Abi, formatEther, parseEther, zeroHash} from 'viem';
|
|
|
|
import {CLAIMABLE_FUNDING_AMOUNT} from '../../generated/gmxsdk/configs/dataStore.js';
|
|
|
|
import {CLAIMABLE_FUNDING_AMOUNT} from '../../generated/gmxsdk/configs/dataStore.js';
|
|
|
|
@@ -114,8 +114,12 @@ export async function checkGasFeeBalance(
|
|
|
|
const estimatedGasFeeEth = Number(formatEther(estimatedGasFee));
|
|
|
|
const estimatedGasFeeEth = Number(formatEther(estimatedGasFee));
|
|
|
|
const estimatedGasFeeUsd = estimatedGasFeeEth * ethPrice;
|
|
|
|
const estimatedGasFeeUsd = estimatedGasFeeEth * ethPrice;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if gas fee exceeds maximum allowed (1 USDC)
|
|
|
|
// Check if:
|
|
|
|
const hasSufficientBalance = estimatedGasFeeUsd <= MAX_GAS_FEE_USD;
|
|
|
|
// 1. Wallet has enough ETH balance to cover gas
|
|
|
|
|
|
|
|
// 2. Gas fee is under the maximum allowed USD threshold
|
|
|
|
|
|
|
|
const hasEnoughEth = ethBalance >= estimatedGasFee;
|
|
|
|
|
|
|
|
const isUnderMaxFee = estimatedGasFeeUsd <= MAX_GAS_FEE_USD;
|
|
|
|
|
|
|
|
const hasSufficientBalance = hasEnoughEth && isUnderMaxFee;
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`⛽ Gas fee check:`, {
|
|
|
|
console.log(`⛽ Gas fee check:`, {
|
|
|
|
ethBalance: ethBalanceFormatted,
|
|
|
|
ethBalance: ethBalanceFormatted,
|
|
|
|
@@ -123,16 +127,27 @@ export async function checkGasFeeBalance(
|
|
|
|
estimatedGasFeeUsd: estimatedGasFeeUsd.toFixed(2),
|
|
|
|
estimatedGasFeeUsd: estimatedGasFeeUsd.toFixed(2),
|
|
|
|
ethPrice: ethPrice.toFixed(2),
|
|
|
|
ethPrice: ethPrice.toFixed(2),
|
|
|
|
maxAllowedUsd: MAX_GAS_FEE_USD,
|
|
|
|
maxAllowedUsd: MAX_GAS_FEE_USD,
|
|
|
|
|
|
|
|
hasEnoughEth,
|
|
|
|
|
|
|
|
isUnderMaxFee,
|
|
|
|
hasSufficientBalance
|
|
|
|
hasSufficientBalance
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate appropriate error message
|
|
|
|
|
|
|
|
let errorMessage: string | undefined;
|
|
|
|
|
|
|
|
if (!hasSufficientBalance) {
|
|
|
|
|
|
|
|
if (!hasEnoughEth) {
|
|
|
|
|
|
|
|
errorMessage = `Insufficient ETH balance for gas: need ${estimatedGasFeeEth.toFixed(6)} ETH (~$${estimatedGasFeeUsd.toFixed(2)}), but only have ${ethBalanceFormatted} ETH. Please add more ETH to your wallet.`;
|
|
|
|
|
|
|
|
} else if (!isUnderMaxFee) {
|
|
|
|
|
|
|
|
errorMessage = `Gas fee too high: $${estimatedGasFeeUsd.toFixed(2)} exceeds maximum of $${MAX_GAS_FEE_USD}. Please wait for lower gas fees.`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
hasSufficientBalance,
|
|
|
|
hasSufficientBalance,
|
|
|
|
ethBalance: ethBalanceFormatted,
|
|
|
|
ethBalance: ethBalanceFormatted,
|
|
|
|
estimatedGasFeeUsd,
|
|
|
|
estimatedGasFeeUsd,
|
|
|
|
ethPrice,
|
|
|
|
ethPrice,
|
|
|
|
errorMessage: hasSufficientBalance ? undefined :
|
|
|
|
errorMessage
|
|
|
|
`Gas fee too high: $${estimatedGasFeeUsd.toFixed(2)} exceeds maximum of $${MAX_GAS_FEE_USD}. Please wait for lower gas fees or add more ETH.`
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
@@ -505,12 +520,20 @@ async function approveTokenForContract(
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const contractAddress = getContract(sdk.chainId, contractName as ContractName);
|
|
|
|
const contractAddress = getContract(sdk.chainId, contractName as ContractName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:523',message:'Allowance check start',data:{ticker:fromTicker,contractName,contractAddress,tokenAddress:fromTokenData.address,requiredAmount:fromTokenAmount.toString(),account:sdk.account},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'A'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
const currentAllowance = await getTokenAllowance(
|
|
|
|
const currentAllowance = await getTokenAllowance(
|
|
|
|
sdk.account,
|
|
|
|
sdk.account,
|
|
|
|
fromTokenData.address,
|
|
|
|
fromTokenData.address,
|
|
|
|
contractAddress
|
|
|
|
contractAddress
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:530',message:'Allowance check result',data:{ticker:fromTicker,contractName,currentAllowance:currentAllowance.toString(),requiredAmount:fromTokenAmount.toString(),isSufficient:currentAllowance>=fromTokenAmount},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'A'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`Current allowance for ${fromTicker}:`, currentAllowance);
|
|
|
|
console.log(`Current allowance for ${fromTicker}:`, currentAllowance);
|
|
|
|
console.log(`Required amount:`, fromTokenAmount);
|
|
|
|
console.log(`Required amount:`, fromTokenAmount);
|
|
|
|
|
|
|
|
|
|
|
|
@@ -519,20 +542,88 @@ async function approveTokenForContract(
|
|
|
|
|
|
|
|
|
|
|
|
// Approve maximum amount (2^256 - 1) to avoid future approval transactions
|
|
|
|
// Approve maximum amount (2^256 - 1) to avoid future approval transactions
|
|
|
|
const maxApprovalAmount = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
const maxApprovalAmount = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:537',message:'Approval transaction start',data:{ticker:fromTicker,contractName,contractAddress,approvalAmount:maxApprovalAmount.toString()},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'B'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
const approvalHash = await approveContractImpl(
|
|
|
|
const approvalHash = await approveContractImpl(
|
|
|
|
sdk.account,
|
|
|
|
sdk.account,
|
|
|
|
fromTokenData.address,
|
|
|
|
fromTokenData.address,
|
|
|
|
contractAddress,
|
|
|
|
contractAddress,
|
|
|
|
sdk.chainId,
|
|
|
|
sdk.chainId,
|
|
|
|
maxApprovalAmount
|
|
|
|
maxApprovalAmount,
|
|
|
|
|
|
|
|
true // waitForConfirmation = true (already default, but being explicit)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:545',message:'Approval transaction hash received',data:{ticker:fromTicker,contractName,approvalHash},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'B'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
|
|
|
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
|
|
|
console.log(`📝 Approved maximum amount ${fromTicker} for ${contractName}`);
|
|
|
|
console.log(`📝 Approved maximum amount ${fromTicker} for ${contractName}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Contract ${contractName} address: ${contractAddress}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Token ${fromTicker} address: ${fromTokenData.address}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Account: ${sdk.account}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify the approval transaction was actually mined by checking the receipt
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const receipt = await sdk.publicClient.getTransactionReceipt({ hash: approvalHash as `0x${string}` });
|
|
|
|
|
|
|
|
console.log(`✅ Approval transaction confirmed in block ${receipt.blockNumber}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Approval transaction status: ${receipt.status === 'success' ? 'success' : 'failed'}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (receipt.status !== 'success') {
|
|
|
|
|
|
|
|
throw new Error(`Approval transaction failed: ${approvalHash}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (receiptError) {
|
|
|
|
|
|
|
|
console.warn(`⚠️ Could not verify approval transaction receipt: ${receiptError}`);
|
|
|
|
|
|
|
|
// Continue anyway as approveContractImpl should have already waited
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait additional time for state to propagate across RPC nodes
|
|
|
|
|
|
|
|
console.log(`⏳ Waiting 5 seconds for state to propagate across RPC nodes...`);
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify allowance multiple times to ensure state has propagated
|
|
|
|
|
|
|
|
let postApprovalAllowance = await getTokenAllowance(
|
|
|
|
|
|
|
|
sdk.account,
|
|
|
|
|
|
|
|
fromTokenData.address,
|
|
|
|
|
|
|
|
contractAddress
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Post-approval allowance (first read): ${postApprovalAllowance.toString()}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If still insufficient, wait more and retry with fresh RPC call
|
|
|
|
|
|
|
|
if (postApprovalAllowance < fromTokenAmount) {
|
|
|
|
|
|
|
|
console.log(`⏳ Allowance still insufficient, waiting 5 more seconds and retrying...`);
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
|
|
|
|
|
|
postApprovalAllowance = await getTokenAllowance(
|
|
|
|
|
|
|
|
sdk.account,
|
|
|
|
|
|
|
|
fromTokenData.address,
|
|
|
|
|
|
|
|
contractAddress
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Post-approval allowance (second read): ${postApprovalAllowance.toString()}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:575',message:'Post-approval allowance check',data:{ticker:fromTicker,contractName,postApprovalAllowance:postApprovalAllowance.toString(),requiredAmount:fromTokenAmount.toString(),isSufficient:postApprovalAllowance>=fromTokenAmount},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'B'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Final post-approval allowance: ${postApprovalAllowance.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Required amount: ${fromTokenAmount.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Is sufficient: ${postApprovalAllowance >= fromTokenAmount}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (postApprovalAllowance < fromTokenAmount) {
|
|
|
|
|
|
|
|
console.error(`❌ CRITICAL: Approval failed! Allowance ${postApprovalAllowance.toString()} is less than required ${fromTokenAmount.toString()}`);
|
|
|
|
|
|
|
|
throw new Error(`Token approval failed: allowance ${postApprovalAllowance.toString()} is less than required ${fromTokenAmount.toString()}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
|
|
|
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (allowanceError) {
|
|
|
|
} catch (allowanceError) {
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:550',message:'Allowance approval error',data:{ticker:fromTicker,contractName,error:allowanceError instanceof Error ? allowanceError.message : 'Unknown error'},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'E'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
console.warn('Could not check or approve token allowance:', allowanceError);
|
|
|
|
console.warn('Could not check or approve token allowance:', allowanceError);
|
|
|
|
throw new Error(`Failed to handle token allowance: ${allowanceError instanceof Error ? allowanceError.message : 'Unknown error'}`);
|
|
|
|
throw new Error(`Failed to handle token allowance: ${allowanceError instanceof Error ? allowanceError.message : 'Unknown error'}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -736,8 +827,58 @@ export const openGmxPositionImpl = async (
|
|
|
|
console.log(`No price provided, using current market price: ${currentPrice}`);
|
|
|
|
console.log(`No price provided, using current market price: ${currentPrice}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate the collateral amount in USDC (quantity * price)
|
|
|
|
// Calculate the collateral amount in USDC
|
|
|
|
const collateralAmount = BigInt(Math.floor((quantity || 0) * currentPrice * 1e6)); // USDC has 6 decimals
|
|
|
|
// quantity represents position size in BTC
|
|
|
|
|
|
|
|
// With leverage, collateral = (position size in USD) / leverage
|
|
|
|
|
|
|
|
const positionSizeUsd = (quantity || 0) * currentPrice;
|
|
|
|
|
|
|
|
const collateralAmountUsd = positionSizeUsd / (leverage || 1);
|
|
|
|
|
|
|
|
const collateralAmount = BigInt(Math.floor(collateralAmountUsd * 1e6)); // USDC has 6 decimals
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('💰 Collateral calculation:', {
|
|
|
|
|
|
|
|
quantity,
|
|
|
|
|
|
|
|
currentPrice,
|
|
|
|
|
|
|
|
leverage,
|
|
|
|
|
|
|
|
positionSizeUsd,
|
|
|
|
|
|
|
|
collateralAmountUsd,
|
|
|
|
|
|
|
|
collateralAmountRaw: collateralAmountUsd * 1e6,
|
|
|
|
|
|
|
|
collateralAmount: collateralAmount.toString(),
|
|
|
|
|
|
|
|
collateralAmountUSDC: Number(collateralAmount) / 1e6
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check USDC balance before proceeding
|
|
|
|
|
|
|
|
// Use SDK's executeMulticall to get balance (more reliable)
|
|
|
|
|
|
|
|
const balanceResult = await sdk.executeMulticall({
|
|
|
|
|
|
|
|
token: {
|
|
|
|
|
|
|
|
contractAddress: collateralToken.address,
|
|
|
|
|
|
|
|
abiId: "ERC20",
|
|
|
|
|
|
|
|
calls: {
|
|
|
|
|
|
|
|
balanceOf: {
|
|
|
|
|
|
|
|
methodName: "balanceOf",
|
|
|
|
|
|
|
|
params: [sdk.account]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
const usdcBalance = balanceResult.data.token.balanceOf.returnValues[0] as bigint;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('💵 USDC Balance check:', {
|
|
|
|
|
|
|
|
walletAddress: sdk.account,
|
|
|
|
|
|
|
|
usdcBalance: usdcBalance.toString(),
|
|
|
|
|
|
|
|
usdcBalanceFormatted: Number(usdcBalance) / 1e6,
|
|
|
|
|
|
|
|
requiredAmount: collateralAmount.toString(),
|
|
|
|
|
|
|
|
requiredAmountFormatted: Number(collateralAmount) / 1e6,
|
|
|
|
|
|
|
|
hasSufficientBalance: usdcBalance >= collateralAmount
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:835',message:'USDC balance check',data:{usdcBalance:usdcBalance.toString(),requiredAmount:collateralAmount.toString(),hasSufficientBalance:usdcBalance>=collateralAmount,quantity,currentPrice},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'F'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (usdcBalance < collateralAmount) {
|
|
|
|
|
|
|
|
const errorMsg = `Insufficient USDC balance: need ${Number(collateralAmount) / 1e6} USDC, but only have ${Number(usdcBalance) / 1e6} USDC`;
|
|
|
|
|
|
|
|
console.error(`❌ ${errorMsg}`);
|
|
|
|
|
|
|
|
throw new Error(errorMsg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Note: Wallet initialization should be done separately before opening positions
|
|
|
|
// Note: Wallet initialization should be done separately before opening positions
|
|
|
|
// This reduces position opening time and allows for better error handling
|
|
|
|
// This reduces position opening time and allows for better error handling
|
|
|
|
@@ -801,6 +942,54 @@ export const openGmxPositionImpl = async (
|
|
|
|
|
|
|
|
|
|
|
|
console.log('✅ Token allowances verified, proceeding with position opening...');
|
|
|
|
console.log('✅ Token allowances verified, proceeding with position opening...');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
const exchangeRouterAddress = getContract(sdk.chainId, "ExchangeRouter");
|
|
|
|
|
|
|
|
const orderVaultAddress = getContract(sdk.chainId, "OrderVault");
|
|
|
|
|
|
|
|
const syntheticsRouterAddress = getContract(sdk.chainId, "SyntheticsRouter");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: ExchangeRouter address: ${exchangeRouterAddress}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: OrderVault address: ${orderVaultAddress}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: SyntheticsRouter address: ${syntheticsRouterAddress}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: USDC token address: ${collateralToken.address}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Required collateral amount: ${collateralAmount.toString()}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Force a fresh state read by waiting and checking multiple times
|
|
|
|
|
|
|
|
console.log(`⏳ Waiting 3 seconds for state to propagate...`);
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check allowance using getTokenAllowance (which uses multicall, but we'll verify with multiple reads)
|
|
|
|
|
|
|
|
console.log(`🔍 Reading ExchangeRouter allowance...`);
|
|
|
|
|
|
|
|
const finalExchangeRouterAllowance = await getTokenAllowance(sdk.account, collateralToken.address, exchangeRouterAddress);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 Reading OrderVault allowance...`);
|
|
|
|
|
|
|
|
const finalOrderVaultAllowance = await getTokenAllowance(sdk.account, collateralToken.address, orderVaultAddress);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 Reading SyntheticsRouter allowance...`);
|
|
|
|
|
|
|
|
const finalSyntheticsRouterAllowance = await getTokenAllowance(sdk.account, collateralToken.address, syntheticsRouterAddress);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Double-check ExchangeRouter with a second read to catch any caching issues
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
|
const secondExchangeRouterAllowance = await getTokenAllowance(sdk.account, collateralToken.address, exchangeRouterAddress);
|
|
|
|
|
|
|
|
console.log(`🔍 Second ExchangeRouter allowance read: ${secondExchangeRouterAllowance.toString()}`);
|
|
|
|
|
|
|
|
if (finalExchangeRouterAllowance !== secondExchangeRouterAllowance) {
|
|
|
|
|
|
|
|
console.warn(`⚠️ Allowance values differ between reads! First: ${finalExchangeRouterAllowance.toString()}, Second: ${secondExchangeRouterAllowance.toString()}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: ExchangeRouter allowance (direct read): ${finalExchangeRouterAllowance.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: OrderVault allowance (direct read): ${finalOrderVaultAllowance.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: SyntheticsRouter allowance (direct read): ${finalSyntheticsRouterAllowance.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: ExchangeRouter sufficient: ${finalExchangeRouterAllowance >= collateralAmount}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: OrderVault sufficient: ${finalOrderVaultAllowance >= collateralAmount}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:850',message:'Pre-order allowance verification (direct read)',data:{exchangeRouterAddress,orderVaultAddress,syntheticsRouterAddress,exchangeRouterAllowance:finalExchangeRouterAllowance.toString(),orderVaultAllowance:finalOrderVaultAllowance.toString(),syntheticsRouterAllowance:finalSyntheticsRouterAllowance.toString(),requiredAmount:collateralAmount.toString()},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'C'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Double-check ExchangeRouter allowance is sufficient before proceeding
|
|
|
|
|
|
|
|
if (finalExchangeRouterAllowance < collateralAmount) {
|
|
|
|
|
|
|
|
console.error(`❌ CRITICAL: ExchangeRouter allowance ${finalExchangeRouterAllowance.toString()} is insufficient for required ${collateralAmount.toString()}`);
|
|
|
|
|
|
|
|
throw new Error(`Insufficient ExchangeRouter allowance: ${finalExchangeRouterAllowance.toString()} < ${collateralAmount.toString()}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check gas fees before opening position
|
|
|
|
// Check gas fees before opening position
|
|
|
|
console.log('⛽ Checking gas fees before opening position...');
|
|
|
|
console.log('⛽ Checking gas fees before opening position...');
|
|
|
|
const estimatedGasFee = await estimatePositionGasFee(sdk);
|
|
|
|
const estimatedGasFee = await estimatePositionGasFee(sdk);
|
|
|
|
@@ -814,11 +1003,35 @@ export const openGmxPositionImpl = async (
|
|
|
|
|
|
|
|
|
|
|
|
console.log('🚀 Executing position order...');
|
|
|
|
console.log('🚀 Executing position order...');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Final allowance check RIGHT before transaction execution
|
|
|
|
|
|
|
|
// Also check the current block number to ensure we're reading from the latest state
|
|
|
|
|
|
|
|
const currentBlock = await sdk.publicClient.getBlockNumber();
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Current block number: ${currentBlock.toString()}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const finalCheckAllowance = await getTokenAllowance(sdk.account, collateralToken.address, exchangeRouterAddress);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Final allowance check RIGHT before order execution: ${finalCheckAllowance.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Required amount: ${collateralAmount.toString()}`);
|
|
|
|
|
|
|
|
console.log(`🔍 DEBUG: Is sufficient: ${finalCheckAllowance >= collateralAmount}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:850',message:'Final allowance check before execution',data:{blockNumber:currentBlock.toString(),finalAllowance:finalCheckAllowance.toString(),requiredAmount:collateralAmount.toString(),isSufficient:finalCheckAllowance>=collateralAmount},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'E'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (finalCheckAllowance < collateralAmount) {
|
|
|
|
|
|
|
|
console.error(`❌ CRITICAL: ExchangeRouter allowance ${finalCheckAllowance.toString()} is insufficient RIGHT before execution!`);
|
|
|
|
|
|
|
|
throw new Error(`Insufficient ExchangeRouter allowance at execution time: ${finalCheckAllowance.toString()} < ${collateralAmount.toString()}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (direction === TradeDirection.Long) {
|
|
|
|
if (direction === TradeDirection.Long) {
|
|
|
|
await sdk.orders.long(params);
|
|
|
|
await sdk.orders.long(params);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
await sdk.orders.short(params);
|
|
|
|
await sdk.orders.short(params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region agent log
|
|
|
|
|
|
|
|
fetch('http://127.0.0.1:7242/ingest/556ee8d6-75a9-41e6-9311-6e6215b38a77',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'gmx.ts:837',message:'Order execution completed',data:{direction},timestamp:Date.now(),sessionId:'debug-session',runId:'run1',hypothesisId:'D'})}).catch(()=>{});
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
console.log('✅ Position order executed successfully');
|
|
|
|
console.log('✅ Position order executed successfully');
|
|
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
return "";
|
|
|
|
|