@metamask-previews/perps-controller 1.3.0-preview-a0caca0c0 → 1.3.0-preview-afe010990

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.
Files changed (158) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/PerpsController-method-action-types.cjs +3 -0
  3. package/dist/PerpsController-method-action-types.cjs.map +1 -0
  4. package/dist/PerpsController-method-action-types.d.cts +147 -0
  5. package/dist/PerpsController-method-action-types.d.cts.map +1 -0
  6. package/dist/PerpsController-method-action-types.d.mts +147 -0
  7. package/dist/PerpsController-method-action-types.d.mts.map +1 -0
  8. package/dist/PerpsController-method-action-types.mjs +2 -0
  9. package/dist/PerpsController-method-action-types.mjs.map +1 -0
  10. package/dist/PerpsController.cjs +488 -183
  11. package/dist/PerpsController.cjs.map +1 -1
  12. package/dist/PerpsController.d.cts +91 -131
  13. package/dist/PerpsController.d.cts.map +1 -1
  14. package/dist/PerpsController.d.mts +91 -131
  15. package/dist/PerpsController.d.mts.map +1 -1
  16. package/dist/PerpsController.mjs +485 -182
  17. package/dist/PerpsController.mjs.map +1 -1
  18. package/dist/constants/eventNames.cjs +26 -0
  19. package/dist/constants/eventNames.cjs.map +1 -1
  20. package/dist/constants/eventNames.d.cts +19 -0
  21. package/dist/constants/eventNames.d.cts.map +1 -1
  22. package/dist/constants/eventNames.d.mts +19 -0
  23. package/dist/constants/eventNames.d.mts.map +1 -1
  24. package/dist/constants/eventNames.mjs +26 -0
  25. package/dist/constants/eventNames.mjs.map +1 -1
  26. package/dist/constants/hyperLiquidConfig.cjs +24 -4
  27. package/dist/constants/hyperLiquidConfig.cjs.map +1 -1
  28. package/dist/constants/hyperLiquidConfig.d.cts +8 -3
  29. package/dist/constants/hyperLiquidConfig.d.cts.map +1 -1
  30. package/dist/constants/hyperLiquidConfig.d.mts +8 -3
  31. package/dist/constants/hyperLiquidConfig.d.mts.map +1 -1
  32. package/dist/constants/hyperLiquidConfig.mjs +23 -3
  33. package/dist/constants/hyperLiquidConfig.mjs.map +1 -1
  34. package/dist/constants/myxConfig.cjs +75 -51
  35. package/dist/constants/myxConfig.cjs.map +1 -1
  36. package/dist/constants/myxConfig.d.cts +58 -19
  37. package/dist/constants/myxConfig.d.cts.map +1 -1
  38. package/dist/constants/myxConfig.d.mts +58 -19
  39. package/dist/constants/myxConfig.d.mts.map +1 -1
  40. package/dist/constants/myxConfig.mjs +74 -50
  41. package/dist/constants/myxConfig.mjs.map +1 -1
  42. package/dist/constants/perpsConfig.cjs +6 -2
  43. package/dist/constants/perpsConfig.cjs.map +1 -1
  44. package/dist/constants/perpsConfig.d.cts +5 -1
  45. package/dist/constants/perpsConfig.d.cts.map +1 -1
  46. package/dist/constants/perpsConfig.d.mts +5 -1
  47. package/dist/constants/perpsConfig.d.mts.map +1 -1
  48. package/dist/constants/perpsConfig.mjs +5 -1
  49. package/dist/constants/perpsConfig.mjs.map +1 -1
  50. package/dist/index.cjs +243 -25
  51. package/dist/index.cjs.map +1 -1
  52. package/dist/index.d.cts +48 -5
  53. package/dist/index.d.cts.map +1 -1
  54. package/dist/index.d.mts +48 -5
  55. package/dist/index.d.mts.map +1 -1
  56. package/dist/index.mjs +36 -10
  57. package/dist/index.mjs.map +1 -1
  58. package/dist/providers/AggregatedPerpsProvider.cjs +24 -0
  59. package/dist/providers/AggregatedPerpsProvider.cjs.map +1 -1
  60. package/dist/providers/AggregatedPerpsProvider.d.cts +4 -0
  61. package/dist/providers/AggregatedPerpsProvider.d.cts.map +1 -1
  62. package/dist/providers/AggregatedPerpsProvider.d.mts +4 -0
  63. package/dist/providers/AggregatedPerpsProvider.d.mts.map +1 -1
  64. package/dist/providers/AggregatedPerpsProvider.mjs +24 -0
  65. package/dist/providers/AggregatedPerpsProvider.mjs.map +1 -1
  66. package/dist/providers/HyperLiquidProvider.cjs +470 -154
  67. package/dist/providers/HyperLiquidProvider.cjs.map +1 -1
  68. package/dist/providers/HyperLiquidProvider.d.cts +11 -2
  69. package/dist/providers/HyperLiquidProvider.d.cts.map +1 -1
  70. package/dist/providers/HyperLiquidProvider.d.mts +11 -2
  71. package/dist/providers/HyperLiquidProvider.d.mts.map +1 -1
  72. package/dist/providers/HyperLiquidProvider.mjs +471 -155
  73. package/dist/providers/HyperLiquidProvider.mjs.map +1 -1
  74. package/dist/selectors.cjs +1 -1
  75. package/dist/selectors.cjs.map +1 -1
  76. package/dist/selectors.mjs +1 -1
  77. package/dist/selectors.mjs.map +1 -1
  78. package/dist/services/AccountService.cjs +1 -1
  79. package/dist/services/AccountService.cjs.map +1 -1
  80. package/dist/services/AccountService.d.cts.map +1 -1
  81. package/dist/services/AccountService.d.mts.map +1 -1
  82. package/dist/services/AccountService.mjs +2 -2
  83. package/dist/services/AccountService.mjs.map +1 -1
  84. package/dist/services/DepositService.cjs +2 -1
  85. package/dist/services/DepositService.cjs.map +1 -1
  86. package/dist/services/DepositService.d.cts.map +1 -1
  87. package/dist/services/DepositService.d.mts.map +1 -1
  88. package/dist/services/DepositService.mjs +2 -1
  89. package/dist/services/DepositService.mjs.map +1 -1
  90. package/dist/services/EligibilityService.cjs +8 -103
  91. package/dist/services/EligibilityService.cjs.map +1 -1
  92. package/dist/services/EligibilityService.d.cts +4 -15
  93. package/dist/services/EligibilityService.d.cts.map +1 -1
  94. package/dist/services/EligibilityService.d.mts +4 -15
  95. package/dist/services/EligibilityService.d.mts.map +1 -1
  96. package/dist/services/EligibilityService.mjs +8 -103
  97. package/dist/services/EligibilityService.mjs.map +1 -1
  98. package/dist/services/HyperLiquidSubscriptionService.cjs +252 -46
  99. package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
  100. package/dist/services/HyperLiquidSubscriptionService.d.cts +1 -0
  101. package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
  102. package/dist/services/HyperLiquidSubscriptionService.d.mts +1 -0
  103. package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
  104. package/dist/services/HyperLiquidSubscriptionService.mjs +252 -46
  105. package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
  106. package/dist/services/MarketDataService.cjs +2 -4
  107. package/dist/services/MarketDataService.cjs.map +1 -1
  108. package/dist/services/MarketDataService.d.cts.map +1 -1
  109. package/dist/services/MarketDataService.d.mts.map +1 -1
  110. package/dist/services/MarketDataService.mjs +2 -4
  111. package/dist/services/MarketDataService.mjs.map +1 -1
  112. package/dist/services/TradingService.cjs +72 -6
  113. package/dist/services/TradingService.cjs.map +1 -1
  114. package/dist/services/TradingService.d.cts.map +1 -1
  115. package/dist/services/TradingService.d.mts.map +1 -1
  116. package/dist/services/TradingService.mjs +72 -6
  117. package/dist/services/TradingService.mjs.map +1 -1
  118. package/dist/types/index.cjs.map +1 -1
  119. package/dist/types/index.d.cts +51 -3
  120. package/dist/types/index.d.cts.map +1 -1
  121. package/dist/types/index.d.mts +51 -3
  122. package/dist/types/index.d.mts.map +1 -1
  123. package/dist/types/index.mjs.map +1 -1
  124. package/dist/types/messenger.cjs.map +1 -1
  125. package/dist/types/messenger.d.cts +2 -1
  126. package/dist/types/messenger.d.cts.map +1 -1
  127. package/dist/types/messenger.d.mts +2 -1
  128. package/dist/types/messenger.d.mts.map +1 -1
  129. package/dist/types/messenger.mjs.map +1 -1
  130. package/dist/types/myx-types.cjs +19 -3
  131. package/dist/types/myx-types.cjs.map +1 -1
  132. package/dist/types/myx-types.d.cts +33 -2
  133. package/dist/types/myx-types.d.cts.map +1 -1
  134. package/dist/types/myx-types.d.mts +33 -2
  135. package/dist/types/myx-types.d.mts.map +1 -1
  136. package/dist/types/myx-types.mjs +6 -2
  137. package/dist/types/myx-types.mjs.map +1 -1
  138. package/dist/types/perps-types.cjs +3 -0
  139. package/dist/types/perps-types.cjs.map +1 -1
  140. package/dist/types/perps-types.d.cts +3 -3
  141. package/dist/types/perps-types.d.cts.map +1 -1
  142. package/dist/types/perps-types.d.mts +3 -3
  143. package/dist/types/perps-types.d.mts.map +1 -1
  144. package/dist/types/perps-types.mjs +3 -0
  145. package/dist/types/perps-types.mjs.map +1 -1
  146. package/dist/utils/accountUtils.cjs.map +1 -1
  147. package/dist/utils/accountUtils.d.cts.map +1 -1
  148. package/dist/utils/accountUtils.d.mts.map +1 -1
  149. package/dist/utils/accountUtils.mjs.map +1 -1
  150. package/dist/utils/myxAdapter.cjs +360 -15
  151. package/dist/utils/myxAdapter.cjs.map +1 -1
  152. package/dist/utils/myxAdapter.d.cts +94 -7
  153. package/dist/utils/myxAdapter.d.cts.map +1 -1
  154. package/dist/utils/myxAdapter.d.mts +94 -7
  155. package/dist/utils/myxAdapter.d.mts.map +1 -1
  156. package/dist/utils/myxAdapter.mjs +351 -16
  157. package/dist/utils/myxAdapter.mjs.map +1 -1
  158. package/package.json +4 -2
