@metamask-previews/ramps-controller 15.1.0-preview-685219bbf → 15.1.0-preview-bbd2df29f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,14 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
+ - Add pure quote-selection helpers `getSmartSelectedQuote` and `fitsProviderLimits`, the amount validator `validateBuyAmount`, and a `BuyAmountValidation` type so headless-buy consumers can share the controller's provider-agnostic quote ranking (`preferredProviderIds` then reliability then price then first surviving candidate), scope-aware in-app filtering, and per-provider fiat-limit enforcement instead of re-deriving them ([#9414](https://github.com/MetaMask/core/pull/9414))
12
13
  - Add pure provider-availability helpers `providerServesAsset`, `getProvidersServingAsset`, `regionHasProviderForAsset`, and `isFiatDepositAvailable` so headless-buy consumers can share the controller's case-insensitive CAIP-19 asset matching and scope-aware region/availability gating instead of re-deriving it, keeping the two from disagreeing ([#9409](https://github.com/MetaMask/core/pull/9409))
13
14
  - Add pure quote-classification helpers `isExternalBrowserQuote`, `isCustomActionQuote`, and `isInAppOnlyQuote` so consumers can share the controller's in-app-vs-external browser-mode classification without owning host redirect/deeplink concerns ([#9409](https://github.com/MetaMask/core/pull/9409))
