skip eth approve
This commit is contained in:
@@ -1770,40 +1770,45 @@ export const swapGmxTokensImpl = async (
|
|||||||
// Check and handle token allowance for ExchangeRouter contract
|
// Check and handle token allowance for ExchangeRouter contract
|
||||||
const syntheticsRouterRouterAddress = getContract(sdk.chainId, "SyntheticsRouter");
|
const syntheticsRouterRouterAddress = getContract(sdk.chainId, "SyntheticsRouter");
|
||||||
|
|
||||||
try {
|
// If ticker ETH no need to check allowance
|
||||||
const currentAllowance = await getTokenAllowance(
|
if (fromTicker === "ETH") {
|
||||||
sdk.account,
|
console.log(`✅ ETH no need to check allowance`);
|
||||||
fromTokenData.address,
|
}else {
|
||||||
syntheticsRouterRouterAddress
|
try {
|
||||||
);
|
const currentAllowance = await getTokenAllowance(
|
||||||
|
|
||||||
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,
|
sdk.account,
|
||||||
fromTokenData.address,
|
fromTokenData.address,
|
||||||
syntheticsRouterRouterAddress,
|
syntheticsRouterRouterAddress
|
||||||
sdk.chainId,
|
|
||||||
approvalAmount
|
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
console.log(`Current allowance for ${fromTicker}:`, currentAllowance.toString());
|
||||||
console.log(`📝 Approved ${approvalAmount.toString()} ${fromTicker} for ExchangeRouter`);
|
console.log(`Required amount:`, fromTokenAmount.toString());
|
||||||
} else {
|
|
||||||
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
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
|
// Calculate trigger price for limit orders
|
||||||
|
|||||||
Reference in New Issue
Block a user