@@ -10,26 +10,18 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _EligibilityService_instances, _EligibilityService_geoCacheTtlMs, _EligibilityService_deps, _EligibilityService_geoLocationCache, _EligibilityService_geoLocationFetchPromise, _EligibilityService_performGeoLocationFetch;
13
+ var _EligibilityService_deps;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EligibilityService = void 0;
16
- const controller_utils_1 = require("@metamask/controller-utils");
17
16
  const perpsConfig_1 = require("../constants/perpsConfig.cjs");
18
- const utils_1 = require("../utils/index.cjs");
19
17
  const errorUtils_1 = require("../utils/errorUtils.cjs");
20
- // Geo-blocking API URLs
21
- const ON_RAMP_GEO_BLOCKING_URLS = {
22
- DEV: 'https://on-ramp.uat-api.cx.metamask.io/geolocation',
23
- PROD: 'https://on-ramp.api.cx.metamask.io/geolocation',
24
- };
25
18
  /**
26
19
  * EligibilityService
27
20
  *
28
- * Handles geo-location fetching and eligibility checking.
29
- * Manages caching to minimize API calls.
21
+ * Handles eligibility checking based on geolocation and blocked regions.
22
+ * Geolocation is sourced externally from the GeolocationController.
30
23
  *
31
24
  * Instance-based service with constructor injection of platform dependencies.
32
- * Cache is instance-scoped to support multiple service instances (e.g., testing).
33
25
  */
