Skip to content

chore(card): migrate Card BigInt usage to new library#29679

Merged
Brunonascdev merged 1 commit into
mainfrom
chore/mm-card-bigint-migration
May 5, 2026
Merged

chore(card): migrate Card BigInt usage to new library#29679
Brunonascdev merged 1 commit into
mainfrom
chore/mm-card-bigint-migration

Conversation

@Brunonascdev

@Brunonascdev Brunonascdev commented May 4, 2026

Copy link
Copy Markdown
Contributor

Description

This change migrates Card feature hooks from the legacy BN.js / app/util/number helpers to the native BigInt module at app/util/number/bigint, following the repository bigint migration guide.

Reason: Reduce BN.js usage, align Card code with the bigint utilities, and keep behavior equivalent for balances, delegation amounts, and gas-faucet checks.

What changed:

  • useAssetBalances.tsx — import balanceToFiatNumber from util/number/bigint; test mock updated to match.
  • useCardDelegation.ts — import toTokenMinimalUnit from util/number/bigint; tests mock bigint return values instead of decimal strings.
  • useNeedsGasFaucet.ts — remove bnjs4; use hexToBigInt for wei gas price and balance; compare with native <; multiply with BigInt(gasLimitWithBuffer); cast decGWEIToHexWEI result to string for TypeScript (legacy conversionUtil union type).

Changelog

CHANGELOG entry: null

Related issues

No issue: internal refactor (Card BigInt burndown / migration guide).

Manual testing steps

Feature: Card hooks BigInt migration

  Scenario: Unit tests cover migrated hooks
    Given the branch is checked out with dependencies installed
    When the developer runs `yarn jest app/components/UI/Card/hooks/useAssetBalances.test.ts app/components/UI/Card/hooks/useCardDelegation.test.ts app/components/UI/Card/hooks/useNeedsGasFaucet.test.ts`
    Then all tests pass

  Scenario: Optional smoke on Card delegation flow
    Given a dev build on a test network with Card funding available
    When the user opens Card home and triggers a flow that uses delegation or gas faucet logic
    Then balances and gas-faucet behavior match expectations (no regression vs prior build)

Screenshots/Recordings

Before

N/A

After

N/A

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Medium Risk
Touches Card delegation amount conversion and EVM gas-faucet checks by switching from BN/string math to bigint, which could change edge-case numeric behavior and transaction approval amounts if not equivalent.

Overview
Migrates Card hooks off legacy app/util/number / BN usage onto app/util/number/bigint utilities.

useAssetBalances now uses bigint-based balanceToFiatNumber for fiat calculations, useCardDelegation switches toTokenMinimalUnit to return bigint (with tests updated to expect bigint), and useNeedsGasFaucet removes BN math in favor of hexToBigInt and native bigint arithmetic/comparisons for EVM gas-fee estimation and balance checks.

Reviewed by Cursor Bugbot for commit 42a64d9. Bugbot is set up for automated code reviews on this repo. Configure here.

@Brunonascdev Brunonascdev self-assigned this May 4, 2026
@Brunonascdev Brunonascdev requested a review from a team as a code owner May 4, 2026 17:42
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbotv2 metamaskbotv2 Bot added the team-card Card Team label May 4, 2026
@github-actions github-actions Bot added the size-S label May 4, 2026
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeMoney
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: low
  • AI Confidence: 90%
click to see 🤖 AI reasoning details

E2E Test Selection:
The changes are confined to three hooks in the MetaMask Card feature area (app/components/UI/Card/hooks/):

  1. useAssetBalances.tsx: Import path change only — balanceToFiatNumber moved from ../../../../util/number to ../../../../util/number/bigint. No logic change.

  2. useCardDelegation.ts: Import path change only — toTokenMinimalUnit moved from ../../../../util/number to ../../../../util/number/bigint. No logic change.

  3. useNeedsGasFaucet.ts: Refactoring from bn.js (BN library) to native JavaScript bigint for gas fee arithmetic. The logic is equivalent — hexToBN replaced with hexToBigInt, .muln() replaced with *, .lt() replaced with <. The return type of estimateEvmGasFee changed from Promise<BN> to Promise<bigint>.

These are pure refactoring changes within the Card feature. The Card delegation flow (spending limit increases via ERC20 approve transactions) and asset balance display are the affected user flows. These are covered by SmokeMoney tests.

No shared infrastructure (navigation, TabBar, confirmations modal system, Engine) is modified. The changes don't affect any other feature areas. The test files confirm the new import paths are already updated to match.

Performance Test Selection:
The changes are pure refactoring of numeric arithmetic library (bn.js → native bigint) within Card hooks. No UI rendering, list components, state management patterns, or app startup code is affected. The arithmetic operations themselves are equivalent and native bigint is generally faster than bn.js, but there's no meaningful performance regression risk here that would warrant running performance tests.

View GitHub Actions results

@Brunonascdev Brunonascdev added the skip-e2e skip E2E test jobs label May 4, 2026
@sonarqubecloud

sonarqubecloud Bot commented May 4, 2026

Copy link
Copy Markdown

@Brunonascdev Brunonascdev added this pull request to the merge queue May 5, 2026
Merged via the queue into main with commit dd8cb1d May 5, 2026
148 of 154 checks passed
@Brunonascdev Brunonascdev deleted the chore/mm-card-bigint-migration branch May 5, 2026 14:20
@github-actions github-actions Bot locked and limited conversation to collaborators May 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size-S skip-e2e skip E2E test jobs team-card Card Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants