@metamask/transaction-controller 59.2.0 → 60.1.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 (76) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/dist/TransactionController.cjs +33 -5
  3. package/dist/TransactionController.cjs.map +1 -1
  4. package/dist/TransactionController.d.cts +18 -1
  5. package/dist/TransactionController.d.cts.map +1 -1
  6. package/dist/TransactionController.d.mts +18 -1
  7. package/dist/TransactionController.d.mts.map +1 -1
  8. package/dist/TransactionController.mjs +33 -5
  9. package/dist/TransactionController.mjs.map +1 -1
  10. package/dist/api/simulation-api.cjs +13 -1
  11. package/dist/api/simulation-api.cjs.map +1 -1
  12. package/dist/api/simulation-api.d.cts +11 -0
  13. package/dist/api/simulation-api.d.cts.map +1 -1
  14. package/dist/api/simulation-api.d.mts +11 -0
  15. package/dist/api/simulation-api.d.mts.map +1 -1
  16. package/dist/api/simulation-api.mjs +13 -1
  17. package/dist/api/simulation-api.mjs.map +1 -1
  18. package/dist/hooks/ExtraTransactionsPublishHook.cjs +28 -14
  19. package/dist/hooks/ExtraTransactionsPublishHook.cjs.map +1 -1
  20. package/dist/hooks/ExtraTransactionsPublishHook.d.cts +2 -3
  21. package/dist/hooks/ExtraTransactionsPublishHook.d.cts.map +1 -1
  22. package/dist/hooks/ExtraTransactionsPublishHook.d.mts +2 -3
  23. package/dist/hooks/ExtraTransactionsPublishHook.d.mts.map +1 -1
  24. package/dist/hooks/ExtraTransactionsPublishHook.mjs +28 -14
  25. package/dist/hooks/ExtraTransactionsPublishHook.mjs.map +1 -1
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +1 -1
  28. package/dist/index.d.cts.map +1 -1
  29. package/dist/index.d.mts +1 -1
  30. package/dist/index.d.mts.map +1 -1
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/types.cjs.map +1 -1
  33. package/dist/types.d.cts +64 -1
  34. package/dist/types.d.cts.map +1 -1
  35. package/dist/types.d.mts +64 -1
  36. package/dist/types.d.mts.map +1 -1
  37. package/dist/types.mjs.map +1 -1
  38. package/dist/utils/balance-changes.cjs +3 -1
  39. package/dist/utils/balance-changes.cjs.map +1 -1
  40. package/dist/utils/balance-changes.d.cts +3 -1
  41. package/dist/utils/balance-changes.d.cts.map +1 -1
  42. package/dist/utils/balance-changes.d.mts +3 -1
  43. package/dist/utils/balance-changes.d.mts.map +1 -1
  44. package/dist/utils/balance-changes.mjs +3 -1
  45. package/dist/utils/balance-changes.mjs.map +1 -1
  46. package/dist/utils/batch.cjs +13 -4
  47. package/dist/utils/batch.cjs.map +1 -1
  48. package/dist/utils/batch.d.cts +2 -1
  49. package/dist/utils/batch.d.cts.map +1 -1
  50. package/dist/utils/batch.d.mts +2 -1
  51. package/dist/utils/batch.d.mts.map +1 -1
  52. package/dist/utils/batch.mjs +13 -4
  53. package/dist/utils/batch.mjs.map +1 -1
  54. package/dist/utils/eip7702.cjs +0 -12
  55. package/dist/utils/eip7702.cjs.map +1 -1
  56. package/dist/utils/eip7702.d.cts.map +1 -1
  57. package/dist/utils/eip7702.d.mts.map +1 -1
  58. package/dist/utils/eip7702.mjs +0 -12
  59. package/dist/utils/eip7702.mjs.map +1 -1
  60. package/dist/utils/gas-fee-tokens.cjs +23 -17
  61. package/dist/utils/gas-fee-tokens.cjs.map +1 -1
  62. package/dist/utils/gas-fee-tokens.d.cts +7 -1
  63. package/dist/utils/gas-fee-tokens.d.cts.map +1 -1
  64. package/dist/utils/gas-fee-tokens.d.mts +7 -1
  65. package/dist/utils/gas-fee-tokens.d.mts.map +1 -1
  66. package/dist/utils/gas-fee-tokens.mjs +23 -17
  67. package/dist/utils/gas-fee-tokens.mjs.map +1 -1
  68. package/dist/utils/gas.cjs +16 -7
  69. package/dist/utils/gas.cjs.map +1 -1
  70. package/dist/utils/gas.d.cts +8 -3
  71. package/dist/utils/gas.d.cts.map +1 -1
  72. package/dist/utils/gas.d.mts +8 -3
  73. package/dist/utils/gas.d.mts.map +1 -1
  74. package/dist/utils/gas.mjs +16 -7
  75. package/dist/utils/gas.mjs.map +1 -1
  76. package/package.json +6 -6
@@ -20,13 +20,25 @@ let requestIdCounter = 0;
20
20
  * @returns The response from the simulation API.
21
21
  */