34
26
  class EligibilityService {
35
27
  /**
@@ -38,62 +30,24 @@ class EligibilityService {
38
30
  * @param deps - Platform dependencies for logging, metrics, etc.
39
31
  */
40
32
  constructor(deps) {
41
- _EligibilityService_instances.add(this);
42
- _EligibilityService_geoCacheTtlMs.set(this, 5 * 60 * 1000); // 5 minutes
43
33
  _EligibilityService_deps.set(this, void 0);
44
- _EligibilityService_geoLocationCache.set(this, null);
45
- _EligibilityService_geoLocationFetchPromise.set(this, null);
46
34
  __classPrivateFieldSet(this, _EligibilityService_deps, deps, "f");
47
35
  }
48
36
  /**
49
- * Fetch geo location with caching and deduplication
50
- *
51
- * @returns The user's geo location string.
52
- */
53
- async fetchGeoLocation() {
54
- // Check cache first
55
- if (__classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f")) {
56
- const cacheAge = Date.now() - __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").timestamp;
57
- if (cacheAge < __classPrivateFieldGet(this, _EligibilityService_geoCacheTtlMs, "f")) {
58
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Using cached geo location', {
59
- location: __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").location,
60
- cacheAge: `${(cacheAge / 1000).toFixed(1)}s`,
61
- });
62
- return __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").location;
63
- }
64
- }
65
- // If already fetching, return the existing promise
66
- if (__classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f")) {
67
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Geo location fetch already in progress, waiting...');
68
- return __classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f");
69
- }
70
- // Start new fetch
71
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, __classPrivateFieldGet(this, _EligibilityService_instances, "m", _EligibilityService_performGeoLocationFetch).call(this), "f");
72
- try {
73
- const location = await __classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f");
74
- return location;
75
- }
76
- finally {
77
- // Clear the promise after completion (success or failure)
78
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, null, "f");
79
- }
80
- }
81
- /**
82
- * Check if user is eligible based on geo-blocked regions
37
+ * Check if user is eligible based on geo-blocked regions.
83
38
  *
84
39
  * @param options - The eligibility check parameters.
85
40
  * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).
41
+ * @param options.geoLocation - The user's geolocation string from GeolocationController.
86
42
  * @returns True if eligible (not in blocked region), false otherwise.
87
43
  */
88
44
  async checkEligibility(options) {
89
- const { blockedRegions } = options;
45
+ const { blockedRegions, geoLocation } = options;
90
46
  try {
91
47
  __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Checking eligibility', {
92
48
  blockedRegionsCount: blockedRegions.length,
49
+ geoLocation,
93
50
  });
94
- // Returns UNKNOWN if we can't fetch the geo location
95
- const geoLocation = await this.fetchGeoLocation();
96
- // Only set to eligible if we have valid geolocation and it's not blocked
97
51
  if (geoLocation !== 'UNKNOWN') {
98
52
  const isEligible = blockedRegions.every((geoBlockedRegion) => !geoLocation
99
53
  .toUpperCase()
@@ -105,7 +59,6 @@ class EligibilityService {
105
59
  });
106
60
  return isEligible;
107
61
  }
108
- // Default to eligible if location is unknown
109
62
  return true;
110
63
  }
111
64
  catch (error) {
@@ -116,58 +69,10 @@ class EligibilityService {
116
69
  data: {},
117
70
  },
118
71
  });
119
- // Default to eligible on error
120
72
  return true;
121
73
  }
122
74
  }
123
- /**
124
- * Clear the geo-location cache
125
- * Useful for testing or forcing a fresh fetch
126
- */
127
- clearCache() {
128
- __classPrivateFieldSet(this, _EligibilityService_geoLocationCache, null, "f");
129
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, null, "f");
130
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Cache cleared');
131
- }
132
75
  }
133
76
  exports.EligibilityService = EligibilityService;
