@metamask-previews/perps-controller 2.0.0-preview-de55719 → 2.0.0-preview-51571fe
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/PerpsController.cjs +87 -10
- package/dist/PerpsController.cjs.map +1 -1
- package/dist/PerpsController.d.cts +25 -2
- package/dist/PerpsController.d.cts.map +1 -1
- package/dist/PerpsController.d.mts +25 -2
- package/dist/PerpsController.d.mts.map +1 -1
- package/dist/PerpsController.mjs +87 -10
- package/dist/PerpsController.mjs.map +1 -1
- package/dist/constants/hyperLiquidConfig.cjs +2 -2
- package/dist/constants/hyperLiquidConfig.cjs.map +1 -1
- package/dist/constants/hyperLiquidConfig.mjs +2 -2
- package/dist/constants/hyperLiquidConfig.mjs.map +1 -1
- package/dist/constants/perpsConfig.cjs +8 -1
- package/dist/constants/perpsConfig.cjs.map +1 -1
- package/dist/constants/perpsConfig.d.cts +7 -0
- package/dist/constants/perpsConfig.d.cts.map +1 -1
- package/dist/constants/perpsConfig.d.mts +7 -0
- package/dist/constants/perpsConfig.d.mts.map +1 -1
- package/dist/constants/perpsConfig.mjs +7 -0
- package/dist/constants/perpsConfig.mjs.map +1 -1
- package/dist/providers/HyperLiquidProvider.cjs +31 -0
- package/dist/providers/HyperLiquidProvider.cjs.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.cts.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.mts.map +1 -1
- package/dist/providers/HyperLiquidProvider.mjs +31 -0
- package/dist/providers/HyperLiquidProvider.mjs.map +1 -1
- package/dist/services/AccountService.cjs +35 -35
- package/dist/services/AccountService.cjs.map +1 -1
- package/dist/services/AccountService.d.cts.map +1 -1
- package/dist/services/AccountService.d.mts.map +1 -1
- package/dist/services/AccountService.mjs +35 -35
- package/dist/services/AccountService.mjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.cjs +32 -19
- package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.mjs +32 -19
- package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
- package/dist/services/TradingService.cjs +4 -3
- package/dist/services/TradingService.cjs.map +1 -1
- package/dist/services/TradingService.d.cts.map +1 -1
- package/dist/services/TradingService.d.mts.map +1 -1
- package/dist/services/TradingService.mjs +4 -3
- package/dist/services/TradingService.mjs.map +1 -1
- package/dist/utils/orderCalculations.cjs +4 -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 +5 -2
- package/dist/utils/orderCalculations.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -135,7 +135,33 @@ class AccountService {
|
|
|
135
135
|
context.stateManager.update((state) => {
|
|
136
136
|
state.lastError = null;
|
|
137
137
|
state.lastUpdateTimestamp = Date.now();
|
|
138
|
-
state.
|
|
138
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
139
|
+
if (result.txHash) {
|
|
140
|
+
// Direct completion: remove from queue and record the txHash
|
|
141
|
+
// so the polling hook won't re-match this completion.
|
|
142
|
+
// We do NOT update lastCompletedWithdrawalTimestamp here
|
|
143
|
+
// because Date.now() is local device time while the FIFO guard
|
|
144
|
+
// compares against API server timestamps — mixing domains can
|
|
145
|
+
// poison the guard. The txHash exclusion alone prevents
|
|
146
|
+
// re-matching since the item is also spliced from the queue.
|
|
147
|
+
if (withdrawalRequestIndex !== -1) {
|
|
148
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
149
|
+
}
|
|
150
|
+
state.lastCompletedWithdrawalTxHashes.push(result.txHash);
|
|
151
|
+
const hasOtherPending = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
152
|
+
state.withdrawInProgress = hasOtherPending;
|
|
153
|
+
}
|
|
154
|
+
else if (withdrawalRequestIndex !== -1) {
|
|
155
|
+
const requestToUpdate = state.withdrawalRequests[withdrawalRequestIndex];
|
|
156
|
+
// Withdrawal is bridging (no txHash yet)
|
|
157
|
+
requestToUpdate.status = 'bridging';
|
|
158
|
+
requestToUpdate.success = true;
|
|
159
|
+
if (result.withdrawalId) {
|
|
160
|
+
requestToUpdate.withdrawalId = result.withdrawalId;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// Set lastWithdrawResult when submission is successful (even if bridging)
|
|
164
|
+
// This triggers the "confirmed" toast telling user funds arrive in ~5 mins
|
|
139
165
|
state.lastWithdrawResult = {
|
|
140
166
|
success: true,
|
|
141
167
|
txHash: result.txHash ?? '',
|
|
@@ -144,24 +170,6 @@ class AccountService {
|
|
|
144
170
|
timestamp: Date.now(),
|
|
145
171
|
error: '',
|
|
146
172
|
};
|
|
147
|
-
// Update the withdrawal request by request ID
|
|
148
|
-
if (state.withdrawalRequests.length > 0) {
|
|
149
|
-
const requestToUpdate = state.withdrawalRequests.find((req) => req.id === currentWithdrawalId);
|
|
150
|
-
if (requestToUpdate) {
|
|
151
|
-
if (result.txHash) {
|
|
152
|
-
requestToUpdate.status = 'completed';
|
|
153
|
-
requestToUpdate.success = true;
|
|
154
|
-
requestToUpdate.txHash = result.txHash;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
requestToUpdate.status = 'bridging';
|
|
158
|
-
requestToUpdate.success = true;
|
|
159
|
-
}
|
|
160
|
-
if (result.withdrawalId) {
|
|
161
|
-
requestToUpdate.withdrawalId = result.withdrawalId;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
173
|
});
|
|
166
174
|
}
|
|
167
175
|
__classPrivateFieldGet(this, _AccountService_deps, "f").debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {
|
|
@@ -201,7 +209,6 @@ class AccountService {
|
|
|
201
209
|
context.stateManager.update((state) => {
|
|
202
210
|
state.lastError = result.error ?? perpsErrorCodes_1.PERPS_ERROR_CODES.WITHDRAW_FAILED;
|
|
203
211
|
state.lastUpdateTimestamp = Date.now();
|
|
204
|
-
state.withdrawInProgress = false;
|
|
205
212
|
state.lastWithdrawResult = {
|
|
206
213
|
success: false,
|
|
207
214
|
error: result.error ?? perpsErrorCodes_1.PERPS_ERROR_CODES.WITHDRAW_FAILED,
|
|
@@ -210,14 +217,11 @@ class AccountService {
|
|
|
210
217
|
timestamp: Date.now(),
|
|
211
218
|
txHash: '',
|
|
212
219
|
};
|
|
213
|
-
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
if (requestToUpdate) {
|
|
217
|
-
requestToUpdate.status = 'failed';
|
|
218
|
-
requestToUpdate.success = false;
|
|
219
|
-
}
|
|
220
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
221
|
+
if (withdrawalRequestIndex !== -1) {
|
|
222
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
220
223
|
}
|
|
224
|
+
state.withdrawInProgress = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
221
225
|
});
|
|
222
226
|
}
|
|
223
227
|
__classPrivateFieldGet(this, _AccountService_deps, "f").debugLogger.log('AccountService: WITHDRAWAL FAILED', {
|
|
@@ -253,7 +257,6 @@ class AccountService {
|
|
|
253
257
|
context.stateManager.update((state) => {
|
|
254
258
|
state.lastError = errorMessage;
|
|
255
259
|
state.lastUpdateTimestamp = Date.now();
|
|
256
|
-
state.withdrawInProgress = false;
|
|
257
260
|
state.lastWithdrawResult = {
|
|
258
261
|
success: false,
|
|
259
262
|
error: errorMessage,
|
|
@@ -262,14 +265,11 @@ class AccountService {
|
|
|
262
265
|
timestamp: Date.now(),
|
|
263
266
|
txHash: '',
|
|
264
267
|
};
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
if (requestToUpdate) {
|
|
269
|
-
requestToUpdate.status = 'failed';
|
|
270
|
-
requestToUpdate.success = false;
|
|
271
|
-
}
|
|
268
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
269
|
+
if (withdrawalRequestIndex !== -1) {
|
|
270
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
272
271
|
}
|
|
272
|
+
state.withdrawInProgress = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
273
273
|
});
|
|
274
274
|
}
|
|
275
275
|
// Track withdrawal transaction failed (catch block)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.cjs","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAoC;AAGpC,4DAGiC;AACjC,0EAA6D;AAC7D,8DAGkC;AAClC,4DAAuD;AACvD,8CAIkB;AASlB,4DAA8D;AAC9D,wDAAkD;AAElD;;;;;;;;;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,uBAAA,IAAI,6BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,QAAQ,CAAC,OAKd;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;QAEnE,MAAM,OAAO,GAAG,IAAA,SAAM,GAAE,CAAC;QACzB,MAAM,SAAS,GAAG,uBAAA,IAAI,4BAAM,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;QAC/C,IAAI,SAOS,CAAC;QAEd,8CAA8C;QAC9C,MAAM,mBAAmB,GAAG,YAAY,IAAI,CAAC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O,EAAE;oBACP,IAAI,EAAE,mCAAmC;oBACzC,IAAI,EAAE,EAAE,MAAM,EAAE;iBACjB;aACF,CACF,CAAC;YACF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AAhXD,wCAgXC","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 state.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n if (result.txHash) {\n requestToUpdate.status = 'completed' as TransactionStatus;\n requestToUpdate.success = true;\n requestToUpdate.txHash = result.txHash;\n } else {\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n }\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\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.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n requestToUpdate.status = 'failed' as TransactionStatus;\n requestToUpdate.success = false;\n }\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.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n requestToUpdate.status = 'failed' as TransactionStatus;\n requestToUpdate.success = false;\n }\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;AAGpC,4DAGiC;AACjC,0EAA6D;AAC7D,8DAGkC;AAClC,4DAAuD;AACvD,8CAIkB;AASlB,4DAA8D;AAC9D,wDAAkD;AAElD;;;;;;;;;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,uBAAA,IAAI,6BAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,QAAQ,CAAC,OAKd;QACC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;QAEnE,MAAM,OAAO,GAAG,IAAA,SAAM,GAAE,CAAC;QACzB,MAAM,SAAS,GAAG,uBAAA,IAAI,4BAAM,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;QAC/C,IAAI,SAOS,CAAC;QAEd,8CAA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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 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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.d.cts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAgBvD,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAKvE;;;;;;;;;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;
|
|
1
|
+
{"version":3,"file":"AccountService.d.cts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAgBvD,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAKvE;;;;;;;;;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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.d.mts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAgBvD,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAKvE;;;;;;;;;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;
|
|
1
|
+
{"version":3,"file":"AccountService.d.mts","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAgBvD,OAAO,KAAK,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,yBAAyB,EAC1B,2BAAiB;AAClB,OAAO,KAAK,EAAE,4BAA4B,EAAE,+BAA2B;AAKvE;;;;;;;;;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"}
|
|
@@ -132,7 +132,33 @@ export class AccountService {
|
|
|
132
132
|
context.stateManager.update((state) => {
|
|
133
133
|
state.lastError = null;
|
|
134
134
|
state.lastUpdateTimestamp = Date.now();
|
|
135
|
-
state.
|
|
135
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
136
|
+
if (result.txHash) {
|
|
137
|
+
// Direct completion: remove from queue and record the txHash
|
|
138
|
+
// so the polling hook won't re-match this completion.
|
|
139
|
+
// We do NOT update lastCompletedWithdrawalTimestamp here
|
|
140
|
+
// because Date.now() is local device time while the FIFO guard
|
|
141
|
+
// compares against API server timestamps — mixing domains can
|
|
142
|
+
// poison the guard. The txHash exclusion alone prevents
|
|
143
|
+
// re-matching since the item is also spliced from the queue.
|
|
144
|
+
if (withdrawalRequestIndex !== -1) {
|
|
145
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
146
|
+
}
|
|
147
|
+
state.lastCompletedWithdrawalTxHashes.push(result.txHash);
|
|
148
|
+
const hasOtherPending = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
149
|
+
state.withdrawInProgress = hasOtherPending;
|
|
150
|
+
}
|
|
151
|
+
else if (withdrawalRequestIndex !== -1) {
|
|
152
|
+
const requestToUpdate = state.withdrawalRequests[withdrawalRequestIndex];
|
|
153
|
+
// Withdrawal is bridging (no txHash yet)
|
|
154
|
+
requestToUpdate.status = 'bridging';
|
|
155
|
+
requestToUpdate.success = true;
|
|
156
|
+
if (result.withdrawalId) {
|
|
157
|
+
requestToUpdate.withdrawalId = result.withdrawalId;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Set lastWithdrawResult when submission is successful (even if bridging)
|
|
161
|
+
// This triggers the "confirmed" toast telling user funds arrive in ~5 mins
|
|
136
162
|
state.lastWithdrawResult = {
|
|
137
163
|
success: true,
|
|
138
164
|
txHash: result.txHash ?? '',
|
|
@@ -141,24 +167,6 @@ export class AccountService {
|
|
|
141
167
|
timestamp: Date.now(),
|
|
142
168
|
error: '',
|
|
143
169
|
};
|
|
144
|
-
// Update the withdrawal request by request ID
|
|
145
|
-
if (state.withdrawalRequests.length > 0) {
|
|
146
|
-
const requestToUpdate = state.withdrawalRequests.find((req) => req.id === currentWithdrawalId);
|
|
147
|
-
if (requestToUpdate) {
|
|
148
|
-
if (result.txHash) {
|
|
149
|
-
requestToUpdate.status = 'completed';
|
|
150
|
-
requestToUpdate.success = true;
|
|
151
|
-
requestToUpdate.txHash = result.txHash;
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
requestToUpdate.status = 'bridging';
|
|
155
|
-
requestToUpdate.success = true;
|
|
156
|
-
}
|
|
157
|
-
if (result.withdrawalId) {
|
|
158
|
-
requestToUpdate.withdrawalId = result.withdrawalId;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
170
|
});
|
|
163
171
|
}
|
|
164
172
|
__classPrivateFieldGet(this, _AccountService_deps, "f").debugLogger.log('AccountService: WITHDRAWAL SUCCESSFUL', {
|
|
@@ -198,7 +206,6 @@ export class AccountService {
|
|
|
198
206
|
context.stateManager.update((state) => {
|
|
199
207
|
state.lastError = result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED;
|
|
200
208
|
state.lastUpdateTimestamp = Date.now();
|
|
201
|
-
state.withdrawInProgress = false;
|
|
202
209
|
state.lastWithdrawResult = {
|
|
203
210
|
success: false,
|
|
204
211
|
error: result.error ?? PERPS_ERROR_CODES.WITHDRAW_FAILED,
|
|
@@ -207,14 +214,11 @@ export class AccountService {
|
|
|
207
214
|
timestamp: Date.now(),
|
|
208
215
|
txHash: '',
|
|
209
216
|
};
|
|
210
|
-
|
|
211
|
-
if (
|
|
212
|
-
|
|
213
|
-
if (requestToUpdate) {
|
|
214
|
-
requestToUpdate.status = 'failed';
|
|
215
|
-
requestToUpdate.success = false;
|
|
216
|
-
}
|
|
217
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
218
|
+
if (withdrawalRequestIndex !== -1) {
|
|
219
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
217
220
|
}
|
|
221
|
+
state.withdrawInProgress = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
218
222
|
});
|
|
219
223
|
}
|
|
220
224
|
__classPrivateFieldGet(this, _AccountService_deps, "f").debugLogger.log('AccountService: WITHDRAWAL FAILED', {
|
|
@@ -250,7 +254,6 @@ export class AccountService {
|
|
|
250
254
|
context.stateManager.update((state) => {
|
|
251
255
|
state.lastError = errorMessage;
|
|
252
256
|
state.lastUpdateTimestamp = Date.now();
|
|
253
|
-
state.withdrawInProgress = false;
|
|
254
257
|
state.lastWithdrawResult = {
|
|
255
258
|
success: false,
|
|
256
259
|
error: errorMessage,
|
|
@@ -259,14 +262,11 @@ export class AccountService {
|
|
|
259
262
|
timestamp: Date.now(),
|
|
260
263
|
txHash: '',
|
|
261
264
|
};
|
|
262
|
-
|
|
263
|
-
if (
|
|
264
|
-
|
|
265
|
-
if (requestToUpdate) {
|
|
266
|
-
requestToUpdate.status = 'failed';
|
|
267
|
-
requestToUpdate.success = false;
|
|
268
|
-
}
|
|
265
|
+
const withdrawalRequestIndex = state.withdrawalRequests.findIndex((req) => req.id === currentWithdrawalId);
|
|
266
|
+
if (withdrawalRequestIndex !== -1) {
|
|
267
|
+
state.withdrawalRequests.splice(withdrawalRequestIndex, 1);
|
|
269
268
|
}
|
|
269
|
+
state.withdrawInProgress = state.withdrawalRequests.some((req) => req.status === 'pending' || req.status === 'bridging');
|
|
270
270
|
});
|
|
271
271
|
}
|
|
272
272
|
// Track withdrawal transaction failed (catch block)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountService.mjs","sourceRoot":"","sources":["../../src/services/AccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAGpC,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;AAElD;;;;;;;;;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,CAA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AE,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 state.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n if (result.txHash) {\n requestToUpdate.status = 'completed' as TransactionStatus;\n requestToUpdate.success = true;\n requestToUpdate.txHash = result.txHash;\n } else {\n requestToUpdate.status = 'bridging' as TransactionStatus;\n requestToUpdate.success = true;\n }\n if (result.withdrawalId) {\n requestToUpdate.withdrawalId = result.withdrawalId;\n }\n }\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.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n requestToUpdate.status = 'failed' as TransactionStatus;\n requestToUpdate.success = false;\n }\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.withdrawInProgress = false;\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 // Update the withdrawal request by request ID\n if (state.withdrawalRequests.length > 0) {\n const requestToUpdate = state.withdrawalRequests.find(\n (req) => req.id === currentWithdrawalId,\n );\n if (requestToUpdate) {\n requestToUpdate.status = 'failed' as TransactionStatus;\n requestToUpdate.success = false;\n }\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;AAGpC,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,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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"]}
|
|
@@ -1750,26 +1750,39 @@ async function _HyperLiquidSubscriptionService_ensureOrderFillISubscription(acco
|
|
|
1750
1750
|
const userAddress = await __classPrivateFieldGet(this, _HyperLiquidSubscriptionService_walletService, "f").getUserAddressWithDefault(accountId);
|
|
1751
1751
|
// userFills returns a Promise<ISubscription>, need to await it
|
|
1752
1752
|
const subscription = await subscriptionClient.userFills({ user: userAddress }, (data) => {
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
fee: fill.fee,
|
|
1760
|
-
timestamp: fill.time,
|
|
1761
|
-
pnl: fill.closedPnl,
|
|
1762
|
-
direction: fill.dir,
|
|
1763
|
-
feeToken: fill.feeToken,
|
|
1764
|
-
startPosition: fill.startPosition,
|
|
1765
|
-
liquidation: fill.liquidation
|
|
1766
|
-
? {
|
|
1767
|
-
liquidatedUser: fill.liquidation.liquidatedUser,
|
|
1768
|
-
markPx: fill.liquidation.markPx,
|
|
1769
|
-
method: fill.liquidation.method,
|
|
1753
|
+
// Build a Map for O(1) lookup instead of O(n) find per fill
|
|
1754
|
+
const orderMap = new Map();
|
|
1755
|
+
if (__classPrivateFieldGet(this, _HyperLiquidSubscriptionService_cachedOrders, "f")) {
|
|
1756
|
+
for (const order of __classPrivateFieldGet(this, _HyperLiquidSubscriptionService_cachedOrders, "f")) {
|
|
1757
|
+
if (order.detailedOrderType) {
|
|
1758
|
+
orderMap.set(order.orderId, order.detailedOrderType);
|
|
1770
1759
|
}
|
|
1771
|
-
|
|
1772
|
-
}
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
const orderFills = data.fills.map((fill) => {
|
|
1763
|
+
const oid = fill.oid.toString();
|
|
1764
|
+
return {
|
|
1765
|
+
orderId: oid,
|
|
1766
|
+
symbol: fill.coin,
|
|
1767
|
+
side: fill.side,
|
|
1768
|
+
size: fill.sz,
|
|
1769
|
+
price: fill.px,
|
|
1770
|
+
fee: fill.fee,
|
|
1771
|
+
timestamp: fill.time,
|
|
1772
|
+
pnl: fill.closedPnl,
|
|
1773
|
+
direction: fill.dir,
|
|
1774
|
+
feeToken: fill.feeToken,
|
|
1775
|
+
startPosition: fill.startPosition,
|
|
1776
|
+
liquidation: fill.liquidation
|
|
1777
|
+
? {
|
|
1778
|
+
liquidatedUser: fill.liquidation.liquidatedUser,
|
|
1779
|
+
markPx: fill.liquidation.markPx,
|
|
1780
|
+
method: fill.liquidation.method,
|
|
1781
|
+
}
|
|
1782
|
+
: undefined,
|
|
1783
|
+
detailedOrderType: orderMap.get(oid),
|
|
1784
|
+
};
|
|
1785
|
+
});
|
|
1773
1786
|
// Cache fills for cache-first pattern (similar to price caching)
|
|
1774
1787
|
// This allows getOrFetchFills() to return cached data without REST API calls
|
|
1775
1788
|
if (data.isSnapshot) {
|