22
22
  async function simulateTransactions(chainId, request) {
23
- const url = await getSimulationUrl(chainId);
23
+ let url = await getSimulationUrl(chainId);
24
+ const { newUrl, authorization } = (await request.getSimulationConfig(url)) || {};
25
+ if (newUrl) {
26
+ url = newUrl;
27
+ }
24
28
  const requestId = requestIdCounter;
25
29
  requestIdCounter += 1;
26
30
  const finalRequest = finalizeRequest(request);
27
31
  log('Sending request', url, request);
32
+ const headers = {
33
+ 'Content-Type': 'application/json',
34
+ };
35
+ // Add optional authorization header, if provided.
36
+ if (authorization) {
37
+ headers.Authorization = authorization;
38
+ }
28
39
  const response = await fetch(url, {
29
40
  method: 'POST',
41
+ headers,
30
42
  body: JSON.stringify({
31
43
  id: String(requestId),
32
44
  jsonrpc: '2.0',
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.cjs","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":";;;AAAA,iEAAiE;AACjE,2CAA+D;AAC/D,mCAAmC;AAEnC,gDAGsB;AACtB,0CAA8E;AAC9E,0CAA0C;AAE1C,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;AAgPrC,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACxC,OAAY,EACZ,OAA0B;IAE1B,MAAM,GAAG,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE5C,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACnC,gBAAgB,IA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,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;QACjD,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;YAC1B,SAAS;SACV;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;KACH;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger, 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';\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 * 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\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 const url = await getSimulationUrl(chainId);\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const response = await fetch(url, {\n method: 'POST',\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,2CAA+D;AAC/D,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;AA6P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B,EAAE;YAC1B,SAAS;SACV;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;KACH;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger, 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,4 +1,5 @@
1
1
  import { type Hex } from "@metamask/utils";
2
+ import type { GetSimulationConfig } from "../types.cjs";
2
3
  /** Single transaction to simulate in a simulation API request. */
3
4
  export type SimulationRequestTransaction = {
4
5
  authorizationList?: {
@@ -27,6 +28,10 @@ export type SimulationRequest = {
27
28
  blockOverrides?: {
28
29
  time?: Hex;
29
30
  };
31
+ /**
32
+ * Function to get the simulation configuration.
33
+ */
34
+ getSimulationConfig: GetSimulationConfig;
30
35
  /**
31
36
  * Overrides to the state of the blockchain, keyed by address.
32
37
  */
@@ -169,6 +174,12 @@ export type SimulationResponseTransaction = {
169
174
  export type SimulationResponse = {
170
175
  /** Simulation data for each transaction in the request. */
171
176
  transactions: SimulationResponseTransaction[];
177
+ sponsorship: {
178
+ /** Whether the gas costs are sponsored meaning a transfer is not required. */
179
+ isSponsored: boolean;
180
+ /** Error message for the determination of sponsorship. */
181
+ error: string | null;
182
+ };
172
183
  };
173
184
  /**
174
185
  * Simulate transactions using the transaction simulation API.
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.d.cts","sourceRoot":"","sources":["../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oBAAoB,CACxC,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,kBAAkB,CAAC,CA8C7B"}
@@ -1,4 +1,5 @@
1
1
  import { type Hex } from "@metamask/utils";
2
+ import type { GetSimulationConfig } from "../types.mjs";
2
3
  /** Single transaction to simulate in a simulation API request. */
3
4
  export type SimulationRequestTransaction = {
4
5
  authorizationList?: {
@@ -27,6 +28,10 @@ export type SimulationRequest = {
27
28
  blockOverrides?: {
28
29
  time?: Hex;
29
30
  };
31
+ /**
32
+ * Function to get the simulation configuration.
33
+ */
34
+ getSimulationConfig: GetSimulationConfig;
30
35
  /**
31
36
  * Overrides to the state of the blockchain, keyed by address.
32
37
  */
@@ -169,6 +174,12 @@ export type SimulationResponseTransaction = {
169
174
  export type SimulationResponse = {
170
175
  /** Simulation data for each transaction in the request. */
171
176
  transactions: SimulationResponseTransaction[];
177
+ sponsorship: {
178
+ /** Whether the gas costs are sponsored meaning a transfer is not required. */
179
+ isSponsored: boolean;
180
+ /** Error message for the determination of sponsorship. */
181
+ error: string | null;
182
+ };
172
183
  };
173
184
  /**
174
185
  * Simulate transactions using the transaction simulation API.
@@ -1 +1 @@
1
- {"version":3,"file":"simulation-api.d.mts","sourceRoot":"","sources":["../../src/api/simulation-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,GAAG,EAAE,wBAAwB;AAgB/D,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,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AY,EAAE,GAAG,CAAC;QAElB,sDAAsD;QACtD,oBAAoB,EAAE,GAAG,CAAC;QAE1B,wCAAwC;QACxC,SAAS,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"}
@@ -18,13 +18,25 @@ let requestIdCounter = 0;
18
18
  * @returns The response from the simulation API.
19
19
  */
20
20
  export async function simulateTransactions(chainId, request) {
21
- const url = await getSimulationUrl(chainId);
21
+ let url = await getSimulationUrl(chainId);
22
+ const { newUrl, authorization } = (await request.getSimulationConfig(url)) || {};
23
+ if (newUrl) {
24
+ url = newUrl;
25
+ }
22
26
  const requestId = requestIdCounter;
23
27
  requestIdCounter += 1;
24
28
  const finalRequest = finalizeRequest(request);
25
29
  log('Sending request', url, request);
30
+ const headers = {
31
+ 'Content-Type': 'application/json',
32
+ };
33
+ // Add optional authorization header, if provided.
34
+ if (authorization) {
35
+ headers.Authorization = authorization;
36
+ }
26
37
  const response = await fetch(url, {
27
38
  method: 'POST',
39
+ headers,
28
40
  body: JSON.stringify({
29
41
  id: String(requestId),
30
42
  jsonrpc: '2.0',
@@ -1 +1 @@
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,EAAY,wBAAwB;;;AAG/D,OAAO,EACL,2CAA2C,EAC3C,4BAA4B,EAC7B,yBAAqB;AACtB,OAAO,EAAE,gCAAgC,EAAE,eAAe,EAAE,sBAAkB;AAC9E,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAE1C,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;AAgPrC,IAAI,gBAAgB,GAAG,CAAC,CAAC;AAEzB;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAY,EACZ,OAA0B;IAE1B,MAAM,GAAG,GAAG,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE5C,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,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;QAChC,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,EAAE,EAAE,MAAM,CAAC,S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,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;QACjD,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;YAC1B,SAAS;SACV;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;KACH;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger, 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';\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 * 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\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 const url = await getSimulationUrl(chainId);\n\n const requestId = requestIdCounter;\n requestIdCounter += 1;\n\n const finalRequest = finalizeRequest(request);\n\n log('Sending request', url, request);\n\n const response = await fetch(url, {\n method: 'POST',\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,EAAY,wBAAwB;;;AAG/D,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;AA6PrC,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;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CAAC;AACpB,CAAC","sourcesContent":["import { convertHexToDecimal } from '@metamask/controller-utils';\nimport { createModuleLogger, 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"]}
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _ExtraTransactionsPublishHook_instances, _ExtraTransactionsPublishHook_addTransactionBatch, _ExtraTransactionsPublishHook_transactions, _ExtraTransactionsPublishHook_hook;
13
+ var _ExtraTransactionsPublishHook_instances, _ExtraTransactionsPublishHook_addTransactionBatch, _ExtraTransactionsPublishHook_hook;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ExtraTransactionsPublishHook = void 0;
16
16
  const utils_1 = require("@metamask/utils");
@@ -21,12 +21,10 @@ const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'extra-trans
21
21
  * Requires the batch to be successful to resolve.
22
22
  */
23
23
  class ExtraTransactionsPublishHook {
24
- constructor({ addTransactionBatch, transactions, }) {
24
+ constructor({ addTransactionBatch, }) {
25
25
  _ExtraTransactionsPublishHook_instances.add(this);
26
26
  _ExtraTransactionsPublishHook_addTransactionBatch.set(this, void 0);
27
- _ExtraTransactionsPublishHook_transactions.set(this, void 0);
28
27
  __classPrivateFieldSet(this, _ExtraTransactionsPublishHook_addTransactionBatch, addTransactionBatch, "f");
29
- __classPrivateFieldSet(this, _ExtraTransactionsPublishHook_transactions, transactions, "f");
30
28
  }
31
29
  /**
32
30
  * @returns The publish hook function.
@@ -36,9 +34,9 @@ class ExtraTransactionsPublishHook {
36
34
  }
37
35
  }
38
36
  exports.ExtraTransactionsPublishHook = ExtraTransactionsPublishHook;
39
- _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransactionsPublishHook_transactions = new WeakMap(), _ExtraTransactionsPublishHook_instances = new WeakSet(), _ExtraTransactionsPublishHook_hook = async function _ExtraTransactionsPublishHook_hook(transactionMeta, signedTx) {
37
+ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransactionsPublishHook_instances = new WeakSet(), _ExtraTransactionsPublishHook_hook = async function _ExtraTransactionsPublishHook_hook(transactionMeta, signedTx) {
40
38
  log('Publishing transaction as batch', { transactionMeta, signedTx });
41
- const { id, networkClientId, txParams } = transactionMeta;
39
+ const { batchTransactions, batchTransactionsOptions, id, networkClientId, txParams, } = transactionMeta;
42
40
  const from = txParams.from;
43
41
  const to = txParams.to;
44
42
  const data = txParams.data;
@@ -59,7 +57,7 @@ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransac
59
57
  to,
60
58
  value,
61
59
  };
62
- const firstTransaction = {
60
+ const mainTransaction = {
63
61
  existingTransaction: {
64
62
  id,
65
63
  onPublish,
@@ -67,29 +65,45 @@ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransac
67
65
  },
68
66
  params: firstParams,
69
67
  };
70
- const extraTransactions = __classPrivateFieldGet(this, _ExtraTransactionsPublishHook_transactions, "f").map((transaction) => {
71
- const { type, ...rest } = transaction;
68
+ const extraTransactions = (batchTransactions ?? []).map((transaction) => {
69
+ const { isAfter, type, ...rest } = transaction;
72
70
  return {
71
+ isAfter,
73
72
  params: rest,
74
73
  type,
75
74
  };
76
75
  });
76
+ const beforeTransactions = extraTransactions
77
+ .filter((transaction) => transaction.isAfter === false)
78
+ .map(({ isAfter, ...rest }) => ({
79
+ ...rest,
80
+ }));
81
+ const afterTransactions = extraTransactions
82
+ .filter((transaction) => transaction.isAfter === undefined || transaction.isAfter)
83
+ .map(({ isAfter, ...rest }) => ({
84
+ ...rest,
85
+ }));
77
86
  const transactions = [
78
- firstTransaction,
79
- ...extraTransactions,
87
+ ...beforeTransactions,
88
+ mainTransaction,
89
+ ...afterTransactions,
80
90
  ];
81
91
  log('Adding transaction batch', {
82
92
  from,
83
93
  networkClientId,
84
94
  transactions,
85
95
  });
96
+ const options = batchTransactionsOptions ?? {
97
+ disable7702: true,
98
+ disableHook: false,
99
+ disableSequential: true,
100
+ };
86
101
  await __classPrivateFieldGet(this, _ExtraTransactionsPublishHook_addTransactionBatch, "f").call(this, {
87
102
  from,
88
103
  networkClientId,
104
+ requireApproval: false,
89
105
  transactions,
90
- disable7702: true,
91
- disableHook: false,
92
- disableSequential: true,
106
+ ...options,
93
107
  });
94
108
  return resultPromise.promise;
95
109
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ExtraTransactionsPublishHook.cjs","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAIyB;AAGzB,0CAA0C;AAU1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAC5B,sBAAa,EACb,iCAAiC,CAClC,CAAC;AAEF;;;GAGG;AACH,MAAa,4BAA4B;IAKvC,YAAY,EACV,mBAAmB,EACnB,YAAY,GAIb;;QAVQ,oEAAmE;QAEnE,6DAA2C;QASlD,uBAAA,IAAI,qDAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,8CAAiB,YAAY,MAAA,CAAC;IACpC,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,uBAAA,IAAI,mFAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;CA0EF;AA/FD,oEA+FC;6NAxEC,KAAK,6CACH,eAAgC,EAChC,QAAgB;IAEhB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AoC;QACpD,gBAAgB;QAChB,GAAG,iBAAiB;KACrB,CAAC;IAEF,GAAG,CAAC,0BAA0B,EAAE;QAC9B,IAAI;QACJ,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IAEH,MAAM,uBAAA,IAAI,yDAAqB,MAAzB,IAAI,EAAsB;QAC9B,IAAI;QACJ,eAAe;QACf,YAAY;QACZ,WAAW,EAAE,IAAI;QACjB,WAAW,EAAE,KAAK;QAClB,iBAAiB,EAAE,IAAI;KACxB,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC,OAAO,CAAC;AAC/B,CAAC","sourcesContent":["import {\n createDeferredPromise,\n createModuleLogger,\n type Hex,\n} from '@metamask/utils';\n\nimport type { TransactionController } from '..';\nimport { projectLogger } from '../logger';\nimport type {\n BatchTransactionParams,\n NestedTransactionMetadata,\n PublishHook,\n PublishHookResult,\n TransactionBatchSingleRequest,\n TransactionMeta,\n} from '../types';\n\nconst log = createModuleLogger(\n projectLogger,\n 'extra-transactions-publish-hook',\n);\n\n/**\n * Custom publish logic that also publishes additional transactions in an batch.\n * Requires the batch to be successful to resolve.\n */\nexport class ExtraTransactionsPublishHook {\n readonly #addTransactionBatch: TransactionController['addTransactionBatch'];\n\n readonly #transactions: NestedTransactionMetadata[];\n\n constructor({\n addTransactionBatch,\n transactions,\n }: {\n addTransactionBatch: TransactionController['addTransactionBatch'];\n transactions: NestedTransactionMetadata[];\n }) {\n this.#addTransactionBatch = addTransactionBatch;\n this.#transactions = transactions;\n }\n\n /**\n * @returns The publish hook function.\n */\n getHook(): PublishHook {\n return this.#hook.bind(this);\n }\n\n async #hook(\n transactionMeta: TransactionMeta,\n signedTx: string,\n ): Promise<PublishHookResult> {\n log('Publishing transaction as batch', { transactionMeta, signedTx });\n\n const { id, networkClientId, txParams } = transactionMeta;\n const from = txParams.from as Hex;\n const to = txParams.to as Hex | undefined;\n const data = txParams.data as Hex | undefined;\n const value = txParams.value as Hex | undefined;\n const gas = txParams.gas as Hex | undefined;\n const maxFeePerGas = txParams.maxFeePerGas as Hex | undefined;\n const maxPriorityFeePerGas = txParams.maxPriorityFeePerGas as\n | Hex\n | undefined;\n const signedTransaction = signedTx as Hex;\n const resultPromise = createDeferredPromise<PublishHookResult>();\n\n const onPublish = ({ transactionHash }: { transactionHash?: string }) => {\n resultPromise.resolve({ transactionHash });\n };\n\n const firstParams: BatchTransactionParams = {\n data,\n gas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n to,\n value,\n };\n\n const firstTransaction: TransactionBatchSingleRequest = {\n existingTransaction: {\n id,\n onPublish,\n signedTransaction,\n },\n params: firstParams,\n };\n\n const extraTransactions: TransactionBatchSingleRequest[] =\n this.#transactions.map((transaction) => {\n const { type, ...rest } = transaction;\n return {\n params: rest,\n type,\n };\n });\n\n const transactions: TransactionBatchSingleRequest[] = [\n firstTransaction,\n ...extraTransactions,\n ];\n\n log('Adding transaction batch', {\n from,\n networkClientId,\n transactions,\n });\n\n await this.#addTransactionBatch({\n from,\n networkClientId,\n transactions,\n disable7702: true,\n disableHook: false,\n disableSequential: true,\n });\n\n return resultPromise.promise;\n }\n}\n"]}
1
+ {"version":3,"file":"ExtraTransactionsPublishHook.cjs","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAIyB;AAGzB,0CAA0C;AAS1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAC5B,sBAAa,EACb,iCAAiC,CAClC,CAAC;AAEF;;;GAGG;AACH,MAAa,4BAA4B;IAGvC,YAAY,EACV,mBAAmB,GAGpB;;QANQ,oEAAmE;QAO1E,uBAAA,IAAI,qDAAwB,mBAAmB,MAAA,CAAC;IAClD,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,uBAAA,IAAI,mFAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;CAyGF;AAzHD,oEAyHC;iKAvGC,KAAK,6CACH,eAAgC,EAChC,QAAgB;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ACZ,IAAI;SACL,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,MAAM,kBAAkB,GACtB,iBAAiB;SACd,MAAM,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,KAAK,KAAK,CAAC;SACtD,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QAC9B,GAAG,IAAI;KACR,CAAC,CAAC,CAAC;IAER,MAAM,iBAAiB,GAAoC,iBAAiB;SACzE,MAAM,CACL,CAAC,WAAW,EAAE,EAAE,CACd,WAAW,CAAC,OAAO,KAAK,SAAS,IAAI,WAAW,CAAC,OAAO,CAC3D;SACA,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QAC9B,GAAG,IAAI;KACR,CAAC,CAAC,CAAC;IAEN,MAAM,YAAY,GAAoC;QACpD,GAAG,kBAAkB;QACrB,eAAe;QACf,GAAG,iBAAiB;KACrB,CAAC;IAEF,GAAG,CAAC,0BAA0B,EAAE;QAC9B,IAAI;QACJ,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,wBAAwB,IAAI;QAC1C,WAAW,EAAE,IAAI;QACjB,WAAW,EAAE,KAAK;QAClB,iBAAiB,EAAE,IAAI;KACxB,CAAC;IAEF,MAAM,uBAAA,IAAI,yDAAqB,MAAzB,IAAI,EAAsB;QAC9B,IAAI;QACJ,eAAe;QACf,eAAe,EAAE,KAAK;QACtB,YAAY;QACZ,GAAG,OAAO;KACX,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC,OAAO,CAAC;AAC/B,CAAC","sourcesContent":["import {\n createDeferredPromise,\n createModuleLogger,\n type Hex,\n} from '@metamask/utils';\n\nimport type { TransactionController } from '..';\nimport { projectLogger } from '../logger';\nimport type {\n BatchTransactionParams,\n PublishHook,\n PublishHookResult,\n TransactionBatchSingleRequest,\n TransactionMeta,\n} from '../types';\n\nconst log = createModuleLogger(\n projectLogger,\n 'extra-transactions-publish-hook',\n);\n\n/**\n * Custom publish logic that also publishes additional transactions in an batch.\n * Requires the batch to be successful to resolve.\n */\nexport class ExtraTransactionsPublishHook {\n readonly #addTransactionBatch: TransactionController['addTransactionBatch'];\n\n constructor({\n addTransactionBatch,\n }: {\n addTransactionBatch: TransactionController['addTransactionBatch'];\n }) {\n this.#addTransactionBatch = addTransactionBatch;\n }\n\n /**\n * @returns The publish hook function.\n */\n getHook(): PublishHook {\n return this.#hook.bind(this);\n }\n\n async #hook(\n transactionMeta: TransactionMeta,\n signedTx: string,\n ): Promise<PublishHookResult> {\n log('Publishing transaction as batch', { transactionMeta, signedTx });\n\n const {\n batchTransactions,\n batchTransactionsOptions,\n id,\n networkClientId,\n txParams,\n } = transactionMeta;\n\n const from = txParams.from as Hex;\n const to = txParams.to as Hex | undefined;\n const data = txParams.data as Hex | undefined;\n const value = txParams.value as Hex | undefined;\n const gas = txParams.gas as Hex | undefined;\n const maxFeePerGas = txParams.maxFeePerGas as Hex | undefined;\n\n const maxPriorityFeePerGas = txParams.maxPriorityFeePerGas as\n | Hex\n | undefined;\n\n const signedTransaction = signedTx as Hex;\n const resultPromise = createDeferredPromise<PublishHookResult>();\n\n const onPublish = ({ transactionHash }: { transactionHash?: string }) => {\n resultPromise.resolve({ transactionHash });\n };\n\n const firstParams: BatchTransactionParams = {\n data,\n gas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n to,\n value,\n };\n\n const mainTransaction: TransactionBatchSingleRequest = {\n existingTransaction: {\n id,\n onPublish,\n signedTransaction,\n },\n params: firstParams,\n };\n\n const extraTransactions = (batchTransactions ?? []).map((transaction) => {\n const { isAfter, type, ...rest } = transaction;\n return {\n isAfter,\n params: rest,\n type,\n };\n });\n\n const beforeTransactions: TransactionBatchSingleRequest[] =\n extraTransactions\n .filter((transaction) => transaction.isAfter === false)\n .map(({ isAfter, ...rest }) => ({\n ...rest,\n }));\n\n const afterTransactions: TransactionBatchSingleRequest[] = extraTransactions\n .filter(\n (transaction) =>\n transaction.isAfter === undefined || transaction.isAfter,\n )\n .map(({ isAfter, ...rest }) => ({\n ...rest,\n }));\n\n const transactions: TransactionBatchSingleRequest[] = [\n ...beforeTransactions,\n mainTransaction,\n ...afterTransactions,\n ];\n\n log('Adding transaction batch', {\n from,\n networkClientId,\n transactions,\n });\n\n const options = batchTransactionsOptions ?? {\n disable7702: true,\n disableHook: false,\n disableSequential: true,\n };\n\n await this.#addTransactionBatch({\n from,\n networkClientId,\n requireApproval: false,\n transactions,\n ...options,\n });\n\n return resultPromise.promise;\n }\n}\n"]}
@@ -1,14 +1,13 @@
1
1
  import type { TransactionController } from "../index.cjs";
2
- import type { NestedTransactionMetadata, PublishHook } from "../types.cjs";
2
+ import type { PublishHook } from "../types.cjs";
3
3
  /**
4
4
  * Custom publish logic that also publishes additional transactions in an batch.
5
5
  * Requires the batch to be successful to resolve.
6
6
  */
7
7
  export declare class ExtraTransactionsPublishHook {
8
8
  #private;
9
- constructor({ addTransactionBatch, transactions, }: {
9
+ constructor({ addTransactionBatch, }: {
10
10
  addTransactionBatch: TransactionController['addTransactionBatch'];
11
- transactions: NestedTransactionMetadata[];
12
11
  });
13
12
  /**
14
13
  * @returns The publish hook function.
@@ -1 +1 @@
1
- {"version":3,"file":"ExtraTransactionsPublishHook.d.cts","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAW;AAEhD,OAAO,KAAK,EAEV,yBAAyB,EACzB,WAAW,EAIZ,qBAAiB;AAOlB;;;GAGG;AACH,qBAAa,4BAA4B;;gBAK3B,EACV,mBAAmB,EACnB,YAAY,GACb,EAAE;QACD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;QAClE,YAAY,EAAE,yBAAyB,EAAE,CAAC;KAC3C;IAKD;;OAEG;IACH,OAAO,IAAI,WAAW;CA4EvB"}
1
+ {"version":3,"file":"ExtraTransactionsPublishHook.d.cts","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAW;AAEhD,OAAO,KAAK,EAEV,WAAW,EAIZ,qBAAiB;AAOlB;;;GAGG;AACH,qBAAa,4BAA4B;;gBAG3B,EACV,mBAAmB,GACpB,EAAE;QACD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;KACnE;IAID;;OAEG;IACH,OAAO,IAAI,WAAW;CA2GvB"}
@@ -1,14 +1,13 @@
1
1
  import type { TransactionController } from "../index.mjs";
2
- import type { NestedTransactionMetadata, PublishHook } from "../types.mjs";
2
+ import type { PublishHook } from "../types.mjs";
3
3
  /**
4
4
  * Custom publish logic that also publishes additional transactions in an batch.
5
5
  * Requires the batch to be successful to resolve.
6
6
  */
7
7
  export declare class ExtraTransactionsPublishHook {
8
8
  #private;
9
- constructor({ addTransactionBatch, transactions, }: {
9
+ constructor({ addTransactionBatch, }: {
10
10
  addTransactionBatch: TransactionController['addTransactionBatch'];
11
- transactions: NestedTransactionMetadata[];
12
11
  });
13
12
  /**
14
13
  * @returns The publish hook function.
@@ -1 +1 @@
1
- {"version":3,"file":"ExtraTransactionsPublishHook.d.mts","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAW;AAEhD,OAAO,KAAK,EAEV,yBAAyB,EACzB,WAAW,EAIZ,qBAAiB;AAOlB;;;GAGG;AACH,qBAAa,4BAA4B;;gBAK3B,EACV,mBAAmB,EACnB,YAAY,GACb,EAAE;QACD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;QAClE,YAAY,EAAE,yBAAyB,EAAE,CAAC;KAC3C;IAKD;;OAEG;IACH,OAAO,IAAI,WAAW;CA4EvB"}
1
+ {"version":3,"file":"ExtraTransactionsPublishHook.d.mts","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAW;AAEhD,OAAO,KAAK,EAEV,WAAW,EAIZ,qBAAiB;AAOlB;;;GAGG;AACH,qBAAa,4BAA4B;;gBAG3B,EACV,mBAAmB,GACpB,EAAE;QACD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;KACnE;IAID;;OAEG;IACH,OAAO,IAAI,WAAW;CA2GvB"}
@@ -9,7 +9,7 @@ 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 _ExtraTransactionsPublishHook_instances, _ExtraTransactionsPublishHook_addTransactionBatch, _ExtraTransactionsPublishHook_transactions, _ExtraTransactionsPublishHook_hook;
12
+ var _ExtraTransactionsPublishHook_instances, _ExtraTransactionsPublishHook_addTransactionBatch, _ExtraTransactionsPublishHook_hook;
13
13
  import { createDeferredPromise, createModuleLogger } from "@metamask/utils";
14
14
  import { projectLogger } from "../logger.mjs";
15
15
  const log = createModuleLogger(projectLogger, 'extra-transactions-publish-hook');
@@ -18,12 +18,10 @@ const log = createModuleLogger(projectLogger, 'extra-transactions-publish-hook')
18
18
  * Requires the batch to be successful to resolve.
19
19
  */
20
20
  export class ExtraTransactionsPublishHook {
21
- constructor({ addTransactionBatch, transactions, }) {
21
+ constructor({ addTransactionBatch, }) {
22
22
  _ExtraTransactionsPublishHook_instances.add(this);
23
23
  _ExtraTransactionsPublishHook_addTransactionBatch.set(this, void 0);
24
- _ExtraTransactionsPublishHook_transactions.set(this, void 0);
25
24
  __classPrivateFieldSet(this, _ExtraTransactionsPublishHook_addTransactionBatch, addTransactionBatch, "f");
26
- __classPrivateFieldSet(this, _ExtraTransactionsPublishHook_transactions, transactions, "f");
27
25
  }
28
26
  /**
29
27
  * @returns The publish hook function.
@@ -32,9 +30,9 @@ export class ExtraTransactionsPublishHook {
32
30
  return __classPrivateFieldGet(this, _ExtraTransactionsPublishHook_instances, "m", _ExtraTransactionsPublishHook_hook).bind(this);
33
31
  }
34
32
  }
35
- _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransactionsPublishHook_transactions = new WeakMap(), _ExtraTransactionsPublishHook_instances = new WeakSet(), _ExtraTransactionsPublishHook_hook = async function _ExtraTransactionsPublishHook_hook(transactionMeta, signedTx) {
33
+ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransactionsPublishHook_instances = new WeakSet(), _ExtraTransactionsPublishHook_hook = async function _ExtraTransactionsPublishHook_hook(transactionMeta, signedTx) {
36
34
  log('Publishing transaction as batch', { transactionMeta, signedTx });
37
- const { id, networkClientId, txParams } = transactionMeta;
35
+ const { batchTransactions, batchTransactionsOptions, id, networkClientId, txParams, } = transactionMeta;
38
36
  const from = txParams.from;
39
37
  const to = txParams.to;
40
38
  const data = txParams.data;
@@ -55,7 +53,7 @@ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransac
55
53
  to,
56
54
  value,
57
55
  };
58
- const firstTransaction = {
56
+ const mainTransaction = {
59
57
  existingTransaction: {
60
58
  id,
61
59
  onPublish,
@@ -63,29 +61,45 @@ _ExtraTransactionsPublishHook_addTransactionBatch = new WeakMap(), _ExtraTransac
63
61
  },
64
62
  params: firstParams,
65
63
  };
66
- const extraTransactions = __classPrivateFieldGet(this, _ExtraTransactionsPublishHook_transactions, "f").map((transaction) => {
67
- const { type, ...rest } = transaction;
64
+ const extraTransactions = (batchTransactions ?? []).map((transaction) => {
65
+ const { isAfter, type, ...rest } = transaction;
68
66
  return {
67
+ isAfter,
69
68
  params: rest,
70
69
  type,
71
70
  };
72
71
  });
72
+ const beforeTransactions = extraTransactions
73
+ .filter((transaction) => transaction.isAfter === false)
74
+ .map(({ isAfter, ...rest }) => ({
75
+ ...rest,
76
+ }));
77
+ const afterTransactions = extraTransactions
78
+ .filter((transaction) => transaction.isAfter === undefined || transaction.isAfter)
79
+ .map(({ isAfter, ...rest }) => ({
80
+ ...rest,
81
+ }));
73
82
  const transactions = [
74
- firstTransaction,
75
- ...extraTransactions,
83
+ ...beforeTransactions,
84
+ mainTransaction,
85
+ ...afterTransactions,
76
86
  ];
77
87
  log('Adding transaction batch', {
78
88
  from,
79
89
  networkClientId,
80
90
  transactions,
81
91
  });
92
+ const options = batchTransactionsOptions ?? {
93
+ disable7702: true,
94
+ disableHook: false,
95
+ disableSequential: true,
96
+ };
82
97
  await __classPrivateFieldGet(this, _ExtraTransactionsPublishHook_addTransactionBatch, "f").call(this, {
83
98
  from,
84
99
  networkClientId,
100
+ requireApproval: false,
85
101
  transactions,
86
- disable7702: true,
87
- disableHook: false,
88
- disableSequential: true,
102
+ ...options,
89
103
  });
90
104
  return resultPromise.promise;
91
105
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ExtraTransactionsPublishHook.mjs","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAEnB,wBAAwB;AAGzB,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAU1C,MAAM,GAAG,GAAG,kBAAkB,CAC5B,aAAa,EACb,iCAAiC,CAClC,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,4BAA4B;IAKvC,YAAY,EACV,mBAAmB,EACnB,YAAY,GAIb;;QAVQ,oEAAmE;QAEnE,6DAA2C;QASlD,uBAAA,IAAI,qDAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,8CAAiB,YAAY,MAAA,CAAC;IACpC,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,uBAAA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AW,CAAC;QACtC,OAAO;YACL,MAAM,EAAE,IAAI;YACZ,IAAI;SACL,CAAC;IACJ,CAAC,CAAC,CAAC;IAEL,MAAM,YAAY,GAAoC;QACpD,gBAAgB;QAChB,GAAG,iBAAiB;KACrB,CAAC;IAEF,GAAG,CAAC,0BAA0B,EAAE;QAC9B,IAAI;QACJ,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IAEH,MAAM,uBAAA,IAAI,yDAAqB,MAAzB,IAAI,EAAsB;QAC9B,IAAI;QACJ,eAAe;QACf,YAAY;QACZ,WAAW,EAAE,IAAI;QACjB,WAAW,EAAE,KAAK;QAClB,iBAAiB,EAAE,IAAI;KACxB,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC,OAAO,CAAC;AAC/B,CAAC","sourcesContent":["import {\n createDeferredPromise,\n createModuleLogger,\n type Hex,\n} from '@metamask/utils';\n\nimport type { TransactionController } from '..';\nimport { projectLogger } from '../logger';\nimport type {\n BatchTransactionParams,\n NestedTransactionMetadata,\n PublishHook,\n PublishHookResult,\n TransactionBatchSingleRequest,\n TransactionMeta,\n} from '../types';\n\nconst log = createModuleLogger(\n projectLogger,\n 'extra-transactions-publish-hook',\n);\n\n/**\n * Custom publish logic that also publishes additional transactions in an batch.\n * Requires the batch to be successful to resolve.\n */\nexport class ExtraTransactionsPublishHook {\n readonly #addTransactionBatch: TransactionController['addTransactionBatch'];\n\n readonly #transactions: NestedTransactionMetadata[];\n\n constructor({\n addTransactionBatch,\n transactions,\n }: {\n addTransactionBatch: TransactionController['addTransactionBatch'];\n transactions: NestedTransactionMetadata[];\n }) {\n this.#addTransactionBatch = addTransactionBatch;\n this.#transactions = transactions;\n }\n\n /**\n * @returns The publish hook function.\n */\n getHook(): PublishHook {\n return this.#hook.bind(this);\n }\n\n async #hook(\n transactionMeta: TransactionMeta,\n signedTx: string,\n ): Promise<PublishHookResult> {\n log('Publishing transaction as batch', { transactionMeta, signedTx });\n\n const { id, networkClientId, txParams } = transactionMeta;\n const from = txParams.from as Hex;\n const to = txParams.to as Hex | undefined;\n const data = txParams.data as Hex | undefined;\n const value = txParams.value as Hex | undefined;\n const gas = txParams.gas as Hex | undefined;\n const maxFeePerGas = txParams.maxFeePerGas as Hex | undefined;\n const maxPriorityFeePerGas = txParams.maxPriorityFeePerGas as\n | Hex\n | undefined;\n const signedTransaction = signedTx as Hex;\n const resultPromise = createDeferredPromise<PublishHookResult>();\n\n const onPublish = ({ transactionHash }: { transactionHash?: string }) => {\n resultPromise.resolve({ transactionHash });\n };\n\n const firstParams: BatchTransactionParams = {\n data,\n gas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n to,\n value,\n };\n\n const firstTransaction: TransactionBatchSingleRequest = {\n existingTransaction: {\n id,\n onPublish,\n signedTransaction,\n },\n params: firstParams,\n };\n\n const extraTransactions: TransactionBatchSingleRequest[] =\n this.#transactions.map((transaction) => {\n const { type, ...rest } = transaction;\n return {\n params: rest,\n type,\n };\n });\n\n const transactions: TransactionBatchSingleRequest[] = [\n firstTransaction,\n ...extraTransactions,\n ];\n\n log('Adding transaction batch', {\n from,\n networkClientId,\n transactions,\n });\n\n await this.#addTransactionBatch({\n from,\n networkClientId,\n transactions,\n disable7702: true,\n disableHook: false,\n disableSequential: true,\n });\n\n return resultPromise.promise;\n }\n}\n"]}
1
+ {"version":3,"file":"ExtraTransactionsPublishHook.mjs","sourceRoot":"","sources":["../../src/hooks/ExtraTransactionsPublishHook.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAEnB,wBAAwB;AAGzB,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAS1C,MAAM,GAAG,GAAG,kBAAkB,CAC5B,aAAa,EACb,iCAAiC,CAClC,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,4BAA4B;IAGvC,YAAY,EACV,mBAAmB,GAGpB;;QANQ,oEAAmE;QAO1E,uBAAA,IAAI,qDAAwB,mBAAmB,MAAA,CAAC;IAClD,C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\n createModuleLogger,\n type Hex,\n} from '@metamask/utils';\n\nimport type { TransactionController } from '..';\nimport { projectLogger } from '../logger';\nimport type {\n BatchTransactionParams,\n PublishHook,\n PublishHookResult,\n TransactionBatchSingleRequest,\n TransactionMeta,\n} from '../types';\n\nconst log = createModuleLogger(\n projectLogger,\n 'extra-transactions-publish-hook',\n);\n\n/**\n * Custom publish logic that also publishes additional transactions in an batch.\n * Requires the batch to be successful to resolve.\n */\nexport class ExtraTransactionsPublishHook {\n readonly #addTransactionBatch: TransactionController['addTransactionBatch'];\n\n constructor({\n addTransactionBatch,\n }: {\n addTransactionBatch: TransactionController['addTransactionBatch'];\n }) {\n this.#addTransactionBatch = addTransactionBatch;\n }\n\n /**\n * @returns The publish hook function.\n */\n getHook(): PublishHook {\n return this.#hook.bind(this);\n }\n\n async #hook(\n transactionMeta: TransactionMeta,\n signedTx: string,\n ): Promise<PublishHookResult> {\n log('Publishing transaction as batch', { transactionMeta, signedTx });\n\n const {\n batchTransactions,\n batchTransactionsOptions,\n id,\n networkClientId,\n txParams,\n } = transactionMeta;\n\n const from = txParams.from as Hex;\n const to = txParams.to as Hex | undefined;\n const data = txParams.data as Hex | undefined;\n const value = txParams.value as Hex | undefined;\n const gas = txParams.gas as Hex | undefined;\n const maxFeePerGas = txParams.maxFeePerGas as Hex | undefined;\n\n const maxPriorityFeePerGas = txParams.maxPriorityFeePerGas as\n | Hex\n | undefined;\n\n const signedTransaction = signedTx as Hex;\n const resultPromise = createDeferredPromise<PublishHookResult>();\n\n const onPublish = ({ transactionHash }: { transactionHash?: string }) => {\n resultPromise.resolve({ transactionHash });\n };\n\n const firstParams: BatchTransactionParams = {\n data,\n gas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n to,\n value,\n };\n\n const mainTransaction: TransactionBatchSingleRequest = {\n existingTransaction: {\n id,\n onPublish,\n signedTransaction,\n },\n params: firstParams,\n };\n\n const extraTransactions = (batchTransactions ?? []).map((transaction) => {\n const { isAfter, type, ...rest } = transaction;\n return {\n isAfter,\n params: rest,\n type,\n };\n });\n\n const beforeTransactions: TransactionBatchSingleRequest[] =\n extraTransactions\n .filter((transaction) => transaction.isAfter === false)\n .map(({ isAfter, ...rest }) => ({\n ...rest,\n }));\n\n const afterTransactions: TransactionBatchSingleRequest[] = extraTransactions\n .filter(\n (transaction) =>\n transaction.isAfter === undefined || transaction.isAfter,\n )\n .map(({ isAfter, ...rest }) => ({\n ...rest,\n }));\n\n const transactions: TransactionBatchSingleRequest[] = [\n ...beforeTransactions,\n mainTransaction,\n ...afterTransactions,\n ];\n\n log('Adding transaction batch', {\n from,\n networkClientId,\n transactions,\n });\n\n const options = batchTransactionsOptions ?? {\n disable7702: true,\n disableHook: false,\n disableSequential: true,\n };\n\n await this.#addTransactionBatch({\n from,\n networkClientId,\n requireApproval: false,\n transactions,\n ...options,\n });\n\n return resultPromise.promise;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA4BA,qEAIiC;AAH/B,oHAAA,WAAW,OAAA;AACX,sHAAA,aAAa,OAAA;AACb,8HAAA,qBAAqB,OAAA;AAmDvB,qCAWiB;AAVf,4GAAA,mBAAmB,OAAA;AACnB,2GAAA,kBAAkB,OAAA;AAClB,4GAAA,mBAAmB,OAAA;AACnB,gHAAA,uBAAuB,OAAA;AACvB,iHAAA,wBAAwB,OAAA;AACxB,gHAAA,uBAAuB,OAAA;AACvB,0GAAA,iBAAiB,OAAA;AACjB,wGAAA,eAAe,OAAA;AACf,qGAAA,YAAY,OAAA;AACZ,qGAAA,YAAY,OAAA;AAEd,+CAGyB;AAFvB,8HAAA,mCAAmC,OAAA;AACnC,yHAAA,8BAA8B,OAAA;AAEhC,iEAAoE;AAA3D,4HAAA,wBAAwB,OAAA;AACjC,iDAAwD;AAA/C,gHAAA,oBAAoB,OAAA;AAC7B,2CAGuB;AAFrB,6GAAA,oBAAoB,OAAA;AACpB,mHAAA,0BAA0B,OAAA;AAE5B,6CAAwC;AAA/B,sGAAA,SAAS,OAAA;AAClB,uGAAgI;AAAvH,+JAAA,mBAAmB,OAA6C;AACzE,+CAA2C;AAAlC,mGAAA,QAAQ,OAAA","sourcesContent":["export type {\n MethodData,\n Result,\n TransactionControllerActions,\n TransactionControllerEvents,\n TransactionControllerEstimateGasAction,\n TransactionControllerGetStateAction,\n TransactionControllerIncomingTransactionsReceivedEvent,\n TransactionControllerPostTransactionBalanceUpdatedEvent,\n TransactionControllerSpeedupTransactionAddedEvent,\n TransactionControllerState,\n TransactionControllerStateChangeEvent,\n TransactionControllerTransactionApprovedEvent,\n TransactionControllerTransactionConfirmedEvent,\n TransactionControllerTransactionDroppedEvent,\n TransactionControllerTransactionFailedEvent,\n TransactionControllerTransactionFinishedEvent,\n TransactionControllerTransactionNewSwapApprovalEvent,\n TransactionControllerTransactionNewSwapEvent,\n TransactionControllerTransactionPublishingSkipped,\n TransactionControllerTransactionRejectedEvent,\n TransactionControllerTransactionStatusUpdatedEvent,\n TransactionControllerTransactionSubmittedEvent,\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionControllerUpdateCustodialTransactionAction,\n TransactionControllerMessenger,\n TransactionControllerOptions,\n} from './TransactionController';\nexport {\n CANCEL_RATE,\n SPEED_UP_RATE,\n TransactionController,\n} from './TransactionController';\nexport type {\n AfterAddHook,\n AfterSimulateHook,\n Authorization,\n AuthorizationList,\n BatchTransactionParams,\n BeforeSignHook,\n DappSuggestedGasFees,\n DefaultGasEstimates,\n FeeMarketEIP1559Values,\n FeeMarketGasFeeEstimateForLevel,\n FeeMarketGasFeeEstimates,\n GasFeeEstimates,\n GasFeeToken,\n GasPriceGasFeeEstimates,\n GasPriceValue,\n InferTransactionTypeResult,\n IsAtomicBatchSupportedRequest,\n IsAtomicBatchSupportedResult,\n IsAtomicBatchSupportedResultEntry,\n LegacyGasFeeEstimates,\n Log,\n NestedTransactionMetadata,\n PublishBatchHook,\n PublishBatchHookRequest,\n PublishBatchHookResult,\n PublishBatchHookTransaction,\n PublishHook,\n PublishHookResult,\n SavedGasFees,\n SecurityAlertResponse,\n SecurityProviderRequest,\n SendFlowHistoryEntry,\n SimulationBalanceChange,\n SimulationData,\n SimulationError,\n SimulationToken,\n SimulationTokenBalanceChange,\n TransactionBatchMeta,\n TransactionBatchRequest,\n TransactionBatchResult,\n TransactionError,\n TransactionHistory,\n TransactionHistoryEntry,\n TransactionMeta,\n TransactionParams,\n TransactionReceipt,\n ValidateSecurityRequest,\n} from './types';\nexport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n SimulationErrorCode,\n SimulationTokenStandard,\n TransactionContainerType,\n TransactionEnvelopeType,\n TransactionStatus,\n TransactionType,\n UserFeeLevel,\n WalletDevice,\n} from './types';\nexport {\n DISPLAYED_TRANSACTION_HISTORY_PATHS,\n MAX_TRANSACTION_HISTORY_LENGTH,\n} from './utils/history';\nexport { determineTransactionType } from './utils/transaction-type';\nexport { mergeGasFeeEstimates } from './utils/gas-flow';\nexport {\n isEIP1559Transaction,\n normalizeTransactionParams,\n} from './utils/utils';\nexport { CHAIN_IDS } from './constants';\nexport { SUPPORTED_CHAIN_IDS as INCOMING_TRANSACTIONS_SUPPORTED_CHAIN_IDS } from './helpers/AccountsApiRemoteTransactionSource';\nexport { HARDFORK } from './utils/prepare';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA4BA,qEAIiC;AAH/B,oHAAA,WAAW,OAAA;AACX,sHAAA,aAAa,OAAA;AACb,8HAAA,qBAAqB,OAAA;AAsDvB,qCAWiB;AAVf,4GAAA,mBAAmB,OAAA;AACnB,2GAAA,kBAAkB,OAAA;AAClB,4GAAA,mBAAmB,OAAA;AACnB,gHAAA,uBAAuB,OAAA;AACvB,iHAAA,wBAAwB,OAAA;AACxB,gHAAA,uBAAuB,OAAA;AACvB,0GAAA,iBAAiB,OAAA;AACjB,wGAAA,eAAe,OAAA;AACf,qGAAA,YAAY,OAAA;AACZ,qGAAA,YAAY,OAAA;AAEd,+CAGyB;AAFvB,8HAAA,mCAAmC,OAAA;AACnC,yHAAA,8BAA8B,OAAA;AAEhC,iEAAoE;AAA3D,4HAAA,wBAAwB,OAAA;AACjC,iDAAwD;AAA/C,gHAAA,oBAAoB,OAAA;AAC7B,2CAGuB;AAFrB,6GAAA,oBAAoB,OAAA;AACpB,mHAAA,0BAA0B,OAAA;AAE5B,6CAAwC;AAA/B,sGAAA,SAAS,OAAA;AAClB,uGAAgI;AAAvH,+JAAA,mBAAmB,OAA6C;AACzE,+CAA2C;AAAlC,mGAAA,QAAQ,OAAA","sourcesContent":["export type {\n MethodData,\n Result,\n TransactionControllerActions,\n TransactionControllerEvents,\n TransactionControllerEstimateGasAction,\n TransactionControllerGetStateAction,\n TransactionControllerIncomingTransactionsReceivedEvent,\n TransactionControllerPostTransactionBalanceUpdatedEvent,\n TransactionControllerSpeedupTransactionAddedEvent,\n TransactionControllerState,\n TransactionControllerStateChangeEvent,\n TransactionControllerTransactionApprovedEvent,\n TransactionControllerTransactionConfirmedEvent,\n TransactionControllerTransactionDroppedEvent,\n TransactionControllerTransactionFailedEvent,\n TransactionControllerTransactionFinishedEvent,\n TransactionControllerTransactionNewSwapApprovalEvent,\n TransactionControllerTransactionNewSwapEvent,\n TransactionControllerTransactionPublishingSkipped,\n TransactionControllerTransactionRejectedEvent,\n TransactionControllerTransactionStatusUpdatedEvent,\n TransactionControllerTransactionSubmittedEvent,\n TransactionControllerUnapprovedTransactionAddedEvent,\n TransactionControllerUpdateCustodialTransactionAction,\n TransactionControllerMessenger,\n TransactionControllerOptions,\n} from './TransactionController';\nexport {\n CANCEL_RATE,\n SPEED_UP_RATE,\n TransactionController,\n} from './TransactionController';\nexport type {\n AfterAddHook,\n AfterSimulateHook,\n Authorization,\n AuthorizationList,\n BatchTransaction,\n BatchTransactionParams,\n BeforeSignHook,\n DappSuggestedGasFees,\n DefaultGasEstimates,\n FeeMarketEIP1559Values,\n FeeMarketGasFeeEstimateForLevel,\n FeeMarketGasFeeEstimates,\n GasFeeEstimates,\n GasFeeToken,\n GasPriceGasFeeEstimates,\n GasPriceValue,\n GetSimulationConfig,\n InferTransactionTypeResult,\n IsAtomicBatchSupportedRequest,\n IsAtomicBatchSupportedResult,\n IsAtomicBatchSupportedResultEntry,\n LegacyGasFeeEstimates,\n Log,\n MetamaskPayMetadata,\n NestedTransactionMetadata,\n PublishBatchHook,\n PublishBatchHookRequest,\n PublishBatchHookResult,\n PublishBatchHookTransaction,\n PublishHook,\n PublishHookResult,\n SavedGasFees,\n SecurityAlertResponse,\n SecurityProviderRequest,\n SendFlowHistoryEntry,\n SimulationBalanceChange,\n SimulationData,\n SimulationError,\n SimulationToken,\n SimulationTokenBalanceChange,\n TransactionBatchMeta,\n TransactionBatchRequest,\n TransactionBatchResult,\n TransactionError,\n TransactionHistory,\n TransactionHistoryEntry,\n TransactionMeta,\n TransactionParams,\n TransactionReceipt,\n ValidateSecurityRequest,\n} from './types';\nexport {\n GasFeeEstimateLevel,\n GasFeeEstimateType,\n SimulationErrorCode,\n SimulationTokenStandard,\n TransactionContainerType,\n TransactionEnvelopeType,\n TransactionStatus,\n TransactionType,\n UserFeeLevel,\n WalletDevice,\n} from './types';\nexport {\n DISPLAYED_TRANSACTION_HISTORY_PATHS,\n MAX_TRANSACTION_HISTORY_LENGTH,\n} from './utils/history';\nexport { determineTransactionType } from './utils/transaction-type';\nexport { mergeGasFeeEstimates } from './utils/gas-flow';\nexport {\n isEIP1559Transaction,\n normalizeTransactionParams,\n} from './utils/utils';\nexport { CHAIN_IDS } from './constants';\nexport { SUPPORTED_CHAIN_IDS as INCOMING_TRANSACTIONS_SUPPORTED_CHAIN_IDS } from './helpers/AccountsApiRemoteTransactionSource';\nexport { HARDFORK } from './utils/prepare';\n"]}