134
- _EligibilityService_geoCacheTtlMs = new WeakMap(), _EligibilityService_deps = new WeakMap(), _EligibilityService_geoLocationCache = new WeakMap(), _EligibilityService_geoLocationFetchPromise = new WeakMap(), _EligibilityService_instances = new WeakSet(), _EligibilityService_performGeoLocationFetch =
135
- /**
136
- * Perform the actual geo location fetch
137
- * Separated to allow proper promise management
138
- *
139
- * @returns The fetched geo location string, or 'UNKNOWN' on failure.
140
- */
141
- async function _EligibilityService_performGeoLocationFetch() {
142
- let location = 'UNKNOWN';
143
- try {
144
- const environment = (0, utils_1.getEnvironment)();
145
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Fetching geo location from API', {
146
- environment,
147
- });
148
- const response = await (0, controller_utils_1.successfulFetch)(ON_RAMP_GEO_BLOCKING_URLS[environment]);
149
- const textResult = await response?.text();
150
- location = textResult || 'UNKNOWN';
151
- // Cache the successful result
152
- __classPrivateFieldSet(this, _EligibilityService_geoLocationCache, {
153
- location,
154
- timestamp: Date.now(),
155
- }, "f");
156
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Geo location fetched successfully', {
157
- location,
158
- });
159
- return location;
160
- }
161
- catch (error) {
162
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").logger.error((0, errorUtils_1.ensureError)(error, 'EligibilityService.performGeoLocationFetch'), {
163
- tags: { feature: perpsConfig_1.PERPS_CONSTANTS.FeatureName },
164
- context: {
165
- name: 'EligibilityService.performGeoLocationFetch',
166
- data: {},
167
- },
168
- });
169
- // Don't cache failures
170
- return location;
171
- }
172
- };
77
+ _EligibilityService_deps = new WeakMap();
173
78
  //# sourceMappingURL=EligibilityService.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"EligibilityService.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W,EAAC,KAAK,EAAE,4CAA4C,CAAC,EAChE;YACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;YAC9C,OAAO,EAAE;gBACP,IAAI,EAAE,4CAA4C;gBAClD,IAAI,EAAE,EAAE;aACT;SACF,CACF,CAAC;QACF,uBAAuB;QACvB,OAAO,QAAQ,CAAC;IAClB,CAAC;AACH,CAAC","sourcesContent":["import { successfulFetch } from '@metamask/controller-utils';\n\nimport { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport type {\n PerpsPlatformDependencies,\n CheckEligibilityParams,\n} from '../types';\nimport { getEnvironment } from '../utils';\nimport { ensureError } from '../utils/errorUtils';\n\n// Geo-blocking API URLs\nconst ON_RAMP_GEO_BLOCKING_URLS = {\n DEV: 'https://on-ramp.uat-api.cx.metamask.io/geolocation',\n PROD: 'https://on-ramp.api.cx.metamask.io/geolocation',\n} as const;\n\n/**\n * Geo-location cache entry\n */\ntype GeoLocationCache = {\n location: string;\n timestamp: number;\n};\n\n/**\n * EligibilityService\n *\n * Handles geo-location fetching and eligibility checking.\n * Manages caching to minimize API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n * Cache is instance-scoped to support multiple service instances (e.g., testing).\n */\nexport class EligibilityService {\n readonly #geoCacheTtlMs = 5 * 60 * 1000; // 5 minutes\n\n readonly #deps: PerpsPlatformDependencies;\n\n #geoLocationCache: GeoLocationCache | null = null;\n\n #geoLocationFetchPromise: Promise<string> | null = null;\n\n /**\n * Create a new EligibilityService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Fetch geo location with caching and deduplication\n *\n * @returns The user's geo location string.\n */\n async fetchGeoLocation(): Promise<string> {\n // Check cache first\n if (this.#geoLocationCache) {\n const cacheAge = Date.now() - this.#geoLocationCache.timestamp;\n if (cacheAge < this.#geoCacheTtlMs) {\n this.#deps.debugLogger.log(\n 'EligibilityService: Using cached geo location',\n {\n location: this.#geoLocationCache.location,\n cacheAge: `${(cacheAge / 1000).toFixed(1)}s`,\n },\n );\n return this.#geoLocationCache.location;\n }\n }\n\n // If already fetching, return the existing promise\n if (this.#geoLocationFetchPromise) {\n this.#deps.debugLogger.log(\n 'EligibilityService: Geo location fetch already in progress, waiting...',\n );\n return this.#geoLocationFetchPromise;\n }\n\n // Start new fetch\n this.#geoLocationFetchPromise = this.#performGeoLocationFetch();\n\n try {\n const location = await this.#geoLocationFetchPromise;\n return location;\n } finally {\n // Clear the promise after completion (success or failure)\n this.#geoLocationFetchPromise = null;\n }\n }\n\n /**\n * Perform the actual geo location fetch\n * Separated to allow proper promise management\n *\n * @returns The fetched geo location string, or 'UNKNOWN' on failure.\n */\n async #performGeoLocationFetch(): Promise<string> {\n let location = 'UNKNOWN';\n\n try {\n const environment = getEnvironment();\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Fetching geo location from API',\n {\n environment,\n },\n );\n\n const response = await successfulFetch(\n ON_RAMP_GEO_BLOCKING_URLS[environment],\n );\n\n const textResult = await response?.text();\n location = textResult || 'UNKNOWN';\n\n // Cache the successful result\n this.#geoLocationCache = {\n location,\n timestamp: Date.now(),\n };\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Geo location fetched successfully',\n {\n location,\n },\n );\n\n return location;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.performGeoLocationFetch'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.performGeoLocationFetch',\n data: {},\n },\n },\n );\n // Don't cache failures\n return location;\n }\n }\n\n /**\n * Check if user is eligible based on geo-blocked regions\n *\n * @param options - The eligibility check parameters.\n * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).\n * @returns True if eligible (not in blocked region), false otherwise.\n */\n async checkEligibility(options: CheckEligibilityParams): Promise<boolean> {\n const { blockedRegions } = options;\n try {\n this.#deps.debugLogger.log('EligibilityService: Checking eligibility', {\n blockedRegionsCount: blockedRegions.length,\n });\n\n // Returns UNKNOWN if we can't fetch the geo location\n const geoLocation = await this.fetchGeoLocation();\n\n // Only set to eligible if we have valid geolocation and it's not blocked\n if (geoLocation !== 'UNKNOWN') {\n const isEligible = blockedRegions.every(\n (geoBlockedRegion) =>\n !geoLocation\n .toUpperCase()\n .startsWith(geoBlockedRegion.toUpperCase()),\n );\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Eligibility check completed',\n {\n geoLocation,\n isEligible,\n blockedRegions,\n },\n );\n\n return isEligible;\n }\n\n // Default to eligible if location is unknown\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.checkEligibility'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.checkEligibility',\n data: {},\n },\n },\n );\n // Default to eligible on error\n return true;\n }\n }\n\n /**\n * Clear the geo-location cache\n * Useful for testing or forcing a fresh fetch\n */\n clearCache(): void {\n this.#geoLocationCache = null;\n this.#geoLocationFetchPromise = null;\n this.#deps.debugLogger.log('EligibilityService: Cache cleared');\n }\n}\n"]}
