Skip to content

fix(perps): centralize Arbitrum network check in deposit hooks to prevent missing network errors cp-7.72.0#27484

Merged
michalconsensys merged 9 commits into
mainfrom
fix/perps/deposit-arbitrum
Mar 30, 2026
Merged

fix(perps): centralize Arbitrum network check in deposit hooks to prevent missing network errors cp-7.72.0#27484
michalconsensys merged 9 commits into
mainfrom
fix/perps/deposit-arbitrum

Conversation

@michalconsensys

@michalconsensys michalconsensys commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Description

Ensures the Arbitrum network exists before every perps deposit transaction, preventing No network client found for chain errors when users haven't added Arbitrum to their wallet.

Problem: ensureArbitrumNetworkExists() was previously called at individual call sites (button handlers, redirect screens), making it easy to miss entry points. Notably, PerpsOrderRedirect — reachable from Token Details without mounting PerpsHomeView — had the check removed, causing deposits to fail when Arbitrum wasn't present. Additionally, the useFocusEffect call in PerpsHomeView had no .catch() handler, creating unhandled promise rejections when the network addition failed.

Solution:

  1. Centralized the network check in usePerpsTrading — both depositWithConfirmation() and depositWithOrder() now call await ensureArbitrumNetworkExists() before invoking the controller. Every caller is automatically covered.
  2. Kept a preemptive warm-up in PerpsHomeView's useFocusEffect with a .catch() handler so the network is added before the user taps deposit, avoiding latency.
  3. Removed redundant checks from PerpsOrderRedirect, usePerpsHomeActions (deposit and withdraw handlers), and usePerpsBalanceTokenFilter.
  4. Updated tests to mock usePerpsNetworkManagement where needed due to the new transitive dependency.

Changelog

CHANGELOG entry: Fixed a bug where depositing into Perps from Token Details could fail if the Arbitrum network had not been added to the wallet

Related issues

Fixes: #28231
Fixes jira issue: https://consensyssoftware.atlassian.net/browse/TAT-2716
Fixes jira issue: https://consensyssoftware.atlassian.net/browse/TAT-2715

Manual testing steps

Feature: Perps deposit with Arbitrum network guarantee

  Scenario: User deposits from Token Details without Arbitrum in wallet
    Given the user has NOT added the Arbitrum network to their wallet
    And the user is viewing a perps-eligible token on Token Details

    When the user taps the deposit/trade button
    Then the Arbitrum network is automatically added
    And the deposit transaction is created successfully
    And the user is navigated to the confirmation screen

  Scenario: User deposits from Perps Home with Arbitrum already added
    Given the user has the Arbitrum network in their wallet
    And the user is on the Perps Home screen

    When the user taps "Add Funds"
    Then the deposit proceeds without delay
    And no duplicate network addition occurs

  Scenario: User lands on Perps Home without Arbitrum
    Given the user has NOT added the Arbitrum network
    
    When the user navigates to the Perps Home screen
    Then the Arbitrum network is silently added in the background
    And no error is shown if the addition fails

Screenshots/Recordings

N/A — no UI changes, logic-only fix.

Before

N/A

After

N/A

Pre-merge author checklist

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
Moves the Arbitrum network precondition into core perps deposit entrypoints, affecting all deposit flows; failures or timing changes could impact transaction creation and navigation to confirmations.

Overview
Centralizes Arbitrum network setup for deposits. usePerpsTrading now calls ensureArbitrumNetworkExists() inside both depositWithConfirmation and depositWithOrder, so every perps deposit path enforces the network prerequisite.

Removes redundant pre-checks at call sites and hardens navigation timing. Callers like PerpsOrderRedirect, usePerpsHomeActions, and usePerpsBalanceTokenFilter no longer manually gate on the network check; PerpsHomeView keeps a best-effort warm-up on focus with an added .catch() to avoid unhandled rejections, and PerpsMarketDetailsView triggers navigateToConfirmation inside the deposit try/catch.

Tests/presets updated for new transitive dependency. Multiple perps view/hook tests add or adjust mocks for usePerpsNetworkManagement, add waitFor where async ordering changed, and the perps state preset now includes an Arbitrum network configuration to keep view tests stable.

Written by Cursor Bugbot for commit 18ee027. This will update automatically on new commits. Configure here.