14
15
  - Add pure error-normalization helpers `getErrorMessage`, `extractExplicitTypedError`, and `normalizeToTypedError` (with a `TypedError<Code>` type) so consumers can share error-shape extraction while keeping their own error-code taxonomy ([#9409](https://github.com/MetaMask/core/pull/9409))
15
16
 
16
17
  ### Changed
17
18
 
19
+ - `RampsController.getQuotes` now derives its provider-widening decision from the `getProviderScope` scope (`off` stays native-only; `in-app`/`all` widen) rather than the `restrictToKnownOrNativeProviders` flag, and picks the widened auto-selected quote via the shared `getSmartSelectedQuote`, feeding in the user's completed-order provider preference so a returning user's previously-used provider is preferred over the reliability/price winner ([#9414](https://github.com/MetaMask/core/pull/9414))
18
20
  - `RampsController` now derives its internal region provider-asset matching and quote in-app/custom-action/external filtering from the shared `providerAvailability` and `quoteClassification` helpers, so the exposed helpers stay behaviourally identical to the controller's own selection ([#9409](https://github.com/MetaMask/core/pull/9409))
19
21
 
22
+ ### Deprecated
23
+
24
+ - **`RampsController.getQuotes`'s `restrictToKnownOrNativeProviders` option is deprecated** in favor of provider-class scope (`getProviderScope`): `off` keeps native-only auto-selection and `in-app`/`all` widen, so the flag is redundant. It is still honored for one release (like `autoSelectProvider`, it routes a no-`providers` request through the gated path) and will be removed in a later major ([#9414](https://github.com/MetaMask/core/pull/9414))
25
+
20
26
  ## [15.1.0]
21
27
 
22
28
  ### Added
@@ -16,7 +16,7 @@ exports.RampsController = exports.getInternalOrderCode = exports.normalizeProvid
16
16
  const base_controller_1 = require("@metamask/base-controller");
17
17
  const controller_utils_1 = require("@metamask/controller-utils");
18
18
  const providerAvailability_1 = require("./providerAvailability.cjs");
19
- const quoteClassification_1 = require("./quoteClassification.cjs");
19
+ const quoteSelection_1 = require("./quoteSelection.cjs");
20
20
  const rampsErrorCodes_1 = require("./rampsErrorCodes.cjs");
21
21
  const RampsService_1 = require("./RampsService.cjs");
22
22
  const RequestCache_1 = require("./RequestCache.cjs");
@@ -993,11 +993,17 @@ class RampsController extends base_controller_1.BaseController {
993
993
  * during auto-selection, in priority order (e.g. derived by the caller
994
994
  * from completed-order history). Only used when `autoSelectProvider` is
995
995
  * true and `providers` is omitted.
996
- * @param options.restrictToKnownOrNativeProviders - Headless-buy v0 gating. When
997
- * true, auto-selection resolves only a native provider, and an explicitly
998
- * passed `providers` list is filtered to those supporting the region and
999
- * asset. If nothing qualifies, `getQuotes` returns an empty response
1000
- * instead of quoting other providers.
996
+ * @param options.restrictToKnownOrNativeProviders - **Deprecated** (still
997
+ * honored for one release). Headless-buy v0 gating: when true,
998
+ * auto-selection resolves only a native provider, and an explicitly passed
999
+ * `providers` list is filtered to those supporting the region and asset; if
1000
+ * nothing qualifies, `getQuotes` returns an empty response instead of
1001
+ * quoting other providers. Provider-class scope (`getProviderScope`) is now
1002
+ * the gate (`off` stays native-only; `in-app`/`all` widen), so this flag is
1003
+ * redundant and will be removed in a later major. Like `autoSelectProvider`,
1004
+ * it still routes a no-`providers` request through the gated path so
1005
+ * existing callers keep working; new callers should rely on the scope
1006
+ * instead of passing this flag.
1001
1007
  * @param options.redirectUrl - Optional redirect URL after order completion.
1002
1008
  * @param options.action - The ramp action type. Defaults to 'buy'.
1003
1009
  * @param options.forceRefresh - Whether to bypass cache.
@@ -1018,17 +1024,24 @@ class RampsController extends base_controller_1.BaseController {
1018
1024
  if (normalizedAssetIdForValidation === '') {
1019
1025
  throw new Error('assetId is required.');
1020
1026
  }
1021
- // When a non-`off` provider scope is active, widen the native-only
1022
- // auto-selection path to every supporting provider and pick the best in-app
1023
- // quote from the results (in-app vs external is only knowable per-quote via
1024
- // `buyWidget.browser`). Only the auto-select/restrict path that MM Pay's
1027
+ // Provider-class scope is the gate (P2.M8): `off` keeps native-only
1028
+ // auto-selection (the kill switch); `in-app`/`all` widen the auto-selection
1029
+ // path to every supporting provider and pick the best quote from the
1030
+ // results (in-app vs external is only knowable per-quote via
1031
+ // `buyWidget.browser`). Only the auto-select path that MM Pay's
1025
1032
  // `getRampsQuote` uses is affected; explicit-`providers` callers and the
1026
1033
  // plain all-provider path are untouched.
1027
1034
  const providerScope = __classPrivateFieldGet(this, _RampsController_getProviderScope, "f").call(this);
1028
- const widenToInAppProviders = providerScope !== 'off' &&
1029
- !options.providers &&
1035
+ const scopeWidens = providerScope !== 'off';
1036
+ // The auto-select entry path. `restrictToKnownOrNativeProviders` is
1037
+ // `@deprecated` (P2.M8) but still honored here: like `autoSelectProvider`,
1038
+ // it routes a no-explicit-`providers` request through the resolved/gated
1039
+ // path, so existing callers (TPC `getRampsQuote`) are unaffected until the
1040
+ // option is removed in a later major.
1041
+ const usesAutoSelectPath = !options.providers &&
1030
1042
  (options.autoSelectProvider === true ||
1031
1043
  options.restrictToKnownOrNativeProviders === true);
1044
+ const widenToInAppProviders = scopeWidens && usesAutoSelectPath;
1032
1045
  let providersToUse;
1033
1046
  let inAppProviderCatalog = this.state.providers.data;
1034
1047
  if (options.providers) {
@@ -1085,7 +1098,8 @@ class RampsController extends base_controller_1.BaseController {
1085
1098
  // `restrictToKnownOrNativeProviders`), and an empty supporting set must not
1086
1099
  // fall through to unfiltered quotes from providers that do not support the
1087
1100
  // asset.
1088
- if ((options.restrictToKnownOrNativeProviders || widenToInAppProviders) &&
1101
+ if ((widenToInAppProviders ||
1102
+ options.restrictToKnownOrNativeProviders === true) &&
1089
1103
  providersToUse.length === 0) {
1090
1104
  return { success: [], sorted: [], error: [], customActions: [] };
1091
1105
  }
@@ -1900,59 +1914,13 @@ _RampsController_requestCacheTTL = new WeakMap(), _RampsController_requestCacheM
1900
1914
  state.userRegion = userRegion;
1901
1915
  });
1902
1916
  }, _RampsController_pickInAppQuote = function _RampsController_pickInAppQuote(response, { scope, amount, fiat, providers, }) {
1903
- const providerByCode = new Map(providers.map((provider) => [
1904
- normalizeProviderCode(provider.id),
1905
- provider,
1906
- ]));
1907
- const customActionProviderCodes = new Set(response.customActions.map((action) => normalizeProviderCode(action.buy.providerId)));
1908
- const fitsProviderLimits = (quote) => {
1909
- const provider = providerByCode.get(normalizeProviderCode(quote.provider));
1910
- const limit = provider?.limits?.fiat?.[fiat]?.[quote.quote.paymentMethod];
1911
- if (!limit) {
1912
- // No published limits for this provider/payment method: treat as
1913
- // eligible and let the provider enforce limits at checkout.
1914
- return true;
1915
- }
1916
- return amount >= limit.minAmount && amount <= limit.maxAmount;
1917
- };
1918
- const isEligible = (quote) => {
1919
- // `all` (Phase 2) skips the in-app-only exclusions; both scopes still
1920
- // enforce provider limits up front.
1921
- if (scope !== 'all') {
1922
- const providerCode = normalizeProviderCode(quote.provider);
1923
- if (customActionProviderCodes.has(providerCode)) {
1924
- return false;
1925
- }
1926
- // Custom-action and external-browser classification is shared with the
1927
- // consuming client via `quoteClassification` so both filter identically.
1928
- if ((0, quoteClassification_1.isCustomActionQuote)(quote) || (0, quoteClassification_1.isExternalBrowserQuote)(quote)) {
1929
- return false;
1930
- }
1931
- }
1932
- return fitsProviderLimits(quote);
1933
- };
1934
- const candidates = response.success.filter(isEligible);
1935
- if (candidates.length === 0) {
1936
- return undefined;
1937
- }
1938
- const candidateByCode = new Map(candidates.map((quote) => [normalizeProviderCode(quote.provider), quote]));
1939
- const pickBySortOrder = (sortBy) => {
1940
- const order = response.sorted.find((entry) => entry.sortBy === sortBy)?.ids;
1941
- if (!order) {
1942
- return undefined;
1943
- }
1944
- for (const providerId of order) {
1945
- const match = candidateByCode.get(normalizeProviderCode(providerId));
1946
- if (match) {
1947
- return match;
1948
- }
1949
- }
1950
- return undefined;
1951
- };
1952
- // Reliability first, then price, then the first surviving candidate.
1953
- return (pickBySortOrder('reliability') ??
1954
- pickBySortOrder('price') ??
1955
- candidates[0]);
1917
+ return (0, quoteSelection_1.getSmartSelectedQuote)(response, {
1918
+ scope,
1919
+ amount,
1920
+ fiat,
1921
+ providers,
1922
+ preferredProviderIds: __classPrivateFieldGet(this, _RampsController_instances, "m", _RampsController_getPreferredProviderIdsFromOrders).call(this),
1923
+ });
1956
1924
  }, _RampsController_getSupportingProvidersForRegion =
1957
1925
  /**
1958
1926
  * Returns the region's providers that support the given asset, plus the full