1
+ {"version":3,"file":"EligibilityService.cjs","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,8DAA2D;AAK3D,wDAAkD;AAElD;;;;;;;GAOG;AACH,MAAa,kBAAkB;IAG7B;;;;OAIG;IACH,YAAY,IAA+B;QAPlC,2CAAiC;QAQxC,uBAAA,IAAI,4BAAS,IAAI,MAAA,CAAC;IACpB,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAA+B;QACpD,MAAM,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;QAChD,IAAI,CAAC;YACH,uBAAA,IAAI,gCAAM,CAAC,WAAW,CAAC,GAAG,CAAC,0CAA0C,EAAE;gBACrE,mBAAmB,EAAE,cAAc,CAAC,MAAM;gBAC1C,WAAW;aACZ,CAAC,CAAC;YAEH,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;gBAC9B,MAAM,UAAU,GAAG,cAAc,CAAC,KAAK,CACrC,CAAC,gBAAgB,EAAE,EAAE,CACnB,CAAC,WAAW;qBACT,WAAW,EAAE;qBACb,UAAU,CAAC,gBAAgB,CAAC,WAAW,EAAE,CAAC,CAChD,CAAC;gBAEF,uBAAA,IAAI,gCAAM,CAAC,WAAW,CAAC,GAAG,CACxB,iDAAiD,EACjD;oBACE,WAAW;oBACX,UAAU;oBACV,cAAc;iBACf,CACF,CAAC;gBAEF,OAAO,UAAU,CAAC;YACpB,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,gCAAM,CAAC,MAAM,CAAC,KAAK,CACrB,IAAA,wBAAW,EAAC,KAAK,EAAE,qCAAqC,CAAC,EACzD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,qCAAqC;oBAC3C,IAAI,EAAE,EAAE;iBACT;aACF,CACF,CAAC;YACF,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF;AA/DD,gDA+DC","sourcesContent":["import { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport type {\n PerpsPlatformDependencies,\n CheckEligibilityParams,\n} from '../types';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * EligibilityService\n *\n * Handles eligibility checking based on geolocation and blocked regions.\n * Geolocation is sourced externally from the GeolocationController.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class EligibilityService {\n readonly #deps: PerpsPlatformDependencies;\n\n /**\n * Create a new EligibilityService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Check if user is eligible based on geo-blocked regions.\n *\n * @param options - The eligibility check parameters.\n * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).\n * @param options.geoLocation - The user's geolocation string from GeolocationController.\n * @returns True if eligible (not in blocked region), false otherwise.\n */\n async checkEligibility(options: CheckEligibilityParams): Promise<boolean> {\n const { blockedRegions, geoLocation } = options;\n try {\n this.#deps.debugLogger.log('EligibilityService: Checking eligibility', {\n blockedRegionsCount: blockedRegions.length,\n geoLocation,\n });\n\n if (geoLocation !== 'UNKNOWN') {\n const isEligible = blockedRegions.every(\n (geoBlockedRegion) =>\n !geoLocation\n .toUpperCase()\n .startsWith(geoBlockedRegion.toUpperCase()),\n );\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Eligibility check completed',\n {\n geoLocation,\n isEligible,\n blockedRegions,\n },\n );\n\n return isEligible;\n }\n\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.checkEligibility'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.checkEligibility',\n data: {},\n },\n },\n );\n return true;\n }\n }\n}\n"]}
@@ -2,11 +2,10 @@ import type { PerpsPlatformDependencies, CheckEligibilityParams } from "../types
2
2
  /**
3
3
  * EligibilityService
4
4
  *
5
- * Handles geo-location fetching and eligibility checking.
6
- * Manages caching to minimize API calls.
5
+ * Handles eligibility checking based on geolocation and blocked regions.
6
+ * Geolocation is sourced externally from the GeolocationController.
7
7
  *
8
8
  * Instance-based service with constructor injection of platform dependencies.
9
- * Cache is instance-scoped to support multiple service instances (e.g., testing).
10
9
  */
