skip eth approve
This commit is contained in:
@@ -1770,40 +1770,45 @@ export const swapGmxTokensImpl = async (
|
||||
// Check and handle token allowance for ExchangeRouter contract
|
||||
const syntheticsRouterRouterAddress = getContract(sdk.chainId, "SyntheticsRouter");
|
||||
|
||||
try {
|
||||
const currentAllowance = await getTokenAllowance(
|
||||
sdk.account,
|
||||
fromTokenData.address,
|
||||
syntheticsRouterRouterAddress
|
||||
);
|
||||
|
||||
console.log(`Current allowance for ${fromTicker}:`, currentAllowance.toString());
|
||||
console.log(`Required amount:`, fromTokenAmount.toString());
|
||||
|
||||
if (currentAllowance < fromTokenAmount) {
|
||||
console.log(`🔧 Insufficient allowance for ${fromTicker}. Auto-approving ExchangeRouter...`);
|
||||
|
||||
// Calculate a reasonable approval amount (use the larger of required amount or 1000 tokens)
|
||||
const tokenUnit = BigInt(Math.pow(10, fromTokenData.decimals));
|
||||
const minApprovalAmount = tokenUnit * 1000n; // 1000 tokens
|
||||
const approvalAmount = fromTokenAmount > minApprovalAmount ? fromTokenAmount : minApprovalAmount;
|
||||
|
||||
const approvalHash = await approveContractImpl(
|
||||
// If ticker ETH no need to check allowance
|
||||
if (fromTicker === "ETH") {
|
||||
console.log(`✅ ETH no need to check allowance`);
|
||||
}else {
|
||||
try {
|
||||
const currentAllowance = await getTokenAllowance(
|
||||
sdk.account,
|
||||
fromTokenData.address,
|
||||
syntheticsRouterRouterAddress,
|
||||
sdk.chainId,
|
||||
approvalAmount
|
||||
syntheticsRouterRouterAddress
|
||||
);
|
||||
|
||||
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
||||
console.log(`📝 Approved ${approvalAmount.toString()} ${fromTicker} for ExchangeRouter`);
|
||||
} else {
|
||||
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
||||
|
||||
console.log(`Current allowance for ${fromTicker}:`, currentAllowance.toString());
|
||||
console.log(`Required amount:`, fromTokenAmount.toString());
|
||||
|
||||
if (currentAllowance < fromTokenAmount) {
|
||||
console.log(`🔧 Insufficient allowance for ${fromTicker}. Auto-approving ExchangeRouter...`);
|
||||
|
||||
// Calculate a reasonable approval amount (use the larger of required amount or 1000 tokens)
|
||||
const tokenUnit = BigInt(Math.pow(10, fromTokenData.decimals));
|
||||
const minApprovalAmount = tokenUnit * 1000n; // 1000 tokens
|
||||
const approvalAmount = fromTokenAmount > minApprovalAmount ? fromTokenAmount : minApprovalAmount;
|
||||
|
||||
const approvalHash = await approveContractImpl(
|
||||
sdk.account,
|
||||
fromTokenData.address,
|
||||
syntheticsRouterRouterAddress,
|
||||
sdk.chainId,
|
||||
approvalAmount
|
||||
);
|
||||
|
||||
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
||||
console.log(`📝 Approved ${approvalAmount.toString()} ${fromTicker} for ExchangeRouter`);
|
||||
} else {
|
||||
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
||||
}
|
||||
} catch (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'}`);
|
||||
}
|
||||
} catch (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'}`);
|
||||
}
|
||||
|
||||
// Calculate trigger price for limit orders
|
||||
|
||||
Reference in New Issue
Block a user