@metamask/transaction-controller 62.9.2 → 62.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +34 -1
  2. package/dist/TransactionController.cjs +8 -8
  3. package/dist/TransactionController.cjs.map +1 -1
  4. package/dist/TransactionController.d.cts +11 -4
  5. package/dist/TransactionController.d.cts.map +1 -1
  6. package/dist/TransactionController.d.mts +11 -4
  7. package/dist/TransactionController.d.mts.map +1 -1
  8. package/dist/TransactionController.mjs +8 -8
  9. package/dist/TransactionController.mjs.map +1 -1
  10. package/dist/api/simulation-api.cjs.map +1 -1
  11. package/dist/api/simulation-api.d.cts +4 -2
  12. package/dist/api/simulation-api.d.cts.map +1 -1
  13. package/dist/api/simulation-api.d.mts +4 -2
  14. package/dist/api/simulation-api.d.mts.map +1 -1
  15. package/dist/api/simulation-api.mjs.map +1 -1
  16. package/dist/helpers/IncomingTransactionHelper.cjs +58 -3
  17. package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
  18. package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
  19. package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
  20. package/dist/helpers/IncomingTransactionHelper.mjs +59 -4
  21. package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
  22. package/dist/types.cjs +5 -0
  23. package/dist/types.cjs.map +1 -1
  24. package/dist/types.d.cts +7 -0
  25. package/dist/types.d.cts.map +1 -1
  26. package/dist/types.d.mts +7 -0
  27. package/dist/types.d.mts.map +1 -1
  28. package/dist/types.mjs +5 -0
  29. package/dist/types.mjs.map +1 -1
  30. package/dist/utils/balance-changes.cjs +22 -2
  31. package/dist/utils/balance-changes.cjs.map +1 -1
  32. package/dist/utils/balance-changes.d.cts.map +1 -1
  33. package/dist/utils/balance-changes.d.mts.map +1 -1
  34. package/dist/utils/balance-changes.mjs +22 -2
  35. package/dist/utils/balance-changes.mjs.map +1 -1
  36. package/dist/utils/batch.cjs +2 -2
  37. package/dist/utils/batch.cjs.map +1 -1
  38. package/dist/utils/batch.mjs +2 -2
  39. package/dist/utils/batch.mjs.map +1 -1
  40. package/dist/utils/feature-flags.cjs +42 -1
  41. package/dist/utils/feature-flags.cjs.map +1 -1
  42. package/dist/utils/feature-flags.d.cts +31 -0
  43. package/dist/utils/feature-flags.d.cts.map +1 -1
  44. package/dist/utils/feature-flags.d.mts +31 -0
  45. package/dist/utils/feature-flags.d.mts.map +1 -1
  46. package/dist/utils/feature-flags.mjs +38 -0
  47. package/dist/utils/feature-flags.mjs.map +1 -1
  48. package/package.json +2 -1
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.cjs","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,2CAAqD;AAErD,mCAAmC;AAEnC,gDAGsB;AACtB,0CAA8E;AAC9E,0CAA0C;AAG1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,UAAU,GAAG,6BAA6B,CAAC;AACjD,MAAM,QAAQ,GAAG,6CAA6C,CAAC;AAC/D,MAAM,iBAAiB,GAAG,UAAU,CAAC;AA6PrC,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACxC,OAAY,EACZ,OAA0B;IAE1B,IAAI,GAAG,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE1C,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAC7B,CAAC,MAAM,OAAO,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACjD,IAAI,MAAM,EAAE,CAAC;QACX,GAAG,GAAG,MAAM,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACnC,gBAAgB,IAAI,CAAC,CAAC;IAEtB,MAAM,YAAY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AAC;QACvC,MAAM,IAAI,yCAAgC,CAAC,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,cAAc;IAC3B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,kBAAkB,CAAC,GAAG,iBAAiB,EAAE,CAAC;IAChE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAS,MAAM,CAAC,SAAiB;IAC/B,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,eAAe,CAAC,OAA0B;IACjD,MAAM,UAAU,GAAG,IAAA,kBAAS,EAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,MAAM,WAAW,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;QAClD,MAAM,YAAY,GAAG,WAAW,CAAC,EAAE,EAAE,WAAW,EAAS,CAAC;QAE1D,MAAM,qBAAqB,GACzB,wCAA4B,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAEtD,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC3B,SAAS;QACX,CAAC;QAED,UAAU,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC;QAElD,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG;YACnC,IAAI,EAAE,uDAA2C;SAClD,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport {\n CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n DELEGATION_MANAGER_ADDRESSES,\n} from '../constants';\nimport { SimulationChainNotSupportedError, SimulationError } from '../errors';\nimport { projectLogger } from '../logger';\nimport type { GetSimulationConfig } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'simulation-api');\n\nconst RPC_METHOD = 'infura_simulateTransactions';\nconst BASE_URL = 'https://tx-sentinel-{0}.api.cx.metamask.io/';\nconst ENDPOINT_NETWORKS = 'networks';\n\n/** Single transaction to simulate in a simulation API request. */\nexport type SimulationRequestTransaction = {\n authorizationList?: {\n /** Address of a smart contract that contains the code to be set. */\n address: Hex;\n\n /** Address of the account being upgraded. */\n from: Hex;\n }[];\n\n /** Data to send with the transaction. */\n data?: Hex;\n\n /** Sender of the transaction. */\n from: Hex;\n\n /** Gas limit for the transaction. */\n gas?: Hex;\n\n /** Maximum fee per gas for the transaction. */\n maxFeePerGas?: Hex;\n\n /** Maximum priority fee per gas for the transaction. */\n maxPriorityFeePerGas?: Hex;\n\n /** Recipient of the transaction. */\n to?: Hex;\n\n /** Value to send with the transaction. */\n value?: Hex;\n};\n\n/** Request to the simulation API to simulate transactions. */\nexport type SimulationRequest = {\n blockOverrides?: {\n time?: Hex;\n };\n\n /**\n * Function to get the simulation configuration.\n */\n getSimulationConfig: GetSimulationConfig;\n\n /**\n * Overrides to the state of the blockchain, keyed by address.\n */\n overrides?: {\n [address: Hex]: {\n /** Override the code for an address. */\n code?: Hex;\n\n /** Overrides to the storage slots for an address. */\n stateDiff?: {\n [slot: Hex]: Hex;\n };\n };\n };\n\n /**\n * Whether to include available token fees.\n */\n suggestFees?: {\n /* Whether to estimate gas for the transaction being submitted via a delegation. */\n with7702?: boolean;\n\n /* Whether to include the gas fee of the token transfer. */\n withFeeTransfer?: boolean;\n\n /* Whether to include the native transfer if available. */\n withTransfer?: boolean;\n };\n\n /**\n * Transactions to be sequentially simulated.\n * State changes impact subsequent transactions in the list.\n */\n transactions: SimulationRequestTransaction[];\n\n /**\n * Whether to include call traces in the response.\n * Defaults to false.\n */\n withCallTrace?: boolean;\n\n /**\n * Whether to include the default block data in the simulation.\n * Defaults to false.\n */\n withDefaultBlockOverrides?: boolean;\n\n /**\n * Whether to use the gas fees in the simulation.\n * Defaults to false.\n */\n withGas?: boolean;\n\n /**\n * Whether to include event logs in the response.\n * Defaults to false.\n */\n withLogs?: boolean;\n};\n\n/** Raw event log emitted by a simulated transaction. */\nexport type SimulationResponseLog = {\n /** Address of the account that created the event. */\n address: Hex;\n\n /** Raw data in the event that is not indexed. */\n data: Hex;\n\n /** Raw indexed data from the event. */\n topics: Hex[];\n};\n\n/** Call trace of a single simulated transaction. */\nexport type SimulationResponseCallTrace = {\n /** Nested calls. */\n calls: SimulationResponseCallTrace[];\n\n /** Raw event logs created by the call. */\n logs: SimulationResponseLog[];\n};\n\n/**\n * Changes to the blockchain state.\n * Keyed by account address.\n */\nexport type SimulationResponseStateDiff = {\n [address: Hex]: {\n /** Native balance of the account. */\n balance?: Hex;\n\n /** Nonce of the account. */\n nonce?: Hex;\n\n /** Storage values per slot. */\n storage?: {\n [slot: Hex]: Hex;\n };\n };\n};\n\nexport type SimulationResponseTokenFee = {\n /** Token data independent of current transaction. */\n token: {\n /** Address of the token contract. */\n address: Hex;\n\n /** Decimals of the token. */\n decimals: number;\n\n /** Symbol of the token. */\n symbol: string;\n };\n\n /** Amount of tokens needed to pay for gas. */\n balanceNeededToken: Hex;\n\n /** Current token balance of sender. */\n currentBalanceToken: Hex;\n\n /** Account address that token should be transferred to. */\n feeRecipient: Hex;\n\n /** Conversation rate of 1 token to native WEI. */\n rateWei: Hex;\n\n /** Portion of `balanceNeededToken` that is the fee paid to MetaMask. */\n serviceFee?: Hex;\n\n /** Estimated gas limit required for fee transfer. */\n transferEstimate: Hex;\n};\n\n/** Response from the simulation API for a single transaction. */\nexport type SimulationResponseTransaction = {\n /** Hierarchy of call data including nested calls and logs. */\n callTrace?: SimulationResponseCallTrace;\n\n /** An error message indicating the transaction could not be simulated. */\n error?: string;\n\n /** Recommended gas fees for the transaction. */\n fees?: {\n /** Gas limit for the fee level. */\n gas: Hex;\n\n /** Maximum fee per gas for the fee level. */\n maxFeePerGas: Hex;\n\n /** Maximum priority fee per gas for the fee level. */\n maxPriorityFeePerGas: Hex;\n\n /** Token fee data for the fee level. */\n tokenFees: SimulationResponseTokenFee[];\n }[];\n\n /**\n * Estimated total gas cost of the transaction.\n * Included in the stateDiff if `withGas` is true.\n */\n gasCost?: number;\n\n /** Required `gasLimit` for the transaction. */\n gasLimit?: Hex;\n\n /** Total gas used by the transaction. */\n gasUsed?: Hex;\n\n /** Return value of the transaction, such as the balance if calling balanceOf. */\n return: Hex;\n\n /** Changes to the blockchain state. */\n stateDiff?: {\n /** Initial blockchain state before the transaction. */\n pre?: SimulationResponseStateDiff;\n\n /** Updated blockchain state after the transaction. */\n post?: SimulationResponseStateDiff;\n };\n};\n\n/** Response from the simulation API. */\nexport type SimulationResponse = {\n /** Simulation data for each transaction in the request. */\n transactions: SimulationResponseTransaction[];\n\n sponsorship: {\n /** Whether the gas costs are sponsored meaning a transfer is not required. */\n isSponsored: boolean;\n\n /** Error message for the determination of sponsorship. */\n error: string | null;\n };\n};\n\n/** Data for a network supported by the Simulation API. */\ntype SimulationNetwork = {\n /** Subdomain of the API for the network. */\n network: string;\n\n /** Whether the network supports confirmation simulations. */\n confirmations: boolean;\n};\n\n/** Response from the simulation API containing supported networks. */\ntype SimulationNetworkResponse = {\n [chainIdDecimal: string]: SimulationNetwork;\n};\n\nlet requestIdCounter = 0;\n\n/**\n * Simulate transactions using the transaction simulation API.\n *\n * @param chainId - The chain ID to simulate transactions on.\n * @param request - The request to simulate transactions.\n * @returns The response from the simulation API.\n */\nexport async function simulateTransactions(\n chainId: Hex,\n request: SimulationRequest,\n): Promise<SimulationResponse> {\n let url = await getSimulationUrl(chainId);\n\n const { newUrl, authorization } =\n (await request.getSimulationConfig(url)) || {};\n if (newUrl) {\n url = newUrl;\n }\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Add optional authorization header, if provided.\n if (authorization) {\n headers.Authorization = authorization;\n }\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n id: String(requestId),\n jsonrpc: '2.0',\n method: RPC_METHOD,\n params: [finalRequest],\n }),\n });\n\n const responseJson = await response.json();\n\n log('Received response', responseJson);\n\n if (responseJson.error) {\n const { code, message } = responseJson.error;\n throw new SimulationError(message, code);\n }\n\n return responseJson?.result;\n}\n\n/**\n * Get the URL for the transaction simulation API.\n *\n * @param chainId - The chain ID to get the URL for.\n * @returns The URL for the transaction simulation API.\n */\nasync function getSimulationUrl(chainId: Hex): Promise<string> {\n const networkData = await getNetworkData();\n const chainIdDecimal = convertHexToDecimal(chainId);\n const network = networkData[chainIdDecimal];\n\n if (!network?.confirmations) {\n log('Chain is not supported', chainId);\n throw new SimulationChainNotSupportedError(chainId);\n }\n\n return getUrl(network.network);\n}\n\n/**\n * Retrieve the supported network data from the simulation API.\n *\n * @returns The network data response from the simulation API.\n */\nasync function getNetworkData(): Promise<SimulationNetworkResponse> {\n const url = `${getUrl('ethereum-mainnet')}${ENDPOINT_NETWORKS}`;\n const response = await fetch(url);\n return response.json();\n}\n\n/**\n * Generate the URL for the specified subdomain in the simulation API.\n *\n * @param subdomain - The subdomain to generate the URL for.\n * @returns The URL for the transaction simulation API.\n */\nfunction getUrl(subdomain: string): string {\n return BASE_URL.replace('{0}', subdomain);\n}\n\n/**\n * Finalize the simulation request.\n * Overrides the DelegationManager code to remove signature errors.\n * Temporary pending support in the simulation API.\n *\n * @param request - The simulation request to finalize.\n * @returns The finalized simulation request.\n */\nfunction finalizeRequest(request: SimulationRequest): SimulationRequest {\n const newRequest = cloneDeep(request);\n\n for (const transaction of newRequest.transactions) {\n const normalizedTo = transaction.to?.toLowerCase() as Hex;\n\n const isToDelegationManager =\n DELEGATION_MANAGER_ADDRESSES.includes(normalizedTo);\n\n if (!isToDelegationManager) {\n continue;\n }\n\n newRequest.overrides = newRequest.overrides ?? {};\n\n newRequest.overrides[normalizedTo] = {\n code: CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n };\n }\n\n return newRequest;\n}\n"]}
1
+ {"version":3,"file":"simulation-api.cjs","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,2CAAqD;AAErD,mCAAmC;AAEnC,gDAGsB;AACtB,0CAA8E;AAC9E,0CAA0C;AAG1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,UAAU,GAAG,6BAA6B,CAAC;AACjD,MAAM,QAAQ,GAAG,6CAA6C,CAAC;AAC/D,MAAM,iBAAiB,GAAG,UAAU,CAAC;AAgQrC,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACxC,OAAY,EACZ,OAA0B;IAE1B,IAAI,GAAG,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE1C,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAC7B,CAAC,MAAM,OAAO,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACjD,IAAI,MAAM,EAAE,CAAC;QACX,GAAG,GAAG,MAAM,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACnC,gBAAgB,IAAI,CAAC,CAAC;IAEtB,MAAM,YAAY,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAE9C,GAAG,CAAC,iBAAiB,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAErC,MAAM,OAAO,GAA2B;QACtC,cAAc,EAAE,kBAAkB;KACnC,CAAC;IAEF,kDAAkD;IAClD,IAAI,aAAa,EAAE,CAAC;QAClB,OAA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B,CAAC,GAAG,iBAAiB,EAAE,CAAC;IAChE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAS,MAAM,CAAC,SAAiB;IAC/B,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,eAAe,CAAC,OAA0B;IACjD,MAAM,UAAU,GAAG,IAAA,kBAAS,EAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,MAAM,WAAW,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;QAClD,MAAM,YAAY,GAAG,WAAW,CAAC,EAAE,EAAE,WAAW,EAAS,CAAC;QAE1D,MAAM,qBAAqB,GACzB,wCAA4B,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAEtD,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC3B,SAAS;QACX,CAAC;QAED,UAAU,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC;QAElD,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG;YACnC,IAAI,EAAE,uDAA2C;SAClD,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport {\n CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n DELEGATION_MANAGER_ADDRESSES,\n} from '../constants';\nimport { SimulationChainNotSupportedError, SimulationError } from '../errors';\nimport { projectLogger } from '../logger';\nimport type { GetSimulationConfig } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'simulation-api');\n\nconst RPC_METHOD = 'infura_simulateTransactions';\nconst BASE_URL = 'https://tx-sentinel-{0}.api.cx.metamask.io/';\nconst ENDPOINT_NETWORKS = 'networks';\n\n/** Single transaction to simulate in a simulation API request. */\nexport type SimulationRequestTransaction = {\n authorizationList?: {\n /** Address of a smart contract that contains the code to be set. */\n address: Hex;\n\n /** Address of the account being upgraded. */\n from: Hex;\n }[];\n\n /** Data to send with the transaction. */\n data?: Hex;\n\n /** Sender of the transaction. */\n from: Hex;\n\n /** Gas limit for the transaction. */\n gas?: Hex;\n\n /** Maximum fee per gas for the transaction. */\n maxFeePerGas?: Hex;\n\n /** Maximum priority fee per gas for the transaction. */\n maxPriorityFeePerGas?: Hex;\n\n /** Recipient of the transaction. */\n to?: Hex;\n\n /** Value to send with the transaction. */\n value?: Hex;\n};\n\n/** Request to the simulation API to simulate transactions. */\nexport type SimulationRequest = {\n blockOverrides?: {\n time?: Hex;\n };\n\n /**\n * Function to get the simulation configuration.\n */\n getSimulationConfig: GetSimulationConfig;\n\n /**\n * Overrides to the state of the blockchain, keyed by address.\n */\n overrides?: {\n [address: Hex]: {\n /** Override the code for an address. */\n code?: Hex;\n\n /** Overrides to the storage slots for an address. */\n stateDiff?: {\n [slot: Hex]: Hex;\n };\n };\n };\n\n /**\n * Whether to include available token fees.\n */\n suggestFees?: {\n /* Whether to estimate gas for the transaction being submitted via a delegation. */\n with7702?: boolean;\n\n /* Whether to include the gas fee of the token transfer. */\n withFeeTransfer?: boolean;\n\n /* Whether to include the native transfer if available. */\n withTransfer?: boolean;\n };\n\n /**\n * Transactions to be sequentially simulated.\n * State changes impact subsequent transactions in the list.\n */\n transactions: SimulationRequestTransaction[];\n\n /**\n * Whether to include call traces in the response.\n * Defaults to false.\n */\n withCallTrace?: boolean;\n\n /**\n * Whether to include the default block data in the simulation.\n * Defaults to false.\n */\n withDefaultBlockOverrides?: boolean;\n\n /**\n * Whether to use the gas fees in the simulation.\n * Defaults to false.\n */\n withGas?: boolean;\n\n /**\n * Whether to include event logs in the response.\n * Defaults to false.\n */\n withLogs?: boolean;\n};\n\n/** Raw event log emitted by a simulated transaction. */\nexport type SimulationResponseLog = {\n /** Address of the account that created the event. */\n address: Hex;\n\n /** Raw data in the event that is not indexed. */\n data: Hex;\n\n /** Raw indexed data from the event. */\n topics: Hex[];\n};\n\n/** Call trace of a single simulated transaction. */\nexport type SimulationResponseCallTrace = {\n /** Nested calls. */\n calls?: SimulationResponseCallTrace[] | null;\n\n /** Error message for the call, if any. */\n error?: string;\n\n /** Raw event logs created by the call. */\n logs?: SimulationResponseLog[] | null;\n};\n\n/**\n * Changes to the blockchain state.\n * Keyed by account address.\n */\nexport type SimulationResponseStateDiff = {\n [address: Hex]: {\n /** Native balance of the account. */\n balance?: Hex;\n\n /** Nonce of the account. */\n nonce?: Hex;\n\n /** Storage values per slot. */\n storage?: {\n [slot: Hex]: Hex;\n };\n };\n};\n\nexport type SimulationResponseTokenFee = {\n /** Token data independent of current transaction. */\n token: {\n /** Address of the token contract. */\n address: Hex;\n\n /** Decimals of the token. */\n decimals: number;\n\n /** Symbol of the token. */\n symbol: string;\n };\n\n /** Amount of tokens needed to pay for gas. */\n balanceNeededToken: Hex;\n\n /** Current token balance of sender. */\n currentBalanceToken: Hex;\n\n /** Account address that token should be transferred to. */\n feeRecipient: Hex;\n\n /** Conversation rate of 1 token to native WEI. */\n rateWei: Hex;\n\n /** Portion of `balanceNeededToken` that is the fee paid to MetaMask. */\n serviceFee?: Hex;\n\n /** Estimated gas limit required for fee transfer. */\n transferEstimate: Hex;\n};\n\n/** Response from the simulation API for a single transaction. */\nexport type SimulationResponseTransaction = {\n /** Hierarchy of call data including nested calls and logs. */\n callTrace?: SimulationResponseCallTrace;\n\n /** An error message indicating the transaction could not be simulated. */\n error?: string;\n\n /** Recommended gas fees for the transaction. */\n fees?: {\n /** Gas limit for the fee level. */\n gas: Hex;\n\n /** Maximum fee per gas for the fee level. */\n maxFeePerGas: Hex;\n\n /** Maximum priority fee per gas for the fee level. */\n maxPriorityFeePerGas: Hex;\n\n /** Token fee data for the fee level. */\n tokenFees: SimulationResponseTokenFee[];\n }[];\n\n /**\n * Estimated total gas cost of the transaction.\n * Included in the stateDiff if `withGas` is true.\n */\n gasCost?: number;\n\n /** Required `gasLimit` for the transaction. */\n gasLimit?: Hex;\n\n /** Total gas used by the transaction. */\n gasUsed?: Hex;\n\n /** Return value of the transaction, such as the balance if calling balanceOf. */\n return: Hex;\n\n /** Changes to the blockchain state. */\n stateDiff?: {\n /** Initial blockchain state before the transaction. */\n pre?: SimulationResponseStateDiff;\n\n /** Updated blockchain state after the transaction. */\n post?: SimulationResponseStateDiff;\n };\n};\n\n/** Response from the simulation API. */\nexport type SimulationResponse = {\n /** Simulation data for each transaction in the request. */\n transactions: SimulationResponseTransaction[];\n\n sponsorship: {\n /** Whether the gas costs are sponsored meaning a transfer is not required. */\n isSponsored: boolean;\n\n /** Error message for the determination of sponsorship. */\n error: string | null;\n };\n};\n\n/** Data for a network supported by the Simulation API. */\ntype SimulationNetwork = {\n /** Subdomain of the API for the network. */\n network: string;\n\n /** Whether the network supports confirmation simulations. */\n confirmations: boolean;\n};\n\n/** Response from the simulation API containing supported networks. */\ntype SimulationNetworkResponse = {\n [chainIdDecimal: string]: SimulationNetwork;\n};\n\nlet requestIdCounter = 0;\n\n/**\n * Simulate transactions using the transaction simulation API.\n *\n * @param chainId - The chain ID to simulate transactions on.\n * @param request - The request to simulate transactions.\n * @returns The response from the simulation API.\n */\nexport async function simulateTransactions(\n chainId: Hex,\n request: SimulationRequest,\n): Promise<SimulationResponse> {\n let url = await getSimulationUrl(chainId);\n\n const { newUrl, authorization } =\n (await request.getSimulationConfig(url)) || {};\n if (newUrl) {\n url = newUrl;\n }\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Add optional authorization header, if provided.\n if (authorization) {\n headers.Authorization = authorization;\n }\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n id: String(requestId),\n jsonrpc: '2.0',\n method: RPC_METHOD,\n params: [finalRequest],\n }),\n });\n\n const responseJson = await response.json();\n\n log('Received response', responseJson);\n\n if (responseJson.error) {\n const { code, message } = responseJson.error;\n throw new SimulationError(message, code);\n }\n\n return responseJson?.result;\n}\n\n/**\n * Get the URL for the transaction simulation API.\n *\n * @param chainId - The chain ID to get the URL for.\n * @returns The URL for the transaction simulation API.\n */\nasync function getSimulationUrl(chainId: Hex): Promise<string> {\n const networkData = await getNetworkData();\n const chainIdDecimal = convertHexToDecimal(chainId);\n const network = networkData[chainIdDecimal];\n\n if (!network?.confirmations) {\n log('Chain is not supported', chainId);\n throw new SimulationChainNotSupportedError(chainId);\n }\n\n return getUrl(network.network);\n}\n\n/**\n * Retrieve the supported network data from the simulation API.\n *\n * @returns The network data response from the simulation API.\n */\nasync function getNetworkData(): Promise<SimulationNetworkResponse> {\n const url = `${getUrl('ethereum-mainnet')}${ENDPOINT_NETWORKS}`;\n const response = await fetch(url);\n return response.json();\n}\n\n/**\n * Generate the URL for the specified subdomain in the simulation API.\n *\n * @param subdomain - The subdomain to generate the URL for.\n * @returns The URL for the transaction simulation API.\n */\nfunction getUrl(subdomain: string): string {\n return BASE_URL.replace('{0}', subdomain);\n}\n\n/**\n * Finalize the simulation request.\n * Overrides the DelegationManager code to remove signature errors.\n * Temporary pending support in the simulation API.\n *\n * @param request - The simulation request to finalize.\n * @returns The finalized simulation request.\n */\nfunction finalizeRequest(request: SimulationRequest): SimulationRequest {\n const newRequest = cloneDeep(request);\n\n for (const transaction of newRequest.transactions) {\n const normalizedTo = transaction.to?.toLowerCase() as Hex;\n\n const isToDelegationManager =\n DELEGATION_MANAGER_ADDRESSES.includes(normalizedTo);\n\n if (!isToDelegationManager) {\n continue;\n }\n\n newRequest.overrides = newRequest.overrides ?? {};\n\n newRequest.overrides[normalizedTo] = {\n code: CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n };\n }\n\n return newRequest;\n}\n"]}
@@ -91,9 +91,11 @@ export type SimulationResponseLog = {
91
91
  /** Call trace of a single simulated transaction. */
92
92
  export type SimulationResponseCallTrace = {
93
93
  /** Nested calls. */
94
- calls: SimulationResponseCallTrace[];
94
+ calls?: SimulationResponseCallTrace[] | null;
95
+ /** Error message for the call, if any. */
96
+ error?: string;
95
97
  /** Raw event logs created by the call. */
96
- logs: SimulationResponseLog[];
98
+ logs?: SimulationResponseLog[] | null;
97
99
  };
98
100
  /**
99
101
  * Changes to the blockchain state.
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.d.cts","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAQpD,mEAAmE;AACnE,MAAM,MAAM,4BAA4B,GAAG;IACzC,iBAAiB,CAAC,EAAE;QAClB,oEAAoE;QACpE,OAAO,EAAE,GAAG,CAAC;QAEb,6CAA6C;QAC7C,IAAI,EAAE,GAAG,CAAC;KACX,EAAE,CAAC;IAEJ,yCAAyC;IACzC,IAAI,CAAC,EAAE,GAAG,CAAC;IAEX,iCAAiC;IACjC,IAAI,EAAE,GAAG,CAAC;IAEV,qCAAqC;IACrC,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,+CAA+C;IAC/C,YAAY,CAAC,EAAE,GAAG,CAAC;IAEnB,wDAAwD;IACxD,oBAAoB,CAAC,EAAE,GAAG,CAAC;IAE3B,oCAAoC;IACpC,EAAE,CAAC,EAAE,GAAG,CAAC;IAET,0CAA0C;IAC1C,KAAK,CAAC,EAAE,GAAG,CAAC;CACb,CAAC;AAEF,8DAA8D;AAC9D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,cAAc,CAAC,EAAE;QACf,IAAI,CAAC,EAAE,GAAG,CAAC;KACZ,CAAC;IAEF;;OAEG;IACH,mBAAmB,EAAE,mBAAmB,CAAC;IAEzC;;OAEG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AS,EAAE,0BAA0B,EAAE,CAAC;KACzC,EAAE,CAAC;IAEJ;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,GAAG,CAAC;IAEf,yCAAyC;IACzC,OAAO,CAAC,EAAE,GAAG,CAAC;IAEd,iFAAiF;IACjF,MAAM,EAAE,GAAG,CAAC;IAEZ,uCAAuC;IACvC,SAAS,CAAC,EAAE;QACV,uDAAuD;QACvD,GAAG,CAAC,EAAE,2BAA2B,CAAC;QAElC,sDAAsD;QACtD,IAAI,CAAC,EAAE,2BAA2B,CAAC;KACpC,CAAC;CACH,CAAC;AAEF,wCAAwC;AACxC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,2DAA2D;IAC3D,YAAY,EAAE,6BAA6B,EAAE,CAAC;IAE9C,WAAW,EAAE;QACX,8EAA8E;QAC9E,WAAW,EAAE,OAAO,CAAC;QAErB,0DAA0D;QAC1D,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;KACtB,CAAC;CACH,CAAC;AAkBF;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,kBAAkB,CAAC,CA8C7B"}
@@ -91,9 +91,11 @@ export type SimulationResponseLog = {
91
91
  /** Call trace of a single simulated transaction. */
92
92
  export type SimulationResponseCallTrace = {
93
93
  /** Nested calls. */
94
- calls: SimulationResponseCallTrace[];
94
+ calls?: SimulationResponseCallTrace[] | null;
95
+ /** Error message for the call, if any. */
96
+ error?: string;
95
97
  /** Raw event logs created by the call. */
96
- logs: SimulationResponseLog[];
98
+ logs?: SimulationResponseLog[] | null;
97
99
  };
98
100
  /**
99
101
  * Changes to the blockchain state.
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.d.mts","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EA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CAAC,OAAO,EAAE,aAAa,EAAE,CAAC;QAC5B,GAAG,CAAC,wBAAwB,EAAE,OAAO,CAAC,CAAC;QACvC,MAAM,IAAI,gCAAgC,CAAC,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,cAAc;IAC3B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,kBAAkB,CAAC,GAAG,iBAAiB,EAAE,CAAC;IAChE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAS,MAAM,CAAC,SAAiB;IAC/B,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,eAAe,CAAC,OAA0B;IACjD,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,MAAM,WAAW,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;QAClD,MAAM,YAAY,GAAG,WAAW,CAAC,EAAE,EAAE,WAAW,EAAS,CAAC;QAE1D,MAAM,qBAAqB,GACzB,4BAA4B,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAEtD,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC3B,SAAS;QACX,CAAC;QAED,UAAU,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC;QAElD,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG;YACnC,IAAI,EAAE,2CAA2C;SAClD,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport {\n CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n DELEGATION_MANAGER_ADDRESSES,\n} from '../constants';\nimport { SimulationChainNotSupportedError, SimulationError } from '../errors';\nimport { projectLogger } from '../logger';\nimport type { GetSimulationConfig } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'simulation-api');\n\nconst RPC_METHOD = 'infura_simulateTransactions';\nconst BASE_URL = 'https://tx-sentinel-{0}.api.cx.metamask.io/';\nconst ENDPOINT_NETWORKS = 'networks';\n\n/** Single transaction to simulate in a simulation API request. */\nexport type SimulationRequestTransaction = {\n authorizationList?: {\n /** Address of a smart contract that contains the code to be set. */\n address: Hex;\n\n /** Address of the account being upgraded. */\n from: Hex;\n }[];\n\n /** Data to send with the transaction. */\n data?: Hex;\n\n /** Sender of the transaction. */\n from: Hex;\n\n /** Gas limit for the transaction. */\n gas?: Hex;\n\n /** Maximum fee per gas for the transaction. */\n maxFeePerGas?: Hex;\n\n /** Maximum priority fee per gas for the transaction. */\n maxPriorityFeePerGas?: Hex;\n\n /** Recipient of the transaction. */\n to?: Hex;\n\n /** Value to send with the transaction. */\n value?: Hex;\n};\n\n/** Request to the simulation API to simulate transactions. */\nexport type SimulationRequest = {\n blockOverrides?: {\n time?: Hex;\n };\n\n /**\n * Function to get the simulation configuration.\n */\n getSimulationConfig: GetSimulationConfig;\n\n /**\n * Overrides to the state of the blockchain, keyed by address.\n */\n overrides?: {\n [address: Hex]: {\n /** Override the code for an address. */\n code?: Hex;\n\n /** Overrides to the storage slots for an address. */\n stateDiff?: {\n [slot: Hex]: Hex;\n };\n };\n };\n\n /**\n * Whether to include available token fees.\n */\n suggestFees?: {\n /* Whether to estimate gas for the transaction being submitted via a delegation. */\n with7702?: boolean;\n\n /* Whether to include the gas fee of the token transfer. */\n withFeeTransfer?: boolean;\n\n /* Whether to include the native transfer if available. */\n withTransfer?: boolean;\n };\n\n /**\n * Transactions to be sequentially simulated.\n * State changes impact subsequent transactions in the list.\n */\n transactions: SimulationRequestTransaction[];\n\n /**\n * Whether to include call traces in the response.\n * Defaults to false.\n */\n withCallTrace?: boolean;\n\n /**\n * Whether to include the default block data in the simulation.\n * Defaults to false.\n */\n withDefaultBlockOverrides?: boolean;\n\n /**\n * Whether to use the gas fees in the simulation.\n * Defaults to false.\n */\n withGas?: boolean;\n\n /**\n * Whether to include event logs in the response.\n * Defaults to false.\n */\n withLogs?: boolean;\n};\n\n/** Raw event log emitted by a simulated transaction. */\nexport type SimulationResponseLog = {\n /** Address of the account that created the event. */\n address: Hex;\n\n /** Raw data in the event that is not indexed. */\n data: Hex;\n\n /** Raw indexed data from the event. */\n topics: Hex[];\n};\n\n/** Call trace of a single simulated transaction. */\nexport type SimulationResponseCallTrace = {\n /** Nested calls. */\n calls: SimulationResponseCallTrace[];\n\n /** Raw event logs created by the call. */\n logs: SimulationResponseLog[];\n};\n\n/**\n * Changes to the blockchain state.\n * Keyed by account address.\n */\nexport type SimulationResponseStateDiff = {\n [address: Hex]: {\n /** Native balance of the account. */\n balance?: Hex;\n\n /** Nonce of the account. */\n nonce?: Hex;\n\n /** Storage values per slot. */\n storage?: {\n [slot: Hex]: Hex;\n };\n };\n};\n\nexport type SimulationResponseTokenFee = {\n /** Token data independent of current transaction. */\n token: {\n /** Address of the token contract. */\n address: Hex;\n\n /** Decimals of the token. */\n decimals: number;\n\n /** Symbol of the token. */\n symbol: string;\n };\n\n /** Amount of tokens needed to pay for gas. */\n balanceNeededToken: Hex;\n\n /** Current token balance of sender. */\n currentBalanceToken: Hex;\n\n /** Account address that token should be transferred to. */\n feeRecipient: Hex;\n\n /** Conversation rate of 1 token to native WEI. */\n rateWei: Hex;\n\n /** Portion of `balanceNeededToken` that is the fee paid to MetaMask. */\n serviceFee?: Hex;\n\n /** Estimated gas limit required for fee transfer. */\n transferEstimate: Hex;\n};\n\n/** Response from the simulation API for a single transaction. */\nexport type SimulationResponseTransaction = {\n /** Hierarchy of call data including nested calls and logs. */\n callTrace?: SimulationResponseCallTrace;\n\n /** An error message indicating the transaction could not be simulated. */\n error?: string;\n\n /** Recommended gas fees for the transaction. */\n fees?: {\n /** Gas limit for the fee level. */\n gas: Hex;\n\n /** Maximum fee per gas for the fee level. */\n maxFeePerGas: Hex;\n\n /** Maximum priority fee per gas for the fee level. */\n maxPriorityFeePerGas: Hex;\n\n /** Token fee data for the fee level. */\n tokenFees: SimulationResponseTokenFee[];\n }[];\n\n /**\n * Estimated total gas cost of the transaction.\n * Included in the stateDiff if `withGas` is true.\n */\n gasCost?: number;\n\n /** Required `gasLimit` for the transaction. */\n gasLimit?: Hex;\n\n /** Total gas used by the transaction. */\n gasUsed?: Hex;\n\n /** Return value of the transaction, such as the balance if calling balanceOf. */\n return: Hex;\n\n /** Changes to the blockchain state. */\n stateDiff?: {\n /** Initial blockchain state before the transaction. */\n pre?: SimulationResponseStateDiff;\n\n /** Updated blockchain state after the transaction. */\n post?: SimulationResponseStateDiff;\n };\n};\n\n/** Response from the simulation API. */\nexport type SimulationResponse = {\n /** Simulation data for each transaction in the request. */\n transactions: SimulationResponseTransaction[];\n\n sponsorship: {\n /** Whether the gas costs are sponsored meaning a transfer is not required. */\n isSponsored: boolean;\n\n /** Error message for the determination of sponsorship. */\n error: string | null;\n };\n};\n\n/** Data for a network supported by the Simulation API. */\ntype SimulationNetwork = {\n /** Subdomain of the API for the network. */\n network: string;\n\n /** Whether the network supports confirmation simulations. */\n confirmations: boolean;\n};\n\n/** Response from the simulation API containing supported networks. */\ntype SimulationNetworkResponse = {\n [chainIdDecimal: string]: SimulationNetwork;\n};\n\nlet requestIdCounter = 0;\n\n/**\n * Simulate transactions using the transaction simulation API.\n *\n * @param chainId - The chain ID to simulate transactions on.\n * @param request - The request to simulate transactions.\n * @returns The response from the simulation API.\n */\nexport async function simulateTransactions(\n chainId: Hex,\n request: SimulationRequest,\n): Promise<SimulationResponse> {\n let url = await getSimulationUrl(chainId);\n\n const { newUrl, authorization } =\n (await request.getSimulationConfig(url)) || {};\n if (newUrl) {\n url = newUrl;\n }\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Add optional authorization header, if provided.\n if (authorization) {\n headers.Authorization = authorization;\n }\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n id: String(requestId),\n jsonrpc: '2.0',\n method: RPC_METHOD,\n params: [finalRequest],\n }),\n });\n\n const responseJson = await response.json();\n\n log('Received response', responseJson);\n\n if (responseJson.error) {\n const { code, message } = responseJson.error;\n throw new SimulationError(message, code);\n }\n\n return responseJson?.result;\n}\n\n/**\n * Get the URL for the transaction simulation API.\n *\n * @param chainId - The chain ID to get the URL for.\n * @returns The URL for the transaction simulation API.\n */\nasync function getSimulationUrl(chainId: Hex): Promise<string> {\n const networkData = await getNetworkData();\n const chainIdDecimal = convertHexToDecimal(chainId);\n const network = networkData[chainIdDecimal];\n\n if (!network?.confirmations) {\n log('Chain is not supported', chainId);\n throw new SimulationChainNotSupportedError(chainId);\n }\n\n return getUrl(network.network);\n}\n\n/**\n * Retrieve the supported network data from the simulation API.\n *\n * @returns The network data response from the simulation API.\n */\nasync function getNetworkData(): Promise<SimulationNetworkResponse> {\n const url = `${getUrl('ethereum-mainnet')}${ENDPOINT_NETWORKS}`;\n const response = await fetch(url);\n return response.json();\n}\n\n/**\n * Generate the URL for the specified subdomain in the simulation API.\n *\n * @param subdomain - The subdomain to generate the URL for.\n * @returns The URL for the transaction simulation API.\n */\nfunction getUrl(subdomain: string): string {\n return BASE_URL.replace('{0}', subdomain);\n}\n\n/**\n * Finalize the simulation request.\n * Overrides the DelegationManager code to remove signature errors.\n * Temporary pending support in the simulation API.\n *\n * @param request - The simulation request to finalize.\n * @returns The finalized simulation request.\n */\nfunction finalizeRequest(request: SimulationRequest): SimulationRequest {\n const newRequest = cloneDeep(request);\n\n for (const transaction of newRequest.transactions) {\n const normalizedTo = transaction.to?.toLowerCase() as Hex;\n\n const isToDelegationManager =\n DELEGATION_MANAGER_ADDRESSES.includes(normalizedTo);\n\n if (!isToDelegationManager) {\n continue;\n }\n\n newRequest.overrides = newRequest.overrides ?? {};\n\n newRequest.overrides[normalizedTo] = {\n code: CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n };\n }\n\n return newRequest;\n}\n"]}
1
+ {"version":3,"file":"simulation-api.mjs","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AACjE,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;;;AAIrD,OAAO,EACL,2CAA2C,EAC3C,4BAA4B,EAC7B,yBAAqB;AACtB,OAAO,EAAE,gCAAgC,EAAE,eAAe,EAAE,sBAAkB;AAC9E,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,UAAU,GAAG,6BAA6B,CAAC;AACjD,MAAM,QAAQ,GAAG,6CAA6C,CAAC;AAC/D,MAAM,iBAAiB,GAAG,UAAU,CAAC;AAgQrC,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAY,EACZ,OAA0B;IAE1B,IAAI,GAAG,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE1C,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAC7B,CAAC,MAAM,OAAO,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACjD,IAAI,MAAM,EAAE,CAAC;QACX,GAAG,GAAG,MAAM,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACnC,gBAAgB,IAAI,CAAC,CAAC;IAEtB,MAAM,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AAI,gCAAgC,CAAC,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,cAAc;IAC3B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,kBAAkB,CAAC,GAAG,iBAAiB,EAAE,CAAC;IAChE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAS,MAAM,CAAC,SAAiB;IAC/B,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,eAAe,CAAC,OAA0B;IACjD,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,MAAM,WAAW,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;QAClD,MAAM,YAAY,GAAG,WAAW,CAAC,EAAE,EAAE,WAAW,EAAS,CAAC;QAE1D,MAAM,qBAAqB,GACzB,4BAA4B,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAEtD,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC3B,SAAS;QACX,CAAC;QAED,UAAU,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC;QAElD,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG;YACnC,IAAI,EAAE,2CAA2C;SAClD,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport {\n CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n DELEGATION_MANAGER_ADDRESSES,\n} from '../constants';\nimport { SimulationChainNotSupportedError, SimulationError } from '../errors';\nimport { projectLogger } from '../logger';\nimport type { GetSimulationConfig } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'simulation-api');\n\nconst RPC_METHOD = 'infura_simulateTransactions';\nconst BASE_URL = 'https://tx-sentinel-{0}.api.cx.metamask.io/';\nconst ENDPOINT_NETWORKS = 'networks';\n\n/** Single transaction to simulate in a simulation API request. */\nexport type SimulationRequestTransaction = {\n authorizationList?: {\n /** Address of a smart contract that contains the code to be set. */\n address: Hex;\n\n /** Address of the account being upgraded. */\n from: Hex;\n }[];\n\n /** Data to send with the transaction. */\n data?: Hex;\n\n /** Sender of the transaction. */\n from: Hex;\n\n /** Gas limit for the transaction. */\n gas?: Hex;\n\n /** Maximum fee per gas for the transaction. */\n maxFeePerGas?: Hex;\n\n /** Maximum priority fee per gas for the transaction. */\n maxPriorityFeePerGas?: Hex;\n\n /** Recipient of the transaction. */\n to?: Hex;\n\n /** Value to send with the transaction. */\n value?: Hex;\n};\n\n/** Request to the simulation API to simulate transactions. */\nexport type SimulationRequest = {\n blockOverrides?: {\n time?: Hex;\n };\n\n /**\n * Function to get the simulation configuration.\n */\n getSimulationConfig: GetSimulationConfig;\n\n /**\n * Overrides to the state of the blockchain, keyed by address.\n */\n overrides?: {\n [address: Hex]: {\n /** Override the code for an address. */\n code?: Hex;\n\n /** Overrides to the storage slots for an address. */\n stateDiff?: {\n [slot: Hex]: Hex;\n };\n };\n };\n\n /**\n * Whether to include available token fees.\n */\n suggestFees?: {\n /* Whether to estimate gas for the transaction being submitted via a delegation. */\n with7702?: boolean;\n\n /* Whether to include the gas fee of the token transfer. */\n withFeeTransfer?: boolean;\n\n /* Whether to include the native transfer if available. */\n withTransfer?: boolean;\n };\n\n /**\n * Transactions to be sequentially simulated.\n * State changes impact subsequent transactions in the list.\n */\n transactions: SimulationRequestTransaction[];\n\n /**\n * Whether to include call traces in the response.\n * Defaults to false.\n */\n withCallTrace?: boolean;\n\n /**\n * Whether to include the default block data in the simulation.\n * Defaults to false.\n */\n withDefaultBlockOverrides?: boolean;\n\n /**\n * Whether to use the gas fees in the simulation.\n * Defaults to false.\n */\n withGas?: boolean;\n\n /**\n * Whether to include event logs in the response.\n * Defaults to false.\n */\n withLogs?: boolean;\n};\n\n/** Raw event log emitted by a simulated transaction. */\nexport type SimulationResponseLog = {\n /** Address of the account that created the event. */\n address: Hex;\n\n /** Raw data in the event that is not indexed. */\n data: Hex;\n\n /** Raw indexed data from the event. */\n topics: Hex[];\n};\n\n/** Call trace of a single simulated transaction. */\nexport type SimulationResponseCallTrace = {\n /** Nested calls. */\n calls?: SimulationResponseCallTrace[] | null;\n\n /** Error message for the call, if any. */\n error?: string;\n\n /** Raw event logs created by the call. */\n logs?: SimulationResponseLog[] | null;\n};\n\n/**\n * Changes to the blockchain state.\n * Keyed by account address.\n */\nexport type SimulationResponseStateDiff = {\n [address: Hex]: {\n /** Native balance of the account. */\n balance?: Hex;\n\n /** Nonce of the account. */\n nonce?: Hex;\n\n /** Storage values per slot. */\n storage?: {\n [slot: Hex]: Hex;\n };\n };\n};\n\nexport type SimulationResponseTokenFee = {\n /** Token data independent of current transaction. */\n token: {\n /** Address of the token contract. */\n address: Hex;\n\n /** Decimals of the token. */\n decimals: number;\n\n /** Symbol of the token. */\n symbol: string;\n };\n\n /** Amount of tokens needed to pay for gas. */\n balanceNeededToken: Hex;\n\n /** Current token balance of sender. */\n currentBalanceToken: Hex;\n\n /** Account address that token should be transferred to. */\n feeRecipient: Hex;\n\n /** Conversation rate of 1 token to native WEI. */\n rateWei: Hex;\n\n /** Portion of `balanceNeededToken` that is the fee paid to MetaMask. */\n serviceFee?: Hex;\n\n /** Estimated gas limit required for fee transfer. */\n transferEstimate: Hex;\n};\n\n/** Response from the simulation API for a single transaction. */\nexport type SimulationResponseTransaction = {\n /** Hierarchy of call data including nested calls and logs. */\n callTrace?: SimulationResponseCallTrace;\n\n /** An error message indicating the transaction could not be simulated. */\n error?: string;\n\n /** Recommended gas fees for the transaction. */\n fees?: {\n /** Gas limit for the fee level. */\n gas: Hex;\n\n /** Maximum fee per gas for the fee level. */\n maxFeePerGas: Hex;\n\n /** Maximum priority fee per gas for the fee level. */\n maxPriorityFeePerGas: Hex;\n\n /** Token fee data for the fee level. */\n tokenFees: SimulationResponseTokenFee[];\n }[];\n\n /**\n * Estimated total gas cost of the transaction.\n * Included in the stateDiff if `withGas` is true.\n */\n gasCost?: number;\n\n /** Required `gasLimit` for the transaction. */\n gasLimit?: Hex;\n\n /** Total gas used by the transaction. */\n gasUsed?: Hex;\n\n /** Return value of the transaction, such as the balance if calling balanceOf. */\n return: Hex;\n\n /** Changes to the blockchain state. */\n stateDiff?: {\n /** Initial blockchain state before the transaction. */\n pre?: SimulationResponseStateDiff;\n\n /** Updated blockchain state after the transaction. */\n post?: SimulationResponseStateDiff;\n };\n};\n\n/** Response from the simulation API. */\nexport type SimulationResponse = {\n /** Simulation data for each transaction in the request. */\n transactions: SimulationResponseTransaction[];\n\n sponsorship: {\n /** Whether the gas costs are sponsored meaning a transfer is not required. */\n isSponsored: boolean;\n\n /** Error message for the determination of sponsorship. */\n error: string | null;\n };\n};\n\n/** Data for a network supported by the Simulation API. */\ntype SimulationNetwork = {\n /** Subdomain of the API for the network. */\n network: string;\n\n /** Whether the network supports confirmation simulations. */\n confirmations: boolean;\n};\n\n/** Response from the simulation API containing supported networks. */\ntype SimulationNetworkResponse = {\n [chainIdDecimal: string]: SimulationNetwork;\n};\n\nlet requestIdCounter = 0;\n\n/**\n * Simulate transactions using the transaction simulation API.\n *\n * @param chainId - The chain ID to simulate transactions on.\n * @param request - The request to simulate transactions.\n * @returns The response from the simulation API.\n */\nexport async function simulateTransactions(\n chainId: Hex,\n request: SimulationRequest,\n): Promise<SimulationResponse> {\n let url = await getSimulationUrl(chainId);\n\n const { newUrl, authorization } =\n (await request.getSimulationConfig(url)) || {};\n if (newUrl) {\n url = newUrl;\n }\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n // Add optional authorization header, if provided.\n if (authorization) {\n headers.Authorization = authorization;\n }\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n id: String(requestId),\n jsonrpc: '2.0',\n method: RPC_METHOD,\n params: [finalRequest],\n }),\n });\n\n const responseJson = await response.json();\n\n log('Received response', responseJson);\n\n if (responseJson.error) {\n const { code, message } = responseJson.error;\n throw new SimulationError(message, code);\n }\n\n return responseJson?.result;\n}\n\n/**\n * Get the URL for the transaction simulation API.\n *\n * @param chainId - The chain ID to get the URL for.\n * @returns The URL for the transaction simulation API.\n */\nasync function getSimulationUrl(chainId: Hex): Promise<string> {\n const networkData = await getNetworkData();\n const chainIdDecimal = convertHexToDecimal(chainId);\n const network = networkData[chainIdDecimal];\n\n if (!network?.confirmations) {\n log('Chain is not supported', chainId);\n throw new SimulationChainNotSupportedError(chainId);\n }\n\n return getUrl(network.network);\n}\n\n/**\n * Retrieve the supported network data from the simulation API.\n *\n * @returns The network data response from the simulation API.\n */\nasync function getNetworkData(): Promise<SimulationNetworkResponse> {\n const url = `${getUrl('ethereum-mainnet')}${ENDPOINT_NETWORKS}`;\n const response = await fetch(url);\n return response.json();\n}\n\n/**\n * Generate the URL for the specified subdomain in the simulation API.\n *\n * @param subdomain - The subdomain to generate the URL for.\n * @returns The URL for the transaction simulation API.\n */\nfunction getUrl(subdomain: string): string {\n return BASE_URL.replace('{0}', subdomain);\n}\n\n/**\n * Finalize the simulation request.\n * Overrides the DelegationManager code to remove signature errors.\n * Temporary pending support in the simulation API.\n *\n * @param request - The simulation request to finalize.\n * @returns The finalized simulation request.\n */\nfunction finalizeRequest(request: SimulationRequest): SimulationRequest {\n const newRequest = cloneDeep(request);\n\n for (const transaction of newRequest.transactions) {\n const normalizedTo = transaction.to?.toLowerCase() as Hex;\n\n const isToDelegationManager =\n DELEGATION_MANAGER_ADDRESSES.includes(normalizedTo);\n\n if (!isToDelegationManager) {\n continue;\n }\n\n newRequest.overrides = newRequest.overrides ?? {};\n\n newRequest.overrides[normalizedTo] = {\n code: CODE_DELEGATION_MANAGER_NO_SIGNATURE_ERRORS,\n };\n }\n\n return newRequest;\n}\n"]}
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_isUpdating, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
16
+ var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_isUpdating, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_useWebsockets, _IncomingTransactionHelper_connectionStateChangedHandler, _IncomingTransactionHelper_transactionUpdatedHandler, _IncomingTransactionHelper_selectedAccountChangedHandler, _IncomingTransactionHelper_onConnectionStateChanged, _IncomingTransactionHelper_startTransactionHistoryRetrieval, _IncomingTransactionHelper_stopTransactionHistoryRetrieval, _IncomingTransactionHelper_onTransactionUpdated, _IncomingTransactionHelper_onSelectedAccountChanged, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.IncomingTransactionHelper = void 0;
19
19
  // This package purposefully relies on Node's EventEmitter module.