11
10
  export declare class EligibilityService {
12
11
  #private;
@@ -17,23 +16,13 @@ export declare class EligibilityService {
17
16
  */
18
17
  constructor(deps: PerpsPlatformDependencies);
19
18
  /**
20
- * Fetch geo location with caching and deduplication
21
- *
22
- * @returns The user's geo location string.
23
- */
24
- fetchGeoLocation(): Promise<string>;
25
- /**
26
- * Check if user is eligible based on geo-blocked regions
19
+ * Check if user is eligible based on geo-blocked regions.
27
20
  *
28
21
  * @param options - The eligibility check parameters.
29
22
  * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).
23
+ * @param options.geoLocation - The user's geolocation string from GeolocationController.
30
24
  * @returns True if eligible (not in blocked region), false otherwise.
31
25
  */
32
26
  checkEligibility(options: CheckEligibilityParams): Promise<boolean>;
33
- /**
34
- * Clear the geo-location cache
35
- * Useful for testing or forcing a fresh fetch
36
- */
37
- clearCache(): void;
38
27
  }
39
28
  //# sourceMappingURL=EligibilityService.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EligibilityService.d.cts","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,yBAAyB,EACzB,sBAAsB,EACvB,2BAAiB;AAkBlB;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;;IAS7B;;;;OAIG;gBACS,IAAI,EAAE,yBAAyB;IAI3C;;;;OAIG;IACG,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IA4FzC;;;;;;OAMG;IACG,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,OAAO,CAAC;IAiDzE;;;OAGG;IACH,UAAU,IAAI,IAAI;CAKnB"}
1
+ {"version":3,"file":"EligibilityService.d.cts","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,yBAAyB,EACzB,sBAAsB,EACvB,2BAAiB;AAGlB;;;;;;;GAOG;AACH,qBAAa,kBAAkB;;IAG7B;;;;OAIG;gBACS,IAAI,EAAE,yBAAyB;IAI3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,OAAO,CAAC;CA2C1E"}
@@ -2,11 +2,10 @@ import type { PerpsPlatformDependencies, CheckEligibilityParams } from "../types
2
2
  /**
3
3
  * EligibilityService
4
4
  *
5
- * Handles geo-location fetching and eligibility checking.
6
- * Manages caching to minimize API calls.
5
+ * Handles eligibility checking based on geolocation and blocked regions.
6
+ * Geolocation is sourced externally from the GeolocationController.
7
7
  *
8
8
  * Instance-based service with constructor injection of platform dependencies.
9
- * Cache is instance-scoped to support multiple service instances (e.g., testing).
10
9
  */
11
10
  export declare class EligibilityService {
12
11
  #private;
@@ -17,23 +16,13 @@ export declare class EligibilityService {
17
16
  */
18
17
  constructor(deps: PerpsPlatformDependencies);
19
18
  /**
20
- * Fetch geo location with caching and deduplication
21
- *
22
- * @returns The user's geo location string.
23
- */
24
- fetchGeoLocation(): Promise<string>;
25
- /**
26
- * Check if user is eligible based on geo-blocked regions
19
+ * Check if user is eligible based on geo-blocked regions.
27
20
  *
28
21
  * @param options - The eligibility check parameters.
29
22
  * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).
23
+ * @param options.geoLocation - The user's geolocation string from GeolocationController.
30
24
  * @returns True if eligible (not in blocked region), false otherwise.
31
25
  */
32
26
  checkEligibility(options: CheckEligibilityParams): Promise<boolean>;
33
- /**
34
- * Clear the geo-location cache
35
- * Useful for testing or forcing a fresh fetch
36
- */
37
- clearCache(): void;
38
27
  }
39
28
  //# sourceMappingURL=EligibilityService.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EligibilityService.d.mts","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,yBAAyB,EACzB,sBAAsB,EACvB,2BAAiB;AAkBlB;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;;IAS7B;;;;OAIG;gBACS,IAAI,EAAE,yBAAyB;IAI3C;;;;OAIG;IACG,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IA4FzC;;;;;;OAMG;IACG,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,OAAO,CAAC;IAiDzE;;;OAGG;IACH,UAAU,IAAI,IAAI;CAKnB"}
1
+ {"version":3,"file":"EligibilityService.d.mts","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,yBAAyB,EACzB,sBAAsB,EACvB,2BAAiB;AAGlB;;;;;;;GAOG;AACH,qBAAa,kBAAkB;;IAG7B;;;;OAIG;gBACS,IAAI,EAAE,yBAAyB;IAI3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,OAAO,CAAC;CA2C1E"}
@@ -9,24 +9,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _EligibilityService_instances, _EligibilityService_geoCacheTtlMs, _EligibilityService_deps, _EligibilityService_geoLocationCache, _EligibilityService_geoLocationFetchPromise, _EligibilityService_performGeoLocationFetch;
13
- import { successfulFetch } from "@metamask/controller-utils";
12
+ var _EligibilityService_deps;
14
13
  import { PERPS_CONSTANTS } from "../constants/perpsConfig.mjs";
15
- import { getEnvironment } from "../utils/index.mjs";
16
14
  import { ensureError } from "../utils/errorUtils.mjs";
17
- // Geo-blocking API URLs
18
- const ON_RAMP_GEO_BLOCKING_URLS = {
19
- DEV: 'https://on-ramp.uat-api.cx.metamask.io/geolocation',
20
- PROD: 'https://on-ramp.api.cx.metamask.io/geolocation',
21
- };
22
15
  /**
23
16
  * EligibilityService
24
17
  *
25
- * Handles geo-location fetching and eligibility checking.
26
- * Manages caching to minimize API calls.
18
+ * Handles eligibility checking based on geolocation and blocked regions.
19
+ * Geolocation is sourced externally from the GeolocationController.
27
20
  *
28
21
  * Instance-based service with constructor injection of platform dependencies.
29
- * Cache is instance-scoped to support multiple service instances (e.g., testing).
30
22
  */
31
23
  export class EligibilityService {
32
24
  /**
@@ -35,62 +27,24 @@ export class EligibilityService {
35
27
  * @param deps - Platform dependencies for logging, metrics, etc.
36
28
  */
37
29
  constructor(deps) {
38
- _EligibilityService_instances.add(this);
39
- _EligibilityService_geoCacheTtlMs.set(this, 5 * 60 * 1000); // 5 minutes
40
30
  _EligibilityService_deps.set(this, void 0);
41
- _EligibilityService_geoLocationCache.set(this, null);
42
- _EligibilityService_geoLocationFetchPromise.set(this, null);
43
31
  __classPrivateFieldSet(this, _EligibilityService_deps, deps, "f");
44
32
  }
45
33
  /**
46
- * Fetch geo location with caching and deduplication
47
- *
48
- * @returns The user's geo location string.
49
- */
50
- async fetchGeoLocation() {
51
- // Check cache first
52
- if (__classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f")) {
53
- const cacheAge = Date.now() - __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").timestamp;
54
- if (cacheAge < __classPrivateFieldGet(this, _EligibilityService_geoCacheTtlMs, "f")) {
55
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Using cached geo location', {
56
- location: __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").location,
57
- cacheAge: `${(cacheAge / 1000).toFixed(1)}s`,
58
- });
59
- return __classPrivateFieldGet(this, _EligibilityService_geoLocationCache, "f").location;
60
- }
61
- }
62
- // If already fetching, return the existing promise
63
- if (__classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f")) {
64
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Geo location fetch already in progress, waiting...');
65
- return __classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f");
66
- }
67
- // Start new fetch
68
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, __classPrivateFieldGet(this, _EligibilityService_instances, "m", _EligibilityService_performGeoLocationFetch).call(this), "f");
69
- try {
70
- const location = await __classPrivateFieldGet(this, _EligibilityService_geoLocationFetchPromise, "f");
71
- return location;
72
- }
73
- finally {
74
- // Clear the promise after completion (success or failure)
75
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, null, "f");
76
- }
77
- }
78
- /**
79
- * Check if user is eligible based on geo-blocked regions
34
+ * Check if user is eligible based on geo-blocked regions.
80
35
  *
81
36
  * @param options - The eligibility check parameters.
82
37
  * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).
38
+ * @param options.geoLocation - The user's geolocation string from GeolocationController.
83
39
  * @returns True if eligible (not in blocked region), false otherwise.
84
40
  */
85
41
  async checkEligibility(options) {
86
- const { blockedRegions } = options;
42
+ const { blockedRegions, geoLocation } = options;
87
43
  try {
88
44
  __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Checking eligibility', {
89
45
  blockedRegionsCount: blockedRegions.length,
46
+ geoLocation,
90
47
  });
