Skip to content

Commit 0cd15ef

Browse files
authored
feat: Allow MM Connect deeplinks to be retried sequentially (MetaMask#30346)
https://consensyssoftware.atlassian.net/browse/WAPI-1386 <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until this PR meets the canonical Definition of Ready For Review in `docs/readme/ready-for-review.md`. In short: the template must be materially complete (not just section titles present), all status checks must be currently passing, and the only expected follow-up commits must be reviewer-driven. --> ## **Description** Currently when a MM Connect deeplink is used once, it cannot be used again even if the connection failed to establish for some reason unless the app is restarted. This PR changes it so that a deeplink can be reused but only if a connection has not already successfully been established with it. ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: Improves MM Connect deeplink handling ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** <!-- Every checklist item must be consciously assessed before marking this PR as "Ready for review". A checked box means you deliberately considered that responsibility, not that you literally performed every action listed. Unchecked boxes are ambiguous: they are not an implicit "N/A" and they are not a silent "skip". See `docs/readme/ready-for-review.md` for the full checklist semantics. --> - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I've included tests if applicable - [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. #### 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](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/edit-v2/401401446401?draftShareId=9d77e1e1-4bdc-4be1-9ebb-ccd916988d93) to import wallets with many accounts and tokens - [ ] I've instrumented key operations with Sentry traces for production performance metrics - See [`trace()`](/app/util/trace.ts) for usage and [`addToken`](/app/components/Views/AddAsset/components/AddCustomToken/AddCustomToken.tsx#L274) for an example For performance guidelines and tooling, see the [Performance Guide](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/400085549067/Performance+Guide+for+Engineers). ## **Pre-merge reviewer checklist** <!-- Reviewer checklist items follow the same semantics as the author checklist: an unchecked box is ambiguous, a checked box means the reviewer consciously assessed that responsibility. See `docs/readme/ready-for-review.md`. --> - [ ] 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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches connection-establishment control flow and capacity eviction ordering; mistakes could lead to skipped connections or unexpected evictions, though changes are localized and covered by new tests. > > **Overview** > **Enables sequential reuse of MM Connect (MWP) deeplinks after failures** by clearing the in-flight deeplink guard (`deeplinks`) in a `finally` block, so the same URL can be retried without restarting the app. > > Adds a duplicate-connection safeguard in `handleConnectDeeplink` to **skip creating a new connection when a connection with the same session `id` already exists**, and updates tests to cover sequential duplicate handling and retry-after-failure behavior. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 3e1f731. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent b09b78f commit 0cd15ef

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

app/core/SDKConnectV2/services/connection-registry.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,63 @@ describe('ConnectionRegistry', () => {
739739
expect(mockStore.save).toHaveBeenCalledTimes(1);
740740
});
741741

742+
it('skips creating a connection if one with the same id already exists', async () => {
743+
// Given: a registry ready to handle connections
744+
registry = new ConnectionRegistry(
745+
RELAY_URL,
746+
mockKeyManager,
747+
mockHostApp,
748+
mockStore,
749+
);
750+
751+
// When: handling the same deeplink twice sequentially. After the first
752+
// call resolves the in-flight `deeplinks` guard is cleared (see finally
753+
// block), so the second call would otherwise proceed — the connection
754+
// id duplicate check is what must short-circuit it.
755+
await registry.handleConnectDeeplink(validDeeplink);
756+
jest.clearAllMocks();
757+
758+
await registry.handleConnectDeeplink(validDeeplink);
759+
760+
// Then: the second call must not create a duplicate connection nor
761+
// surface a loading state or error to the user.
762+
expect(Connection.create).not.toHaveBeenCalled();
763+
expect(mockConnection.connect).not.toHaveBeenCalled();
764+
expect(mockStore.save).not.toHaveBeenCalled();
765+
expect(mockHostApp.showConnectionLoading).not.toHaveBeenCalled();
766+
expect(mockHostApp.showConnectionError).not.toHaveBeenCalled();
767+
expect(mockHostApp.syncConnectionList).not.toHaveBeenCalled();
768+
});
769+
770+
it('allows retrying the same deeplink URL after a previous attempt fails', async () => {
771+
// Given: a registry where the first connect() attempt fails
772+
registry = new ConnectionRegistry(
773+
RELAY_URL,
774+
mockKeyManager,
775+
mockHostApp,
776+
mockStore,
777+
);
778+
779+
mockConnection.connect.mockRejectedValueOnce(new Error('Connect failed'));
780+
781+
// First call fails — no connection is registered.
782+
await registry.handleConnectDeeplink(validDeeplink);
783+
expect(mockHostApp.showConnectionError).toHaveBeenCalledTimes(1);
784+
785+
jest.clearAllMocks();
786+
787+
// When: the same URL is submitted again after the failure. The
788+
// `deeplinks` in-flight guard must have been cleared in the finally
789+
// block, allowing the retry to proceed.
790+
await registry.handleConnectDeeplink(validDeeplink);
791+
792+
// Then: the retry runs the full happy path.
793+
expect(Connection.create).toHaveBeenCalledTimes(1);
794+
expect(mockConnection.connect).toHaveBeenCalledTimes(1);
795+
expect(mockStore.save).toHaveBeenCalledTimes(1);
796+
expect(mockHostApp.showConnectionError).not.toHaveBeenCalled();
797+
});
798+
742799
it('should handle deeplinks with no payload parameter', async () => {
743800
// Given: a registry ready to handle connections
744801
registry = new ConnectionRegistry(

app/core/SDKConnectV2/services/connection-registry.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,17 @@ export class ConnectionRegistry {
301301
message: 'External transactions cannot use internal origins',
302302
});
303303
}
304-
await this.evictIfAtCapacity();
305304

306305
connInfo = this.toConnectionInfo(connReq);
306+
if (this.connections.has(connInfo.id)) {
307+
logger.debug(
308+
'Already have a connection with this id, skipping',
309+
redactUrl(url),
310+
);
311+
return;
312+
}
313+
314+
await this.evictIfAtCapacity();
307315

308316
this.hostapp.showConnectionLoading(connInfo);
309317
conn = await Connection.create(
@@ -364,6 +372,7 @@ export class ConnectionRegistry {
364372

365373
if (conn) await this.disconnect(conn.id);
366374
} finally {
375+
this.deeplinks.delete(url);
367376
// Loading-toast dismissal rules:
368377
// - On failure, always dismiss the loading toast. Otherwise the user
369378
// would briefly see both a "loading" toast and the error toast at

0 commit comments

Comments
 (0)