@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.
- package/CHANGELOG.md +9 -0
- package/dist/PerpsController-method-action-types.cjs +3 -0
- package/dist/PerpsController-method-action-types.cjs.map +1 -0
- package/dist/PerpsController-method-action-types.d.cts +147 -0
- package/dist/PerpsController-method-action-types.d.cts.map +1 -0
- package/dist/PerpsController-method-action-types.d.mts +147 -0
- package/dist/PerpsController-method-action-types.d.mts.map +1 -0
- package/dist/PerpsController-method-action-types.mjs +2 -0
- package/dist/PerpsController-method-action-types.mjs.map +1 -0
- package/dist/PerpsController.cjs +488 -183
- package/dist/PerpsController.cjs.map +1 -1
- package/dist/PerpsController.d.cts +91 -131
- package/dist/PerpsController.d.cts.map +1 -1
- package/dist/PerpsController.d.mts +91 -131
- package/dist/PerpsController.d.mts.map +1 -1
- package/dist/PerpsController.mjs +485 -182
- package/dist/PerpsController.mjs.map +1 -1
- package/dist/constants/eventNames.cjs +26 -0
- package/dist/constants/eventNames.cjs.map +1 -1
- package/dist/constants/eventNames.d.cts +19 -0
- package/dist/constants/eventNames.d.cts.map +1 -1
- package/dist/constants/eventNames.d.mts +19 -0
- package/dist/constants/eventNames.d.mts.map +1 -1
- package/dist/constants/eventNames.mjs +26 -0
- package/dist/constants/eventNames.mjs.map +1 -1
- package/dist/constants/hyperLiquidConfig.cjs +24 -4
- package/dist/constants/hyperLiquidConfig.cjs.map +1 -1
- package/dist/constants/hyperLiquidConfig.d.cts +8 -3
- package/dist/constants/hyperLiquidConfig.d.cts.map +1 -1
- package/dist/constants/hyperLiquidConfig.d.mts +8 -3
- package/dist/constants/hyperLiquidConfig.d.mts.map +1 -1
- package/dist/constants/hyperLiquidConfig.mjs +23 -3
- package/dist/constants/hyperLiquidConfig.mjs.map +1 -1
- package/dist/constants/myxConfig.cjs +75 -51
- package/dist/constants/myxConfig.cjs.map +1 -1
- package/dist/constants/myxConfig.d.cts +58 -19
- package/dist/constants/myxConfig.d.cts.map +1 -1
- package/dist/constants/myxConfig.d.mts +58 -19
- package/dist/constants/myxConfig.d.mts.map +1 -1
- package/dist/constants/myxConfig.mjs +74 -50
- package/dist/constants/myxConfig.mjs.map +1 -1
- package/dist/constants/perpsConfig.cjs +6 -2
- package/dist/constants/perpsConfig.cjs.map +1 -1
- package/dist/constants/perpsConfig.d.cts +5 -1
- package/dist/constants/perpsConfig.d.cts.map +1 -1
- package/dist/constants/perpsConfig.d.mts +5 -1
- package/dist/constants/perpsConfig.d.mts.map +1 -1
- package/dist/constants/perpsConfig.mjs +5 -1
- package/dist/constants/perpsConfig.mjs.map +1 -1
- package/dist/index.cjs +243 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +48 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +36 -10
- package/dist/index.mjs.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.cjs +24 -0
- package/dist/providers/AggregatedPerpsProvider.cjs.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.cts +4 -0
- package/dist/providers/AggregatedPerpsProvider.d.cts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.mts +4 -0
- package/dist/providers/AggregatedPerpsProvider.d.mts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.mjs +24 -0
- package/dist/providers/AggregatedPerpsProvider.mjs.map +1 -1
- package/dist/providers/HyperLiquidProvider.cjs +470 -154
- package/dist/providers/HyperLiquidProvider.cjs.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.cts +11 -2
- package/dist/providers/HyperLiquidProvider.d.cts.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.mts +11 -2
- package/dist/providers/HyperLiquidProvider.d.mts.map +1 -1
- package/dist/providers/HyperLiquidProvider.mjs +471 -155
- package/dist/providers/HyperLiquidProvider.mjs.map +1 -1
- package/dist/selectors.cjs +1 -1
- package/dist/selectors.cjs.map +1 -1
- package/dist/selectors.mjs +1 -1
- package/dist/selectors.mjs.map +1 -1
- package/dist/services/AccountService.cjs +1 -1
- package/dist/services/AccountService.cjs.map +1 -1
- package/dist/services/AccountService.d.cts.map +1 -1
- package/dist/services/AccountService.d.mts.map +1 -1
- package/dist/services/AccountService.mjs +2 -2
- package/dist/services/AccountService.mjs.map +1 -1
- package/dist/services/DepositService.cjs +2 -1
- package/dist/services/DepositService.cjs.map +1 -1
- package/dist/services/DepositService.d.cts.map +1 -1
- package/dist/services/DepositService.d.mts.map +1 -1
- package/dist/services/DepositService.mjs +2 -1
- package/dist/services/DepositService.mjs.map +1 -1
- package/dist/services/EligibilityService.cjs +8 -103
- package/dist/services/EligibilityService.cjs.map +1 -1
- package/dist/services/EligibilityService.d.cts +4 -15
- package/dist/services/EligibilityService.d.cts.map +1 -1
- package/dist/services/EligibilityService.d.mts +4 -15
- package/dist/services/EligibilityService.d.mts.map +1 -1
- package/dist/services/EligibilityService.mjs +8 -103
- package/dist/services/EligibilityService.mjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.cjs +252 -46
- package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.cts +1 -0
- package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.mts +1 -0
- package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.mjs +252 -46
- package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
- package/dist/services/MarketDataService.cjs +2 -4
- package/dist/services/MarketDataService.cjs.map +1 -1
- package/dist/services/MarketDataService.d.cts.map +1 -1
- package/dist/services/MarketDataService.d.mts.map +1 -1
- package/dist/services/MarketDataService.mjs +2 -4
- package/dist/services/MarketDataService.mjs.map +1 -1
- package/dist/services/TradingService.cjs +72 -6
- package/dist/services/TradingService.cjs.map +1 -1
- package/dist/services/TradingService.d.cts.map +1 -1
- package/dist/services/TradingService.d.mts.map +1 -1
- package/dist/services/TradingService.mjs +72 -6
- package/dist/services/TradingService.mjs.map +1 -1
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.d.cts +51 -3
- package/dist/types/index.d.cts.map +1 -1
- package/dist/types/index.d.mts +51 -3
- package/dist/types/index.d.mts.map +1 -1
- package/dist/types/index.mjs.map +1 -1
- package/dist/types/messenger.cjs.map +1 -1
- package/dist/types/messenger.d.cts +2 -1
- package/dist/types/messenger.d.cts.map +1 -1
- package/dist/types/messenger.d.mts +2 -1
- package/dist/types/messenger.d.mts.map +1 -1
- package/dist/types/messenger.mjs.map +1 -1
- package/dist/types/myx-types.cjs +19 -3
- package/dist/types/myx-types.cjs.map +1 -1
- package/dist/types/myx-types.d.cts +33 -2
- package/dist/types/myx-types.d.cts.map +1 -1
- package/dist/types/myx-types.d.mts +33 -2
- package/dist/types/myx-types.d.mts.map +1 -1
- package/dist/types/myx-types.mjs +6 -2
- package/dist/types/myx-types.mjs.map +1 -1
- package/dist/types/perps-types.cjs +3 -0
- package/dist/types/perps-types.cjs.map +1 -1
- package/dist/types/perps-types.d.cts +3 -3
- package/dist/types/perps-types.d.cts.map +1 -1
- package/dist/types/perps-types.d.mts +3 -3
- package/dist/types/perps-types.d.mts.map +1 -1
- package/dist/types/perps-types.mjs +3 -0
- package/dist/types/perps-types.mjs.map +1 -1
- package/dist/utils/accountUtils.cjs.map +1 -1
- package/dist/utils/accountUtils.d.cts.map +1 -1
- package/dist/utils/accountUtils.d.mts.map +1 -1
- package/dist/utils/accountUtils.mjs.map +1 -1
- package/dist/utils/myxAdapter.cjs +360 -15
- package/dist/utils/myxAdapter.cjs.map +1 -1
- package/dist/utils/myxAdapter.d.cts +94 -7
- package/dist/utils/myxAdapter.d.cts.map +1 -1
- package/dist/utils/myxAdapter.d.mts +94 -7
- package/dist/utils/myxAdapter.d.mts.map +1 -1
- package/dist/utils/myxAdapter.mjs +351 -16
- package/dist/utils/myxAdapter.mjs.map +1 -1
- 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
|
|
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
|
|
29
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
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.cjs","sourceRoot":"","sources":["../../src/services/EligibilityService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
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
|
|
6
|
-
*
|
|
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
|
-
*
|
|
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":"
|
|
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
|
|
6
|
-
*
|
|
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
|
-
*
|
|
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":"
|
|
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
|
|
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
|
|
26
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
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,
|
|
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"]}
|