91
- // Returns UNKNOWN if we can't fetch the geo location
92
- const geoLocation = await this.fetchGeoLocation();
93
- // Only set to eligible if we have valid geolocation and it's not blocked
94
48
  if (geoLocation !== 'UNKNOWN') {
95
49
  const isEligible = blockedRegions.every((geoBlockedRegion) => !geoLocation
96
50
  .toUpperCase()
@@ -102,7 +56,6 @@ export class EligibilityService {
102
56
  });
103
57
  return isEligible;
104
58
  }
105
- // Default to eligible if location is unknown
106
59
  return true;
107
60
  }
108
61
  catch (error) {
@@ -113,57 +66,9 @@ export class EligibilityService {
113
66
  data: {},
114
67
  },
115
68
  });
116
- // Default to eligible on error
117
69
  return true;
118
70
  }
119
71
  }
120
- /**
121
- * Clear the geo-location cache
122
- * Useful for testing or forcing a fresh fetch
123
- */
124
- clearCache() {
125
- __classPrivateFieldSet(this, _EligibilityService_geoLocationCache, null, "f");
126
- __classPrivateFieldSet(this, _EligibilityService_geoLocationFetchPromise, null, "f");
127
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Cache cleared');
128
- }
129
72
  }
130
- _EligibilityService_geoCacheTtlMs = new WeakMap(), _EligibilityService_deps = new WeakMap(), _EligibilityService_geoLocationCache = new WeakMap(), _EligibilityService_geoLocationFetchPromise = new WeakMap(), _EligibilityService_instances = new WeakSet(), _EligibilityService_performGeoLocationFetch =
131
- /**
132
- * Perform the actual geo location fetch
133
- * Separated to allow proper promise management
134
- *
135
- * @returns The fetched geo location string, or 'UNKNOWN' on failure.
136
- */
137
- async function _EligibilityService_performGeoLocationFetch() {
138
- let location = 'UNKNOWN';
139
- try {
140
- const environment = getEnvironment();
141
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Fetching geo location from API', {
142
- environment,
143
- });
144
- const response = await successfulFetch(ON_RAMP_GEO_BLOCKING_URLS[environment]);
145
- const textResult = await response?.text();
146
- location = textResult || 'UNKNOWN';
147
- // Cache the successful result
148
- __classPrivateFieldSet(this, _EligibilityService_geoLocationCache, {
149
- location,
150
- timestamp: Date.now(),
151
- }, "f");
152
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").debugLogger.log('EligibilityService: Geo location fetched successfully', {
153
- location,
154
- });
155
- return location;
156
- }
157
- catch (error) {
158
- __classPrivateFieldGet(this, _EligibilityService_deps, "f").logger.error(ensureError(error, 'EligibilityService.performGeoLocationFetch'), {
159
- tags: { feature: PERPS_CONSTANTS.FeatureName },
160
- context: {
161
- name: 'EligibilityService.performGeoLocationFetch',
162
- data: {},
163
- },
164
- });
165
- // Don't cache failures
166
- return location;
167
- }
168
- };
73
+ _EligibilityService_deps = new WeakMap();
169
74
  //# sourceMappingURL=EligibilityService.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"EligibilityService.mjs","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,mCAAmC;AAE7D,OAAO,EAAE,eAAe,EAAE,qCAAiC;AAK3D,OAAO,EAAE,cAAc,EAAE,2BAAiB;AAC1C,OAAO,EAAE,WAAW,EAAE,gCAA4B;AAElD,wBAAwB;AACxB,MAAM,yBAAyB,GAAG;IAChC,GAAG,EAAE,oDAAoD;IACzD,IAAI,EAAE,gDAAgD;CAC9C,CAAC;AAUX;;;;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';\n\nimport { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport type {\n PerpsPlatformDependencies,\n CheckEligibilityParams,\n} from '../types';\nimport { getEnvironment } from '../utils';\nimport { ensureError } from '../utils/errorUtils';\n\n// Geo-blocking API URLs\nconst ON_RAMP_GEO_BLOCKING_URLS = {\n DEV: 'https://on-ramp.uat-api.cx.metamask.io/geolocation',\n PROD: 'https://on-ramp.api.cx.metamask.io/geolocation',\n} as const;\n\n/**\n * Geo-location cache entry\n */\ntype GeoLocationCache = {\n location: string;\n timestamp: number;\n};\n\n/**\n * EligibilityService\n *\n * Handles geo-location fetching and eligibility checking.\n * Manages caching to minimize API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n * Cache is instance-scoped to support multiple service instances (e.g., testing).\n */\nexport class EligibilityService {\n readonly #geoCacheTtlMs = 5 * 60 * 1000; // 5 minutes\n\n readonly #deps: PerpsPlatformDependencies;\n\n #geoLocationCache: GeoLocationCache | null = null;\n\n #geoLocationFetchPromise: Promise<string> | null = null;\n\n /**\n * Create a new EligibilityService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Fetch geo location with caching and deduplication\n *\n * @returns The user's geo location string.\n */\n async fetchGeoLocation(): Promise<string> {\n // Check cache first\n if (this.#geoLocationCache) {\n const cacheAge = Date.now() - this.#geoLocationCache.timestamp;\n if (cacheAge < this.#geoCacheTtlMs) {\n this.#deps.debugLogger.log(\n 'EligibilityService: Using cached geo location',\n {\n location: this.#geoLocationCache.location,\n cacheAge: `${(cacheAge / 1000).toFixed(1)}s`,\n },\n );\n return this.#geoLocationCache.location;\n }\n }\n\n // If already fetching, return the existing promise\n if (this.#geoLocationFetchPromise) {\n this.#deps.debugLogger.log(\n 'EligibilityService: Geo location fetch already in progress, waiting...',\n );\n return this.#geoLocationFetchPromise;\n }\n\n // Start new fetch\n this.#geoLocationFetchPromise = this.#performGeoLocationFetch();\n\n try {\n const location = await this.#geoLocationFetchPromise;\n return location;\n } finally {\n // Clear the promise after completion (success or failure)\n this.#geoLocationFetchPromise = null;\n }\n }\n\n /**\n * Perform the actual geo location fetch\n * Separated to allow proper promise management\n *\n * @returns The fetched geo location string, or 'UNKNOWN' on failure.\n */\n async #performGeoLocationFetch(): Promise<string> {\n let location = 'UNKNOWN';\n\n try {\n const environment = getEnvironment();\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Fetching geo location from API',\n {\n environment,\n },\n );\n\n const response = await successfulFetch(\n ON_RAMP_GEO_BLOCKING_URLS[environment],\n );\n\n const textResult = await response?.text();\n location = textResult || 'UNKNOWN';\n\n // Cache the successful result\n this.#geoLocationCache = {\n location,\n timestamp: Date.now(),\n };\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Geo location fetched successfully',\n {\n location,\n },\n );\n\n return location;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.performGeoLocationFetch'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.performGeoLocationFetch',\n data: {},\n },\n },\n );\n // Don't cache failures\n return location;\n }\n }\n\n /**\n * Check if user is eligible based on geo-blocked regions\n *\n * @param options - The eligibility check parameters.\n * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).\n * @returns True if eligible (not in blocked region), false otherwise.\n */\n async checkEligibility(options: CheckEligibilityParams): Promise<boolean> {\n const { blockedRegions } = options;\n try {\n this.#deps.debugLogger.log('EligibilityService: Checking eligibility', {\n blockedRegionsCount: blockedRegions.length,\n });\n\n // Returns UNKNOWN if we can't fetch the geo location\n const geoLocation = await this.fetchGeoLocation();\n\n // Only set to eligible if we have valid geolocation and it's not blocked\n if (geoLocation !== 'UNKNOWN') {\n const isEligible = blockedRegions.every(\n (geoBlockedRegion) =>\n !geoLocation\n .toUpperCase()\n .startsWith(geoBlockedRegion.toUpperCase()),\n );\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Eligibility check completed',\n {\n geoLocation,\n isEligible,\n blockedRegions,\n },\n );\n\n return isEligible;\n }\n\n // Default to eligible if location is unknown\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.checkEligibility'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.checkEligibility',\n data: {},\n },\n },\n );\n // Default to eligible on error\n return true;\n }\n }\n\n /**\n * Clear the geo-location cache\n * Useful for testing or forcing a fresh fetch\n */\n clearCache(): void {\n this.#geoLocationCache = null;\n this.#geoLocationFetchPromise = null;\n this.#deps.debugLogger.log('EligibilityService: Cache cleared');\n }\n}\n"]}
