Skip to content

fix: wrap getContract in try-catch + null guards + user rejection handling (fixes #152)#245

Open
jiangyj545 wants to merge 1 commit into
capofficial:mainfrom
jiangyj545:fix/approveasset-error-handling-152
Open

fix: wrap getContract in try-catch + null guards + user rejection handling (fixes #152)#245
jiangyj545 wants to merge 1 commit into
capofficial:mainfrom
jiangyj545:fix/approveasset-error-handling-152

Conversation

@jiangyj545
Copy link
Copy Markdown

Summary

Fixes #152approveAsset() (and getAllowance()) threw premature errors when contracts failed to resolve (e.g., wrong network, RPC unavailable) before reaching the wallet interaction.

Root Cause

Both functions called getContract() outside the try/catch block. When a contract could not be resolved (wrong chain, network issue), the error propagated as an unhandled rejection instead of a user-friendly toast.

Changes

File: src/api/assets.js

approveAsset():

  • Moved getContract() calls inside try/catch
  • Added separate null checks for asset contract vs spender contract with specific error messages
  • Silent suppression of MetaMask user rejection (ACTION_REJECTED / code 4001)

getAllowance()also fixed, same vulnerability:

  • Wrapped contract resolution in try/catch
  • Added null guard to prevent .address access on undefined
  • Fails silently (allowance keeps last known value) instead of crashing

Diff Stats

+30 / -12 lines across 1 file

Verification

  • npx rollup -c ✅ builds successfully
  • No new dependencies
  • No behavior change for happy path (contracts resolve correctly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant