From 0eaccf36ab4b55b053ad8d96cde07e6f4f9f08e2 Mon Sep 17 00:00:00 2001 From: George Weiler Date: Sat, 26 Sep 2026 07:17:43 -0600 Subject: [PATCH 1/4] fix(ramps-controller): treat relay-approved KYC as approved in hydrate `hydrateVbaOnboarding` mapped the VBA snapshot `kycStatus` solely from the vendor-side `finalStatus`. When the relay has approved a session (`kycStatus: 'approved'`) but the vendor is still finalizing (`finalStatus: 'pending'`), this left relay-approved users stranded on the pending screen indefinitely. Map `kycStatus` to `'approved'` when either `finalStatus` or the relay-side `kycStatus` is `'approved'`, mirroring the UKYC backend's own `isKycAlreadyApproved` check (`finalStatus === 'approved' || kycStatus === 'approved'`). Co-Authored-By: Claude Opus 4.8 --- packages/ramps-controller/CHANGELOG.md | 4 ++ .../src/RampsController.test.ts | 50 ++++++++++++++++++- .../ramps-controller/src/RampsController.ts | 11 +++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/packages/ramps-controller/CHANGELOG.md b/packages/ramps-controller/CHANGELOG.md index 913e55d4032..fce85df67f8 100644 --- a/packages/ramps-controller/CHANGELOG.md +++ b/packages/ramps-controller/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Treat a relay-approved KYC session as approved in `hydrateVbaOnboarding` even when the vendor-side `finalStatus` is still `pending`, so relay-approved users are no longer stranded on the pending screen ([#PR_NUMBER](https://github.com/MetaMask/core/pull/PR_NUMBER)) + ## [26.0.0] ### Changed diff --git a/packages/ramps-controller/src/RampsController.test.ts b/packages/ramps-controller/src/RampsController.test.ts index 8761e5d166b..0e44cbe3b83 100644 --- a/packages/ramps-controller/src/RampsController.test.ts +++ b/packages/ramps-controller/src/RampsController.test.ts @@ -10444,7 +10444,9 @@ describe('RampsController', () => { const sessionWithStatus = (finalStatus: string): KycSession => ({ id: 'session-1', finalStatus, - kycStatus: 'approved', + // Relay and vendor decisions agree in these cases; the relay-approved, + // vendor-pending divergence is covered by its own test below. + kycStatus: finalStatus, vendorStatus: finalStatus, externalUserId: CANONICAL_PROFILE_ID, }); @@ -10684,6 +10686,52 @@ describe('RampsController', () => { }); }); + it('treats a relay-approved session as approved even while the vendor finalizes', async () => { + await withController(async ({ controller, rootMessenger }) => { + // The relay has approved (`kycStatus: 'approved'`) but the vendor is + // still finalizing (`finalStatus: 'pending'`). Activation must proceed + // rather than stranding the user on a pending screen. + registerKycHandlers(rootMessenger, { + session: { + ...approvedSession, + finalStatus: 'pending', + vendorStatus: 'new', + }, + }); + jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ + type: 'registered', + registration: { + id: 'wallet-1', + address: '0xabc', + blockchain: 'Monad', + disabled: false, + isSelf: true, + }, + }); + const createAutoramp = jest + .spyOn(controller, 'createAutoramp') + .mockImplementation(async () => + controller.addAutoramp({ + id: 'autoramp-1', + customerId: 'customer-1', + walletAddress: '0xabc', + status: AutorampStatus.Created, + }), + ); + + expect( + await controller.hydrateVbaOnboarding({ walletAddress: '0xabc' }), + ).toStrictEqual( + factsSnapshot({ + kycStatus: 'approved', + autorampStatus: 'ready', + }), + ); + + expect(createAutoramp).toHaveBeenCalledWith({}); + }); + }); + it('loads an existing autoramp from the service instead of creating another', async () => { await withController(async ({ controller, rootMessenger }) => { const handlers = registerKycHandlers(rootMessenger); diff --git a/packages/ramps-controller/src/RampsController.ts b/packages/ramps-controller/src/RampsController.ts index 14594077e0b..7cd32b65a4e 100644 --- a/packages/ramps-controller/src/RampsController.ts +++ b/packages/ramps-controller/src/RampsController.ts @@ -3977,7 +3977,16 @@ export class RampsController extends BaseController< vendorDisclaimersComplete, sessionDisclaimersComplete, providerFlowStatus, - kycStatus: toVbaKycStatus(session.finalStatus), + // The relay approves (`kycStatus: 'approved'`) as soon as the KYC decision + // is made, while the vendor-side `finalStatus` can lag at `'pending'` until + // the vendor record is finalized (and in sandbox may never advance). Treat + // either signal as approval so a relay-approved user is not stranded on the + // pending screen. This mirrors the UKYC backend, whose own "already + // approved" check is `finalStatus === 'approved' || kycStatus === 'approved'`. + kycStatus: + session.finalStatus === 'approved' || session.kycStatus === 'approved' + ? 'approved' + : toVbaKycStatus(session.finalStatus), autorampStatus: 'not_ready', }; From 9fdec675b4675ebf1de509b75a6b468521225838 Mon Sep 17 00:00:00 2001 From: George Weiler Date: Sat, 26 Sep 2026 07:18:20 -0600 Subject: [PATCH 2/4] docs(ramps-controller): link changelog entry to PR Co-Authored-By: Claude Opus 4.8 --- packages/ramps-controller/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ramps-controller/CHANGELOG.md b/packages/ramps-controller/CHANGELOG.md index fce85df67f8..e27f98c0428 100644 --- a/packages/ramps-controller/CHANGELOG.md +++ b/packages/ramps-controller/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Treat a relay-approved KYC session as approved in `hydrateVbaOnboarding` even when the vendor-side `finalStatus` is still `pending`, so relay-approved users are no longer stranded on the pending screen ([#PR_NUMBER](https://github.com/MetaMask/core/pull/PR_NUMBER)) +- Treat a relay-approved KYC session as approved in `hydrateVbaOnboarding` even when the vendor-side `finalStatus` is still `pending`, so relay-approved users are no longer stranded on the pending screen ([#10497](https://github.com/MetaMask/core/pull/10497)) ## [26.0.0] From b46bc03ff82f30c6d794458f05db16eeb9f2716f Mon Sep 17 00:00:00 2001 From: George Weiler Date: Mon, 28 Sep 2026 05:10:27 -0600 Subject: [PATCH 3/4] fix(ramps-controller): reconcile autoramp by id, drop unsupported list call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `hydrateVbaOnboarding` called `NeoBankService:getAutoramps` to list the customer's autoramps, but the neobank proxy (and MoonPay) expose autoramps by id only — there is no list-by-customer endpoint — so the call always 404'd and provisioning fell into `retryable_failure` ("couldn't create your account"). Reconcile the autoramps already known for the wallet by refreshing each by id instead of listing, and treat a duplicate-create conflict (MoonPay allows one autoramp per customer) as already provisioned rather than a failure. Remove `NeoBankService:getAutoramps` from the controller's required service actions since it is no longer called. Co-Authored-By: Claude Opus 4.8 --- packages/ramps-controller/CHANGELOG.md | 1 + .../src/RampsController.test.ts | 118 ++++++++++++------ .../ramps-controller/src/RampsController.ts | 66 +++++++--- 3 files changed, 129 insertions(+), 56 deletions(-) diff --git a/packages/ramps-controller/CHANGELOG.md b/packages/ramps-controller/CHANGELOG.md index e27f98c0428..053e43071c2 100644 --- a/packages/ramps-controller/CHANGELOG.md +++ b/packages/ramps-controller/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Treat a relay-approved KYC session as approved in `hydrateVbaOnboarding` even when the vendor-side `finalStatus` is still `pending`, so relay-approved users are no longer stranded on the pending screen ([#10497](https://github.com/MetaMask/core/pull/10497)) +- Reconcile the customer's autoramp by id in `hydrateVbaOnboarding` instead of calling the unsupported `NeoBankService:getAutoramps` list action (the neobank proxy and MoonPay expose autoramps by id only), and treat a duplicate-create conflict as already provisioned; `NeoBankService:getAutoramps` is no longer a required service action ([#10497](https://github.com/MetaMask/core/pull/10497)) ## [26.0.0] diff --git a/packages/ramps-controller/src/RampsController.test.ts b/packages/ramps-controller/src/RampsController.test.ts index 0e44cbe3b83..ace79bce9b5 100644 --- a/packages/ramps-controller/src/RampsController.test.ts +++ b/packages/ramps-controller/src/RampsController.test.ts @@ -1,5 +1,5 @@ import { deriveStateFromMetadata } from '@metamask/base-controller'; -import { BrokenCircuitError } from '@metamask/controller-utils'; +import { BrokenCircuitError, HttpError } from '@metamask/controller-utils'; import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger'; import type { MockAnyNamespace, @@ -10324,6 +10324,7 @@ describe('RampsController', () => { clearState: jest.Mock; getSessionProfile: jest.Mock; getAutoramps: jest.Mock; + getAutoramp: jest.Mock; }; type KycValues = { @@ -10403,6 +10404,7 @@ describe('RampsController', () => { .fn() .mockResolvedValue({ canonicalProfileId: values.profileCanonicalId }), getAutoramps: jest.fn().mockResolvedValue([]), + getAutoramp: jest.fn(), }; rootMessenger.registerActionHandler( @@ -10437,6 +10439,10 @@ describe('RampsController', () => { 'NeoBankService:getAutoramps' as never, handlers.getAutoramps as never, ); + rootMessenger.registerActionHandler( + 'NeoBankService:getAutoramp' as never, + handlers.getAutoramp as never, + ); return handlers; }; @@ -10732,17 +10738,23 @@ describe('RampsController', () => { }); }); - it('loads an existing autoramp from the service instead of creating another', async () => { + it('reconciles an existing autoramp by id instead of creating another', async () => { await withController(async ({ controller, rootMessenger }) => { const handlers = registerKycHandlers(rootMessenger); - handlers.getAutoramps.mockResolvedValue([ - { - id: 'autoramp-1', - customerId: 'customer-1', - walletAddress: '0xAbC', - status: AutorampStatus.Approved, - }, - ]); + // Persisted from a previous run; there is no list endpoint, so hydrate + // rediscovers it by refreshing the known id. + controller.addAutoramp({ + id: 'autoramp-1', + customerId: 'customer-1', + walletAddress: '0xAbC', + status: AutorampStatus.Created, + }); + handlers.getAutoramp.mockResolvedValue({ + id: 'autoramp-1', + customerId: 'customer-1', + walletAddress: '0xAbC', + status: AutorampStatus.Approved, + }); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10766,37 +10778,31 @@ describe('RampsController', () => { }), ); + expect(handlers.getAutoramp).toHaveBeenCalledWith('autoramp-1'); expect(createAutoramp).not.toHaveBeenCalled(); - expect( - controller.state.autoramps.map( - ({ updatedAt: _updatedAt, ...account }) => account, - ), - ).toMatchInlineSnapshot(` - [ - { - "customerId": "customer-1", - "depositRailsSummary": undefined, - "id": "autoramp-1", - "lastSeenStatus": "Approved", - "status": "Approved", - "walletAddress": "0xAbC", - }, - ] - `); + expect(controller.state.autoramps).toHaveLength(1); + expect(controller.state.autoramps[0]).toMatchObject({ + id: 'autoramp-1', + status: AutorampStatus.Approved, + }); }); }); it('creates a new autoramp when the existing account is terminal', async () => { await withController(async ({ controller, rootMessenger }) => { const handlers = registerKycHandlers(rootMessenger); - handlers.getAutoramps.mockResolvedValue([ - { - id: 'autoramp-rejected', - customerId: 'customer-1', - walletAddress: '0xabc', - status: AutorampStatus.Rejected, - }, - ]); + controller.addAutoramp({ + id: 'autoramp-rejected', + customerId: 'customer-1', + walletAddress: '0xabc', + status: AutorampStatus.Created, + }); + handlers.getAutoramp.mockResolvedValue({ + id: 'autoramp-rejected', + customerId: 'customer-1', + walletAddress: '0xabc', + status: AutorampStatus.Rejected, + }); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10918,11 +10924,9 @@ describe('RampsController', () => { }); }); - it('marks the autoramp retryable when loading autoramps fails', async () => { + it('marks the autoramp retryable when autoramp creation fails', async () => { await withController(async ({ controller, rootMessenger }) => { - const handlers = registerKycHandlers(rootMessenger); - const error = new Error('autoramp lookup failed'); - handlers.getAutoramps.mockRejectedValue(error); + registerKycHandlers(rootMessenger); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10933,7 +10937,9 @@ describe('RampsController', () => { isSelf: true, }, }); - const createAutoramp = jest.spyOn(controller, 'createAutoramp'); + jest + .spyOn(controller, 'createAutoramp') + .mockRejectedValue(new Error('autoramp creation failed')); expect( await controller.hydrateVbaOnboarding({ walletAddress: '0xabc' }), @@ -10943,7 +10949,39 @@ describe('RampsController', () => { autorampStatus: 'retryable_failure', }), ); - expect(createAutoramp).not.toHaveBeenCalled(); + }); + }); + + it('treats a duplicate-create conflict as already provisioned', async () => { + await withController(async ({ controller, rootMessenger }) => { + registerKycHandlers(rootMessenger); + jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ + type: 'alreadyRegistered', + registration: { + id: 'wallet-1', + address: '0xabc', + blockchain: 'Monad', + disabled: false, + isSelf: true, + }, + }); + // MoonPay permits one autoramp per customer; with no local id to + // rediscover it, the create is rejected as a duplicate. That means the + // account already exists, so activation is ready rather than failed. + jest + .spyOn(controller, 'createAutoramp') + .mockRejectedValue( + new HttpError(400, 'Customer already has a Pix onramp'), + ); + + expect( + await controller.hydrateVbaOnboarding({ walletAddress: '0xabc' }), + ).toStrictEqual( + factsSnapshot({ + kycStatus: 'approved', + autorampStatus: 'ready', + }), + ); }); }); diff --git a/packages/ramps-controller/src/RampsController.ts b/packages/ramps-controller/src/RampsController.ts index 7cd32b65a4e..5f869e8ede1 100644 --- a/packages/ramps-controller/src/RampsController.ts +++ b/packages/ramps-controller/src/RampsController.ts @@ -5,7 +5,7 @@ import type { } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; import type { TraceCallback } from '@metamask/controller-utils'; -import { BrokenCircuitError } from '@metamask/controller-utils'; +import { BrokenCircuitError, HttpError } from '@metamask/controller-utils'; import type { Messenger } from '@metamask/messenger'; import type { AuthenticationController, @@ -221,7 +221,6 @@ export const RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS = [ 'TransakService:cancelAllActiveOrders', 'TransakService:getActiveOrders', 'NeoBankService:getAutoramp', - 'NeoBankService:getAutoramps', 'NeoBankService:createAutoramp', 'NeoBankService:getCustomerByExternalId', 'NeoBankService:getWalletRegistrationStatus', @@ -424,6 +423,27 @@ function toVbaKycStatus(value: string | undefined): VbaKycStatus { return 'none'; } +/** + * Whether an autoramp-creation error means the customer already has an autoramp. + * + * The neobank proxy mirrors MoonPay's response verbatim, and MoonPay permits a + * single autoramp per customer — a duplicate create is rejected with a `400` + * whose body reads e.g. `"Customer already has a Pix onramp"`. There is no + * list-by-customer endpoint to rediscover the existing autoramp's id (only + * fetch-by-id), so this conflict is the signal that provisioning is already + * done rather than a failure. + * + * @param error - The error thrown by {@link RampsController.createAutoramp}. + * @returns Whether the error indicates an autoramp already exists. + */ +function isAutorampAlreadyExistsError(error: unknown): boolean { + return ( + error instanceof HttpError && + error.httpStatus === 400 && + /already has/iu.test(error.message) + ); +} + /** * Distinguishes an already-materialized {@link AutorampAccount} from the * create-fields shape accepted by {@link RampsController.addAutoramp}. @@ -4010,22 +4030,26 @@ export class RampsController extends BaseController< throw registration.error; } - const remoteAutoramps = await this.messenger.call( - 'NeoBankService:getAutoramps', + const normalizedWalletAddress = walletAddress.toLowerCase(); + // The neobank proxy (and MoonPay) expose autoramps by id only — there is + // no list-by-customer endpoint — so reconcile the autoramps already known + // for this wallet by refreshing each by id. A refresh failure is + // non-fatal: the last-known snapshot is kept for the usable check below. + const knownAutorampsForWallet = this.state.autoramps.filter( + (autoramp) => + autoramp.walletAddress.toLowerCase() === normalizedWalletAddress, ); - const remoteAutorampIds = new Set( - remoteAutoramps.map((autoramp) => autoramp.id), + await Promise.all( + knownAutorampsForWallet.map(async (autoramp) => { + try { + await this.refreshAutoramp(autoramp.id); + } catch { + // Keep the last-known snapshot; a transient refresh failure must not + // trigger a duplicate create. + } + }), ); - for (const autoramp of remoteAutoramps) { - this.#applyAutorampRemoteSnapshot(autoramp); - } - this.update((state) => { - state.autoramps = state.autoramps.filter((autoramp) => - remoteAutorampIds.has(autoramp.id), - ); - }); - const normalizedWalletAddress = walletAddress.toLowerCase(); const hasUsableAutoramp = this.state.autoramps.some( (autoramp) => autoramp.walletAddress.toLowerCase() === normalizedWalletAddress && @@ -4033,7 +4057,17 @@ export class RampsController extends BaseController< autoramp.status !== AutorampStatus.Cancelled, ); if (!hasUsableAutoramp) { - await this.createAutoramp({}); + try { + await this.createAutoramp({}); + } catch (error) { + // Without a list endpoint the client cannot rediscover an autoramp + // that already exists server-side (e.g. after a reinstall cleared + // local state). MoonPay permits one per customer, so a duplicate + // create conflict means provisioning is already done. + if (!isAutorampAlreadyExistsError(error)) { + throw error; + } + } } } catch { return { ...snapshot, autorampStatus: 'retryable_failure' }; From 5b890139a9128892d923127f8cd92d910520e5bf Mon Sep 17 00:00:00 2001 From: George Weiler Date: Mon, 28 Sep 2026 05:40:30 -0600 Subject: [PATCH 4/4] Revert "fix(ramps-controller): reconcile autoramp by id, drop unsupported list call" This reverts commit b46bc03ff82f30c6d794458f05db16eeb9f2716f. --- packages/ramps-controller/CHANGELOG.md | 1 - .../src/RampsController.test.ts | 118 ++++++------------ .../ramps-controller/src/RampsController.ts | 66 +++------- 3 files changed, 56 insertions(+), 129 deletions(-) diff --git a/packages/ramps-controller/CHANGELOG.md b/packages/ramps-controller/CHANGELOG.md index 053e43071c2..e27f98c0428 100644 --- a/packages/ramps-controller/CHANGELOG.md +++ b/packages/ramps-controller/CHANGELOG.md @@ -10,7 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Treat a relay-approved KYC session as approved in `hydrateVbaOnboarding` even when the vendor-side `finalStatus` is still `pending`, so relay-approved users are no longer stranded on the pending screen ([#10497](https://github.com/MetaMask/core/pull/10497)) -- Reconcile the customer's autoramp by id in `hydrateVbaOnboarding` instead of calling the unsupported `NeoBankService:getAutoramps` list action (the neobank proxy and MoonPay expose autoramps by id only), and treat a duplicate-create conflict as already provisioned; `NeoBankService:getAutoramps` is no longer a required service action ([#10497](https://github.com/MetaMask/core/pull/10497)) ## [26.0.0] diff --git a/packages/ramps-controller/src/RampsController.test.ts b/packages/ramps-controller/src/RampsController.test.ts index ace79bce9b5..0e44cbe3b83 100644 --- a/packages/ramps-controller/src/RampsController.test.ts +++ b/packages/ramps-controller/src/RampsController.test.ts @@ -1,5 +1,5 @@ import { deriveStateFromMetadata } from '@metamask/base-controller'; -import { BrokenCircuitError, HttpError } from '@metamask/controller-utils'; +import { BrokenCircuitError } from '@metamask/controller-utils'; import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger'; import type { MockAnyNamespace, @@ -10324,7 +10324,6 @@ describe('RampsController', () => { clearState: jest.Mock; getSessionProfile: jest.Mock; getAutoramps: jest.Mock; - getAutoramp: jest.Mock; }; type KycValues = { @@ -10404,7 +10403,6 @@ describe('RampsController', () => { .fn() .mockResolvedValue({ canonicalProfileId: values.profileCanonicalId }), getAutoramps: jest.fn().mockResolvedValue([]), - getAutoramp: jest.fn(), }; rootMessenger.registerActionHandler( @@ -10439,10 +10437,6 @@ describe('RampsController', () => { 'NeoBankService:getAutoramps' as never, handlers.getAutoramps as never, ); - rootMessenger.registerActionHandler( - 'NeoBankService:getAutoramp' as never, - handlers.getAutoramp as never, - ); return handlers; }; @@ -10738,23 +10732,17 @@ describe('RampsController', () => { }); }); - it('reconciles an existing autoramp by id instead of creating another', async () => { + it('loads an existing autoramp from the service instead of creating another', async () => { await withController(async ({ controller, rootMessenger }) => { const handlers = registerKycHandlers(rootMessenger); - // Persisted from a previous run; there is no list endpoint, so hydrate - // rediscovers it by refreshing the known id. - controller.addAutoramp({ - id: 'autoramp-1', - customerId: 'customer-1', - walletAddress: '0xAbC', - status: AutorampStatus.Created, - }); - handlers.getAutoramp.mockResolvedValue({ - id: 'autoramp-1', - customerId: 'customer-1', - walletAddress: '0xAbC', - status: AutorampStatus.Approved, - }); + handlers.getAutoramps.mockResolvedValue([ + { + id: 'autoramp-1', + customerId: 'customer-1', + walletAddress: '0xAbC', + status: AutorampStatus.Approved, + }, + ]); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10778,31 +10766,37 @@ describe('RampsController', () => { }), ); - expect(handlers.getAutoramp).toHaveBeenCalledWith('autoramp-1'); expect(createAutoramp).not.toHaveBeenCalled(); - expect(controller.state.autoramps).toHaveLength(1); - expect(controller.state.autoramps[0]).toMatchObject({ - id: 'autoramp-1', - status: AutorampStatus.Approved, - }); + expect( + controller.state.autoramps.map( + ({ updatedAt: _updatedAt, ...account }) => account, + ), + ).toMatchInlineSnapshot(` + [ + { + "customerId": "customer-1", + "depositRailsSummary": undefined, + "id": "autoramp-1", + "lastSeenStatus": "Approved", + "status": "Approved", + "walletAddress": "0xAbC", + }, + ] + `); }); }); it('creates a new autoramp when the existing account is terminal', async () => { await withController(async ({ controller, rootMessenger }) => { const handlers = registerKycHandlers(rootMessenger); - controller.addAutoramp({ - id: 'autoramp-rejected', - customerId: 'customer-1', - walletAddress: '0xabc', - status: AutorampStatus.Created, - }); - handlers.getAutoramp.mockResolvedValue({ - id: 'autoramp-rejected', - customerId: 'customer-1', - walletAddress: '0xabc', - status: AutorampStatus.Rejected, - }); + handlers.getAutoramps.mockResolvedValue([ + { + id: 'autoramp-rejected', + customerId: 'customer-1', + walletAddress: '0xabc', + status: AutorampStatus.Rejected, + }, + ]); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10924,9 +10918,11 @@ describe('RampsController', () => { }); }); - it('marks the autoramp retryable when autoramp creation fails', async () => { + it('marks the autoramp retryable when loading autoramps fails', async () => { await withController(async ({ controller, rootMessenger }) => { - registerKycHandlers(rootMessenger); + const handlers = registerKycHandlers(rootMessenger); + const error = new Error('autoramp lookup failed'); + handlers.getAutoramps.mockRejectedValue(error); jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ type: 'alreadyRegistered', registration: { @@ -10937,9 +10933,7 @@ describe('RampsController', () => { isSelf: true, }, }); - jest - .spyOn(controller, 'createAutoramp') - .mockRejectedValue(new Error('autoramp creation failed')); + const createAutoramp = jest.spyOn(controller, 'createAutoramp'); expect( await controller.hydrateVbaOnboarding({ walletAddress: '0xabc' }), @@ -10949,39 +10943,7 @@ describe('RampsController', () => { autorampStatus: 'retryable_failure', }), ); - }); - }); - - it('treats a duplicate-create conflict as already provisioned', async () => { - await withController(async ({ controller, rootMessenger }) => { - registerKycHandlers(rootMessenger); - jest.spyOn(controller, 'registerMoneyAccountWallet').mockResolvedValue({ - type: 'alreadyRegistered', - registration: { - id: 'wallet-1', - address: '0xabc', - blockchain: 'Monad', - disabled: false, - isSelf: true, - }, - }); - // MoonPay permits one autoramp per customer; with no local id to - // rediscover it, the create is rejected as a duplicate. That means the - // account already exists, so activation is ready rather than failed. - jest - .spyOn(controller, 'createAutoramp') - .mockRejectedValue( - new HttpError(400, 'Customer already has a Pix onramp'), - ); - - expect( - await controller.hydrateVbaOnboarding({ walletAddress: '0xabc' }), - ).toStrictEqual( - factsSnapshot({ - kycStatus: 'approved', - autorampStatus: 'ready', - }), - ); + expect(createAutoramp).not.toHaveBeenCalled(); }); }); diff --git a/packages/ramps-controller/src/RampsController.ts b/packages/ramps-controller/src/RampsController.ts index 5f869e8ede1..7cd32b65a4e 100644 --- a/packages/ramps-controller/src/RampsController.ts +++ b/packages/ramps-controller/src/RampsController.ts @@ -5,7 +5,7 @@ import type { } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; import type { TraceCallback } from '@metamask/controller-utils'; -import { BrokenCircuitError, HttpError } from '@metamask/controller-utils'; +import { BrokenCircuitError } from '@metamask/controller-utils'; import type { Messenger } from '@metamask/messenger'; import type { AuthenticationController, @@ -221,6 +221,7 @@ export const RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS = [ 'TransakService:cancelAllActiveOrders', 'TransakService:getActiveOrders', 'NeoBankService:getAutoramp', + 'NeoBankService:getAutoramps', 'NeoBankService:createAutoramp', 'NeoBankService:getCustomerByExternalId', 'NeoBankService:getWalletRegistrationStatus', @@ -423,27 +424,6 @@ function toVbaKycStatus(value: string | undefined): VbaKycStatus { return 'none'; } -/** - * Whether an autoramp-creation error means the customer already has an autoramp. - * - * The neobank proxy mirrors MoonPay's response verbatim, and MoonPay permits a - * single autoramp per customer — a duplicate create is rejected with a `400` - * whose body reads e.g. `"Customer already has a Pix onramp"`. There is no - * list-by-customer endpoint to rediscover the existing autoramp's id (only - * fetch-by-id), so this conflict is the signal that provisioning is already - * done rather than a failure. - * - * @param error - The error thrown by {@link RampsController.createAutoramp}. - * @returns Whether the error indicates an autoramp already exists. - */ -function isAutorampAlreadyExistsError(error: unknown): boolean { - return ( - error instanceof HttpError && - error.httpStatus === 400 && - /already has/iu.test(error.message) - ); -} - /** * Distinguishes an already-materialized {@link AutorampAccount} from the * create-fields shape accepted by {@link RampsController.addAutoramp}. @@ -4030,26 +4010,22 @@ export class RampsController extends BaseController< throw registration.error; } - const normalizedWalletAddress = walletAddress.toLowerCase(); - // The neobank proxy (and MoonPay) expose autoramps by id only — there is - // no list-by-customer endpoint — so reconcile the autoramps already known - // for this wallet by refreshing each by id. A refresh failure is - // non-fatal: the last-known snapshot is kept for the usable check below. - const knownAutorampsForWallet = this.state.autoramps.filter( - (autoramp) => - autoramp.walletAddress.toLowerCase() === normalizedWalletAddress, + const remoteAutoramps = await this.messenger.call( + 'NeoBankService:getAutoramps', ); - await Promise.all( - knownAutorampsForWallet.map(async (autoramp) => { - try { - await this.refreshAutoramp(autoramp.id); - } catch { - // Keep the last-known snapshot; a transient refresh failure must not - // trigger a duplicate create. - } - }), + const remoteAutorampIds = new Set( + remoteAutoramps.map((autoramp) => autoramp.id), ); + for (const autoramp of remoteAutoramps) { + this.#applyAutorampRemoteSnapshot(autoramp); + } + this.update((state) => { + state.autoramps = state.autoramps.filter((autoramp) => + remoteAutorampIds.has(autoramp.id), + ); + }); + const normalizedWalletAddress = walletAddress.toLowerCase(); const hasUsableAutoramp = this.state.autoramps.some( (autoramp) => autoramp.walletAddress.toLowerCase() === normalizedWalletAddress && @@ -4057,17 +4033,7 @@ export class RampsController extends BaseController< autoramp.status !== AutorampStatus.Cancelled, ); if (!hasUsableAutoramp) { - try { - await this.createAutoramp({}); - } catch (error) { - // Without a list endpoint the client cannot rediscover an autoramp - // that already exists server-side (e.g. after a reinstall cleared - // local state). MoonPay permits one per customer, so a duplicate - // create conflict means provisioning is already done. - if (!isAutorampAlreadyExistsError(error)) { - throw error; - } - } + await this.createAutoramp({}); } } catch { return { ...snapshot, autorampStatus: 'retryable_failure' };