@@ -22,6 +22,11 @@ const events_1 = __importDefault(require("events"));
22
22
  const logger_1 = require("../logger.cjs");
23
23
  const feature_flags_1 = require("../utils/feature-flags.cjs");
24
24
  const TAG_POLLING = 'automatic-polling';
25
+ var WebSocketState;
26
+ (function (WebSocketState) {
27
+ WebSocketState["CONNECTED"] = "connected";
28
+ WebSocketState["DISCONNECTED"] = "disconnected";
29
+ })(WebSocketState || (WebSocketState = {}));
25
30
  class IncomingTransactionHelper {
26
31
  constructor({ client, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, remoteTransactionSource, trimTransactions, updateTransactions, }) {
27
32
  _IncomingTransactionHelper_instances.add(this);
@@ -37,6 +42,16 @@ class IncomingTransactionHelper {
37
42
  _IncomingTransactionHelper_timeoutId.set(this, void 0);
38
43
  _IncomingTransactionHelper_trimTransactions.set(this, void 0);
39
44
  _IncomingTransactionHelper_updateTransactions.set(this, void 0);
45
+ _IncomingTransactionHelper_useWebsockets.set(this, void 0);
46
+ _IncomingTransactionHelper_connectionStateChangedHandler.set(this, (connectionInfo) => {
47
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onConnectionStateChanged).call(this, connectionInfo);
48
+ });
49
+ _IncomingTransactionHelper_transactionUpdatedHandler.set(this, (transaction) => {
50
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onTransactionUpdated).call(this, transaction);
51
+ });
52
+ _IncomingTransactionHelper_selectedAccountChangedHandler.set(this, () => {
53
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onSelectedAccountChanged).call(this);
54
+ });
40
55
  this.hub = new events_1.default();