@github-actions

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.

@metamaskbot metamaskbot added the team-perps Perps team label Mar 16, 2026
@michalconsensys michalconsensys marked this pull request as ready for review March 16, 2026 13:42
@michalconsensys michalconsensys requested a review from a team as a code owner March 16, 2026 13:42
…posit rejection test

The test relied on the mock's initial factory implementation surviving
jest.clearAllMocks(), which would break under resetAllMocks/restoreAllMocks.
@github-actions github-actions Bot added size-M and removed size-S labels Mar 16, 2026
@michalconsensys

Copy link
Copy Markdown
Contributor Author

ensureArbitrumNetworkExists should be called on the main screen, not when someone clicked buttons

…n button click

- Call ensureArbitrumNetworkExists in PerpsHomeView on focus (useFocusEffect)
- Remove ensureArbitrumNetworkExists from usePerpsHomeActions (add funds/withdraw)
- Remove from usePerpsNavigation.navigateToOrder, PerpsMarketDetailsView
  handleAddFunds, usePerpsBalanceTokenFilter, PerpsOrderRedirect
- Update unit tests to match new flow and fix PerpsMarketDetailsView test syntax
@github-actions github-actions Bot added the risk-medium Moderate testing recommended · Possible bug introduction risk label Mar 17, 2026
Comment thread app/components/UI/Perps/Views/PerpsHomeView/PerpsHomeView.tsx
Comment thread app/components/UI/Perps/Views/PerpsOrderRedirect.tsx
Comment on lines +92 to +96
useFocusEffect(
useCallback(() => {
ensureArbitrumNetworkExists();
}, [ensureArbitrumNetworkExists]),
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be in useFocusEffect? Wouldn't it make more sense to call this when we actually run business logic that requires arbitrum to exist?

Can you help me understand why we only need it when the component mounts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved now

…etwork check in order redirect

Add .catch() to ensureArbitrumNetworkExists in PerpsHomeView to prevent
unhandled promise rejections when network addition fails. Restore the
Arbitrum network existence check in PerpsOrderRedirect, which is
reachable from Token Details without mounting PerpsHomeView.
… deposit methods

Move ensureArbitrumNetworkExists into depositWithConfirmation and
depositWithOrder in usePerpsTrading so every caller is automatically
covered. Remove the now-redundant check from PerpsOrderRedirect.
…centralized network check

usePerpsTrading now imports usePerpsNetworkManagement, which requires
Redux context. Add direct-path mocks in usePerpsTrading, PerpsOrderView,
and PerpsMarketDetailsView tests to prevent Invalid CAIP chain ID errors
from transitive module loading.
@michalconsensys michalconsensys changed the title fix(perps): ensure Arbitrum network exists in all deposit and order flows fix(perps): centralize Arbitrum network check in deposit hooks to prevent missing network errors Mar 18, 2026
@github-actions github-actions Bot added risk-medium Moderate testing recommended · Possible bug introduction risk and removed risk-medium Moderate testing recommended · Possible bug introduction risk labels Mar 18, 2026
Comment thread app/components/UI/Perps/hooks/usePerpsBalanceTokenFilter.ts
Comment thread app/components/UI/Perps/hooks/usePerpsHomeActions.ts
@github-actions github-actions Bot added risk-medium Moderate testing recommended · Possible bug introduction risk and removed risk-medium Moderate testing recommended · Possible bug introduction risk labels Mar 24, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 4 total unresolved issues (including 3 from previous reviews).

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread app/components/UI/Perps/hooks/usePerpsHomeActions.ts
…ew tests

The hook calls useNetworkEnablement which invokes parseCaipChainId,
causing "Invalid CAIP chain ID" errors when useSelector returns
unmocked values.
@github-actions github-actions Bot added risk-medium Moderate testing recommended · Possible bug introduction risk and removed risk-medium Moderate testing recommended · Possible bug introduction risk labels Mar 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps, SmokeWalletPlatform, SmokeConfirmations
  • Selected Performance tags: @PerformancePreps
  • Risk Level: medium
  • AI Confidence: 88%
click to see 🤖 AI reasoning details

E2E Test Selection:
The PR refactors where ensureArbitrumNetworkExists is called in the Perps feature:

  1. PerpsHomeView: Now calls ensureArbitrumNetworkExists proactively on screen focus via useFocusEffect, rather than on button click in usePerpsHomeActions.

  2. usePerpsTrading: Now calls ensureArbitrumNetworkExists inside depositWithConfirmation and depositWithOrder callbacks, centralizing the network check at the trading hook level.

  3. Removed from: usePerpsHomeActions, usePerpsBalanceTokenFilter, and PerpsOrderRedirect - these no longer call ensureArbitrumNetworkExists directly since it's now handled upstream.

  4. PerpsMarketDetailsView: Minor fix moving navigateToConfirmation inside the try block.

  5. Test infrastructure: perpsStatePreset.ts updated to include Arbitrum network config so component-view tests don't trigger addNetwork calls.

This is a behavioral change in the Perps deposit flow - the timing of when Arbitrum network is ensured has changed. The Add Funds flow (deposit with confirmation) is the core Perps E2E test scenario.

Tags selected:

  • SmokePerps: Primary - directly tests the Add Funds flow and deposit functionality that was refactored
  • SmokeWalletPlatform: Required by SmokePerps description (Perps is a section inside Trending tab)
  • SmokeConfirmations: Required by SmokePerps description (Add Funds deposits are on-chain transactions)

Performance Test Selection:
The PerpsHomeView now calls ensureArbitrumNetworkExists on every screen focus event via useFocusEffect. This adds an async network check on each navigation to the Perps home screen, which could impact the perps screen load and rendering performance. The @PerformancePreps tag covers perps market loading and add funds flow performance, making it relevant to validate this change doesn't introduce latency.

View GitHub Actions results

@github-actions

Copy link
Copy Markdown
Contributor

E2E Fixture Validation — Schema is up to date
16 value mismatches detected (expected — fixture represents an existing user).
View details

@sonarqubecloud

Copy link
Copy Markdown

@abretonc7s abretonc7s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: PR #27484 — fix(perps): centralize Arbitrum network check in deposit hooks

Tier: full | Tests: all pass | TSC: clean | Device: validated on iOS sim with recipe

Correct centralization of ensureArbitrumNetworkExists() into depositWithConfirmation/depositWithOrder. All call sites covered. 1 suggestion, 2 nitpicks.


[suggestion] usePerpsTrading.test.ts:416
The depositWithConfirmation test verifies the controller was called but doesn't assert mockEnsureArbitrumNetworkExists was called. The central behavioral change of this PR — that the Arbitrum check now runs inside depositWithConfirmation — has no test coverage. If await ensureArbitrumNetworkExists() is deleted from usePerpsTrading.ts, all 289 tests still pass.

Suggested addition:

expect(mockEnsureArbitrumNetworkExists).toHaveBeenCalledTimes(1);

Same pattern applies to the depositWithOrder test at line ~428 (currently not present in the test file — there is no depositWithOrder test). Both should assert the network check fires.


[nitpick] PerpsOrderView.test.tsx:297
Two mocks for usePerpsNetworkManagement are defined: one at the top-level jest.mock('../../hooks/usePerpsNetworkManagement', ...) (line 297) and one inside jest.mock('../../hooks', ...). Having both is defensive but adds noise — only one is needed depending on how the code imports it. Consider removing one.

Comment thread app/components/UI/Perps/hooks/usePerpsBalanceTokenFilter.ts
@michalconsensys michalconsensys added this pull request to the merge queue Mar 30, 2026
Merged via the queue into main with commit dcf5011 Mar 30, 2026
108 checks passed
@michalconsensys michalconsensys deleted the fix/perps/deposit-arbitrum branch March 30, 2026 10:42
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 30, 2026
@metamaskbot metamaskbot added the release-7.73.0 Issue or pull request that will be included in release 7.73.0 label Mar 30, 2026
@michalconsensys michalconsensys changed the title fix(perps): centralize Arbitrum network check in deposit hooks to prevent missing network errors fix(perps): centralize Arbitrum network check in deposit hooks to prevent missing network errors cp-7.72.0 Apr 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.73.0 Issue or pull request that will be included in release 7.73.0 risk-medium Moderate testing recommended · Possible bug introduction risk size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Can't trade perps when Arbitrum network is not enabled

4 participants