Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/ramps-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `NeoBankService.getCustomerAbilities`, exposed on the messenger, which returns the customer's rail abilities from neobank-proxy unmodified, and the `NeoBankCustomerAbilities`, `NeoBankFiatAbilities`, `NeoBankAbilityStatus`, `NeoBankCurrencyAbility` and `NeoBankAbilityCurrency` types ([#11225](https://github.com/MetaMask/core/pull/11225))
- MoonPay rejects an autoramp create with a 422 when the rail it needs is not `Active`, so clients can check the rail first. Any non-2xx response throws an `HttpError`.
- It resolves the MoonPay customer through `AuthenticationController:getSessionProfile`, so hosts must delegate that action to the `NeoBankService` messenger as well as `AuthenticationController:getBearerToken`. `getMicrodeposits` needs it too.
- Add `RampsController.refreshCustomerAbilities`, exposed on the messenger, which stores the result in the new `vbaCustomerAbilities` state ([#11225](https://github.com/MetaMask/core/pull/11225))
- The state is not persisted. A failed fetch throws and keeps the stored value. Discarding a VBA session owned by another profile clears it.
- Add `getMusdAutorampDepositRailReadiness` and the `MusdAutorampDepositRailReadiness` type, which read from customer abilities whether the deposit rails a fiat → mUSD autoramp needs are `active`, `pending` or `blocked` ([#11225](https://github.com/MetaMask/core/pull/11225))
- BRL needs `pix` and `pix_thirdparty`, USD needs `ach` and `ach_thirdparty`. Other fiats report `active`.

### Changed

- **BREAKING:** `RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS` now includes `NeoBankService:getCustomerAbilities`. Hosts must delegate that action to `RampsController` ([#11225](https://github.com/MetaMask/core/pull/11225))

### Fixed

- `NeoBankService.getMicrodeposits` now looks the MoonPay customer up again when the session profile changes, instead of reusing the first profile's customer id for the life of the service ([#11225](https://github.com/MetaMask/core/pull/11225))

## [28.1.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ export type NeoBankServiceGetMicrodepositsAction = {
handler: NeoBankService['getMicrodeposits'];
};

/**
* Reads the customer's rail abilities via neobank-proxy
* `GET /neobank/customers/{customer_id}/abilities` (MoonPay
* `GET /api/customers/{id}/abilities`).
*
* MoonPay rejects an autoramp create with a 422 when the rail it needs is not
* `Active`, even for an `Active` customer. The customer id is resolved the
* same way as in {@link getMicrodeposits}. Any non-2xx response, including a
* 404 when there is no customer yet, throws an {@link HttpError}.
*
* @returns MoonPay customer abilities, unmodified.
*/
export type NeoBankServiceGetCustomerAbilitiesAction = {
type: `NeoBankService:getCustomerAbilities`;
handler: NeoBankService['getCustomerAbilities'];
};

/**
* Checks whether a Monad Money Account address is already registered for the
* given Iron customer.
Expand Down Expand Up @@ -234,5 +251,6 @@ export type NeoBankServiceMethodActions =
| NeoBankServiceGetCustomerByExternalIdAction
| NeoBankServiceGetMoonpayCustomerIdAction
| NeoBankServiceGetMicrodepositsAction
| NeoBankServiceGetCustomerAbilitiesAction
| NeoBankServiceGetWalletRegistrationStatusAction
| NeoBankServiceRegisterSelfHostedWalletAction;
169 changes: 163 additions & 6 deletions packages/ramps-controller/src/NeoBankService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ const isAutorampTransactionQuery = (
* `{ maxRetries: 0 }` so tests fail fast unless they opt into retries.
* @param options.canonicalProfileId - Canonical profile id returned by the
* stubbed `AuthenticationController:getSessionProfile` (wallet registration).
* Pass a function to change it between calls.
* @returns Service instance for the test.
*/
function createService(options?: {
environment?: RampsEnvironment;
baseUrlOverride?: string;
omitDefaults?: boolean;
policyOptions?: CreateServicePolicyOptions;
canonicalProfileId?: string;
canonicalProfileId?: string | (() => string);
}): NeoBankService {
const rootMessenger = new Messenger({
namespace: MOCK_ANY_NAMESPACE,
Expand All @@ -56,17 +57,21 @@ function createService(options?: {
'AuthenticationController:getBearerToken',
async () => 'test-token',
);
const canonicalProfileId =
options?.canonicalProfileId ?? 'canonical-profile-1';
const profileIdOption = options?.canonicalProfileId ?? 'canonical-profile-1';
rootMessenger.registerActionHandler(
'AuthenticationController:getSessionProfile',
async () =>
({
async () => {
const canonicalProfileId =
typeof profileIdOption === 'function'
? profileIdOption()
: profileIdOption;
return {
identifierId: 'id-1',
profileId: canonicalProfileId,
canonicalProfileId,
metaMetricsId: 'mm-1',
}) as never,
} as never;
},
);

const messenger = new Messenger({
Expand Down Expand Up @@ -1195,6 +1200,158 @@ describe('NeoBankService', () => {
});
});

describe('getCustomerAbilities', () => {
const CUSTOMER_PATH = '/neobank/customers/canonical-profile-1/external';
const ABILITIES_PATH = '/neobank/customers/iron-customer-1/abilities';

const abilities = {
fiat_deposit: {
brl: { pix: 'Active', pix_thirdparty: 'Active' },
usd: { ach: 'Pending', ach_thirdparty: 'Unavailable' },
},
fiat_payout: {
brl: { pix: 'Active', pix_thirdparty: 'Active' },
},
currencies: [],
blocking_abilities: { ach_thirdparty: 'rule engine preflight' },
};

/**
* Stubs the customer lookup that `getCustomerAbilities` performs first.
*
* @returns The nock scope for the lookup.
*/
function mockCustomerLookup(): nock.Scope {
return nock(STAGING_BASE)
.get(CUSTOMER_PATH)
.reply(200, { id: 'iron-customer-1' });
}

it('returns the abilities unchanged', async () => {
const customerScope = mockCustomerLookup();
const scope = nock(STAGING_BASE)
.get(ABILITIES_PATH)
.query({ sdk: /.+/u, controller: /.+/u, context: 'test' })
.matchHeader('Authorization', 'Bearer test-token')
.reply(200, abilities);

const service = createService();

expect(await service.getCustomerAbilities()).toStrictEqual(abilities);
expect(customerScope.isDone()).toBe(true);
expect(scope.isDone()).toBe(true);
});

it('throws an HttpError when the customer does not exist yet (404)', async () => {
nock(STAGING_BASE).get(CUSTOMER_PATH).reply(404, 'customer not found');

const service = createService();

await expect(service.getCustomerAbilities()).rejects.toMatchObject({
httpStatus: 404,
});
});

it('throws an HttpError carrying the upstream body on a 5xx', async () => {
mockCustomerLookup();
nock(STAGING_BASE)
.get(ABILITIES_PATH)
.query(true)
.reply(500, 'upstream exploded');

const service = createService();

await expect(service.getCustomerAbilities()).rejects.toThrow(
/status '500' - upstream exploded/u,
);
});

it.each([
['an array', []],
['an object without fiat_deposit', { fiat_payout: {} }],
['a null fiat_deposit', { fiat_deposit: null }],
])('throws when the body is %s', async (_label, body) => {
mockCustomerLookup();
nock(STAGING_BASE).get(ABILITIES_PATH).query(true).reply(200, body);

const service = createService();

await expect(service.getCustomerAbilities()).rejects.toThrow(
'Malformed response received from neo-bank abilities API',
);
});

it('shares the customer id lookup with getMicrodeposits', async () => {
const customerScope = nock(STAGING_BASE)
.get(CUSTOMER_PATH)
.once()
.reply(200, { id: 'iron-customer-1' });
nock(STAGING_BASE).get(ABILITIES_PATH).query(true).reply(200, abilities);
nock(STAGING_BASE)
.get('/neobank/customers/iron-customer-1/microdeposits')
.query(true)
.reply(200, []);

const service = createService();

await Promise.all([
service.getCustomerAbilities(),
service.getMicrodeposits(),
]);

expect(customerScope.isDone()).toBe(true);
});

it('looks the customer up again after the session profile changes', async () => {
let profileId = 'canonical-profile-1';
mockCustomerLookup();
nock(STAGING_BASE).get(ABILITIES_PATH).query(true).reply(200, abilities);
nock(STAGING_BASE)
.get('/neobank/customers/canonical-profile-2/external')
.reply(200, { id: 'iron-customer-2' });
const secondProfileScope = nock(STAGING_BASE)
.get('/neobank/customers/iron-customer-2/abilities')
.query(true)
.reply(200, abilities);

const service = createService({ canonicalProfileId: () => profileId });

await service.getCustomerAbilities();
profileId = 'canonical-profile-2';
await service.getCustomerAbilities();

expect(secondProfileScope.isDone()).toBe(true);
});

it('keeps the new profile lookup when the old profile lookup fails late', async () => {
let profileId = 'canonical-profile-1';
nock(STAGING_BASE)
.get(CUSTOMER_PATH)
.delay(50)
.reply(500, 'old lookup failed');
const newLookupScope = nock(STAGING_BASE)
.get('/neobank/customers/canonical-profile-2/external')
.once()
.reply(200, { id: 'iron-customer-2' });
nock(STAGING_BASE)
.get('/neobank/customers/iron-customer-2/abilities')
.query(true)
.times(2)
.reply(200, abilities);

const service = createService({ canonicalProfileId: () => profileId });

const oldProfileCall = service.getCustomerAbilities();
await new Promise((resolve) => setTimeout(resolve, 0));
profileId = 'canonical-profile-2';
await service.getCustomerAbilities();
await expect(oldProfileCall).rejects.toMatchObject({ httpStatus: 500 });

expect(await service.getCustomerAbilities()).toStrictEqual(abilities);
expect(newLookupScope.isDone()).toBe(true);
});
});

describe('Money Account wallet registration', () => {
it('resolves the Iron customer id via neobank customer lookup', async () => {
nock(STAGING_BASE)
Expand Down
Loading
Loading