41
56
  __classPrivateFieldSet(this, _IncomingTransactionHelper_client, client, "f");
42
57
  __classPrivateFieldSet(this, _IncomingTransactionHelper_getCurrentAccount, getCurrentAccount, "f");
@@ -49,9 +64,13 @@ class IncomingTransactionHelper {
49
64
  __classPrivateFieldSet(this, _IncomingTransactionHelper_remoteTransactionSource, remoteTransactionSource, "f");
50
65
  __classPrivateFieldSet(this, _IncomingTransactionHelper_trimTransactions, trimTransactions, "f");
51
66
  __classPrivateFieldSet(this, _IncomingTransactionHelper_updateTransactions, updateTransactions, "f");
67
+ __classPrivateFieldSet(this, _IncomingTransactionHelper_useWebsockets, (0, feature_flags_1.isIncomingTransactionsUseWebsocketsEnabled)(messenger), "f");
68
+ if (__classPrivateFieldGet(this, _IncomingTransactionHelper_useWebsockets, "f")) {
69
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('BackendWebSocketService:connectionStateChanged', __classPrivateFieldGet(this, _IncomingTransactionHelper_connectionStateChangedHandler, "f"));
70
+ }
52
71
  }
53
72
  start() {
54
- if (__classPrivateFieldGet(this, _IncomingTransactionHelper_isRunning, "f")) {
73
+ if (__classPrivateFieldGet(this, _IncomingTransactionHelper_isRunning, "f") || __classPrivateFieldGet(this, _IncomingTransactionHelper_useWebsockets, "f")) {
55
74
  return;
56
75
  }
57
76
  if (!__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_canStart).call(this)) {
@@ -133,7 +152,43 @@ class IncomingTransactionHelper {
133
152
  }
134
153
  }
135
154
  exports.IncomingTransactionHelper = IncomingTransactionHelper;
136
- _IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_isUpdating = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
155
+ _IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_isUpdating = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_useWebsockets = new WeakMap(), _IncomingTransactionHelper_connectionStateChangedHandler = new WeakMap(), _IncomingTransactionHelper_transactionUpdatedHandler = new WeakMap(), _IncomingTransactionHelper_selectedAccountChangedHandler = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onConnectionStateChanged = function _IncomingTransactionHelper_onConnectionStateChanged(connectionInfo) {
156
+ if (connectionInfo.state === WebSocketState.CONNECTED) {
157
+ (0, logger_1.incomingTransactionsLogger)('WebSocket connected, starting enhanced mode');
158
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_startTransactionHistoryRetrieval).call(this);
159
+ }
160
+ else if (connectionInfo.state === WebSocketState.DISCONNECTED) {
161
+ (0, logger_1.incomingTransactionsLogger)('WebSocket disconnected, stopping enhanced mode');
162
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_stopTransactionHistoryRetrieval).call(this);
163
+ }
164
+ }, _IncomingTransactionHelper_startTransactionHistoryRetrieval = function _IncomingTransactionHelper_startTransactionHistoryRetrieval() {
165
+ if (!__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_canStart).call(this)) {
166
+ return;
167
+ }
168
+ (0, logger_1.incomingTransactionsLogger)('Started transaction history retrieval (event-driven)');
169
+ this.update().catch((error) => {
170
+ (0, logger_1.incomingTransactionsLogger)('Initial update in transaction history retrieval failed', error);
171
+ });
172
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('AccountActivityService:transactionUpdated', __classPrivateFieldGet(this, _IncomingTransactionHelper_transactionUpdatedHandler, "f"));
173
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('AccountsController:selectedAccountChange', __classPrivateFieldGet(this, _IncomingTransactionHelper_selectedAccountChangedHandler, "f"));
174
+ }, _IncomingTransactionHelper_stopTransactionHistoryRetrieval = function _IncomingTransactionHelper_stopTransactionHistoryRetrieval() {
175
+ (0, logger_1.incomingTransactionsLogger)('Stopped transaction history retrieval');
176
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").unsubscribe('AccountActivityService:transactionUpdated', __classPrivateFieldGet(this, _IncomingTransactionHelper_transactionUpdatedHandler, "f"));
177
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").unsubscribe('AccountsController:selectedAccountChange', __classPrivateFieldGet(this, _IncomingTransactionHelper_selectedAccountChangedHandler, "f"));
178
+ }, _IncomingTransactionHelper_onTransactionUpdated = function _IncomingTransactionHelper_onTransactionUpdated(transaction) {
179
+ (0, logger_1.incomingTransactionsLogger)('Received relevant transaction update, triggering update', {
180
+ txId: transaction.id,
181
+ chain: transaction.chain,
182
+ });
183
+ this.update().catch((error) => {
184
+ (0, logger_1.incomingTransactionsLogger)('Update after transaction event failed', error);
185
+ });
186
+ }, _IncomingTransactionHelper_onSelectedAccountChanged = function _IncomingTransactionHelper_onSelectedAccountChanged() {
187
+ (0, logger_1.incomingTransactionsLogger)('Selected account changed, triggering update');
188
+ this.update().catch((error) => {
189
+ (0, logger_1.incomingTransactionsLogger)('Update after account change failed', error);
190
+ });
191
+ }, _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
137
192
  __classPrivateFieldSet(this, _IncomingTransactionHelper_isUpdating, true, "f");
