From 898f2135d8b62744b6f740270c1db04c37ca7612 Mon Sep 17 00:00:00 2001 From: Wiki Bot Date: Fri, 15 May 2026 17:53:26 +0800 Subject: [PATCH] fix: prevent premature error on approveAsset by moving getContract inside try-catch - Move getContract() calls inside the try-catch block to prevent uncaught errors before wallet interaction (issue #32) - Add null checks for contract resolution failures - Suppress user-rejection errors (MetaMask ACTION_REJECTED / 4001) - Show friendly error when network connection is missing --- src/api/assets.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/api/assets.js b/src/api/assets.js index 6df529d..9a0369d 100644 --- a/src/api/assets.js +++ b/src/api/assets.js @@ -59,10 +59,16 @@ export async function getAllowance(assetLabel, spenderName) { } export async function approveAsset(assetLabel, spenderName) { - const contract = await getContract(assetLabel, true); - const spenderContract = await getContract(spenderName); - const spenderAddress = spenderContract.address; try { + const contract = await getContract(assetLabel, true); + const spenderContract = await getContract(spenderName); + + if (!contract || !spenderContract) { + showError('Contract not found. Please check your network connection.'); + return; + } + + const spenderAddress = spenderContract.address; let tx = await contract.approve(spenderAddress, ethers.constants.MaxUint256); let receipt = await tx.wait(); if (receipt && receipt.status == 1) { @@ -71,6 +77,10 @@ export async function approveAsset(assetLabel, spenderName) { return true; } } catch(e) { + // Suppress user-rejection errors (MetaMask throws on deny) + if (e?.code === 'ACTION_REJECTED' || e?.code === 4001) { + return; + } showError(e); } } \ No newline at end of file