1
+ {"version":3,"file":"EligibilityService.mjs","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,qCAAiC;AAK3D,OAAO,EAAE,WAAW,EAAE,gCAA4B;AAElD;;;;;;;GAOG;AACH,MAAM,OAAO,kBAAkB;IAG7B;;;;OAIG;IACH,YAAY,IAA+B;QAPlC,2CAAiC;QAQxC,uBAAA,IAAI,4BAAS,IAAI,MAAA,CAAC;IACpB,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAA+B;QACpD,MAAM,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;QAChD,IAAI,CAAC;YACH,uBAAA,IAAI,gCAAM,CAAC,WAAW,CAAC,GAAG,CAAC,0CAA0C,EAAE;gBACrE,mBAAmB,EAAE,cAAc,CAAC,MAAM;gBAC1C,WAAW;aACZ,CAAC,CAAC;YAEH,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;gBAC9B,MAAM,UAAU,GAAG,cAAc,CAAC,KAAK,CACrC,CAAC,gBAAgB,EAAE,EAAE,CACnB,CAAC,WAAW;qBACT,WAAW,EAAE;qBACb,UAAU,CAAC,gBAAgB,CAAC,WAAW,EAAE,CAAC,CAChD,CAAC;gBAEF,uBAAA,IAAI,gCAAM,CAAC,WAAW,CAAC,GAAG,CACxB,iDAAiD,EACjD;oBACE,WAAW;oBACX,UAAU;oBACV,cAAc;iBACf,CACF,CAAC;gBAEF,OAAO,UAAU,CAAC;YACpB,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,gCAAM,CAAC,MAAM,CAAC,KAAK,CACrB,WAAW,CAAC,KAAK,EAAE,qCAAqC,CAAC,EACzD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,eAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,qCAAqC;oBAC3C,IAAI,EAAE,EAAE;iBACT;aACF,CACF,CAAC;YACF,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport type {\n PerpsPlatformDependencies,\n CheckEligibilityParams,\n} from '../types';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * EligibilityService\n *\n * Handles eligibility checking based on geolocation and blocked regions.\n * Geolocation is sourced externally from the GeolocationController.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class EligibilityService {\n readonly #deps: PerpsPlatformDependencies;\n\n /**\n * Create a new EligibilityService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Check if user is eligible based on geo-blocked regions.\n *\n * @param options - The eligibility check parameters.\n * @param options.blockedRegions - List of blocked region codes (e.g., ['US', 'CN']).\n * @param options.geoLocation - The user's geolocation string from GeolocationController.\n * @returns True if eligible (not in blocked region), false otherwise.\n */\n async checkEligibility(options: CheckEligibilityParams): Promise<boolean> {\n const { blockedRegions, geoLocation } = options;\n try {\n this.#deps.debugLogger.log('EligibilityService: Checking eligibility', {\n blockedRegionsCount: blockedRegions.length,\n geoLocation,\n });\n\n if (geoLocation !== 'UNKNOWN') {\n const isEligible = blockedRegions.every(\n (geoBlockedRegion) =>\n !geoLocation\n .toUpperCase()\n .startsWith(geoBlockedRegion.toUpperCase()),\n );\n\n this.#deps.debugLogger.log(\n 'EligibilityService: Eligibility check completed',\n {\n geoLocation,\n isEligible,\n blockedRegions,\n },\n );\n\n return isEligible;\n }\n\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'EligibilityService.checkEligibility'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'EligibilityService.checkEligibility',\n data: {},\n },\n },\n );\n return true;\n }\n }\n}\n"]}