@metamask-previews/perps-controller 3.0.0-preview-d5ac72227 → 3.0.0-preview-2e231093e
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/dist/services/AccountService.cjs.map +1 -1
- package/dist/services/AccountService.d.cts +1 -1
- package/dist/services/AccountService.d.cts.map +1 -1
- package/dist/services/AccountService.d.mts +1 -1
- package/dist/services/AccountService.d.mts.map +1 -1
- package/dist/services/AccountService.mjs.map +1 -1
- package/dist/services/DataLakeService.cjs.map +1 -1
- package/dist/services/DataLakeService.d.cts +1 -1
- package/dist/services/DataLakeService.d.cts.map +1 -1
- package/dist/services/DataLakeService.d.mts +1 -1
- package/dist/services/DataLakeService.d.mts.map +1 -1
- package/dist/services/DataLakeService.mjs.map +1 -1
- package/dist/services/FeatureFlagConfigurationService.cjs.map +1 -1
- package/dist/services/FeatureFlagConfigurationService.d.cts +1 -1
- package/dist/services/FeatureFlagConfigurationService.d.cts.map +1 -1
- package/dist/services/FeatureFlagConfigurationService.d.mts +1 -1
- package/dist/services/FeatureFlagConfigurationService.d.mts.map +1 -1
- package/dist/services/FeatureFlagConfigurationService.mjs.map +1 -1
- package/dist/services/HyperLiquidClientService.cjs.map +1 -1
- package/dist/services/HyperLiquidClientService.mjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.cts +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.mts +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
- package/dist/services/MarketDataService.cjs.map +1 -1
- package/dist/services/MarketDataService.d.cts +1 -1
- package/dist/services/MarketDataService.d.cts.map +1 -1
- package/dist/services/MarketDataService.d.mts +1 -1
- package/dist/services/MarketDataService.d.mts.map +1 -1
- package/dist/services/MarketDataService.mjs.map +1 -1
- package/dist/services/TradingService.cjs.map +1 -1
- package/dist/services/TradingService.d.cts +1 -1
- package/dist/services/TradingService.d.cts.map +1 -1
- package/dist/services/TradingService.d.mts +1 -1
- package/dist/services/TradingService.d.mts.map +1 -1
- package/dist/services/TradingService.mjs.map +1 -1
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.d.cts +1 -1
- package/dist/types/index.d.cts.map +1 -1
- package/dist/types/index.d.mts +1 -1
- package/dist/types/index.d.mts.map +1 -1
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/hyperLiquidAdapter.cjs +1 -1
- package/dist/utils/hyperLiquidAdapter.cjs.map +1 -1
- package/dist/utils/hyperLiquidAdapter.d.cts.map +1 -1
- package/dist/utils/hyperLiquidAdapter.d.mts.map +1 -1
- package/dist/utils/hyperLiquidAdapter.mjs +1 -1
- package/dist/utils/hyperLiquidAdapter.mjs.map +1 -1
- package/dist/utils/marketDataTransform.cjs +1 -1
- package/dist/utils/marketDataTransform.cjs.map +1 -1
- package/dist/utils/marketDataTransform.d.cts.map +1 -1
- package/dist/utils/marketDataTransform.d.mts.map +1 -1
- package/dist/utils/marketDataTransform.mjs +1 -1
- package/dist/utils/marketDataTransform.mjs.map +1 -1
- package/dist/utils/orderCalculations.cjs +1 -1
- package/dist/utils/orderCalculations.cjs.map +1 -1
- package/dist/utils/orderCalculations.d.cts.map +1 -1
- package/dist/utils/orderCalculations.d.mts.map +1 -1
- package/dist/utils/orderCalculations.mjs +1 -1
- package/dist/utils/orderCalculations.mjs.map +1 -1
- package/dist/utils/rewardsUtils.cjs.map +1 -1
- package/dist/utils/rewardsUtils.d.cts.map +1 -1
- package/dist/utils/rewardsUtils.d.mts.map +1 -1
- package/dist/utils/rewardsUtils.mjs.map +1 -1
- package/package.json +18 -18
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.cjs","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA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AAI,EAAE,EAAE,MAAM,EAAE;iBACjB;aACF,CACF,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AA3XD,wCA2XC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport type { ServiceContext } from './ServiceContext';\nimport {\n PERPS_EVENT_PROPERTY,\n PERPS_EVENT_VALUE,\n} from '../constants/eventNames';\nimport { USDC_SYMBOL } from '../constants/hyperLiquidConfig';\nimport {\n PERPS_CONSTANTS,\n WITHDRAWAL_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes';\nimport {\n PerpsAnalyticsEvent,\n PerpsTraceNames,\n PerpsTraceOperations,\n} from '../types';\nimport type {\n PerpsProvider,\n WithdrawParams,\n WithdrawResult,\n PerpsPlatformDependencies,\n} from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport type { TransactionStatus } from '../types/transactionTypes';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * AccountService\n *\n * Handles account operations (deposits, withdrawals).\n * Stateless service that delegates to provider.\n * Controller handles state updates and analytics.\n *\n * Instance-based service with constructor injection of platform dependencies\n * and messenger for inter-controller communication.\n */\nexport class AccountService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new AccountService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Withdraw funds with full orchestration\n * Handles tracing, state management, analytics, and account refresh\n *\n * @param options - The withdrawal configuration.\n * @param options.provider - The perps provider to execute the withdrawal.\n * @param options.params - The withdrawal parameters (amount, destination, etc.).\n * @param options.context - The service context for tracing and dependencies.\n * @param options.refreshAccountState - Callback to refresh account state after withdrawal.\n * @returns The withdrawal result containing success status and transaction details.\n */\n async withdraw(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n context: ServiceContext;\n refreshAccountState: () => Promise<void>;\n }): Promise<WithdrawResult> {\n const { provider, params, context, refreshAccountState } = options;\n\n const traceId = uuidv4();\n const startTime = this.#deps.performance.now();\n let traceData:\n | {\n success: boolean;\n error?: string;\n txHash?: string;\n withdrawalId?: string;\n }\n | undefined;\n\n // Generate withdrawal request ID for tracking\n const currentWithdrawalId = `withdraw-${Date.now()}-${Math.random()\n .toString(36)\n .substring(2, 11)}`;\n\n try {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n op: PerpsTraceOperations.Operation,\n tags: {\n assetId: params.assetId ?? '',\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n\n this.#deps.debugLogger.log('AccountService: STARTING WITHDRAWAL', {\n params,\n timestamp: new Date().toISOString(),\n assetId: params.assetId,\n amount: params.amount,\n destination: params.destination,\n activeProvider: context.tracingContext.provider,\n isTestnet: context.tracingContext.isTestnet,\n });\n\n // Set withdrawal in progress\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.withdrawInProgress = true;\n\n // Calculate net amount after fees\n const grossAmount = parseFloat(params.amount);\n const feeAmount = WITHDRAWAL_CONSTANTS.DefaultFeeAmount;\n const netAmount = Math.max(0, grossAmount - feeAmount);\n\n // Get current account address via messenger\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n const accountAddress = evmAccount?.address ?? 'unknown';\n\n this.#deps.debugLogger.log(\n 'AccountService: Creating withdrawal request',\n {\n accountAddress,\n hasEvmAccount: Boolean(evmAccount),\n evmAccountAddress: evmAccount?.address,\n amount: netAmount.toString(),\n },\n );\n\n // Add withdrawal request to tracking\n const withdrawalRequest = {\n id: currentWithdrawalId,\n timestamp: Date.now(),\n amount: netAmount.toString(), // Use net amount (after fees)\n asset: USDC_SYMBOL,\n accountAddress, // Track which account initiated withdrawal\n success: false, // Will be updated when transaction completes\n txHash: undefined,\n status: 'pending' as TransactionStatus,\n destination: params.destination,\n transactionId: undefined, // Will be set to withdrawalId when available\n };\n\n state.withdrawalRequests.unshift(withdrawalRequest);\n });\n }\n\n this.#deps.debugLogger.log('AccountService: DELEGATING TO PROVIDER', {\n provider: context.tracingContext.provider,\n providerReady: Boolean(provider),\n });\n\n // Execute withdrawal\n const result = await provider.withdraw(params);\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL RESULT', {\n success: result.success,\n error: result.error,\n txHash: result.txHash,\n timestamp: new Date().toISOString(),\n });\n\n // Update state based on result\n if (result.success) {\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = null;\n state.lastUpdateTimestamp = Date.now();\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n\n if (result.txHash) {\n // Direct completion: remove from queue and record the txHash\n // so the polling hook won't re-match this completion.\n // We do NOT update lastCompletedWithdrawalTimestamp here\n // because Date.now() is local device time while the FIFO guard\n // compares against API server timestamps — mixing domains can\n // poison the guard. The txHash exclusion alone prevents\n // re-matching since the item is also spliced from the queue.\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n\n state.lastCompletedWithdrawalTxHashes.push(result.txHash);\n\n const hasOtherPending = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n state.withdrawInProgress = hasOtherPending;\n } else if (withdrawalRequestIndex !== -1) {\n const requestToUpdate =\n state.withdrawalRequests[withdrawalRequestIndex];\n // Withdrawal is bridging (no txHash yet)\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\n\n // Set lastWithdrawResult when submission is successful (even if bridging)\n // This triggers the \"confirmed\" toast telling user funds arrive in ~5 mins\n state.lastWithdrawResult = {\n success: true,\n txHash: result.txHash ?? '',\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n error: '',\n };\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {\n txHash: result.txHash,\n amount: params.amount,\n assetId: params.assetId,\n withdrawalId: result.withdrawalId,\n });\n\n // Track withdrawal transaction executed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.EXECUTED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n },\n );\n\n // Trigger account state refresh after withdrawal\n refreshAccountState().catch((refreshError) => {\n this.#deps.logger.error(\n ensureError(refreshError, 'AccountService.withdraw'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { operation: 'refreshAccountState' },\n },\n },\n );\n });\n\n // Invalidate standalone caches so external hooks (e.g., usePerpsPositionForAsset) refresh\n this.#deps.cacheInvalidator.invalidate({ cacheType: 'accountState' });\n\n traceData = {\n success: true,\n txHash: result.txHash ?? '',\n withdrawalId: result.withdrawalId ?? '',\n };\n\n return result;\n }\n\n // Handle failure\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED,\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL FAILED', {\n error: result.error,\n params,\n });\n\n // Track withdrawal transaction failed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: result.error ?? 'Unknown error',\n },\n );\n\n traceData = {\n success: false,\n error: result.error ?? 'Unknown error',\n };\n\n return result;\n } catch (error) {\n const errorMessage =\n error instanceof Error\n ? error.message\n : PERPS_ERROR_CODES.WITHDRAW_FAILED;\n\n this.#deps.logger.error(ensureError(error, 'AccountService.withdraw'), {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { assetId: params.assetId, amount: params.amount },\n },\n });\n\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = errorMessage;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: errorMessage,\n amount: '0',\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n // Track withdrawal transaction failed (catch block)\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: params.amount,\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: errorMessage,\n },\n );\n\n traceData = {\n success: false,\n error: errorMessage,\n };\n\n return { success: false, error: errorMessage };\n } finally {\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n data: traceData,\n });\n }\n }\n\n /**\n * Validate withdrawal parameters\n *\n * @param options - The validation configuration.\n * @param options.provider - The perps provider to validate against.\n * @param options.params - The withdrawal parameters to validate.\n * @returns An object indicating whether the withdrawal is valid, with an optional error message.\n */\n async validateWithdrawal(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n }): Promise<{ isValid: boolean; error?: string }> {\n const { provider, params } = options;\n\n try {\n return await provider.validateWithdrawal(params);\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'AccountService.validateWithdrawal'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.validateWithdrawal',\n data: { params },\n },\n },\n );\n throw error;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountService.cjs","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAoC;AAEpC,4DAGiC;AACjC,0EAA6D;AAC7D,8DAGkC;AAClC,4DAAuD;AACvD,8CAIkB;AASlB,4DAA8D;AAC9D,wDAAkD;AAGlD;;;;;;;;;GASG;AACH,MAAa,cAAc;IAKzB;;;;;OAKG;IACH,YACE,IAA+B,EAC/B,SAAuC;QAZhC,uCAAiC;QAEjC,4CAAyC;QAYhD,uBAAA,IAAI,wBAAS,IAAI,MAAA,CAAC;QAClB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ACvD,CAAC,iCAAoB,CAAC,mBAAmB,CAAC,EAAE,kBAAkB;gBAC9D,CAAC,iCAAoB,CAAC,aAAa,CAAC,EAAE,YAAY;aACnD,CACF,CAAC;YAEF,SAAS,GAAG;gBACV,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,YAAY;aACpB,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;gBAAS,CAAC;YACT,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,QAAQ,CAAC;gBACzB,IAAI,EAAE,uBAAe,CAAC,QAAQ;gBAC9B,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE,SAAS;aAChB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,kBAAkB,CAAC,OAGxB;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAErC,IAAI,CAAC;YACH,OAAO,MAAM,QAAQ,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,KAAK,CACrB,IAAA,wBAAW,EAAC,KAAK,EAAE,mCAAmC,CAAC,EACvD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,mCAAmC;oBACzC,IAAI,EAAE,EAAE,MAAM,EAAE;iBACjB;aACF,CACF,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AA3XD,wCA2XC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport {\n PERPS_EVENT_PROPERTY,\n PERPS_EVENT_VALUE,\n} from '../constants/eventNames';\nimport { USDC_SYMBOL } from '../constants/hyperLiquidConfig';\nimport {\n PERPS_CONSTANTS,\n WITHDRAWAL_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes';\nimport {\n PerpsAnalyticsEvent,\n PerpsTraceNames,\n PerpsTraceOperations,\n} from '../types';\nimport type {\n PerpsProvider,\n WithdrawParams,\n WithdrawResult,\n PerpsPlatformDependencies,\n} from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport type { TransactionStatus } from '../types/transactionTypes';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\nimport type { ServiceContext } from './ServiceContext';\n\n/**\n * AccountService\n *\n * Handles account operations (deposits, withdrawals).\n * Stateless service that delegates to provider.\n * Controller handles state updates and analytics.\n *\n * Instance-based service with constructor injection of platform dependencies\n * and messenger for inter-controller communication.\n */\nexport class AccountService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new AccountService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Withdraw funds with full orchestration\n * Handles tracing, state management, analytics, and account refresh\n *\n * @param options - The withdrawal configuration.\n * @param options.provider - The perps provider to execute the withdrawal.\n * @param options.params - The withdrawal parameters (amount, destination, etc.).\n * @param options.context - The service context for tracing and dependencies.\n * @param options.refreshAccountState - Callback to refresh account state after withdrawal.\n * @returns The withdrawal result containing success status and transaction details.\n */\n async withdraw(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n context: ServiceContext;\n refreshAccountState: () => Promise<void>;\n }): Promise<WithdrawResult> {\n const { provider, params, context, refreshAccountState } = options;\n\n const traceId = uuidv4();\n const startTime = this.#deps.performance.now();\n let traceData:\n | {\n success: boolean;\n error?: string;\n txHash?: string;\n withdrawalId?: string;\n }\n | undefined;\n\n // Generate withdrawal request ID for tracking\n const currentWithdrawalId = `withdraw-${Date.now()}-${Math.random()\n .toString(36)\n .substring(2, 11)}`;\n\n try {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n op: PerpsTraceOperations.Operation,\n tags: {\n assetId: params.assetId ?? '',\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n\n this.#deps.debugLogger.log('AccountService: STARTING WITHDRAWAL', {\n params,\n timestamp: new Date().toISOString(),\n assetId: params.assetId,\n amount: params.amount,\n destination: params.destination,\n activeProvider: context.tracingContext.provider,\n isTestnet: context.tracingContext.isTestnet,\n });\n\n // Set withdrawal in progress\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.withdrawInProgress = true;\n\n // Calculate net amount after fees\n const grossAmount = parseFloat(params.amount);\n const feeAmount = WITHDRAWAL_CONSTANTS.DefaultFeeAmount;\n const netAmount = Math.max(0, grossAmount - feeAmount);\n\n // Get current account address via messenger\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n const accountAddress = evmAccount?.address ?? 'unknown';\n\n this.#deps.debugLogger.log(\n 'AccountService: Creating withdrawal request',\n {\n accountAddress,\n hasEvmAccount: Boolean(evmAccount),\n evmAccountAddress: evmAccount?.address,\n amount: netAmount.toString(),\n },\n );\n\n // Add withdrawal request to tracking\n const withdrawalRequest = {\n id: currentWithdrawalId,\n timestamp: Date.now(),\n amount: netAmount.toString(), // Use net amount (after fees)\n asset: USDC_SYMBOL,\n accountAddress, // Track which account initiated withdrawal\n success: false, // Will be updated when transaction completes\n txHash: undefined,\n status: 'pending' as TransactionStatus,\n destination: params.destination,\n transactionId: undefined, // Will be set to withdrawalId when available\n };\n\n state.withdrawalRequests.unshift(withdrawalRequest);\n });\n }\n\n this.#deps.debugLogger.log('AccountService: DELEGATING TO PROVIDER', {\n provider: context.tracingContext.provider,\n providerReady: Boolean(provider),\n });\n\n // Execute withdrawal\n const result = await provider.withdraw(params);\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL RESULT', {\n success: result.success,\n error: result.error,\n txHash: result.txHash,\n timestamp: new Date().toISOString(),\n });\n\n // Update state based on result\n if (result.success) {\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = null;\n state.lastUpdateTimestamp = Date.now();\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n\n if (result.txHash) {\n // Direct completion: remove from queue and record the txHash\n // so the polling hook won't re-match this completion.\n // We do NOT update lastCompletedWithdrawalTimestamp here\n // because Date.now() is local device time while the FIFO guard\n // compares against API server timestamps — mixing domains can\n // poison the guard. The txHash exclusion alone prevents\n // re-matching since the item is also spliced from the queue.\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n\n state.lastCompletedWithdrawalTxHashes.push(result.txHash);\n\n const hasOtherPending = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n state.withdrawInProgress = hasOtherPending;\n } else if (withdrawalRequestIndex !== -1) {\n const requestToUpdate =\n state.withdrawalRequests[withdrawalRequestIndex];\n // Withdrawal is bridging (no txHash yet)\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\n\n // Set lastWithdrawResult when submission is successful (even if bridging)\n // This triggers the \"confirmed\" toast telling user funds arrive in ~5 mins\n state.lastWithdrawResult = {\n success: true,\n txHash: result.txHash ?? '',\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n error: '',\n };\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {\n txHash: result.txHash,\n amount: params.amount,\n assetId: params.assetId,\n withdrawalId: result.withdrawalId,\n });\n\n // Track withdrawal transaction executed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.EXECUTED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n },\n );\n\n // Trigger account state refresh after withdrawal\n refreshAccountState().catch((refreshError) => {\n this.#deps.logger.error(\n ensureError(refreshError, 'AccountService.withdraw'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { operation: 'refreshAccountState' },\n },\n },\n );\n });\n\n // Invalidate standalone caches so external hooks (e.g., usePerpsPositionForAsset) refresh\n this.#deps.cacheInvalidator.invalidate({ cacheType: 'accountState' });\n\n traceData = {\n success: true,\n txHash: result.txHash ?? '',\n withdrawalId: result.withdrawalId ?? '',\n };\n\n return result;\n }\n\n // Handle failure\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED,\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL FAILED', {\n error: result.error,\n params,\n });\n\n // Track withdrawal transaction failed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: result.error ?? 'Unknown error',\n },\n );\n\n traceData = {\n success: false,\n error: result.error ?? 'Unknown error',\n };\n\n return result;\n } catch (error) {\n const errorMessage =\n error instanceof Error\n ? error.message\n : PERPS_ERROR_CODES.WITHDRAW_FAILED;\n\n this.#deps.logger.error(ensureError(error, 'AccountService.withdraw'), {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { assetId: params.assetId, amount: params.amount },\n },\n });\n\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = errorMessage;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: errorMessage,\n amount: '0',\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n // Track withdrawal transaction failed (catch block)\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: params.amount,\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: errorMessage,\n },\n );\n\n traceData = {\n success: false,\n error: errorMessage,\n };\n\n return { success: false, error: errorMessage };\n } finally {\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n data: traceData,\n });\n }\n }\n\n /**\n * Validate withdrawal parameters\n *\n * @param options - The validation configuration.\n * @param options.provider - The perps provider to validate against.\n * @param options.params - The withdrawal parameters to validate.\n * @returns An object indicating whether the withdrawal is valid, with an optional error message.\n */\n async validateWithdrawal(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n }): Promise<{ isValid: boolean; error?: string }> {\n const { provider, params } = options;\n\n try {\n return await provider.validateWithdrawal(params);\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'AccountService.validateWithdrawal'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.validateWithdrawal',\n data: { params },\n },\n },\n );\n throw error;\n }\n }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ServiceContext } from "./ServiceContext.cjs";
|
|
2
1
|
import type { PerpsProvider, WithdrawParams, WithdrawResult, PerpsPlatformDependencies } from "../types/index.cjs";
|
|
3
2
|
import type { PerpsControllerMessengerBase } from "../types/messenger.cjs";
|
|
3
|
+
import type { ServiceContext } from "./ServiceContext.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* AccountService
|
|
6
6
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.d.cts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountService.d.cts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAIvE,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAEvD;;;;;;;;;GASG;AACH,qBAAa,cAAc;;IAKzB;;;;;OAKG;gBAED,IAAI,EAAE,yBAAyB,EAC/B,SAAS,EAAE,4BAA4B;IAMzC;;;;;;;;;;OAUG;IACG,QAAQ,CAAC,OAAO,EAAE;QACtB,QAAQ,EAAE,aAAa,CAAC;QACxB,MAAM,EAAE,cAAc,CAAC;QACvB,OAAO,EAAE,cAAc,CAAC;QACxB,mBAAmB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC1C,GAAG,OAAO,CAAC,cAAc,CAAC;IA0T3B;;;;;;;OAOG;IACG,kBAAkB,CAAC,OAAO,EAAE;QAChC,QAAQ,EAAE,aAAa,CAAC;QACxB,MAAM,EAAE,cAAc,CAAC;KACxB,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAmBlD"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ServiceContext } from "./ServiceContext.mjs";
|
|
2
1
|
import type { PerpsProvider, WithdrawParams, WithdrawResult, PerpsPlatformDependencies } from "../types/index.mjs";
|
|
3
2
|
import type { PerpsControllerMessengerBase } from "../types/messenger.mjs";
|
|
3
|
+
import type { ServiceContext } from "./ServiceContext.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* AccountService
|
|
6
6
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.d.mts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountService.d.mts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAIvE,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAEvD;;;;;;;;;GASG;AACH,qBA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kBAAkB;gBAC9D,CAAC,oBAAoB,CAAC,aAAa,CAAC,EAAE,YAAY;aACnD,CACF,CAAC;YAEF,SAAS,GAAG;gBACV,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,YAAY;aACpB,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;gBAAS,CAAC;YACT,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,QAAQ,CAAC;gBACzB,IAAI,EAAE,eAAe,CAAC,QAAQ;gBAC9B,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE,SAAS;aAChB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,kBAAkB,CAAC,OAGxB;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAErC,IAAI,CAAC;YACH,OAAO,MAAM,QAAQ,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,KAAK,CACrB,WAAW,CAAC,KAAK,EAAE,mCAAmC,CAAC,EACvD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,eAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,mCAAmC;oBACzC,IAAI,EAAE,EAAE,MAAM,EAAE;iBACjB;aACF,CACF,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport type { ServiceContext } from './ServiceContext';\nimport {\n PERPS_EVENT_PROPERTY,\n PERPS_EVENT_VALUE,\n} from '../constants/eventNames';\nimport { USDC_SYMBOL } from '../constants/hyperLiquidConfig';\nimport {\n PERPS_CONSTANTS,\n WITHDRAWAL_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes';\nimport {\n PerpsAnalyticsEvent,\n PerpsTraceNames,\n PerpsTraceOperations,\n} from '../types';\nimport type {\n PerpsProvider,\n WithdrawParams,\n WithdrawResult,\n PerpsPlatformDependencies,\n} from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport type { TransactionStatus } from '../types/transactionTypes';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * AccountService\n *\n * Handles account operations (deposits, withdrawals).\n * Stateless service that delegates to provider.\n * Controller handles state updates and analytics.\n *\n * Instance-based service with constructor injection of platform dependencies\n * and messenger for inter-controller communication.\n */\nexport class AccountService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new AccountService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Withdraw funds with full orchestration\n * Handles tracing, state management, analytics, and account refresh\n *\n * @param options - The withdrawal configuration.\n * @param options.provider - The perps provider to execute the withdrawal.\n * @param options.params - The withdrawal parameters (amount, destination, etc.).\n * @param options.context - The service context for tracing and dependencies.\n * @param options.refreshAccountState - Callback to refresh account state after withdrawal.\n * @returns The withdrawal result containing success status and transaction details.\n */\n async withdraw(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n context: ServiceContext;\n refreshAccountState: () => Promise<void>;\n }): Promise<WithdrawResult> {\n const { provider, params, context, refreshAccountState } = options;\n\n const traceId = uuidv4();\n const startTime = this.#deps.performance.now();\n let traceData:\n | {\n success: boolean;\n error?: string;\n txHash?: string;\n withdrawalId?: string;\n }\n | undefined;\n\n // Generate withdrawal request ID for tracking\n const currentWithdrawalId = `withdraw-${Date.now()}-${Math.random()\n .toString(36)\n .substring(2, 11)}`;\n\n try {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n op: PerpsTraceOperations.Operation,\n tags: {\n assetId: params.assetId ?? '',\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n\n this.#deps.debugLogger.log('AccountService: STARTING WITHDRAWAL', {\n params,\n timestamp: new Date().toISOString(),\n assetId: params.assetId,\n amount: params.amount,\n destination: params.destination,\n activeProvider: context.tracingContext.provider,\n isTestnet: context.tracingContext.isTestnet,\n });\n\n // Set withdrawal in progress\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.withdrawInProgress = true;\n\n // Calculate net amount after fees\n const grossAmount = parseFloat(params.amount);\n const feeAmount = WITHDRAWAL_CONSTANTS.DefaultFeeAmount;\n const netAmount = Math.max(0, grossAmount - feeAmount);\n\n // Get current account address via messenger\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n const accountAddress = evmAccount?.address ?? 'unknown';\n\n this.#deps.debugLogger.log(\n 'AccountService: Creating withdrawal request',\n {\n accountAddress,\n hasEvmAccount: Boolean(evmAccount),\n evmAccountAddress: evmAccount?.address,\n amount: netAmount.toString(),\n },\n );\n\n // Add withdrawal request to tracking\n const withdrawalRequest = {\n id: currentWithdrawalId,\n timestamp: Date.now(),\n amount: netAmount.toString(), // Use net amount (after fees)\n asset: USDC_SYMBOL,\n accountAddress, // Track which account initiated withdrawal\n success: false, // Will be updated when transaction completes\n txHash: undefined,\n status: 'pending' as TransactionStatus,\n destination: params.destination,\n transactionId: undefined, // Will be set to withdrawalId when available\n };\n\n state.withdrawalRequests.unshift(withdrawalRequest);\n });\n }\n\n this.#deps.debugLogger.log('AccountService: DELEGATING TO PROVIDER', {\n provider: context.tracingContext.provider,\n providerReady: Boolean(provider),\n });\n\n // Execute withdrawal\n const result = await provider.withdraw(params);\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL RESULT', {\n success: result.success,\n error: result.error,\n txHash: result.txHash,\n timestamp: new Date().toISOString(),\n });\n\n // Update state based on result\n if (result.success) {\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = null;\n state.lastUpdateTimestamp = Date.now();\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n\n if (result.txHash) {\n // Direct completion: remove from queue and record the txHash\n // so the polling hook won't re-match this completion.\n // We do NOT update lastCompletedWithdrawalTimestamp here\n // because Date.now() is local device time while the FIFO guard\n // compares against API server timestamps — mixing domains can\n // poison the guard. The txHash exclusion alone prevents\n // re-matching since the item is also spliced from the queue.\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n\n state.lastCompletedWithdrawalTxHashes.push(result.txHash);\n\n const hasOtherPending = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n state.withdrawInProgress = hasOtherPending;\n } else if (withdrawalRequestIndex !== -1) {\n const requestToUpdate =\n state.withdrawalRequests[withdrawalRequestIndex];\n // Withdrawal is bridging (no txHash yet)\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\n\n // Set lastWithdrawResult when submission is successful (even if bridging)\n // This triggers the \"confirmed\" toast telling user funds arrive in ~5 mins\n state.lastWithdrawResult = {\n success: true,\n txHash: result.txHash ?? '',\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n error: '',\n };\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {\n txHash: result.txHash,\n amount: params.amount,\n assetId: params.assetId,\n withdrawalId: result.withdrawalId,\n });\n\n // Track withdrawal transaction executed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.EXECUTED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n },\n );\n\n // Trigger account state refresh after withdrawal\n refreshAccountState().catch((refreshError) => {\n this.#deps.logger.error(\n ensureError(refreshError, 'AccountService.withdraw'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { operation: 'refreshAccountState' },\n },\n },\n );\n });\n\n // Invalidate standalone caches so external hooks (e.g., usePerpsPositionForAsset) refresh\n this.#deps.cacheInvalidator.invalidate({ cacheType: 'accountState' });\n\n traceData = {\n success: true,\n txHash: result.txHash ?? '',\n withdrawalId: result.withdrawalId ?? '',\n };\n\n return result;\n }\n\n // Handle failure\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED,\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL FAILED', {\n error: result.error,\n params,\n });\n\n // Track withdrawal transaction failed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: result.error ?? 'Unknown error',\n },\n );\n\n traceData = {\n success: false,\n error: result.error ?? 'Unknown error',\n };\n\n return result;\n } catch (error) {\n const errorMessage =\n error instanceof Error\n ? error.message\n : PERPS_ERROR_CODES.WITHDRAW_FAILED;\n\n this.#deps.logger.error(ensureError(error, 'AccountService.withdraw'), {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { assetId: params.assetId, amount: params.amount },\n },\n });\n\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = errorMessage;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: errorMessage,\n amount: '0',\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n // Track withdrawal transaction failed (catch block)\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: params.amount,\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: errorMessage,\n },\n );\n\n traceData = {\n success: false,\n error: errorMessage,\n };\n\n return { success: false, error: errorMessage };\n } finally {\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n data: traceData,\n });\n }\n }\n\n /**\n * Validate withdrawal parameters\n *\n * @param options - The validation configuration.\n * @param options.provider - The perps provider to validate against.\n * @param options.params - The withdrawal parameters to validate.\n * @returns An object indicating whether the withdrawal is valid, with an optional error message.\n */\n async validateWithdrawal(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n }): Promise<{ isValid: boolean; error?: string }> {\n const { provider, params } = options;\n\n try {\n return await provider.validateWithdrawal(params);\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'AccountService.validateWithdrawal'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.validateWithdrawal',\n data: { params },\n },\n },\n );\n throw error;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountService.mjs","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAEpC,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EAClB,oCAAgC;AACjC,OAAO,EAAE,WAAW,EAAE,2CAAuC;AAC7D,OAAO,EACL,eAAe,EACf,oBAAoB,EACrB,qCAAiC;AAClC,OAAO,EAAE,iBAAiB,EAAE,+BAA2B;AACvD,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,oBAAoB,EACrB,2BAAiB;AASlB,OAAO,EAAE,qBAAqB,EAAE,kCAA8B;AAC9D,OAAO,EAAE,WAAW,EAAE,gCAA4B;AAGlD;;;;;;;;;GASG;AACH,MAAM,OAAO,cAAc;IAKzB;;;;;OAKG;IACH,YACE,IAA+B,EAC/B,SAAuC;QAZhC,uCAAiC;QAEjC,4CAAyC;QAYhD,uBAAA,IAAI,wBAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,6BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC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AEF,SAAS,GAAG;gBACV,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,YAAY;aACpB,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;gBAAS,CAAC;YACT,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,QAAQ,CAAC;gBACzB,IAAI,EAAE,eAAe,CAAC,QAAQ;gBAC9B,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE,SAAS;aAChB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,kBAAkB,CAAC,OAGxB;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAErC,IAAI,CAAC;YACH,OAAO,MAAM,QAAQ,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,4BAAM,CAAC,MAAM,CAAC,KAAK,CACrB,WAAW,CAAC,KAAK,EAAE,mCAAmC,CAAC,EACvD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,eAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,mCAAmC;oBACzC,IAAI,EAAE,EAAE,MAAM,EAAE;iBACjB;aACF,CACF,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport {\n PERPS_EVENT_PROPERTY,\n PERPS_EVENT_VALUE,\n} from '../constants/eventNames';\nimport { USDC_SYMBOL } from '../constants/hyperLiquidConfig';\nimport {\n PERPS_CONSTANTS,\n WITHDRAWAL_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes';\nimport {\n PerpsAnalyticsEvent,\n PerpsTraceNames,\n PerpsTraceOperations,\n} from '../types';\nimport type {\n PerpsProvider,\n WithdrawParams,\n WithdrawResult,\n PerpsPlatformDependencies,\n} from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport type { TransactionStatus } from '../types/transactionTypes';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\nimport type { ServiceContext } from './ServiceContext';\n\n/**\n * AccountService\n *\n * Handles account operations (deposits, withdrawals).\n * Stateless service that delegates to provider.\n * Controller handles state updates and analytics.\n *\n * Instance-based service with constructor injection of platform dependencies\n * and messenger for inter-controller communication.\n */\nexport class AccountService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new AccountService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Withdraw funds with full orchestration\n * Handles tracing, state management, analytics, and account refresh\n *\n * @param options - The withdrawal configuration.\n * @param options.provider - The perps provider to execute the withdrawal.\n * @param options.params - The withdrawal parameters (amount, destination, etc.).\n * @param options.context - The service context for tracing and dependencies.\n * @param options.refreshAccountState - Callback to refresh account state after withdrawal.\n * @returns The withdrawal result containing success status and transaction details.\n */\n async withdraw(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n context: ServiceContext;\n refreshAccountState: () => Promise<void>;\n }): Promise<WithdrawResult> {\n const { provider, params, context, refreshAccountState } = options;\n\n const traceId = uuidv4();\n const startTime = this.#deps.performance.now();\n let traceData:\n | {\n success: boolean;\n error?: string;\n txHash?: string;\n withdrawalId?: string;\n }\n | undefined;\n\n // Generate withdrawal request ID for tracking\n const currentWithdrawalId = `withdraw-${Date.now()}-${Math.random()\n .toString(36)\n .substring(2, 11)}`;\n\n try {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n op: PerpsTraceOperations.Operation,\n tags: {\n assetId: params.assetId ?? '',\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n\n this.#deps.debugLogger.log('AccountService: STARTING WITHDRAWAL', {\n params,\n timestamp: new Date().toISOString(),\n assetId: params.assetId,\n amount: params.amount,\n destination: params.destination,\n activeProvider: context.tracingContext.provider,\n isTestnet: context.tracingContext.isTestnet,\n });\n\n // Set withdrawal in progress\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.withdrawInProgress = true;\n\n // Calculate net amount after fees\n const grossAmount = parseFloat(params.amount);\n const feeAmount = WITHDRAWAL_CONSTANTS.DefaultFeeAmount;\n const netAmount = Math.max(0, grossAmount - feeAmount);\n\n // Get current account address via messenger\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n const accountAddress = evmAccount?.address ?? 'unknown';\n\n this.#deps.debugLogger.log(\n 'AccountService: Creating withdrawal request',\n {\n accountAddress,\n hasEvmAccount: Boolean(evmAccount),\n evmAccountAddress: evmAccount?.address,\n amount: netAmount.toString(),\n },\n );\n\n // Add withdrawal request to tracking\n const withdrawalRequest = {\n id: currentWithdrawalId,\n timestamp: Date.now(),\n amount: netAmount.toString(), // Use net amount (after fees)\n asset: USDC_SYMBOL,\n accountAddress, // Track which account initiated withdrawal\n success: false, // Will be updated when transaction completes\n txHash: undefined,\n status: 'pending' as TransactionStatus,\n destination: params.destination,\n transactionId: undefined, // Will be set to withdrawalId when available\n };\n\n state.withdrawalRequests.unshift(withdrawalRequest);\n });\n }\n\n this.#deps.debugLogger.log('AccountService: DELEGATING TO PROVIDER', {\n provider: context.tracingContext.provider,\n providerReady: Boolean(provider),\n });\n\n // Execute withdrawal\n const result = await provider.withdraw(params);\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL RESULT', {\n success: result.success,\n error: result.error,\n txHash: result.txHash,\n timestamp: new Date().toISOString(),\n });\n\n // Update state based on result\n if (result.success) {\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = null;\n state.lastUpdateTimestamp = Date.now();\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n\n if (result.txHash) {\n // Direct completion: remove from queue and record the txHash\n // so the polling hook won't re-match this completion.\n // We do NOT update lastCompletedWithdrawalTimestamp here\n // because Date.now() is local device time while the FIFO guard\n // compares against API server timestamps — mixing domains can\n // poison the guard. The txHash exclusion alone prevents\n // re-matching since the item is also spliced from the queue.\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n\n state.lastCompletedWithdrawalTxHashes.push(result.txHash);\n\n const hasOtherPending = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n state.withdrawInProgress = hasOtherPending;\n } else if (withdrawalRequestIndex !== -1) {\n const requestToUpdate =\n state.withdrawalRequests[withdrawalRequestIndex];\n // Withdrawal is bridging (no txHash yet)\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\n\n // Set lastWithdrawResult when submission is successful (even if bridging)\n // This triggers the \"confirmed\" toast telling user funds arrive in ~5 mins\n state.lastWithdrawResult = {\n success: true,\n txHash: result.txHash ?? '',\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n error: '',\n };\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {\n txHash: result.txHash,\n amount: params.amount,\n assetId: params.assetId,\n withdrawalId: result.withdrawalId,\n });\n\n // Track withdrawal transaction executed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.EXECUTED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n },\n );\n\n // Trigger account state refresh after withdrawal\n refreshAccountState().catch((refreshError) => {\n this.#deps.logger.error(\n ensureError(refreshError, 'AccountService.withdraw'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { operation: 'refreshAccountState' },\n },\n },\n );\n });\n\n // Invalidate standalone caches so external hooks (e.g., usePerpsPositionForAsset) refresh\n this.#deps.cacheInvalidator.invalidate({ cacheType: 'accountState' });\n\n traceData = {\n success: true,\n txHash: result.txHash ?? '',\n withdrawalId: result.withdrawalId ?? '',\n };\n\n return result;\n }\n\n // Handle failure\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED,\n amount: params.amount,\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n this.#deps.debugLogger.log('AccountService: WITHDRAWAL FAILED', {\n error: result.error,\n params,\n });\n\n // Track withdrawal transaction failed\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: parseFloat(params.amount),\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: result.error ?? 'Unknown error',\n },\n );\n\n traceData = {\n success: false,\n error: result.error ?? 'Unknown error',\n };\n\n return result;\n } catch (error) {\n const errorMessage =\n error instanceof Error\n ? error.message\n : PERPS_ERROR_CODES.WITHDRAW_FAILED;\n\n this.#deps.logger.error(ensureError(error, 'AccountService.withdraw'), {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.withdraw',\n data: { assetId: params.assetId, amount: params.amount },\n },\n });\n\n if (context.stateManager) {\n context.stateManager.update((state) => {\n state.lastError = errorMessage;\n state.lastUpdateTimestamp = Date.now();\n state.lastWithdrawResult = {\n success: false,\n error: errorMessage,\n amount: '0',\n asset: USDC_SYMBOL,\n timestamp: Date.now(),\n txHash: '',\n };\n\n const withdrawalRequestIndex = state.withdrawalRequests.findIndex(\n (req) => req.id === currentWithdrawalId,\n );\n if (withdrawalRequestIndex !== -1) {\n state.withdrawalRequests.splice(withdrawalRequestIndex, 1);\n }\n state.withdrawInProgress = state.withdrawalRequests.some(\n (req) => req.status === 'pending' || req.status === 'bridging',\n );\n });\n }\n\n // Track withdrawal transaction failed (catch block)\n const completionDuration = this.#deps.performance.now() - startTime;\n this.#deps.metrics.trackPerpsEvent(\n PerpsAnalyticsEvent.WithdrawalTransaction,\n {\n [PERPS_EVENT_PROPERTY.STATUS]: PERPS_EVENT_VALUE.STATUS.FAILED,\n [PERPS_EVENT_PROPERTY.WITHDRAWAL_AMOUNT]: params.amount,\n [PERPS_EVENT_PROPERTY.COMPLETION_DURATION]: completionDuration,\n [PERPS_EVENT_PROPERTY.ERROR_MESSAGE]: errorMessage,\n },\n );\n\n traceData = {\n success: false,\n error: errorMessage,\n };\n\n return { success: false, error: errorMessage };\n } finally {\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.Withdraw,\n id: traceId,\n data: traceData,\n });\n }\n }\n\n /**\n * Validate withdrawal parameters\n *\n * @param options - The validation configuration.\n * @param options.provider - The perps provider to validate against.\n * @param options.params - The withdrawal parameters to validate.\n * @returns An object indicating whether the withdrawal is valid, with an optional error message.\n */\n async validateWithdrawal(options: {\n provider: PerpsProvider;\n params: WithdrawParams;\n }): Promise<{ isValid: boolean; error?: string }> {\n const { provider, params } = options;\n\n try {\n return await provider.validateWithdrawal(params);\n } catch (error) {\n this.#deps.logger.error(\n ensureError(error, 'AccountService.validateWithdrawal'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'AccountService.validateWithdrawal',\n data: { params },\n },\n },\n );\n throw error;\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataLakeService.cjs","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAoC;AAGpC,4EAAuE;AACvE,8DAGkC;AAClC,8CAAiE;AAGjE,4DAA8D;AAC9D,wDAAkD;AAElD;;;;;;;;GAQG;AACH,MAAa,eAAe;IAK1B;;;;;OAKG;IACH,YACE,IAA+B,EAC/B,SAAuC;;QAZhC,wCAAiC;QAEjC,6CAAyC;QAYhD,uBAAA,IAAI,yBAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,8BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAWD;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,WAAW,CAAC,OASjB;QACC,MAAM,EACJ,MAAM,EACN,MAAM,EACN,OAAO,EACP,OAAO,EACP,SAAS,EACT,OAAO,EACP,UAAU,GAAG,CAAC,EACd,QAAQ,GACT,GAAG,OAAO,CAAC;QAEZ,8EAA8E;QAC9E,IAAI,SAAS,EAAE,CAAC;YACd,uBAAA,IAAI,6BAAM,CAAC,WAAW,CAAC,GAAG,CAAC,oCAAoC,EAAE;gBAC/D,MAAM;gBACN,MAAM;gBACN,OAAO,EAAE,SAAS;aACnB,CAAC,CAAC;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EACjD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,6BAA6B;oBACnC,IAAI,EAAE,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE;iBAChE;aACF,CACF,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;IACH,CAAC;CACF;AAvQD,0CAuQC;;AApPC;;;;GAIG;AACH,KAAK;IACH,OAAO,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;AACzE,CAAC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport type { ServiceContext } from './ServiceContext';\nimport { PerpsMeasurementName } from '../constants/performanceMetrics';\nimport {\n DATA_LAKE_API_CONFIG,\n PERPS_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PerpsTraceNames, PerpsTraceOperations } from '../types';\nimport type { PerpsPlatformDependencies } from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * DataLakeService\n *\n * Handles reporting order events to external Data Lake API.\n * Implements exponential backoff retry logic and performance tracing.\n * Stateless service that operates purely on external API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class DataLakeService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new DataLakeService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Get bearer token via DI authentication controller\n *\n * @returns The bearer token string for API authentication.\n */\n async #getBearerToken(): Promise<string> {\n return this.#messenger.call('AuthenticationController:getBearerToken');\n }\n\n /**\n * Report order events to data lake API with retry (non-blocking)\n * Implements exponential backoff retry logic (max 3 retries)\n *\n * @param options - Configuration object\n * @param options.action - Order action ('open' or 'close')\n * @param options.symbol - Market symbol\n * @param options.slPrice - Optional stop loss price.\n * @param options.tpPrice - Optional take profit price.\n * @param options.isTestnet - Whether this is a testnet operation (skips API call)\n * @param options.context - ServiceContext for dependencies (messenger, tracing)\n * @param options.retryCount - Internal retry counter (managed by service)\n * @param options._traceId - Internal trace ID (managed by service)\n * @returns Result object with success flag and optional error message\n */\n async reportOrder(options: {\n action: 'open' | 'close';\n symbol: string;\n slPrice?: number;\n tpPrice?: number;\n isTestnet: boolean;\n context: ServiceContext;\n retryCount?: number;\n _traceId?: string;\n }): Promise<{ success: boolean; error?: string }> {\n const {\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount = 0,\n _traceId,\n } = options;\n\n // Skip data lake reporting for testnet as the API doesn't handle testnet data\n if (isTestnet) {\n this.#deps.debugLogger.log('DataLake API: Skipping for testnet', {\n action,\n symbol,\n network: 'testnet',\n });\n return { success: true, error: 'Skipped for testnet' };\n }\n\n const MAX_RETRIES = 3;\n const RETRY_DELAY_MS = 1000;\n\n // Generate trace ID once on first call\n const traceId = _traceId ?? uuidv4();\n\n // Start trace only on first attempt\n if (retryCount === 0) {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.DataLakeReport,\n op: PerpsTraceOperations.Operation,\n id: traceId,\n tags: {\n action,\n symbol,\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n }\n\n // Log the attempt\n this.#deps.debugLogger.log('DataLake API: Starting order report', {\n action,\n symbol,\n attempt: retryCount + 1,\n maxAttempts: MAX_RETRIES + 1,\n hasStopLoss: Boolean(slPrice),\n hasTakeProfit: Boolean(tpPrice),\n timestamp: new Date().toISOString(),\n });\n\n const apiCallStartTime = this.#deps.performance.now();\n\n try {\n const token = await this.#getBearerToken();\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n\n if (!evmAccount || !token) {\n this.#deps.debugLogger.log('DataLake API: Missing requirements', {\n hasAccount: Boolean(evmAccount),\n hasToken: Boolean(token),\n action,\n symbol,\n });\n return { success: false, error: 'No account or token available' };\n }\n\n const response = await fetch(DATA_LAKE_API_CONFIG.OrdersEndpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n user_id: evmAccount.address,\n symbol,\n sl_price: slPrice,\n tp_price: tpPrice,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`DataLake API error: ${response.status}`);\n }\n\n // Consume response body (might be empty for 201, but good to check)\n const responseBody = await response.text();\n\n const apiCallDuration = this.#deps.performance.now() - apiCallStartTime;\n\n // Record measurement\n this.#deps.tracer.setMeasurement(\n PerpsMeasurementName.PerpsDataLakeApiCall,\n apiCallDuration,\n 'millisecond',\n );\n\n // Success logging\n this.#deps.debugLogger.log('DataLake API: Order reported successfully', {\n action,\n symbol,\n status: response.status,\n attempt: retryCount + 1,\n responseBody: responseBody || 'empty',\n duration: `${apiCallDuration.toFixed(0)}ms`,\n });\n\n // End trace on success\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: true,\n retries: retryCount,\n },\n });\n\n return { success: true };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n action,\n symbol,\n retryCount,\n willRetry: retryCount < MAX_RETRIES,\n },\n },\n },\n );\n\n // Retry logic\n if (retryCount < MAX_RETRIES) {\n const retryDelay = RETRY_DELAY_MS * Math.pow(2, retryCount);\n this.#deps.debugLogger.log('DataLake API: Scheduling retry', {\n retryIn: `${retryDelay}ms`,\n nextAttempt: retryCount + 2,\n action,\n symbol,\n });\n\n setTimeout(() => {\n this.reportOrder({\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount: retryCount + 1,\n _traceId: traceId,\n }).catch((_retryError) => {\n this.#deps.logger.error(\n ensureError(_retryError, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n operation: 'retry',\n retryCount: retryCount + 1,\n action,\n symbol,\n },\n },\n },\n );\n });\n }, retryDelay);\n\n return { success: false, error: errorMessage };\n }\n\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: false,\n error: errorMessage,\n totalRetries: retryCount,\n },\n });\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: { operation: 'finalFailure', action, symbol, retryCount },\n },\n },\n );\n\n return { success: false, error: errorMessage };\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"DataLakeService.cjs","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAoC;AAEpC,4EAAuE;AACvE,8DAGkC;AAClC,8CAAiE;AAGjE,4DAA8D;AAC9D,wDAAkD;AAGlD;;;;;;;;GAQG;AACH,MAAa,eAAe;IAK1B;;;;;OAKG;IACH,YACE,IAA+B,EAC/B,SAAuC;;QAZhC,wCAAiC;QAEjC,6CAAyC;QAYhD,uBAAA,IAAI,yBAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,8BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAWD;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,WAAW,CAAC,OASjB;QACC,MAAM,EACJ,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,CAAC,QAAQ,CAAC;gBACzB,IAAI,EAAE,uBAAe,CAAC,cAAc;gBACpC,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE;oBACJ,OAAO,EAAE,KAAK;oBACd,KAAK,EAAE,YAAY;oBACnB,YAAY,EAAE,UAAU;iBACzB;aACF,CAAC,CAAC;YAEH,uBAAA,IAAI,6BAAM,CAAC,MAAM,CAAC,KAAK,CACrB,IAAA,wBAAW,EAAC,KAAK,EAAE,6BAA6B,CAAC,EACjD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,6BAA6B;oBACnC,IAAI,EAAE,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE;iBAChE;aACF,CACF,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;IACH,CAAC;CACF;AAvQD,0CAuQC;;AApPC;;;;GAIG;AACH,KAAK;IACH,OAAO,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;AACzE,CAAC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport { PerpsMeasurementName } from '../constants/performanceMetrics';\nimport {\n DATA_LAKE_API_CONFIG,\n PERPS_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PerpsTraceNames, PerpsTraceOperations } from '../types';\nimport type { PerpsPlatformDependencies } from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\nimport type { ServiceContext } from './ServiceContext';\n\n/**\n * DataLakeService\n *\n * Handles reporting order events to external Data Lake API.\n * Implements exponential backoff retry logic and performance tracing.\n * Stateless service that operates purely on external API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class DataLakeService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new DataLakeService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Get bearer token via DI authentication controller\n *\n * @returns The bearer token string for API authentication.\n */\n async #getBearerToken(): Promise<string> {\n return this.#messenger.call('AuthenticationController:getBearerToken');\n }\n\n /**\n * Report order events to data lake API with retry (non-blocking)\n * Implements exponential backoff retry logic (max 3 retries)\n *\n * @param options - Configuration object\n * @param options.action - Order action ('open' or 'close')\n * @param options.symbol - Market symbol\n * @param options.slPrice - Optional stop loss price.\n * @param options.tpPrice - Optional take profit price.\n * @param options.isTestnet - Whether this is a testnet operation (skips API call)\n * @param options.context - ServiceContext for dependencies (messenger, tracing)\n * @param options.retryCount - Internal retry counter (managed by service)\n * @param options._traceId - Internal trace ID (managed by service)\n * @returns Result object with success flag and optional error message\n */\n async reportOrder(options: {\n action: 'open' | 'close';\n symbol: string;\n slPrice?: number;\n tpPrice?: number;\n isTestnet: boolean;\n context: ServiceContext;\n retryCount?: number;\n _traceId?: string;\n }): Promise<{ success: boolean; error?: string }> {\n const {\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount = 0,\n _traceId,\n } = options;\n\n // Skip data lake reporting for testnet as the API doesn't handle testnet data\n if (isTestnet) {\n this.#deps.debugLogger.log('DataLake API: Skipping for testnet', {\n action,\n symbol,\n network: 'testnet',\n });\n return { success: true, error: 'Skipped for testnet' };\n }\n\n const MAX_RETRIES = 3;\n const RETRY_DELAY_MS = 1000;\n\n // Generate trace ID once on first call\n const traceId = _traceId ?? uuidv4();\n\n // Start trace only on first attempt\n if (retryCount === 0) {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.DataLakeReport,\n op: PerpsTraceOperations.Operation,\n id: traceId,\n tags: {\n action,\n symbol,\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n }\n\n // Log the attempt\n this.#deps.debugLogger.log('DataLake API: Starting order report', {\n action,\n symbol,\n attempt: retryCount + 1,\n maxAttempts: MAX_RETRIES + 1,\n hasStopLoss: Boolean(slPrice),\n hasTakeProfit: Boolean(tpPrice),\n timestamp: new Date().toISOString(),\n });\n\n const apiCallStartTime = this.#deps.performance.now();\n\n try {\n const token = await this.#getBearerToken();\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n\n if (!evmAccount || !token) {\n this.#deps.debugLogger.log('DataLake API: Missing requirements', {\n hasAccount: Boolean(evmAccount),\n hasToken: Boolean(token),\n action,\n symbol,\n });\n return { success: false, error: 'No account or token available' };\n }\n\n const response = await fetch(DATA_LAKE_API_CONFIG.OrdersEndpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n user_id: evmAccount.address,\n symbol,\n sl_price: slPrice,\n tp_price: tpPrice,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`DataLake API error: ${response.status}`);\n }\n\n // Consume response body (might be empty for 201, but good to check)\n const responseBody = await response.text();\n\n const apiCallDuration = this.#deps.performance.now() - apiCallStartTime;\n\n // Record measurement\n this.#deps.tracer.setMeasurement(\n PerpsMeasurementName.PerpsDataLakeApiCall,\n apiCallDuration,\n 'millisecond',\n );\n\n // Success logging\n this.#deps.debugLogger.log('DataLake API: Order reported successfully', {\n action,\n symbol,\n status: response.status,\n attempt: retryCount + 1,\n responseBody: responseBody || 'empty',\n duration: `${apiCallDuration.toFixed(0)}ms`,\n });\n\n // End trace on success\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: true,\n retries: retryCount,\n },\n });\n\n return { success: true };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n action,\n symbol,\n retryCount,\n willRetry: retryCount < MAX_RETRIES,\n },\n },\n },\n );\n\n // Retry logic\n if (retryCount < MAX_RETRIES) {\n const retryDelay = RETRY_DELAY_MS * Math.pow(2, retryCount);\n this.#deps.debugLogger.log('DataLake API: Scheduling retry', {\n retryIn: `${retryDelay}ms`,\n nextAttempt: retryCount + 2,\n action,\n symbol,\n });\n\n setTimeout(() => {\n this.reportOrder({\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount: retryCount + 1,\n _traceId: traceId,\n }).catch((_retryError) => {\n this.#deps.logger.error(\n ensureError(_retryError, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n operation: 'retry',\n retryCount: retryCount + 1,\n action,\n symbol,\n },\n },\n },\n );\n });\n }, retryDelay);\n\n return { success: false, error: errorMessage };\n }\n\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: false,\n error: errorMessage,\n totalRetries: retryCount,\n },\n });\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: { operation: 'finalFailure', action, symbol, retryCount },\n },\n },\n );\n\n return { success: false, error: errorMessage };\n }\n }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ServiceContext } from "./ServiceContext.cjs";
|
|
2
1
|
import type { PerpsPlatformDependencies } from "../types/index.cjs";
|
|
3
2
|
import type { PerpsControllerMessengerBase } from "../types/messenger.cjs";
|
|
3
|
+
import type { ServiceContext } from "./ServiceContext.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* DataLakeService
|
|
6
6
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataLakeService.d.cts","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DataLakeService.d.cts","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,2BAAiB;AAC1D,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAGvE,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAEvD;;;;;;;;GAQG;AACH,qBAAa,eAAe;;IAK1B;;;;;OAKG;gBAED,IAAI,EAAE,yBAAyB,EAC/B,SAAS,EAAE,4BAA4B;IAezC;;;;;;;;;;;;;;OAcG;IACG,WAAW,CAAC,OAAO,EAAE;QACzB,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,OAAO,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;QACxB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAmNlD"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ServiceContext } from "./ServiceContext.mjs";
|
|
2
1
|
import type { PerpsPlatformDependencies } from "../types/index.mjs";
|
|
3
2
|
import type { PerpsControllerMessengerBase } from "../types/messenger.mjs";
|
|
3
|
+
import type { ServiceContext } from "./ServiceContext.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* DataLakeService
|
|
6
6
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataLakeService.d.mts","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DataLakeService.d.mts","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,2BAAiB;AAC1D,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAGvE,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAEvD;;;;;;;;GAQG;AACH,qBAAa,eAAe;;IAK1B;;;;;OAKG;gBAED,IAAI,EAAE,yBAAyB,EAC/B,SAAS,EAAE,4BAA4B;IAezC;;;;;;;;;;;;;;OAcG;IACG,WAAW,CAAC,OAAO,EAAE;QACzB,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,OAAO,CAAC;QACnB,OAAO,EAAE,cAAc,CAAC;QACxB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAmNlD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataLakeService.mjs","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAGpC,OAAO,EAAE,oBAAoB,EAAE,4CAAwC;AACvE,OAAO,EACL,oBAAoB,EACpB,eAAe,EAChB,qCAAiC;AAClC,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,2BAAiB;AAGjE,OAAO,EAAE,qBAAqB,E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,6BAA6B;gCACnC,IAAI,EAAE;oCACJ,SAAS,EAAE,OAAO;oCAClB,UAAU,EAAE,UAAU,GAAG,CAAC;oCAC1B,MAAM;oCACN,MAAM;iCACP;6BACF;yBACF,CACF,CAAC;oBACJ,CAAC,CAAC,CAAC;gBACL,CAAC,EAAE,UAAU,CAAC,CAAC;gBAEf,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;YACjD,CAAC;YAED,uBAAA,IAAI,6BAAM,CAAC,MAAM,CAAC,QAAQ,CAAC;gBACzB,IAAI,EAAE,eAAe,CAAC,cAAc;gBACpC,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE;oBACJ,OAAO,EAAE,KAAK;oBACd,KAAK,EAAE,YAAY;oBACnB,YAAY,EAAE,UAAU;iBACzB;aACF,CAAC,CAAC;YAEH,uBAAA,IAAI,6BAAM,CAAC,MAAM,CAAC,KAAK,CACrB,WAAW,CAAC,KAAK,EAAE,6BAA6B,CAAC,EACjD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,eAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,6BAA6B;oBACnC,IAAI,EAAE,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE;iBAChE;aACF,CACF,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;IACH,CAAC;CACF;;AApPC;;;;GAIG;AACH,KAAK;IACH,OAAO,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;AACzE,CAAC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport type { ServiceContext } from './ServiceContext';\nimport { PerpsMeasurementName } from '../constants/performanceMetrics';\nimport {\n DATA_LAKE_API_CONFIG,\n PERPS_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PerpsTraceNames, PerpsTraceOperations } from '../types';\nimport type { PerpsPlatformDependencies } from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\n\n/**\n * DataLakeService\n *\n * Handles reporting order events to external Data Lake API.\n * Implements exponential backoff retry logic and performance tracing.\n * Stateless service that operates purely on external API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class DataLakeService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new DataLakeService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Get bearer token via DI authentication controller\n *\n * @returns The bearer token string for API authentication.\n */\n async #getBearerToken(): Promise<string> {\n return this.#messenger.call('AuthenticationController:getBearerToken');\n }\n\n /**\n * Report order events to data lake API with retry (non-blocking)\n * Implements exponential backoff retry logic (max 3 retries)\n *\n * @param options - Configuration object\n * @param options.action - Order action ('open' or 'close')\n * @param options.symbol - Market symbol\n * @param options.slPrice - Optional stop loss price.\n * @param options.tpPrice - Optional take profit price.\n * @param options.isTestnet - Whether this is a testnet operation (skips API call)\n * @param options.context - ServiceContext for dependencies (messenger, tracing)\n * @param options.retryCount - Internal retry counter (managed by service)\n * @param options._traceId - Internal trace ID (managed by service)\n * @returns Result object with success flag and optional error message\n */\n async reportOrder(options: {\n action: 'open' | 'close';\n symbol: string;\n slPrice?: number;\n tpPrice?: number;\n isTestnet: boolean;\n context: ServiceContext;\n retryCount?: number;\n _traceId?: string;\n }): Promise<{ success: boolean; error?: string }> {\n const {\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount = 0,\n _traceId,\n } = options;\n\n // Skip data lake reporting for testnet as the API doesn't handle testnet data\n if (isTestnet) {\n this.#deps.debugLogger.log('DataLake API: Skipping for testnet', {\n action,\n symbol,\n network: 'testnet',\n });\n return { success: true, error: 'Skipped for testnet' };\n }\n\n const MAX_RETRIES = 3;\n const RETRY_DELAY_MS = 1000;\n\n // Generate trace ID once on first call\n const traceId = _traceId ?? uuidv4();\n\n // Start trace only on first attempt\n if (retryCount === 0) {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.DataLakeReport,\n op: PerpsTraceOperations.Operation,\n id: traceId,\n tags: {\n action,\n symbol,\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n }\n\n // Log the attempt\n this.#deps.debugLogger.log('DataLake API: Starting order report', {\n action,\n symbol,\n attempt: retryCount + 1,\n maxAttempts: MAX_RETRIES + 1,\n hasStopLoss: Boolean(slPrice),\n hasTakeProfit: Boolean(tpPrice),\n timestamp: new Date().toISOString(),\n });\n\n const apiCallStartTime = this.#deps.performance.now();\n\n try {\n const token = await this.#getBearerToken();\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n\n if (!evmAccount || !token) {\n this.#deps.debugLogger.log('DataLake API: Missing requirements', {\n hasAccount: Boolean(evmAccount),\n hasToken: Boolean(token),\n action,\n symbol,\n });\n return { success: false, error: 'No account or token available' };\n }\n\n const response = await fetch(DATA_LAKE_API_CONFIG.OrdersEndpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n user_id: evmAccount.address,\n symbol,\n sl_price: slPrice,\n tp_price: tpPrice,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`DataLake API error: ${response.status}`);\n }\n\n // Consume response body (might be empty for 201, but good to check)\n const responseBody = await response.text();\n\n const apiCallDuration = this.#deps.performance.now() - apiCallStartTime;\n\n // Record measurement\n this.#deps.tracer.setMeasurement(\n PerpsMeasurementName.PerpsDataLakeApiCall,\n apiCallDuration,\n 'millisecond',\n );\n\n // Success logging\n this.#deps.debugLogger.log('DataLake API: Order reported successfully', {\n action,\n symbol,\n status: response.status,\n attempt: retryCount + 1,\n responseBody: responseBody || 'empty',\n duration: `${apiCallDuration.toFixed(0)}ms`,\n });\n\n // End trace on success\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: true,\n retries: retryCount,\n },\n });\n\n return { success: true };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n action,\n symbol,\n retryCount,\n willRetry: retryCount < MAX_RETRIES,\n },\n },\n },\n );\n\n // Retry logic\n if (retryCount < MAX_RETRIES) {\n const retryDelay = RETRY_DELAY_MS * Math.pow(2, retryCount);\n this.#deps.debugLogger.log('DataLake API: Scheduling retry', {\n retryIn: `${retryDelay}ms`,\n nextAttempt: retryCount + 2,\n action,\n symbol,\n });\n\n setTimeout(() => {\n this.reportOrder({\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount: retryCount + 1,\n _traceId: traceId,\n }).catch((_retryError) => {\n this.#deps.logger.error(\n ensureError(_retryError, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n operation: 'retry',\n retryCount: retryCount + 1,\n action,\n symbol,\n },\n },\n },\n );\n });\n }, retryDelay);\n\n return { success: false, error: errorMessage };\n }\n\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: false,\n error: errorMessage,\n totalRetries: retryCount,\n },\n });\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: { operation: 'finalFailure', action, symbol, retryCount },\n },\n },\n );\n\n return { success: false, error: errorMessage };\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"DataLakeService.mjs","sourceRoot":"","sources":["../../src/services/DataLakeService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAEpC,OAAO,EAAE,oBAAoB,EAAE,4CAAwC;AACvE,OAAO,EACL,oBAAoB,EACpB,eAAe,EAChB,qCAAiC;AAClC,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,2BAAiB;AAGjE,OAAO,EAAE,qBAAqB,EAAE,kCAA8B;AAC9D,OAAO,EAAE,WAAW,EAAE,gCAA4B;AAGlD;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IAK1B;;;;;OAKG;IACH,YACE,IAA+B,EAC/B,SAAuC;;QAZhC,wCAAiC;QAEjC,6CAAyC;QAYhD,uBAAA,IAAI,yBAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,8BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAWD;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,WAAW,CAAC,OASjB;QACC,MAAM,EACJ,MAAM,EACN,MAAM,EACN,OAAO,EACP,OAAO,EACP,SAAS,EACT,OAAO,EACP,UAAU,GAAG,CAAC,EACd,QAAQ,GACT,GAAG,OAAO,CAAC;QAEZ,8EAA8E;QAC9E,IAAI,SAAS,EAAE,CAAC;YACd,uBAAA,IAAI,6BAAM,CAAC,WAAW,CAAC,GAAG,CAAC,oCAAoC,EAAE;gBAC/D,MAAM;gBACN,MAAM;gBACN,OAAO,EAAE,SAAS;aACnB,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,qBAAqB,EAAE,CAAC;QACzD,CAAC;QAED,MAAM,WAAW,GAAG,CAAC,CAAC;QACtB,MAAM,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E,MAAM,EAAE,UAAU,EAAE;iBAChE;aACF,CACF,CAAC;YAEF,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACjD,CAAC;IACH,CAAC;CACF;;AApPC;;;;GAIG;AACH,KAAK;IACH,OAAO,uBAAA,IAAI,kCAAW,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;AACzE,CAAC","sourcesContent":["import { v4 as uuidv4 } from 'uuid';\n\nimport { PerpsMeasurementName } from '../constants/performanceMetrics';\nimport {\n DATA_LAKE_API_CONFIG,\n PERPS_CONSTANTS,\n} from '../constants/perpsConfig';\nimport { PerpsTraceNames, PerpsTraceOperations } from '../types';\nimport type { PerpsPlatformDependencies } from '../types';\nimport type { PerpsControllerMessengerBase } from '../types/messenger';\nimport { getSelectedEvmAccount } from '../utils/accountUtils';\nimport { ensureError } from '../utils/errorUtils';\nimport type { ServiceContext } from './ServiceContext';\n\n/**\n * DataLakeService\n *\n * Handles reporting order events to external Data Lake API.\n * Implements exponential backoff retry logic and performance tracing.\n * Stateless service that operates purely on external API calls.\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class DataLakeService {\n readonly #deps: PerpsPlatformDependencies;\n\n readonly #messenger: PerpsControllerMessengerBase;\n\n /**\n * Create a new DataLakeService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n * @param messenger - Controller messenger for cross-controller communication.\n */\n constructor(\n deps: PerpsPlatformDependencies,\n messenger: PerpsControllerMessengerBase,\n ) {\n this.#deps = deps;\n this.#messenger = messenger;\n }\n\n /**\n * Get bearer token via DI authentication controller\n *\n * @returns The bearer token string for API authentication.\n */\n async #getBearerToken(): Promise<string> {\n return this.#messenger.call('AuthenticationController:getBearerToken');\n }\n\n /**\n * Report order events to data lake API with retry (non-blocking)\n * Implements exponential backoff retry logic (max 3 retries)\n *\n * @param options - Configuration object\n * @param options.action - Order action ('open' or 'close')\n * @param options.symbol - Market symbol\n * @param options.slPrice - Optional stop loss price.\n * @param options.tpPrice - Optional take profit price.\n * @param options.isTestnet - Whether this is a testnet operation (skips API call)\n * @param options.context - ServiceContext for dependencies (messenger, tracing)\n * @param options.retryCount - Internal retry counter (managed by service)\n * @param options._traceId - Internal trace ID (managed by service)\n * @returns Result object with success flag and optional error message\n */\n async reportOrder(options: {\n action: 'open' | 'close';\n symbol: string;\n slPrice?: number;\n tpPrice?: number;\n isTestnet: boolean;\n context: ServiceContext;\n retryCount?: number;\n _traceId?: string;\n }): Promise<{ success: boolean; error?: string }> {\n const {\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount = 0,\n _traceId,\n } = options;\n\n // Skip data lake reporting for testnet as the API doesn't handle testnet data\n if (isTestnet) {\n this.#deps.debugLogger.log('DataLake API: Skipping for testnet', {\n action,\n symbol,\n network: 'testnet',\n });\n return { success: true, error: 'Skipped for testnet' };\n }\n\n const MAX_RETRIES = 3;\n const RETRY_DELAY_MS = 1000;\n\n // Generate trace ID once on first call\n const traceId = _traceId ?? uuidv4();\n\n // Start trace only on first attempt\n if (retryCount === 0) {\n this.#deps.tracer.trace({\n name: PerpsTraceNames.DataLakeReport,\n op: PerpsTraceOperations.Operation,\n id: traceId,\n tags: {\n action,\n symbol,\n provider: context.tracingContext.provider,\n isTestnet: String(context.tracingContext.isTestnet),\n },\n });\n }\n\n // Log the attempt\n this.#deps.debugLogger.log('DataLake API: Starting order report', {\n action,\n symbol,\n attempt: retryCount + 1,\n maxAttempts: MAX_RETRIES + 1,\n hasStopLoss: Boolean(slPrice),\n hasTakeProfit: Boolean(tpPrice),\n timestamp: new Date().toISOString(),\n });\n\n const apiCallStartTime = this.#deps.performance.now();\n\n try {\n const token = await this.#getBearerToken();\n const evmAccount = getSelectedEvmAccount(\n this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n ),\n );\n\n if (!evmAccount || !token) {\n this.#deps.debugLogger.log('DataLake API: Missing requirements', {\n hasAccount: Boolean(evmAccount),\n hasToken: Boolean(token),\n action,\n symbol,\n });\n return { success: false, error: 'No account or token available' };\n }\n\n const response = await fetch(DATA_LAKE_API_CONFIG.OrdersEndpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n user_id: evmAccount.address,\n symbol,\n sl_price: slPrice,\n tp_price: tpPrice,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`DataLake API error: ${response.status}`);\n }\n\n // Consume response body (might be empty for 201, but good to check)\n const responseBody = await response.text();\n\n const apiCallDuration = this.#deps.performance.now() - apiCallStartTime;\n\n // Record measurement\n this.#deps.tracer.setMeasurement(\n PerpsMeasurementName.PerpsDataLakeApiCall,\n apiCallDuration,\n 'millisecond',\n );\n\n // Success logging\n this.#deps.debugLogger.log('DataLake API: Order reported successfully', {\n action,\n symbol,\n status: response.status,\n attempt: retryCount + 1,\n responseBody: responseBody || 'empty',\n duration: `${apiCallDuration.toFixed(0)}ms`,\n });\n\n // End trace on success\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: true,\n retries: retryCount,\n },\n });\n\n return { success: true };\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n action,\n symbol,\n retryCount,\n willRetry: retryCount < MAX_RETRIES,\n },\n },\n },\n );\n\n // Retry logic\n if (retryCount < MAX_RETRIES) {\n const retryDelay = RETRY_DELAY_MS * Math.pow(2, retryCount);\n this.#deps.debugLogger.log('DataLake API: Scheduling retry', {\n retryIn: `${retryDelay}ms`,\n nextAttempt: retryCount + 2,\n action,\n symbol,\n });\n\n setTimeout(() => {\n this.reportOrder({\n action,\n symbol,\n slPrice,\n tpPrice,\n isTestnet,\n context,\n retryCount: retryCount + 1,\n _traceId: traceId,\n }).catch((_retryError) => {\n this.#deps.logger.error(\n ensureError(_retryError, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: {\n operation: 'retry',\n retryCount: retryCount + 1,\n action,\n symbol,\n },\n },\n },\n );\n });\n }, retryDelay);\n\n return { success: false, error: errorMessage };\n }\n\n this.#deps.tracer.endTrace({\n name: PerpsTraceNames.DataLakeReport,\n id: traceId,\n data: {\n success: false,\n error: errorMessage,\n totalRetries: retryCount,\n },\n });\n\n this.#deps.logger.error(\n ensureError(error, 'DataLakeService.reportOrder'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'DataLakeService.reportOrder',\n data: { operation: 'finalFailure', action, symbol, retryCount },\n },\n },\n );\n\n return { success: false, error: errorMessage };\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FeatureFlagConfigurationService.cjs","sourceRoot":"","sources":["../../src/services/FeatureFlagConfigurationService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8C;AAG9C,8DAA2D;AAC3D,8CAAqD;AAKrD,wDAAkD;AAClD,0DAA6D;AAC7D,oEAGmC;AAEnC;;;;;;;;;;;;;;;GAeG;AACH,MAAa,+BAA+B;IAG1C;;;;OAIG;IACH,YAAY,IAA+B;;QAPlC,wDAAiC;QAQxC,uBAAA,IAAI,yCAAS,IAAI,MAAA,CAAC;IACpB,CAAC;IAuID;;;;;;;;;;;OAWG;IACH,iBAAiB,CAAC,OAGjB;QACC,MAAM,EAAE,gCAAgC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAE9D,IACE,CAAC,OAAO,CAAC,aAAa;YACtB,CAAC,OAAO,CAAC,aAAa;YACtB,CAAC,OAAO,CAAC,0BAA0B,EACnC,CAAC;YACD,MAAM,IAAI,KAAK,CACb,0DAA0D,CAC3D,CAAC;QACJ,CAAC;QAED,MAAM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D,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE;QACjC,IAAI,CAAC;YACH,IAAA,mCAAqB,EAAC,OAAO,CAAC,CAAC;YAC/B,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,uBAAA,IAAI,6CAAM,CAAC,MAAM,CAAC,KAAK,CACrB,IAAA,wBAAW,EACT,KAAK,EACL,qDAAqD,CACtD,EACD;gBACE,IAAI,EAAE,EAAE,OAAO,EAAE,6BAAe,CAAC,WAAW,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,qDAAqD;oBAC3D,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE;iBAC7B;aACF,CACF,CAAC;YACF,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,mIAS0B,CAAW,EAAE,CAAW;IACjD,OAAO,CACL,IAAI,CAAC,SAAS,CACZ,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAC1D;QACD,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAC1E,CAAC;AACJ,CAAC","sourcesContent":["import { hasProperty } from '@metamask/utils';\n\nimport type { ServiceContext } from './ServiceContext';\nimport { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport { isVersionGatedFeatureFlag } from '../types';\nimport type {\n PerpsPlatformDependencies,\n PerpsRemoteFeatureFlagState,\n} from '../types';\nimport { ensureError } from '../utils/errorUtils';\nimport { validateMarketPattern } from '../utils/marketUtils';\nimport {\n parseCommaSeparatedString,\n stripQuotes,\n} from '../utils/stringParseUtils';\n\n/**\n * FeatureFlagConfigurationService\n *\n * Handles HIP-3 configuration and geo-blocking configuration from remote feature flags.\n * Implements \"sticky remote\" pattern: once remote config is loaded, never downgrade to fallback.\n * Orchestrates validation, change detection, and version management for feature flag updates.\n *\n * Responsibilities:\n * - Remote feature flag validation and parsing\n * - HIP-3 configuration management (equity, allowlist, blocklist)\n * - Geo-blocking configuration from remote flags\n * - Change detection and version management\n * - \"Sticky remote\" pattern enforcement (never downgrade)\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class FeatureFlagConfigurationService {\n readonly #deps: PerpsPlatformDependencies;\n\n /**\n * Create a new FeatureFlagConfigurationService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Validate and parse market list from remote feature flags\n * Handles both string (comma-separated) and array formats from LaunchDarkly\n *\n * @param remoteValue - The raw value from remote feature flags (string or array).\n * @param fieldName - The name of the field being validated (for logging).\n * @param currentValue - The current local market list as fallback reference.\n * @returns The validated market list, or undefined if validation fails.\n */\n #validateMarketList(\n remoteValue: unknown,\n fieldName: string,\n currentValue: string[],\n ): string[] | undefined {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validation`,\n {\n remoteValue,\n type: typeof remoteValue,\n isArray: Array.isArray(remoteValue),\n },\n );\n\n // LaunchDarkly returns comma-separated strings for list values\n // Values may have literal quotes (e.g., '\"xyz\"') due to JSON encoding quirks\n if (typeof remoteValue === 'string') {\n const parsed = this.#filterValidPatterns(\n parseCommaSeparatedString(remoteValue).map(stripQuotes),\n fieldName,\n );\n\n if (parsed.length > 0) {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validated from string`,\n { validatedMarkets: parsed },\n );\n return parsed;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} string was empty after parsing`,\n { fallbackValue: currentValue },\n );\n return undefined;\n }\n\n // Fallback: Validate array of non-empty strings\n if (\n Array.isArray(remoteValue) &&\n remoteValue.every((item) => typeof item === 'string' && item.length > 0)\n ) {\n const validatedMarkets = this.#filterValidPatterns(\n (remoteValue as string[])\n .map((market) => stripQuotes(market.trim()))\n .filter((market) => market.length > 0),\n fieldName,\n );\n\n if (validatedMarkets.length > 0) {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validated from array`,\n { validatedMarkets },\n );\n return validatedMarkets;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} array was empty after filtering`,\n { fallbackValue: currentValue },\n );\n return undefined;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validation FAILED - falling back to local config`,\n {\n reason: Array.isArray(remoteValue)\n ? 'Array contains non-string or empty values'\n : 'Invalid type (expected string or array)',\n fallbackValue: currentValue,\n },\n );\n return undefined;\n }\n\n /**\n * Filter out patterns that fail market pattern validation.\n * Invalid patterns are logged and dropped instead of propagated downstream.\n *\n * @param patterns - The array of market patterns to validate.\n * @param fieldName - The name of the field being validated (for logging).\n * @returns The filtered array containing only valid market patterns.\n */\n #filterValidPatterns(patterns: string[], fieldName: string): string[] {\n return patterns.filter((pattern) => {\n try {\n validateMarketPattern(pattern);\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(\n error,\n `FeatureFlagConfigurationService.filterValidPatterns`,\n ),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'FeatureFlagConfigurationService.filterValidPatterns',\n data: { fieldName, pattern },\n },\n },\n );\n return false;\n }\n });\n }\n\n /**\n * Check if arrays have different values (order-independent comparison)\n *\n * @param a - The first string array to compare.\n * @param b - The second string array to compare.\n * @returns True if the arrays contain different values.\n */\n #arraysHaveDifferentValues(a: string[], b: string[]): boolean {\n return (\n JSON.stringify(\n [...a].sort((itemA, itemB) => itemA.localeCompare(itemB)),\n ) !==\n JSON.stringify([...b].sort((itemA, itemB) => itemA.localeCompare(itemB)))\n );\n }\n\n /**\n * Refresh HIP-3 configuration when remote feature flags change.\n * This method extracts HIP-3 settings from remote flags, validates them,\n * and updates internal state if they differ from current values.\n * When config changes, increments hip3ConfigVersion to trigger ConnectionManager reconnection.\n *\n * Follows the \"sticky remote\" pattern: once remote config is loaded, never downgrade to fallback.\n *\n * @param options - Configuration object\n * @param options.remoteFeatureFlagControllerState - Remote feature flag state\n * @param options.context - ServiceContext providing state access callbacks\n */\n refreshHip3Config(options: {\n remoteFeatureFlagControllerState: PerpsRemoteFeatureFlagState;\n context: ServiceContext;\n }): void {\n const { remoteFeatureFlagControllerState, context } = options;\n\n if (\n !context.getHip3Config ||\n !context.setHip3Config ||\n !context.incrementHip3ConfigVersion\n ) {\n throw new Error(\n 'Required HIP-3 callbacks not available in ServiceContext',\n );\n }\n\n const remoteFlags = remoteFeatureFlagControllerState.remoteFeatureFlags;\n const currentConfig = context.getHip3Config();\n\n // Extract and validate remote HIP-3 equity enabled flag\n const equityFlag = remoteFlags?.perpsHip3Enabled;\n // Use type guard to validate before calling - validatedVersionGatedFeatureFlag also\n // handles invalid flags internally, but proper typing requires the guard\n const validatedEquity = isVersionGatedFeatureFlag(equityFlag)\n ? this.#deps.featureFlags.validateVersionGated(equityFlag)\n : undefined;\n\n this.#deps.debugLogger.log(\n 'PerpsController: HIP-3 equity flag validation',\n {\n equityFlag,\n validatedEquity,\n willUse: validatedEquity === undefined ? 'fallback' : 'remote',\n },\n );\n\n // Extract and validate remote HIP-3 market lists\n const validatedAllowlistMarkets = hasProperty(\n remoteFlags,\n 'perpsHip3AllowlistMarkets',\n )\n ? this.#validateMarketList(\n remoteFlags.perpsHip3AllowlistMarkets,\n 'allowlistMarkets',\n currentConfig.allowlistMarkets,\n )\n : undefined;\n\n const validatedBlocklistMarkets = hasProperty(\n remoteFlags,\n 'perpsHip3BlocklistMarkets',\n )\n ? this.#validateMarketList(\n remoteFlags.perpsHip3BlocklistMarkets,\n 'blocklistMarkets',\n currentConfig.blocklistMarkets,\n )\n : undefined;\n\n // Detect changes (only if we have valid remote values)\n const equityChanged =\n validatedEquity !== undefined &&\n validatedEquity !== currentConfig.enabled;\n const allowlistMarketsChanged =\n validatedAllowlistMarkets !== undefined &&\n this.#arraysHaveDifferentValues(\n validatedAllowlistMarkets,\n currentConfig.allowlistMarkets,\n );\n const blocklistMarketsChanged =\n validatedBlocklistMarkets !== undefined &&\n this.#arraysHaveDifferentValues(\n validatedBlocklistMarkets,\n currentConfig.blocklistMarkets,\n );\n\n if (equityChanged || allowlistMarketsChanged || blocklistMarketsChanged) {\n this.#deps.debugLogger.log(\n 'PerpsController: HIP-3 config changed via remote feature flags',\n {\n equityChanged,\n allowlistMarketsChanged,\n blocklistMarketsChanged,\n oldEquity: currentConfig.enabled,\n newEquity: validatedEquity,\n oldAllowlistMarkets: currentConfig.allowlistMarkets,\n newAllowlistMarkets: validatedAllowlistMarkets,\n oldBlocklistMarkets: currentConfig.blocklistMarkets,\n newBlocklistMarkets: validatedBlocklistMarkets,\n source: 'remote',\n },\n );\n\n // Update internal state (sticky remote - never downgrade)\n context.setHip3Config({\n enabled: validatedEquity,\n allowlistMarkets: validatedAllowlistMarkets\n ? [...validatedAllowlistMarkets]\n : undefined,\n blocklistMarkets: validatedBlocklistMarkets\n ? [...validatedBlocklistMarkets]\n : undefined,\n source: 'remote',\n });\n\n // Increment version to trigger ConnectionManager reconnection and cache clearing\n const newVersion = context.incrementHip3ConfigVersion();\n\n this.#deps.debugLogger.log(\n 'PerpsController: Incremented hip3ConfigVersion to trigger reconnection',\n {\n newVersion,\n newHip3Enabled: validatedEquity ?? currentConfig.enabled,\n newHip3AllowlistMarkets:\n validatedAllowlistMarkets ?? currentConfig.allowlistMarkets,\n newHip3BlocklistMarkets:\n validatedBlocklistMarkets ?? currentConfig.blocklistMarkets,\n },\n );\n\n // Note: ConnectionManager will handle:\n // 1. Detecting hip3ConfigVersion change via Redux monitoring\n // 2. Clearing all StreamManager caches\n // 3. Calling reconnectWithNewContext() -> initializeProviders()\n // 4. Provider reinitialization will read the new HIP-3 config below\n }\n }\n\n /**\n * Respond to RemoteFeatureFlagController state changes\n * Refreshes user eligibility based on geo-blocked regions defined in remote feature flag.\n * Uses fallback configuration when remote feature flag is undefined.\n * Note: Initial eligibility is set in the constructor if fallback regions are provided.\n *\n * @param options - Configuration object\n * @param options.remoteFeatureFlagControllerState - Remote feature flag state\n * @param options.context - ServiceContext providing callbacks\n */\n refreshEligibility(options: {\n remoteFeatureFlagControllerState: PerpsRemoteFeatureFlagState;\n context: ServiceContext;\n }): void {\n const { remoteFeatureFlagControllerState, context } = options;\n\n const perpsGeoBlockedRegionsFeatureFlag =\n // NOTE: Do not use perpsPerpTradingGeoBlockedCountries as it is deprecated.\n remoteFeatureFlagControllerState.remoteFeatureFlags\n ?.perpsPerpTradingGeoBlockedCountriesV2;\n\n const remoteBlockedRegions = (\n perpsGeoBlockedRegionsFeatureFlag as { blockedRegions?: string[] }\n )?.blockedRegions;\n\n if (Array.isArray(remoteBlockedRegions)) {\n this.setBlockedRegions({\n list: remoteBlockedRegions,\n source: 'remote',\n context,\n });\n }\n\n // Also check for HIP-3 config changes\n this.refreshHip3Config({ remoteFeatureFlagControllerState, context });\n }\n\n /**\n * Set blocked region list with \"never downgrade\" pattern enforcement\n * Updates the blocked region list and triggers eligibility refresh.\n * Implements \"sticky remote\": once remote regions are set, never downgrade to fallback.\n *\n * @param options - Configuration object\n * @param options.list - Array of blocked region codes\n * @param options.source - Source of the list ('remote' or 'fallback')\n * @param options.context - ServiceContext providing callbacks\n */\n setBlockedRegions(options: {\n list: string[];\n source: 'remote' | 'fallback';\n context: ServiceContext;\n }): void {\n const { list, source, context } = options;\n\n if (\n !context.getBlockedRegionList ||\n !context.setBlockedRegionList ||\n !context.refreshEligibility\n ) {\n throw new Error(\n 'Required blocked region callbacks not available in ServiceContext',\n );\n }\n\n const currentList = context.getBlockedRegionList();\n\n // Never downgrade from remote to fallback\n if (source === 'fallback' && currentList.source === 'remote') {\n return;\n }\n\n if (Array.isArray(list)) {\n context.setBlockedRegionList(list, source);\n }\n\n context.refreshEligibility().catch((error) => {\n this.#deps.logger.error(\n ensureError(error, 'FeatureFlagConfigurationService.setBlockedRegions'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'FeatureFlagConfigurationService.setBlockedRegions',\n data: { source },\n },\n },\n );\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"FeatureFlagConfigurationService.cjs","sourceRoot":"","sources":["../../src/services/FeatureFlagConfigurationService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8C;AAE9C,8DAA2D;AAC3D,8CAAqD;AAKrD,wDAAkD;AAClD,0DAA6D;AAC7D,oEAGmC;AAGnC;;;;;;;;;;;;;;;GAeG;AACH,MAAa,+BAA+B;IAG1C;;;;OAIG;IACH,YAAY,IAA+B;;QAPlC,wDAAiC;QAQxC,uBAAA,IAAI,yCAAS,IAAI,MAAA,CAAC;IACpB,CAAC;IAuID;;;;;;;;;;;OAWG;IACH,iBAAiB,CAAC,OAGjB;QACC,MAAM,EAAE,gCAAgC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAE9D,IACE,CAAC,OAAO,CAAC,aAAa;YACtB,CAAC,OAAO,CAAC,aAAa;YACtB,CAAC,OAAO,CAAC,0BAA0B,EACnC,CAAC;YACD,MAAM,IAAI,KAAK,CACb,0DAA0D,CAC3D,CAAC;QACJ,CAAC;QAED,MAAM,WAAW,GAAG,gCAAgC,CAAC,kBAAkB,CAAC;QACxE,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;QAE9C,wDAAwD;QACxD,MAAM,UAAU,GAAG,WAAW,EAAE,gBAAgB,CAAC;QACjD,oFAAoF;QACpF,yEAAyE;QACzE,MAAM,eAAe,GAAG,IAAA,iCAAyB,EAAC,UAAU,CAAC;YAC3D,CAAC,CAAC,uBAAA,IAAI,6CAAM,CAAC,YAAY,CAAC,oBAAoB,CAAC,UAAU,CAAC;YAC1D,CAA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,EAAE;gBAC9C,OAAO,EAAE;oBACP,IAAI,EAAE,qDAAqD;oBAC3D,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE;iBAC7B;aACF,CACF,CAAC;YACF,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,mIAS0B,CAAW,EAAE,CAAW;IACjD,OAAO,CACL,IAAI,CAAC,SAAS,CACZ,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAC1D;QACD,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAC1E,CAAC;AACJ,CAAC","sourcesContent":["import { hasProperty } from '@metamask/utils';\n\nimport { PERPS_CONSTANTS } from '../constants/perpsConfig';\nimport { isVersionGatedFeatureFlag } from '../types';\nimport type {\n PerpsPlatformDependencies,\n PerpsRemoteFeatureFlagState,\n} from '../types';\nimport { ensureError } from '../utils/errorUtils';\nimport { validateMarketPattern } from '../utils/marketUtils';\nimport {\n parseCommaSeparatedString,\n stripQuotes,\n} from '../utils/stringParseUtils';\nimport type { ServiceContext } from './ServiceContext';\n\n/**\n * FeatureFlagConfigurationService\n *\n * Handles HIP-3 configuration and geo-blocking configuration from remote feature flags.\n * Implements \"sticky remote\" pattern: once remote config is loaded, never downgrade to fallback.\n * Orchestrates validation, change detection, and version management for feature flag updates.\n *\n * Responsibilities:\n * - Remote feature flag validation and parsing\n * - HIP-3 configuration management (equity, allowlist, blocklist)\n * - Geo-blocking configuration from remote flags\n * - Change detection and version management\n * - \"Sticky remote\" pattern enforcement (never downgrade)\n *\n * Instance-based service with constructor injection of platform dependencies.\n */\nexport class FeatureFlagConfigurationService {\n readonly #deps: PerpsPlatformDependencies;\n\n /**\n * Create a new FeatureFlagConfigurationService instance\n *\n * @param deps - Platform dependencies for logging, metrics, etc.\n */\n constructor(deps: PerpsPlatformDependencies) {\n this.#deps = deps;\n }\n\n /**\n * Validate and parse market list from remote feature flags\n * Handles both string (comma-separated) and array formats from LaunchDarkly\n *\n * @param remoteValue - The raw value from remote feature flags (string or array).\n * @param fieldName - The name of the field being validated (for logging).\n * @param currentValue - The current local market list as fallback reference.\n * @returns The validated market list, or undefined if validation fails.\n */\n #validateMarketList(\n remoteValue: unknown,\n fieldName: string,\n currentValue: string[],\n ): string[] | undefined {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validation`,\n {\n remoteValue,\n type: typeof remoteValue,\n isArray: Array.isArray(remoteValue),\n },\n );\n\n // LaunchDarkly returns comma-separated strings for list values\n // Values may have literal quotes (e.g., '\"xyz\"') due to JSON encoding quirks\n if (typeof remoteValue === 'string') {\n const parsed = this.#filterValidPatterns(\n parseCommaSeparatedString(remoteValue).map(stripQuotes),\n fieldName,\n );\n\n if (parsed.length > 0) {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validated from string`,\n { validatedMarkets: parsed },\n );\n return parsed;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} string was empty after parsing`,\n { fallbackValue: currentValue },\n );\n return undefined;\n }\n\n // Fallback: Validate array of non-empty strings\n if (\n Array.isArray(remoteValue) &&\n remoteValue.every((item) => typeof item === 'string' && item.length > 0)\n ) {\n const validatedMarkets = this.#filterValidPatterns(\n (remoteValue as string[])\n .map((market) => stripQuotes(market.trim()))\n .filter((market) => market.length > 0),\n fieldName,\n );\n\n if (validatedMarkets.length > 0) {\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validated from array`,\n { validatedMarkets },\n );\n return validatedMarkets;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} array was empty after filtering`,\n { fallbackValue: currentValue },\n );\n return undefined;\n }\n\n this.#deps.debugLogger.log(\n `PerpsController: HIP-3 ${fieldName} validation FAILED - falling back to local config`,\n {\n reason: Array.isArray(remoteValue)\n ? 'Array contains non-string or empty values'\n : 'Invalid type (expected string or array)',\n fallbackValue: currentValue,\n },\n );\n return undefined;\n }\n\n /**\n * Filter out patterns that fail market pattern validation.\n * Invalid patterns are logged and dropped instead of propagated downstream.\n *\n * @param patterns - The array of market patterns to validate.\n * @param fieldName - The name of the field being validated (for logging).\n * @returns The filtered array containing only valid market patterns.\n */\n #filterValidPatterns(patterns: string[], fieldName: string): string[] {\n return patterns.filter((pattern) => {\n try {\n validateMarketPattern(pattern);\n return true;\n } catch (error) {\n this.#deps.logger.error(\n ensureError(\n error,\n `FeatureFlagConfigurationService.filterValidPatterns`,\n ),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'FeatureFlagConfigurationService.filterValidPatterns',\n data: { fieldName, pattern },\n },\n },\n );\n return false;\n }\n });\n }\n\n /**\n * Check if arrays have different values (order-independent comparison)\n *\n * @param a - The first string array to compare.\n * @param b - The second string array to compare.\n * @returns True if the arrays contain different values.\n */\n #arraysHaveDifferentValues(a: string[], b: string[]): boolean {\n return (\n JSON.stringify(\n [...a].sort((itemA, itemB) => itemA.localeCompare(itemB)),\n ) !==\n JSON.stringify([...b].sort((itemA, itemB) => itemA.localeCompare(itemB)))\n );\n }\n\n /**\n * Refresh HIP-3 configuration when remote feature flags change.\n * This method extracts HIP-3 settings from remote flags, validates them,\n * and updates internal state if they differ from current values.\n * When config changes, increments hip3ConfigVersion to trigger ConnectionManager reconnection.\n *\n * Follows the \"sticky remote\" pattern: once remote config is loaded, never downgrade to fallback.\n *\n * @param options - Configuration object\n * @param options.remoteFeatureFlagControllerState - Remote feature flag state\n * @param options.context - ServiceContext providing state access callbacks\n */\n refreshHip3Config(options: {\n remoteFeatureFlagControllerState: PerpsRemoteFeatureFlagState;\n context: ServiceContext;\n }): void {\n const { remoteFeatureFlagControllerState, context } = options;\n\n if (\n !context.getHip3Config ||\n !context.setHip3Config ||\n !context.incrementHip3ConfigVersion\n ) {\n throw new Error(\n 'Required HIP-3 callbacks not available in ServiceContext',\n );\n }\n\n const remoteFlags = remoteFeatureFlagControllerState.remoteFeatureFlags;\n const currentConfig = context.getHip3Config();\n\n // Extract and validate remote HIP-3 equity enabled flag\n const equityFlag = remoteFlags?.perpsHip3Enabled;\n // Use type guard to validate before calling - validatedVersionGatedFeatureFlag also\n // handles invalid flags internally, but proper typing requires the guard\n const validatedEquity = isVersionGatedFeatureFlag(equityFlag)\n ? this.#deps.featureFlags.validateVersionGated(equityFlag)\n : undefined;\n\n this.#deps.debugLogger.log(\n 'PerpsController: HIP-3 equity flag validation',\n {\n equityFlag,\n validatedEquity,\n willUse: validatedEquity === undefined ? 'fallback' : 'remote',\n },\n );\n\n // Extract and validate remote HIP-3 market lists\n const validatedAllowlistMarkets = hasProperty(\n remoteFlags,\n 'perpsHip3AllowlistMarkets',\n )\n ? this.#validateMarketList(\n remoteFlags.perpsHip3AllowlistMarkets,\n 'allowlistMarkets',\n currentConfig.allowlistMarkets,\n )\n : undefined;\n\n const validatedBlocklistMarkets = hasProperty(\n remoteFlags,\n 'perpsHip3BlocklistMarkets',\n )\n ? this.#validateMarketList(\n remoteFlags.perpsHip3BlocklistMarkets,\n 'blocklistMarkets',\n currentConfig.blocklistMarkets,\n )\n : undefined;\n\n // Detect changes (only if we have valid remote values)\n const equityChanged =\n validatedEquity !== undefined &&\n validatedEquity !== currentConfig.enabled;\n const allowlistMarketsChanged =\n validatedAllowlistMarkets !== undefined &&\n this.#arraysHaveDifferentValues(\n validatedAllowlistMarkets,\n currentConfig.allowlistMarkets,\n );\n const blocklistMarketsChanged =\n validatedBlocklistMarkets !== undefined &&\n this.#arraysHaveDifferentValues(\n validatedBlocklistMarkets,\n currentConfig.blocklistMarkets,\n );\n\n if (equityChanged || allowlistMarketsChanged || blocklistMarketsChanged) {\n this.#deps.debugLogger.log(\n 'PerpsController: HIP-3 config changed via remote feature flags',\n {\n equityChanged,\n allowlistMarketsChanged,\n blocklistMarketsChanged,\n oldEquity: currentConfig.enabled,\n newEquity: validatedEquity,\n oldAllowlistMarkets: currentConfig.allowlistMarkets,\n newAllowlistMarkets: validatedAllowlistMarkets,\n oldBlocklistMarkets: currentConfig.blocklistMarkets,\n newBlocklistMarkets: validatedBlocklistMarkets,\n source: 'remote',\n },\n );\n\n // Update internal state (sticky remote - never downgrade)\n context.setHip3Config({\n enabled: validatedEquity,\n allowlistMarkets: validatedAllowlistMarkets\n ? [...validatedAllowlistMarkets]\n : undefined,\n blocklistMarkets: validatedBlocklistMarkets\n ? [...validatedBlocklistMarkets]\n : undefined,\n source: 'remote',\n });\n\n // Increment version to trigger ConnectionManager reconnection and cache clearing\n const newVersion = context.incrementHip3ConfigVersion();\n\n this.#deps.debugLogger.log(\n 'PerpsController: Incremented hip3ConfigVersion to trigger reconnection',\n {\n newVersion,\n newHip3Enabled: validatedEquity ?? currentConfig.enabled,\n newHip3AllowlistMarkets:\n validatedAllowlistMarkets ?? currentConfig.allowlistMarkets,\n newHip3BlocklistMarkets:\n validatedBlocklistMarkets ?? currentConfig.blocklistMarkets,\n },\n );\n\n // Note: ConnectionManager will handle:\n // 1. Detecting hip3ConfigVersion change via Redux monitoring\n // 2. Clearing all StreamManager caches\n // 3. Calling reconnectWithNewContext() -> initializeProviders()\n // 4. Provider reinitialization will read the new HIP-3 config below\n }\n }\n\n /**\n * Respond to RemoteFeatureFlagController state changes\n * Refreshes user eligibility based on geo-blocked regions defined in remote feature flag.\n * Uses fallback configuration when remote feature flag is undefined.\n * Note: Initial eligibility is set in the constructor if fallback regions are provided.\n *\n * @param options - Configuration object\n * @param options.remoteFeatureFlagControllerState - Remote feature flag state\n * @param options.context - ServiceContext providing callbacks\n */\n refreshEligibility(options: {\n remoteFeatureFlagControllerState: PerpsRemoteFeatureFlagState;\n context: ServiceContext;\n }): void {\n const { remoteFeatureFlagControllerState, context } = options;\n\n const perpsGeoBlockedRegionsFeatureFlag =\n // NOTE: Do not use perpsPerpTradingGeoBlockedCountries as it is deprecated.\n remoteFeatureFlagControllerState.remoteFeatureFlags\n ?.perpsPerpTradingGeoBlockedCountriesV2;\n\n const remoteBlockedRegions = (\n perpsGeoBlockedRegionsFeatureFlag as { blockedRegions?: string[] }\n )?.blockedRegions;\n\n if (Array.isArray(remoteBlockedRegions)) {\n this.setBlockedRegions({\n list: remoteBlockedRegions,\n source: 'remote',\n context,\n });\n }\n\n // Also check for HIP-3 config changes\n this.refreshHip3Config({ remoteFeatureFlagControllerState, context });\n }\n\n /**\n * Set blocked region list with \"never downgrade\" pattern enforcement\n * Updates the blocked region list and triggers eligibility refresh.\n * Implements \"sticky remote\": once remote regions are set, never downgrade to fallback.\n *\n * @param options - Configuration object\n * @param options.list - Array of blocked region codes\n * @param options.source - Source of the list ('remote' or 'fallback')\n * @param options.context - ServiceContext providing callbacks\n */\n setBlockedRegions(options: {\n list: string[];\n source: 'remote' | 'fallback';\n context: ServiceContext;\n }): void {\n const { list, source, context } = options;\n\n if (\n !context.getBlockedRegionList ||\n !context.setBlockedRegionList ||\n !context.refreshEligibility\n ) {\n throw new Error(\n 'Required blocked region callbacks not available in ServiceContext',\n );\n }\n\n const currentList = context.getBlockedRegionList();\n\n // Never downgrade from remote to fallback\n if (source === 'fallback' && currentList.source === 'remote') {\n return;\n }\n\n if (Array.isArray(list)) {\n context.setBlockedRegionList(list, source);\n }\n\n context.refreshEligibility().catch((error) => {\n this.#deps.logger.error(\n ensureError(error, 'FeatureFlagConfigurationService.setBlockedRegions'),\n {\n tags: { feature: PERPS_CONSTANTS.FeatureName },\n context: {\n name: 'FeatureFlagConfigurationService.setBlockedRegions',\n data: { source },\n },\n },\n );\n });\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FeatureFlagConfigurationService.d.cts","sourceRoot":"","sources":["../../src/services/FeatureFlagConfigurationService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FeatureFlagConfigurationService.d.cts","sourceRoot":"","sources":["../../src/services/FeatureFlagConfigurationService.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,yBAAyB,EACzB,2BAA2B,EAC5B,2BAAiB;AAOlB,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAEvD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,+BAA+B;;IAG1C;;;;OAIG;gBACS,IAAI,EAAE,yBAAyB;IAyI3C;;;;;;;;;;;OAWG;IACH,iBAAiB,CAAC,OAAO,EAAE;QACzB,gCAAgC,EAAE,2BAA2B,CAAC;QAC9D,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,IAAI;IA6HR;;;;;;;;;OASG;IACH,kBAAkB,CAAC,OAAO,EAAE;QAC1B,gCAAgC,EAAE,2BAA2B,CAAC;QAC9D,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,IAAI;IAwBR;;;;;;;;;OASG;IACH,iBAAiB,CAAC,OAAO,EAAE;QACzB,IAAI,EAAE,MAAM,EAAE,CAAC;QACf,MAAM,EAAE,QAAQ,GAAG,UAAU,CAAC;QAC9B,OAAO,EAAE,cAAc,CAAC;KACzB,GAAG,IAAI;CAqCT"}
|