138
193
  try {
139
194
  await this.update({ isInterval: true });
@@ -1 +1 @@
1
- {"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAGlC,0CAA8D;AAE9D,8DAAgF;AAmBhF,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,MAAa,yBAAyB;IA+BpC,YAAY,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GAanB;;QAlDQ,oDAAiB;QAEjB,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEpB,wDAAqB;QAEZ,uDAA2C;QAE3C,qEAAkD;QAE3D,uDAAqB;QAEZ,8DAEc;QAEd,gEAA8B;QAyBrC,IAAI,CAAC,GAAG,GAAG,IAAI,gBAAY,EAAE,CAAC;QAE9B,uBAAA,IAAI,qCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,gDAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,mDAAyB,oBAAoB,MAAA,CAAC;QA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;IAE/B,MAAM,IAAI,GAAG,EAAE,CAAC;IAEhB,IAAI,uBAAA,IAAI,yCAAQ,EAAE,CAAC;QACjB,IAAI,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAQ,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,WAAW,EAAE,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;IAC5B,CAAC;SAAM,IAAI,UAAU,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n /** Name of the client to include in requests. */\n client?: string;\n\n /** Whether to retrieve incoming token transfers. Defaults to false. */\n includeTokenTransfers?: boolean;\n\n /** Callback to determine if incoming transaction polling is enabled. */\n isEnabled?: () => boolean;\n\n /** @deprecated No longer used. */\n queryEntireHistory?: boolean;\n\n /** Whether to retrieve outgoing transactions. Defaults to false. */\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n #isUpdating: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n client,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n remoteTransactionSource,\n trimTransactions,\n updateTransactions,\n }: {\n client?: string;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? ((): boolean => true);\n this.#isRunning = false;\n this.#isUpdating = false;\n this.#messenger = messenger;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateTransactions = updateTransactions;\n }\n\n start(): void {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Started polling', { interval });\n\n this.#isRunning = true;\n\n if (this.#isUpdating) {\n return;\n }\n\n this.#onInterval().catch((error) => {\n log('Initial polling failed', error);\n });\n }\n\n stop(): void {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval(): Promise<void> {\n this.#isUpdating = true;\n\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n this.#isUpdating = false;\n\n if (this.#isRunning) {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n includeTokenTransfers,\n tags: finalTags,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]): void {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
1
+ {"version":3,"file":"IncomingTransactionHelper.cjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAMA,kEAAkE;AAClE,sDAAsD;AACtD,oDAAkC;AAGlC,0CAA8D;AAE9D,8DAGgC;AAmBhC,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,IAAK,cAGJ;AAHD,WAAK,cAAc;IACjB,yCAAuB,CAAA;IACvB,+CAA6B,CAAA;AAC/B,CAAC,EAHI,cAAc,KAAd,cAAc,QAGlB;AAED,MAAa,yBAAyB;IAiDpC,YAAY,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GAanB;;QApEQ,oDAAiB;QAEjB,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEpB,wDAAqB;QAEZ,uDAA2C;QAE3C,qEAAkD;QAE3D,uDAAqB;QAEZ,8DAEc;QAEd,gEAA8B;QAE9B,2DAAwB;QAExB,mEAAiC,CACxC,cAAuC,EACjC,EAAE;YACR,uBAAA,IAAI,iGAA0B,MA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ACjB,IAAI,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAQ,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,WAAW,EAAE,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;IAC5B,CAAC;SAAM,IAAI,UAAU,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type {\n Transaction as AccountActivityTransaction,\n WebSocketConnectionInfo,\n} from '@metamask/core-backend';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport {\n getIncomingTransactionsPollingInterval,\n isIncomingTransactionsUseWebsocketsEnabled,\n} from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n /** Name of the client to include in requests. */\n client?: string;\n\n /** Whether to retrieve incoming token transfers. Defaults to false. */\n includeTokenTransfers?: boolean;\n\n /** Callback to determine if incoming transaction polling is enabled. */\n isEnabled?: () => boolean;\n\n /** @deprecated No longer used. */\n queryEntireHistory?: boolean;\n\n /** Whether to retrieve outgoing transactions. Defaults to false. */\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nenum WebSocketState {\n CONNECTED = 'connected',\n DISCONNECTED = 'disconnected',\n}\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n #isUpdating: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateTransactions?: boolean;\n\n readonly #useWebsockets: boolean;\n\n readonly #connectionStateChangedHandler = (\n connectionInfo: WebSocketConnectionInfo,\n ): void => {\n this.#onConnectionStateChanged(connectionInfo);\n };\n\n readonly #transactionUpdatedHandler = (\n transaction: AccountActivityTransaction,\n ): void => {\n this.#onTransactionUpdated(transaction);\n };\n\n readonly #selectedAccountChangedHandler = (): void => {\n this.#onSelectedAccountChanged();\n };\n\n constructor({\n client,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n remoteTransactionSource,\n trimTransactions,\n updateTransactions,\n }: {\n client?: string;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? ((): boolean => true);\n this.#isRunning = false;\n this.#isUpdating = false;\n this.#messenger = messenger;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateTransactions = updateTransactions;\n this.#useWebsockets = isIncomingTransactionsUseWebsocketsEnabled(messenger);\n\n if (this.#useWebsockets) {\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n this.#connectionStateChangedHandler,\n );\n }\n }\n\n start(): void {\n if (this.#isRunning || this.#useWebsockets) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Started polling', { interval });\n\n this.#isRunning = true;\n\n if (this.#isUpdating) {\n return;\n }\n\n this.#onInterval().catch((error) => {\n log('Initial polling failed', error);\n });\n }\n\n stop(): void {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n #onConnectionStateChanged(connectionInfo: WebSocketConnectionInfo): void {\n if (connectionInfo.state === WebSocketState.CONNECTED) {\n log('WebSocket connected, starting enhanced mode');\n this.#startTransactionHistoryRetrieval();\n } else if (connectionInfo.state === WebSocketState.DISCONNECTED) {\n log('WebSocket disconnected, stopping enhanced mode');\n this.#stopTransactionHistoryRetrieval();\n }\n }\n\n #startTransactionHistoryRetrieval(): void {\n if (!this.#canStart()) {\n return;\n }\n\n log('Started transaction history retrieval (event-driven)');\n\n this.update().catch((error) => {\n log('Initial update in transaction history retrieval failed', error);\n });\n\n this.#messenger.subscribe(\n 'AccountActivityService:transactionUpdated',\n this.#transactionUpdatedHandler,\n );\n\n this.#messenger.subscribe(\n 'AccountsController:selectedAccountChange',\n this.#selectedAccountChangedHandler,\n );\n }\n\n #stopTransactionHistoryRetrieval(): void {\n log('Stopped transaction history retrieval');\n\n this.#messenger.unsubscribe(\n 'AccountActivityService:transactionUpdated',\n this.#transactionUpdatedHandler,\n );\n\n this.#messenger.unsubscribe(\n 'AccountsController:selectedAccountChange',\n this.#selectedAccountChangedHandler,\n );\n }\n\n #onTransactionUpdated(transaction: AccountActivityTransaction): void {\n log('Received relevant transaction update, triggering update', {\n txId: transaction.id,\n chain: transaction.chain,\n });\n\n this.update().catch((error) => {\n log('Update after transaction event failed', error);\n });\n }\n\n #onSelectedAccountChanged(): void {\n log('Selected account changed, triggering update');\n\n this.update().catch((error) => {\n log('Update after account change failed', error);\n });\n }\n\n async #onInterval(): Promise<void> {\n this.#isUpdating = true;\n\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n this.#isUpdating = false;\n\n if (this.#isRunning) {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n includeTokenTransfers,\n tags: finalTags,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]): void {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"IncomingTransactionHelper.d.cts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAIxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAGzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAE1B,kCAAkC;IAClC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B,oEAAoE;IACpE,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAIF,qBAAa,yBAAyB;;IACpC,GAAG,EAAE,YAAY,CAAC;gBA8BN,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GACnB,EAAE;QACD,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,iBAAiB,EAAE,MAAM,UAAU,CACjC,kBAAkB,CAAC,oBAAoB,CAAC,CACzC,CAAC;QACF,oBAAoB,EAAE,MAAM,eAAe,EAAE,CAAC;QAC9C,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,SAAS,EAAE,8BAA8B,CAAC;QAC1C,uBAAuB,EAAE,uBAAuB,CAAC;QACjD,gBAAgB,EAAE,CAAC,YAAY,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,CAAC;QACzE,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B;IAgBD,KAAK,IAAI,IAAI;IAwBb,IAAI,IAAI,IAAI;IAsCN,MAAM,CAAC,EACX,UAAU,EACV,IAAI,GACL,GAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAsHlE"}
1
+ {"version":3,"file":"IncomingTransactionHelper.d.cts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAQxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAMzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAE1B,kCAAkC;IAClC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B,oEAAoE;IACpE,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AASF,qBAAa,yBAAyB;;IACpC,GAAG,EAAE,YAAY,CAAC;gBAgDN,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GACnB,EAAE;QACD,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,iBAAiB,EAAE,MAAM,UAAU,CACjC,kBAAkB,CAAC,oBAAoB,CAAC,CACzC,CAAC;QACF,oBAAoB,EAAE,MAAM,eAAe,EAAE,CAAC;QAC9C,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,SAAS,EAAE,8BAA8B,CAAC;QAC1C,uBAAuB,EAAE,uBAAuB,CAAC;QACjD,gBAAgB,EAAE,CAAC,YAAY,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,CAAC;QACzE,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B;IAwBD,KAAK,IAAI,IAAI;IAwBb,IAAI,IAAI,IAAI;IAuGN,MAAM,CAAC,EACX,UAAU,EACV,IAAI,GACL,GAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAsHlE"}
@@ -1 +1 @@
1
- {"version":3,"file":"IncomingTransactionHelper.d.mts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAIxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,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AAI,CAAC;CAsHlE"}
1
+ {"version":3,"file":"IncomingTransactionHelper.d.mts","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,sCAAsC;AAQxE,OAAO,YAAY,eAAe;AAElC,OAAO,KAAK,EAAE,8BAA8B,EAAE,qBAAW;AAEzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,eAAe,EAAE,qBAAiB;AAMzE,MAAM,MAAM,0BAA0B,GAAG;IACvC,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;IAE1B,kCAAkC;IAClC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B,oEAAoE;IACpE,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AASF,qBAAa,yBAAyB;;IACpC,GAAG,EAAE,YAAY,CAAC;gBAgDN,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GACnB,EAAE;QACD,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,iBAAiB,EAAE,MAAM,UAAU,CACjC,kBAAkB,CAAC,oBAAoB,CAAC,CACzC,CAAC;QACF,oBAAoB,EAAE,MAAM,eAAe,EAAE,CAAC;QAC9C,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,SAAS,EAAE,8BAA8B,CAAC;QAC1C,uBAAuB,EAAE,uBAAuB,CAAC;QACjD,gBAAgB,EAAE,CAAC,YAAY,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,CAAC;QACzE,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B;IAwBD,KAAK,IAAI,IAAI;IAwBb,IAAI,IAAI,IAAI;IAuGN,MAAM,CAAC,EACX,UAAU,EACV,IAAI,GACL,GAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAsHlE"}
@@ -9,13 +9,18 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_isUpdating, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
12
+ var _IncomingTransactionHelper_instances, _IncomingTransactionHelper_client, _IncomingTransactionHelper_getCurrentAccount, _IncomingTransactionHelper_getLocalTransactions, _IncomingTransactionHelper_includeTokenTransfers, _IncomingTransactionHelper_isEnabled, _IncomingTransactionHelper_isRunning, _IncomingTransactionHelper_isUpdating, _IncomingTransactionHelper_messenger, _IncomingTransactionHelper_remoteTransactionSource, _IncomingTransactionHelper_timeoutId, _IncomingTransactionHelper_trimTransactions, _IncomingTransactionHelper_updateTransactions, _IncomingTransactionHelper_useWebsockets, _IncomingTransactionHelper_connectionStateChangedHandler, _IncomingTransactionHelper_transactionUpdatedHandler, _IncomingTransactionHelper_selectedAccountChangedHandler, _IncomingTransactionHelper_onConnectionStateChanged, _IncomingTransactionHelper_startTransactionHistoryRetrieval, _IncomingTransactionHelper_stopTransactionHistoryRetrieval, _IncomingTransactionHelper_onTransactionUpdated, _IncomingTransactionHelper_onSelectedAccountChanged, _IncomingTransactionHelper_onInterval, _IncomingTransactionHelper_sortTransactionsByTime, _IncomingTransactionHelper_canStart, _IncomingTransactionHelper_getInterval, _IncomingTransactionHelper_getTags;
13
13
  // This package purposefully relies on Node's EventEmitter module.
14
14
  // eslint-disable-next-line import-x/no-nodejs-modules
15
15
  import EventEmitter from "events";
16
16
  import { incomingTransactionsLogger as log } from "../logger.mjs";
17
- import { getIncomingTransactionsPollingInterval } from "../utils/feature-flags.mjs";
17
+ import { getIncomingTransactionsPollingInterval, isIncomingTransactionsUseWebsocketsEnabled } from "../utils/feature-flags.mjs";
18
18
  const TAG_POLLING = 'automatic-polling';
19
+ var WebSocketState;
20
+ (function (WebSocketState) {
21
+ WebSocketState["CONNECTED"] = "connected";
22
+ WebSocketState["DISCONNECTED"] = "disconnected";
23
+ })(WebSocketState || (WebSocketState = {}));
19
24
  export class IncomingTransactionHelper {
20
25
  constructor({ client, getCurrentAccount, getLocalTransactions, includeTokenTransfers, isEnabled, messenger, remoteTransactionSource, trimTransactions, updateTransactions, }) {
21
26
  _IncomingTransactionHelper_instances.add(this);
@@ -31,6 +36,16 @@ export class IncomingTransactionHelper {
31
36
  _IncomingTransactionHelper_timeoutId.set(this, void 0);
32
37
  _IncomingTransactionHelper_trimTransactions.set(this, void 0);
33
38
  _IncomingTransactionHelper_updateTransactions.set(this, void 0);
39
+ _IncomingTransactionHelper_useWebsockets.set(this, void 0);
40
+ _IncomingTransactionHelper_connectionStateChangedHandler.set(this, (connectionInfo) => {
41
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onConnectionStateChanged).call(this, connectionInfo);
42
+ });
43
+ _IncomingTransactionHelper_transactionUpdatedHandler.set(this, (transaction) => {
44
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onTransactionUpdated).call(this, transaction);
45
+ });
46
+ _IncomingTransactionHelper_selectedAccountChangedHandler.set(this, () => {
47
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_onSelectedAccountChanged).call(this);
48
+ });
34
49
  this.hub = new EventEmitter();
35
50
  __classPrivateFieldSet(this, _IncomingTransactionHelper_client, client, "f");
36
51
  __classPrivateFieldSet(this, _IncomingTransactionHelper_getCurrentAccount, getCurrentAccount, "f");
@@ -43,9 +58,13 @@ export class IncomingTransactionHelper {
43
58
  __classPrivateFieldSet(this, _IncomingTransactionHelper_remoteTransactionSource, remoteTransactionSource, "f");
44
59
  __classPrivateFieldSet(this, _IncomingTransactionHelper_trimTransactions, trimTransactions, "f");
45
60
  __classPrivateFieldSet(this, _IncomingTransactionHelper_updateTransactions, updateTransactions, "f");
61
+ __classPrivateFieldSet(this, _IncomingTransactionHelper_useWebsockets, isIncomingTransactionsUseWebsocketsEnabled(messenger), "f");
62
+ if (__classPrivateFieldGet(this, _IncomingTransactionHelper_useWebsockets, "f")) {
63
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('BackendWebSocketService:connectionStateChanged', __classPrivateFieldGet(this, _IncomingTransactionHelper_connectionStateChangedHandler, "f"));
64
+ }
46
65
  }
47
66
  start() {
48
- if (__classPrivateFieldGet(this, _IncomingTransactionHelper_isRunning, "f")) {
67
+ if (__classPrivateFieldGet(this, _IncomingTransactionHelper_isRunning, "f") || __classPrivateFieldGet(this, _IncomingTransactionHelper_useWebsockets, "f")) {
49
68
  return;
50
69
  }
51
70
  if (!__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_canStart).call(this)) {
@@ -126,7 +145,43 @@ export class IncomingTransactionHelper {
126
145
  this.hub.emit('transactions', newTransactions);
127
146
  }
128
147
  }
129
- _IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_isUpdating = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
148
+ _IncomingTransactionHelper_client = new WeakMap(), _IncomingTransactionHelper_getCurrentAccount = new WeakMap(), _IncomingTransactionHelper_getLocalTransactions = new WeakMap(), _IncomingTransactionHelper_includeTokenTransfers = new WeakMap(), _IncomingTransactionHelper_isEnabled = new WeakMap(), _IncomingTransactionHelper_isRunning = new WeakMap(), _IncomingTransactionHelper_isUpdating = new WeakMap(), _IncomingTransactionHelper_messenger = new WeakMap(), _IncomingTransactionHelper_remoteTransactionSource = new WeakMap(), _IncomingTransactionHelper_timeoutId = new WeakMap(), _IncomingTransactionHelper_trimTransactions = new WeakMap(), _IncomingTransactionHelper_updateTransactions = new WeakMap(), _IncomingTransactionHelper_useWebsockets = new WeakMap(), _IncomingTransactionHelper_connectionStateChangedHandler = new WeakMap(), _IncomingTransactionHelper_transactionUpdatedHandler = new WeakMap(), _IncomingTransactionHelper_selectedAccountChangedHandler = new WeakMap(), _IncomingTransactionHelper_instances = new WeakSet(), _IncomingTransactionHelper_onConnectionStateChanged = function _IncomingTransactionHelper_onConnectionStateChanged(connectionInfo) {
149
+ if (connectionInfo.state === WebSocketState.CONNECTED) {
150
+ log('WebSocket connected, starting enhanced mode');
151
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_startTransactionHistoryRetrieval).call(this);
152
+ }
153
+ else if (connectionInfo.state === WebSocketState.DISCONNECTED) {
154
+ log('WebSocket disconnected, stopping enhanced mode');
155
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_stopTransactionHistoryRetrieval).call(this);
156
+ }
157
+ }, _IncomingTransactionHelper_startTransactionHistoryRetrieval = function _IncomingTransactionHelper_startTransactionHistoryRetrieval() {
158
+ if (!__classPrivateFieldGet(this, _IncomingTransactionHelper_instances, "m", _IncomingTransactionHelper_canStart).call(this)) {
159
+ return;
160
+ }
161
+ log('Started transaction history retrieval (event-driven)');
162
+ this.update().catch((error) => {
163
+ log('Initial update in transaction history retrieval failed', error);
164
+ });
165
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('AccountActivityService:transactionUpdated', __classPrivateFieldGet(this, _IncomingTransactionHelper_transactionUpdatedHandler, "f"));
166
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").subscribe('AccountsController:selectedAccountChange', __classPrivateFieldGet(this, _IncomingTransactionHelper_selectedAccountChangedHandler, "f"));
167
+ }, _IncomingTransactionHelper_stopTransactionHistoryRetrieval = function _IncomingTransactionHelper_stopTransactionHistoryRetrieval() {
168
+ log('Stopped transaction history retrieval');
169
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").unsubscribe('AccountActivityService:transactionUpdated', __classPrivateFieldGet(this, _IncomingTransactionHelper_transactionUpdatedHandler, "f"));
170
+ __classPrivateFieldGet(this, _IncomingTransactionHelper_messenger, "f").unsubscribe('AccountsController:selectedAccountChange', __classPrivateFieldGet(this, _IncomingTransactionHelper_selectedAccountChangedHandler, "f"));
171
+ }, _IncomingTransactionHelper_onTransactionUpdated = function _IncomingTransactionHelper_onTransactionUpdated(transaction) {
172
+ log('Received relevant transaction update, triggering update', {
173
+ txId: transaction.id,
174
+ chain: transaction.chain,
175
+ });
176
+ this.update().catch((error) => {
177
+ log('Update after transaction event failed', error);
178
+ });
179
+ }, _IncomingTransactionHelper_onSelectedAccountChanged = function _IncomingTransactionHelper_onSelectedAccountChanged() {
180
+ log('Selected account changed, triggering update');
181
+ this.update().catch((error) => {
182
+ log('Update after account change failed', error);
183
+ });
184
+ }, _IncomingTransactionHelper_onInterval = async function _IncomingTransactionHelper_onInterval() {
130
185
  __classPrivateFieldSet(this, _IncomingTransactionHelper_isUpdating, true, "f");
131
186
  try {
132
187
  await this.update({ isInterval: true });
@@ -1 +1 @@
1
- {"version":3,"file":"IncomingTransactionHelper.mjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,YAAY,eAAe;AAGlC,OAAO,EAAE,0BAA0B,IAAI,GAAG,EAAE,sBAAkB;AAE9D,OAAO,EAAE,sCAAsC,EAAE,mCAA+B;AAmBhF,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,MAAM,OAAO,yBAAyB;IA+BpC,YAAY,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GAanB;;QAlDQ,oDAAiB;QAEjB,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEpB,wDAAqB;QAEZ,uDAA2C;QAE3C,qEAAkD;QAE3D,uDAAqB;QAEZ,8DAEc;QAEd,gEAA8B;QAyBrC,IAAI,CAAC,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;QAE9B,uBAAA,IAAI,qCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,gDAAsB,iBAAiB,MAAA,CAAC;QAC5C,uBAAA,IAAI,mDAAyB,oBAAoB,MAAA,CAAC;QAClD,uBAAA,IAAI,oD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C,uBAAA,IAAI,yCAAQ,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,WAAW,EAAE,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;IAC5B,CAAC;SAAM,IAAI,UAAU,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport { getIncomingTransactionsPollingInterval } from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n /** Name of the client to include in requests. */\n client?: string;\n\n /** Whether to retrieve incoming token transfers. Defaults to false. */\n includeTokenTransfers?: boolean;\n\n /** Callback to determine if incoming transaction polling is enabled. */\n isEnabled?: () => boolean;\n\n /** @deprecated No longer used. */\n queryEntireHistory?: boolean;\n\n /** Whether to retrieve outgoing transactions. Defaults to false. */\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n #isUpdating: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateTransactions?: boolean;\n\n constructor({\n client,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n remoteTransactionSource,\n trimTransactions,\n updateTransactions,\n }: {\n client?: string;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? ((): boolean => true);\n this.#isRunning = false;\n this.#isUpdating = false;\n this.#messenger = messenger;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateTransactions = updateTransactions;\n }\n\n start(): void {\n if (this.#isRunning) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Started polling', { interval });\n\n this.#isRunning = true;\n\n if (this.#isUpdating) {\n return;\n }\n\n this.#onInterval().catch((error) => {\n log('Initial polling failed', error);\n });\n }\n\n stop(): void {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n async #onInterval(): Promise<void> {\n this.#isUpdating = true;\n\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n this.#isUpdating = false;\n\n if (this.#isRunning) {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n includeTokenTransfers,\n tags: finalTags,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]): void {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}
1
+ {"version":3,"file":"IncomingTransactionHelper.mjs","sourceRoot":"","sources":["../../src/helpers/IncomingTransactionHelper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,YAAY,eAAe;AAGlC,OAAO,EAAE,0BAA0B,IAAI,GAAG,EAAE,sBAAkB;AAE9D,OAAO,EACL,sCAAsC,EACtC,0CAA0C,EAC3C,mCAA+B;AAmBhC,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAExC,IAAK,cAGJ;AAHD,WAAK,cAAc;IACjB,yCAAuB,CAAA;IACvB,+CAA6B,CAAA;AAC/B,CAAC,EAHI,cAAc,KAAd,cAAc,QAGlB;AAED,MAAM,OAAO,yBAAyB;IAiDpC,YAAY,EACV,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,GAanB;;QApEQ,oDAAiB;QAEjB,+DAEP;QAEO,kEAA+C;QAE/C,mEAAiC;QAEjC,uDAA0B;QAEnC,uDAAoB;QAEpB,wDAAqB;QAEZ,uDAA2C;QAE3C,qEAAkD;QAE3D,uDAAqB;QAEZ,8DAEc;QAEd,gEAA8B;QAE9B,2DAAwB;QAExB,mEAAiC,CACxC,cAAuC,EACjC,EAAE;YACR,uBAAA,IAAI,iGAA0B,MAA9B,IAAI,EAA2B,cAAc,C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AAI,UAAU,EAAE,CAAC;QACtB,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACzC,CAAC","sourcesContent":["import type { AccountsController } from '@metamask/accounts-controller';\nimport type {\n Transaction as AccountActivityTransaction,\n WebSocketConnectionInfo,\n} from '@metamask/core-backend';\nimport type { Hex } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport EventEmitter from 'events';\n\nimport type { TransactionControllerMessenger } from '..';\nimport { incomingTransactionsLogger as log } from '../logger';\nimport type { RemoteTransactionSource, TransactionMeta } from '../types';\nimport {\n getIncomingTransactionsPollingInterval,\n isIncomingTransactionsUseWebsocketsEnabled,\n} from '../utils/feature-flags';\n\nexport type IncomingTransactionOptions = {\n /** Name of the client to include in requests. */\n client?: string;\n\n /** Whether to retrieve incoming token transfers. Defaults to false. */\n includeTokenTransfers?: boolean;\n\n /** Callback to determine if incoming transaction polling is enabled. */\n isEnabled?: () => boolean;\n\n /** @deprecated No longer used. */\n queryEntireHistory?: boolean;\n\n /** Whether to retrieve outgoing transactions. Defaults to false. */\n updateTransactions?: boolean;\n};\n\nconst TAG_POLLING = 'automatic-polling';\n\nenum WebSocketState {\n CONNECTED = 'connected',\n DISCONNECTED = 'disconnected',\n}\n\nexport class IncomingTransactionHelper {\n hub: EventEmitter;\n\n readonly #client?: string;\n\n readonly #getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n\n readonly #getLocalTransactions: () => TransactionMeta[];\n\n readonly #includeTokenTransfers?: boolean;\n\n readonly #isEnabled: () => boolean;\n\n #isRunning: boolean;\n\n #isUpdating: boolean;\n\n readonly #messenger: TransactionControllerMessenger;\n\n readonly #remoteTransactionSource: RemoteTransactionSource;\n\n #timeoutId?: unknown;\n\n readonly #trimTransactions: (\n transactions: TransactionMeta[],\n ) => TransactionMeta[];\n\n readonly #updateTransactions?: boolean;\n\n readonly #useWebsockets: boolean;\n\n readonly #connectionStateChangedHandler = (\n connectionInfo: WebSocketConnectionInfo,\n ): void => {\n this.#onConnectionStateChanged(connectionInfo);\n };\n\n readonly #transactionUpdatedHandler = (\n transaction: AccountActivityTransaction,\n ): void => {\n this.#onTransactionUpdated(transaction);\n };\n\n readonly #selectedAccountChangedHandler = (): void => {\n this.#onSelectedAccountChanged();\n };\n\n constructor({\n client,\n getCurrentAccount,\n getLocalTransactions,\n includeTokenTransfers,\n isEnabled,\n messenger,\n remoteTransactionSource,\n trimTransactions,\n updateTransactions,\n }: {\n client?: string;\n getCurrentAccount: () => ReturnType<\n AccountsController['getSelectedAccount']\n >;\n getLocalTransactions: () => TransactionMeta[];\n includeTokenTransfers?: boolean;\n isEnabled?: () => boolean;\n messenger: TransactionControllerMessenger;\n remoteTransactionSource: RemoteTransactionSource;\n trimTransactions: (transactions: TransactionMeta[]) => TransactionMeta[];\n updateTransactions?: boolean;\n }) {\n this.hub = new EventEmitter();\n\n this.#client = client;\n this.#getCurrentAccount = getCurrentAccount;\n this.#getLocalTransactions = getLocalTransactions;\n this.#includeTokenTransfers = includeTokenTransfers;\n this.#isEnabled = isEnabled ?? ((): boolean => true);\n this.#isRunning = false;\n this.#isUpdating = false;\n this.#messenger = messenger;\n this.#remoteTransactionSource = remoteTransactionSource;\n this.#trimTransactions = trimTransactions;\n this.#updateTransactions = updateTransactions;\n this.#useWebsockets = isIncomingTransactionsUseWebsocketsEnabled(messenger);\n\n if (this.#useWebsockets) {\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n this.#connectionStateChangedHandler,\n );\n }\n }\n\n start(): void {\n if (this.#isRunning || this.#useWebsockets) {\n return;\n }\n\n if (!this.#canStart()) {\n return;\n }\n\n const interval = this.#getInterval();\n\n log('Started polling', { interval });\n\n this.#isRunning = true;\n\n if (this.#isUpdating) {\n return;\n }\n\n this.#onInterval().catch((error) => {\n log('Initial polling failed', error);\n });\n }\n\n stop(): void {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n if (!this.#isRunning) {\n return;\n }\n\n this.#isRunning = false;\n\n log('Stopped polling');\n }\n\n #onConnectionStateChanged(connectionInfo: WebSocketConnectionInfo): void {\n if (connectionInfo.state === WebSocketState.CONNECTED) {\n log('WebSocket connected, starting enhanced mode');\n this.#startTransactionHistoryRetrieval();\n } else if (connectionInfo.state === WebSocketState.DISCONNECTED) {\n log('WebSocket disconnected, stopping enhanced mode');\n this.#stopTransactionHistoryRetrieval();\n }\n }\n\n #startTransactionHistoryRetrieval(): void {\n if (!this.#canStart()) {\n return;\n }\n\n log('Started transaction history retrieval (event-driven)');\n\n this.update().catch((error) => {\n log('Initial update in transaction history retrieval failed', error);\n });\n\n this.#messenger.subscribe(\n 'AccountActivityService:transactionUpdated',\n this.#transactionUpdatedHandler,\n );\n\n this.#messenger.subscribe(\n 'AccountsController:selectedAccountChange',\n this.#selectedAccountChangedHandler,\n );\n }\n\n #stopTransactionHistoryRetrieval(): void {\n log('Stopped transaction history retrieval');\n\n this.#messenger.unsubscribe(\n 'AccountActivityService:transactionUpdated',\n this.#transactionUpdatedHandler,\n );\n\n this.#messenger.unsubscribe(\n 'AccountsController:selectedAccountChange',\n this.#selectedAccountChangedHandler,\n );\n }\n\n #onTransactionUpdated(transaction: AccountActivityTransaction): void {\n log('Received relevant transaction update, triggering update', {\n txId: transaction.id,\n chain: transaction.chain,\n });\n\n this.update().catch((error) => {\n log('Update after transaction event failed', error);\n });\n }\n\n #onSelectedAccountChanged(): void {\n log('Selected account changed, triggering update');\n\n this.update().catch((error) => {\n log('Update after account change failed', error);\n });\n }\n\n async #onInterval(): Promise<void> {\n this.#isUpdating = true;\n\n try {\n await this.update({ isInterval: true });\n } catch (error) {\n console.error('Error while checking incoming transactions', error);\n }\n\n this.#isUpdating = false;\n\n if (this.#isRunning) {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId as number);\n }\n\n this.#timeoutId = setTimeout(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n () => this.#onInterval(),\n this.#getInterval(),\n );\n }\n }\n\n async update({\n isInterval,\n tags,\n }: { isInterval?: boolean; tags?: string[] } = {}): Promise<void> {\n const finalTags = this.#getTags(tags, isInterval);\n\n log('Checking for incoming transactions', {\n isInterval: Boolean(isInterval),\n tags: finalTags,\n });\n\n if (!this.#canStart()) {\n return;\n }\n\n const account = this.#getCurrentAccount();\n const includeTokenTransfers = this.#includeTokenTransfers ?? true;\n const updateTransactions = this.#updateTransactions ?? false;\n\n let remoteTransactions: TransactionMeta[] = [];\n\n try {\n remoteTransactions =\n await this.#remoteTransactionSource.fetchTransactions({\n address: account.address as Hex,\n includeTokenTransfers,\n tags: finalTags,\n updateTransactions,\n });\n } catch (error: unknown) {\n log('Error while fetching remote transactions', error);\n return;\n }\n\n if (!remoteTransactions.length) {\n return;\n }\n\n this.#sortTransactionsByTime(remoteTransactions);\n\n log(\n 'Found potential transactions',\n remoteTransactions.length,\n remoteTransactions,\n );\n\n const localTransactions = this.#getLocalTransactions();\n\n const uniqueTransactions = remoteTransactions.filter(\n (tx) =>\n !localTransactions.some(\n (currentTx) =>\n currentTx.hash?.toLowerCase() === tx.hash?.toLowerCase() &&\n currentTx.txParams.from?.toLowerCase() ===\n tx.txParams.from?.toLowerCase() &&\n currentTx.type === tx.type,\n ),\n );\n\n if (!uniqueTransactions.length) {\n log('All transactions are already known');\n return;\n }\n\n log(\n 'Found unique transactions',\n uniqueTransactions.length,\n uniqueTransactions,\n );\n\n const trimmedTransactions = this.#trimTransactions([\n ...uniqueTransactions,\n ...localTransactions,\n ]);\n\n const uniqueTransactionIds = uniqueTransactions.map((tx) => tx.id);\n\n const newTransactions = trimmedTransactions.filter((tx) =>\n uniqueTransactionIds.includes(tx.id),\n );\n\n if (!newTransactions.length) {\n log('All unique transactions truncated due to limit');\n return;\n }\n\n log('Adding new transactions', newTransactions.length, newTransactions);\n\n this.hub.emit('transactions', newTransactions);\n }\n\n #sortTransactionsByTime(transactions: TransactionMeta[]): void {\n transactions.sort((a, b) => (a.time < b.time ? -1 : 1));\n }\n\n #canStart(): boolean {\n return this.#isEnabled();\n }\n\n #getInterval(): number {\n return getIncomingTransactionsPollingInterval(this.#messenger);\n }\n\n #getTags(\n requestTags: string[] | undefined,\n isInterval: boolean | undefined,\n ): string[] | undefined {\n const tags = [];\n\n if (this.#client) {\n tags.push(this.#client);\n }\n\n if (requestTags?.length) {\n tags.push(...requestTags);\n } else if (isInterval) {\n tags.push(TAG_POLLING);\n }\n\n return tags?.length ? tags : undefined;\n }\n}\n"]}