@metamask/transaction-controller 51.0.0 → 52.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 (58) hide show
  1. package/CHANGELOG.md +24 -1
  2. package/dist/TransactionController.cjs +30 -28
  3. package/dist/TransactionController.cjs.map +1 -1
  4. package/dist/TransactionController.d.cts +6 -3
  5. package/dist/TransactionController.d.cts.map +1 -1
  6. package/dist/TransactionController.d.mts +6 -3
  7. package/dist/TransactionController.d.mts.map +1 -1
  8. package/dist/TransactionController.mjs +31 -29
  9. package/dist/TransactionController.mjs.map +1 -1
  10. package/dist/helpers/AccountsApiRemoteTransactionSource.cjs +3 -8
  11. package/dist/helpers/AccountsApiRemoteTransactionSource.cjs.map +1 -1
  12. package/dist/helpers/AccountsApiRemoteTransactionSource.d.cts.map +1 -1
  13. package/dist/helpers/AccountsApiRemoteTransactionSource.d.mts.map +1 -1
  14. package/dist/helpers/AccountsApiRemoteTransactionSource.mjs +3 -8
  15. package/dist/helpers/AccountsApiRemoteTransactionSource.mjs.map +1 -1
  16. package/dist/helpers/GasFeePoller.cjs +73 -1
  17. package/dist/helpers/GasFeePoller.cjs.map +1 -1
  18. package/dist/helpers/GasFeePoller.d.cts +20 -2
  19. package/dist/helpers/GasFeePoller.d.cts.map +1 -1
  20. package/dist/helpers/GasFeePoller.d.mts +20 -2
  21. package/dist/helpers/GasFeePoller.d.mts.map +1 -1
  22. package/dist/helpers/GasFeePoller.mjs +72 -1
  23. package/dist/helpers/GasFeePoller.mjs.map +1 -1
  24. package/dist/helpers/IncomingTransactionHelper.cjs +4 -12
  25. package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
  26. package/dist/helpers/IncomingTransactionHelper.d.cts +1 -3
  27. package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
  28. package/dist/helpers/IncomingTransactionHelper.d.mts +1 -3
  29. package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
  30. package/dist/helpers/IncomingTransactionHelper.mjs +4 -12
  31. package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
  32. package/dist/types.cjs.map +1 -1
  33. package/dist/types.d.cts +0 -4
  34. package/dist/types.d.cts.map +1 -1
  35. package/dist/types.d.mts +0 -4
  36. package/dist/types.d.mts.map +1 -1
  37. package/dist/types.mjs.map +1 -1
  38. package/dist/utils/feature-flags.cjs +2 -2
  39. package/dist/utils/feature-flags.cjs.map +1 -1
  40. package/dist/utils/feature-flags.d.cts +2 -2
  41. package/dist/utils/feature-flags.d.mts +2 -2
  42. package/dist/utils/feature-flags.mjs +2 -2
  43. package/dist/utils/feature-flags.mjs.map +1 -1
  44. package/dist/utils/gas.cjs +108 -8
  45. package/dist/utils/gas.cjs.map +1 -1
  46. package/dist/utils/gas.d.cts +16 -5
  47. package/dist/utils/gas.d.cts.map +1 -1
  48. package/dist/utils/gas.d.mts +16 -5
  49. package/dist/utils/gas.d.mts.map +1 -1
  50. package/dist/utils/gas.mjs +108 -8
  51. package/dist/utils/gas.mjs.map +1 -1
  52. package/dist/utils/simulation-api.cjs.map +1 -1
  53. package/dist/utils/simulation-api.d.cts +9 -5
  54. package/dist/utils/simulation-api.d.cts.map +1 -1
  55. package/dist/utils/simulation-api.d.mts +9 -5
  56. package/dist/utils/simulation-api.d.mts.map +1 -1
  57. package/dist/utils/simulation-api.mjs.map +1 -1
  58. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAAA,2CAA+D;AAE/D,+CAA+C;AAC/C,0CAA0C;AAG7B,QAAA,yBAAyB,GAAG,4BAA4B,CAAC;AACzD,QAAA,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,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,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
1
+ {"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAAA,2CAA+D;AAE/D,+CAA+C;AAC/C,0CAA0C;AAG7B,QAAA,yBAAyB,GAAG,4BAA4B,CAAC;AACzD,QAAA,qBAAqB,GAAG,wBAAwB,CAAC;AAE9D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AA8BpC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AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { type Hex } from "@metamask/utils";
2
2
  import type { TransactionControllerMessenger } from "../TransactionController.cjs";
3
- export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations-transactions";
4
- export declare const FEATURE_FLAG_EIP_7702 = "confirmations-eip-7702";
3
+ export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations_transactions";
4
+ export declare const FEATURE_FLAG_EIP_7702 = "confirmations_eip_7702";
5
5
  export type TransactionControllerFeatureFlags = {
6
6
  [FEATURE_FLAG_EIP_7702]?: {
7
7
  /**
@@ -1,7 +1,7 @@
1
1
  import { type Hex } from "@metamask/utils";
2
2
  import type { TransactionControllerMessenger } from "../TransactionController.mjs";
3
- export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations-transactions";
4
- export declare const FEATURE_FLAG_EIP_7702 = "confirmations-eip-7702";
3
+ export declare const FEATURE_FLAG_TRANSACTIONS = "confirmations_transactions";
4
+ export declare const FEATURE_FLAG_EIP_7702 = "confirmations_eip_7702";
5
5
  export type TransactionControllerFeatureFlags = {
6
6
  [FEATURE_FLAG_EIP_7702]?: {
7
7
  /**
@@ -1,8 +1,8 @@
1
1
  import { createModuleLogger } from "@metamask/utils";
2
2
  import { isValidSignature } from "./signature.mjs";
3
3
  import { projectLogger } from "../logger.mjs";
4
- export const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';
5
- export const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';
4
+ export const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';
5
+ export const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';
6
6
  const DEFAULT_BATCH_SIZE_LIMIT = 10;
7
7
  const log = createModuleLogger(projectLogger, 'feature-flags');
8
8
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAC/C,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,CAAC,MAAM,yBAAyB,GAAG,4BAA4B,CAAC;AACtE,MAAM,CAAC,MAAM,qBAAqB,GAAG,wBAAw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ACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations-transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations-eip-7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
1
+ {"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,gBAAgB,EAAE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yC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,CACL,YAAY,EAAE,CAAC,yBAAyB,CAAC,EAAE,cAAc;QACzD,wBAAwB,CACzB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,SAAyC;IAEzC,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CACjC,sCAAsC,CACvC,CAAC,kBAAkB,CAAC;IAErB,GAAG,CAAC,yBAAyB,EAAE,YAAY,CAAC,CAAC;IAE7C,OAAO,YAAiD,CAAC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nexport const FEATURE_FLAG_TRANSACTIONS = 'confirmations_transactions';\nexport const FEATURE_FLAG_EIP_7702 = 'confirmations_eip_7702';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\n\nexport type TransactionControllerFeatureFlags = {\n [FEATURE_FLAG_EIP_7702]?: {\n /**\n * All contracts that support EIP-7702 batch transactions.\n * Keyed by chain ID.\n * First entry in each array is the contract that standard EOAs will be upgraded to.\n */\n contracts?: Record<\n Hex,\n {\n /** Address of the smart contract. */\n address: Hex;\n\n /** Signature to verify the contract is authentic. */\n signature: Hex;\n }[]\n >;\n\n /** Chains enabled for EIP-7702 batch transactions. */\n supportedChains?: Hex[];\n };\n\n [FEATURE_FLAG_TRANSACTIONS]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n };\n};\n\nconst log = createModuleLogger(projectLogger, 'feature-flags');\n\n/**\n * Retrieves the supported EIP-7702 chains.\n *\n * @param messenger - The controller messenger instance.\n * @returns The supported chains.\n */\nexport function getEIP7702SupportedChains(\n messenger: TransactionControllerMessenger,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n return featureFlags?.[FEATURE_FLAG_EIP_7702]?.supportedChains ?? [];\n}\n\n/**\n * Retrieves the supported EIP-7702 contract addresses for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The supported contract addresses.\n */\nexport function getEIP7702ContractAddresses(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex[] {\n const featureFlags = getFeatureFlags(messenger);\n\n const contracts =\n featureFlags?.[FEATURE_FLAG_EIP_7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, chainId],\n contract.signature,\n publicKey,\n ),\n )\n .map((contract) => contract.address);\n}\n\n/**\n * Retrieves the EIP-7702 upgrade contract address.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @param publicKey - The public key used to validate the contract authenticity.\n * @returns The upgrade contract address.\n */\nexport function getEIP7702UpgradeContractAddress(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n publicKey: Hex,\n): Hex | undefined {\n return getEIP7702ContractAddresses(chainId, messenger, publicKey)?.[0];\n}\n\n/**\n * Retrieves the batch size limit.\n * Defaults to 10 if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The batch size limit.\n */\nexport function getBatchSizeLimit(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n return (\n featureFlags?.[FEATURE_FLAG_TRANSACTIONS]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the relevant feature flags from the remote feature flag controller.\n *\n * @param messenger - The messenger instance.\n * @returns The feature flags.\n */\nfunction getFeatureFlags(\n messenger: TransactionControllerMessenger,\n): TransactionControllerFeatureFlags {\n const featureFlags = messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags;\n\n log('Retrieved feature flags', featureFlags);\n\n return featureFlags as TransactionControllerFeatureFlags;\n}\n"]}
@@ -1,15 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addGasBuffer = exports.estimateGas = exports.updateGas = exports.MAX_GAS_BLOCK_PERCENT = exports.GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = exports.DEFAULT_GAS_MULTIPLIER = exports.FIXED_GAS = exports.log = void 0;
3
+ exports.addGasBuffer = exports.estimateGas = exports.updateGas = exports.DUMMY_AUTHORIZATION_SIGNATURE = exports.INTRINSIC_GAS = exports.MAX_GAS_BLOCK_PERCENT = exports.GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = exports.DEFAULT_GAS_MULTIPLIER = exports.FIXED_GAS = exports.log = void 0;
4
4
  const controller_utils_1 = require("@metamask/controller-utils");
5
5
  const utils_1 = require("@metamask/utils");
6
+ const eip7702_1 = require("./eip7702.cjs");
7
+ const simulation_api_1 = require("./simulation-api.cjs");
6
8
  const constants_1 = require("../constants.cjs");
7
9
  const logger_1 = require("../logger.cjs");
10
+ const types_1 = require("../types.cjs");
8
11
  exports.log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'gas');
9
12
  exports.FIXED_GAS = '0x5208';
10
13
  exports.DEFAULT_GAS_MULTIPLIER = 1.5;
11
14
  exports.GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;
12
15
  exports.MAX_GAS_BLOCK_PERCENT = 90;
16
+ exports.INTRINSIC_GAS = 21000;
17
+ exports.DUMMY_AUTHORIZATION_SIGNATURE = '0x1111111111111111111111111111111111111111111111111111111111111111';
13
18
  /**
14
19
  * Populate the gas properties of the provided transaction meta.
15
20
  *
@@ -35,25 +40,39 @@ exports.updateGas = updateGas;
35
40
  * Estimate the gas for the provided transaction parameters.
36
41
  * If the gas estimate fails, the fallback value is returned.
37
42
  *
38
- * @param txParams - The transaction parameters.
39
- * @param ethQuery - The EthQuery instance to interact with the network.
43
+ * @param options - The options object.
44
+ * @param options.chainId - The chain ID of the transaction.
45
+ * @param options.ethQuery - The EthQuery instance to interact with the network.
46
+ * @param options.isSimulationEnabled - Whether the simulation is enabled.
47
+ * @param options.txParams - The transaction parameters.
40
48
  * @returns The estimated gas and related info.
41
49
  */
42
- async function estimateGas(txParams, ethQuery) {
50
+ async function estimateGas({ chainId, ethQuery, isSimulationEnabled, txParams, }) {
43
51
  const request = { ...txParams };
44
- const { data, value } = request;
52
+ const { authorizationList, data, from, value, to } = request;
45
53
  const { gasLimit: blockGasLimit, number: blockNumber } = await getLatestBlock(ethQuery);
46
54
  const blockGasLimitBN = (0, controller_utils_1.hexToBN)(blockGasLimit);
47
55
  const fallback = (0, controller_utils_1.BNToHex)((0, controller_utils_1.fractionBN)(blockGasLimitBN, exports.GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT, 100));
48
56
  request.data = data ? (0, utils_1.add0x)(data) : data;
49
57
  request.value = value || '0x0';
58
+ request.authorizationList = normalizeAuthorizationList(request.authorizationList, chainId);
50
59
  delete request.gasPrice;
51
60
  delete request.maxFeePerGas;
52
61
  delete request.maxPriorityFeePerGas;
53
62
  let estimatedGas = fallback;
54
63
  let simulationFails;
64
+ const isUpgradeWithDataToSelf = txParams.type === types_1.TransactionEnvelopeType.setCode &&
65
+ authorizationList?.length &&
66
+ data &&
67
+ data !== '0x' &&
68
+ from?.toLowerCase() === to?.toLowerCase();
55
69
  try {
56
- estimatedGas = await (0, controller_utils_1.query)(ethQuery, 'estimateGas', [request]);
70
+ if (isSimulationEnabled && isUpgradeWithDataToSelf) {
71
+ estimatedGas = await estimateGasUpgradeWithDataToSelf(request, ethQuery, chainId);
72
+ }
73
+ else {
74
+ estimatedGas = await (0, controller_utils_1.query)(ethQuery, 'estimateGas', [request]);
75
+ }
57
76
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
77
  }
59
78
  catch (error) {
@@ -109,7 +128,7 @@ exports.addGasBuffer = addGasBuffer;
109
128
  * @returns The final gas value and the estimate used.
110
129
  */
111
130
  async function getGas(request) {
112
- const { isCustomNetwork, chainId, txMeta } = request;
131
+ const { chainId, isCustomNetwork, isSimulationEnabled, txMeta } = request;
113
132
  if (txMeta.txParams.gas) {
114
133
  (0, exports.log)('Using value from request', txMeta.txParams.gas);
115
134
  return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];
@@ -118,7 +137,12 @@ async function getGas(request) {
118
137
  (0, exports.log)('Using fixed value', exports.FIXED_GAS);
119
138
  return [exports.FIXED_GAS, undefined, exports.FIXED_GAS];
120
139
  }
121
- const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(txMeta.txParams, request.ethQuery);
140
+ const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas({
141
+ chainId: request.chainId,
142
+ ethQuery: request.ethQuery,
143
+ isSimulationEnabled,
144
+ txParams: txMeta.txParams,
145
+ });
122
146
  if (isCustomNetwork || simulationFails) {
123
147
  (0, exports.log)(isCustomNetwork
124
148
  ? 'Using original estimate as custom network'
@@ -165,4 +189,80 @@ async function getCode(ethQuery, address) {
165
189
  async function getLatestBlock(ethQuery) {
166
190
  return await (0, controller_utils_1.query)(ethQuery, 'getBlockByNumber', ['latest', false]);
167
191
  }
192
+ /**
193
+ * Estimate the gas for a type 4 transaction.
194
+ *
195
+ * @param txParams - The transaction parameters.
196
+ * @param ethQuery - The EthQuery instance to interact with the network.
197
+ * @param chainId - The chain ID of the transaction.
198
+ * @returns The estimated gas.
199
+ */
200
+ async function estimateGasUpgradeWithDataToSelf(txParams, ethQuery, chainId) {
201
+ const upgradeGas = await (0, controller_utils_1.query)(ethQuery, 'estimateGas', [
202
+ {
203
+ ...txParams,
204
+ data: '0x',
205
+ },
206
+ ]);
207
+ (0, exports.log)('Upgrade only gas', upgradeGas);
208
+ const delegationAddress = txParams.authorizationList?.[0].address;
209
+ const executeGas = await simulateGas({
210
+ chainId: chainId,
211
+ delegationAddress,
212
+ transaction: txParams,
213
+ });
214
+ (0, exports.log)('Execute gas', executeGas);
215
+ const total = (0, controller_utils_1.BNToHex)((0, controller_utils_1.hexToBN)(upgradeGas).add((0, controller_utils_1.hexToBN)(executeGas)).subn(exports.INTRINSIC_GAS));
216
+ (0, exports.log)('Total type 4 gas', total);
217
+ return total;
218
+ }
219
+ /**
220
+ * Simulate the required gas using the simulation API.
221
+ *
222
+ * @param options - The options object.
223
+ * @param options.chainId - The chain ID of the transaction.
224
+ * @param options.delegationAddress - The delegation address of the sender to mock.
225
+ * @param options.transaction - The transaction parameters.
226
+ * @returns The simulated gas.
227
+ */
228
+ async function simulateGas({ chainId, delegationAddress, transaction, }) {
229
+ const response = await (0, simulation_api_1.simulateTransactions)(chainId, {
230
+ transactions: [
231
+ {
232
+ to: transaction.to,
233
+ from: transaction.from,
234
+ data: transaction.data,
235
+ value: transaction.value,
236
+ },
237
+ ],
238
+ overrides: {
239
+ [transaction.from]: {
240
+ code: delegationAddress &&
241
+ (eip7702_1.DELEGATION_PREFIX + (0, utils_1.remove0x)(delegationAddress)),
242
+ },
243
+ },
244
+ });
245
+ const gasUsed = response?.transactions?.[0].gasUsed;
246
+ if (!gasUsed) {
247
+ throw new Error('No simulated gas returned');
248
+ }
249
+ return gasUsed;
250
+ }
251
+ /**
252
+ * Populate the authorization list with dummy values.
253
+ *
254
+ * @param authorizationList - The authorization list to prepare.
255
+ * @param chainId - The chain ID to use.
256
+ * @returns The authorization list with dummy values.
257
+ */
258
+ function normalizeAuthorizationList(authorizationList, chainId) {
259
+ return authorizationList?.map((authorization) => ({
260
+ ...authorization,
261
+ chainId: authorization.chainId ?? chainId,
262
+ nonce: authorization.nonce ?? '0x1',
263
+ r: authorization.r ?? exports.DUMMY_AUTHORIZATION_SIGNATURE,
264
+ s: authorization.s ?? exports.DUMMY_AUTHORIZATION_SIGNATURE,
265
+ yParity: authorization.yParity ?? '0x1',
266
+ }));
267
+ }
168
268
  //# sourceMappingURL=gas.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.cjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";;;AAAA,iEAKoC;AAGpC,2CA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AE,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACrD,CAAC;AAED;;;;;GAKG;AACH,KAAK,UAAU,cAAc,CAC3B,QAAkB;IAElB,OAAO,MAAM,IAAA,wBAAK,EAAC,QAAQ,EAAE,kBAAkB,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;AACtE,CAAC","sourcesContent":["import {\n BNToHex,\n fractionBN,\n hexToBN,\n query,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, createModuleLogger } from '@metamask/utils';\n\nimport { GAS_BUFFER_CHAIN_OVERRIDES } from '../constants';\nimport { projectLogger } from '../logger';\nimport type { TransactionMeta, TransactionParams } from '../types';\n\nexport type UpdateGasRequest = {\n ethQuery: EthQuery;\n isCustomNetwork: boolean;\n chainId: Hex;\n txMeta: TransactionMeta;\n};\n\nexport const log = createModuleLogger(projectLogger, 'gas');\n\nexport const FIXED_GAS = '0x5208';\nexport const DEFAULT_GAS_MULTIPLIER = 1.5;\nexport const GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;\nexport const MAX_GAS_BLOCK_PERCENT = 90;\n\n/**\n * Populate the gas properties of the provided transaction meta.\n *\n * @param request - The request object including the necessary parameters.\n */\nexport async function updateGas(request: UpdateGasRequest) {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const [gas, simulationFails, gasLimitNoBuffer] = await getGas(request);\n\n txMeta.txParams.gas = gas;\n txMeta.simulationFails = simulationFails;\n txMeta.gasLimitNoBuffer = gasLimitNoBuffer;\n\n if (!initialParams.gas) {\n txMeta.originalGasEstimate = txMeta.txParams.gas;\n }\n\n if (!txMeta.defaultGasEstimates) {\n txMeta.defaultGasEstimates = {};\n }\n\n txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;\n}\n\n/**\n * Estimate the gas for the provided transaction parameters.\n * If the gas estimate fails, the fallback value is returned.\n *\n * @param txParams - The transaction parameters.\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @returns The estimated gas and related info.\n */\nexport async function estimateGas(\n txParams: TransactionParams,\n ethQuery: EthQuery,\n) {\n const request = { ...txParams };\n const { data, value } = request;\n\n const { gasLimit: blockGasLimit, number: blockNumber } =\n await getLatestBlock(ethQuery);\n\n const blockGasLimitBN = hexToBN(blockGasLimit);\n\n const fallback = BNToHex(\n fractionBN(blockGasLimitBN, GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT, 100),\n );\n\n request.data = data ? add0x(data) : data;\n request.value = value || '0x0';\n\n delete request.gasPrice;\n delete request.maxFeePerGas;\n delete request.maxPriorityFeePerGas;\n\n let estimatedGas = fallback;\n let simulationFails: TransactionMeta['simulationFails'];\n\n try {\n estimatedGas = await query(ethQuery, 'estimateGas', [request]);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n simulationFails = {\n reason: error.message,\n errorKey: error.errorKey,\n debug: {\n blockNumber,\n blockGasLimit,\n },\n };\n\n log('Estimation failed', { ...simulationFails, fallback });\n }\n\n return {\n blockGasLimit,\n estimatedGas,\n simulationFails,\n };\n}\n\n/**\n * Add a buffer to the provided estimated gas.\n * The buffer is calculated based on the block gas limit and a multiplier.\n *\n * @param estimatedGas - The estimated gas.\n * @param blockGasLimit - The block gas limit.\n * @param multiplier - The multiplier to apply to the estimated gas.\n * @returns The gas with the buffer applied.\n */\nexport function addGasBuffer(\n estimatedGas: string,\n blockGasLimit: string,\n multiplier: number,\n) {\n const estimatedGasBN = hexToBN(estimatedGas);\n\n const maxGasBN = fractionBN(\n hexToBN(blockGasLimit),\n MAX_GAS_BLOCK_PERCENT,\n 100,\n );\n\n const paddedGasBN = fractionBN(estimatedGasBN, multiplier * 100, 100);\n\n if (estimatedGasBN.gt(maxGasBN)) {\n const estimatedGasHex = add0x(estimatedGas);\n log('Using estimated value', estimatedGasHex);\n return estimatedGasHex;\n }\n\n if (paddedGasBN.lt(maxGasBN)) {\n const paddedHex = add0x(BNToHex(paddedGasBN));\n log('Using padded estimate', paddedHex, multiplier);\n return paddedHex;\n }\n\n const maxHex = add0x(BNToHex(maxGasBN));\n log('Using 90% of block gas limit', maxHex);\n return maxHex;\n}\n\n/**\n * Determine the gas for the provided request.\n *\n * @param request - The request object including the necessary parameters.\n * @returns The final gas value and the estimate used.\n */\nasync function getGas(\n request: UpdateGasRequest,\n): Promise<[string, TransactionMeta['simulationFails']?, string?]> {\n const { isCustomNetwork, chainId, txMeta } = request;\n\n if (txMeta.txParams.gas) {\n log('Using value from request', txMeta.txParams.gas);\n return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];\n }\n\n if (await requiresFixedGas(request)) {\n log('Using fixed value', FIXED_GAS);\n return [FIXED_GAS, undefined, FIXED_GAS];\n }\n\n const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(\n txMeta.txParams,\n request.ethQuery,\n );\n\n if (isCustomNetwork || simulationFails) {\n log(\n isCustomNetwork\n ? 'Using original estimate as custom network'\n : 'Using original fallback estimate as simulation failed',\n );\n return [estimatedGas, simulationFails, estimatedGas];\n }\n\n const bufferMultiplier =\n GAS_BUFFER_CHAIN_OVERRIDES[\n chainId as keyof typeof GAS_BUFFER_CHAIN_OVERRIDES\n ] ?? DEFAULT_GAS_MULTIPLIER;\n\n const bufferedGas = addGasBuffer(\n estimatedGas,\n blockGasLimit,\n bufferMultiplier,\n );\n\n return [bufferedGas, simulationFails, estimatedGas];\n}\n\n/**\n * Determine if the gas for the provided request should be fixed.\n *\n * @param options - The options object.\n * @param options.ethQuery - The EthQuery instance to interact with the network.\n * @param options.txMeta - The transaction meta object.\n * @param options.isCustomNetwork - Whether the network is a custom network.\n * @returns Whether the gas should be fixed.\n */\nasync function requiresFixedGas({\n ethQuery,\n txMeta,\n isCustomNetwork,\n}: UpdateGasRequest): Promise<boolean> {\n const {\n txParams: { to, data },\n } = txMeta;\n\n if (isCustomNetwork || !to || data) {\n return false;\n }\n\n const code = await getCode(ethQuery, to);\n\n return !code || code === '0x';\n}\n\n/**\n * Get the contract code for the provided address.\n *\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @param address - The address to get the code for.\n * @returns The contract code.\n */\nasync function getCode(\n ethQuery: EthQuery,\n address: string,\n): Promise<string | undefined> {\n return await query(ethQuery, 'getCode', [address]);\n}\n\n/**\n * Get the latest block from the network.\n *\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @returns The latest block number.\n */\nasync function getLatestBlock(\n ethQuery: EthQuery,\n): Promise<{ gasLimit: string; number: string }> {\n return await query(ethQuery, 'getBlockByNumber', ['latest', false]);\n}\n"]}
1
+ {"version":3,"file":"gas.cjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";;;AAAA,iEAKoC;AAGpC,2CAAsE;AAEtE,2CAA8C;AAC9C,yDAAwD;AACxD,gDAA0D;AAC1D,0CAA0C;AAC1C,wCAIkB;AAUL,QAAA,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,KAAK,CAAC,CAAC;AAE/C,QAAA,SAAS,GAAG,QAAQ,CAAC;AACrB,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAC7B,QAAA,mCAAmC,GAAG,EAAE,CAAC;AACzC,QAAA,qBAAqB,GAAG,EAAE,CAAC;AAC3B,QAAA,aAAa,GAAG,KAAK,CAAC;AAEtB,QAAA,6BAA6B,GACxC,oEAAoE,CAAC;AAEvE;;;;GAIG;AACI,KAAK,UAAU,SAAS,CAAC,OAAyB;IACvD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,MAAM,aAAa,GAAG,EAAE,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IAE7C,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,gBAAgB,CAAC,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;IAEvE,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,GAAG,CAAC;IAC1B,MAAM,CAAC,eAAe,GAAG,eAAe,CAA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AAC,EAAS;gBACzB,IAAI,EAAE,WAAW,CAAC,IAAW;gBAC7B,IAAI,EAAE,WAAW,CAAC,IAAW;gBAC7B,KAAK,EAAE,WAAW,CAAC,KAAY;aAChC;SACF;QACD,SAAS,EAAE;YACT,CAAC,WAAW,CAAC,IAAc,CAAC,EAAE;gBAC5B,IAAI,EACF,iBAAiB;oBAChB,CAAC,2BAAiB,GAAG,IAAA,gBAAQ,EAAC,iBAAiB,CAAC,CAAS;aAC7D;SACF;KACF,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,QAAQ,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAEpD,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;KAC9C;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,0BAA0B,CACjC,iBAAyD,EACzD,OAAY;IAEZ,OAAO,iBAAiB,EAAE,GAAG,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QAChD,GAAG,aAAa;QAChB,OAAO,EAAE,aAAa,CAAC,OAAO,IAAI,OAAO;QACzC,KAAK,EAAE,aAAa,CAAC,KAAK,IAAI,KAAK;QACnC,CAAC,EAAE,aAAa,CAAC,CAAC,IAAI,qCAA6B;QACnD,CAAC,EAAE,aAAa,CAAC,CAAC,IAAI,qCAA6B;QACnD,OAAO,EAAE,aAAa,CAAC,OAAO,IAAI,KAAK;KACxC,CAAC,CAAC,CAAC;AACN,CAAC","sourcesContent":["import {\n BNToHex,\n fractionBN,\n hexToBN,\n query,\n} from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport type { Hex } from '@metamask/utils';\nimport { add0x, createModuleLogger, remove0x } from '@metamask/utils';\n\nimport { DELEGATION_PREFIX } from './eip7702';\nimport { simulateTransactions } from './simulation-api';\nimport { GAS_BUFFER_CHAIN_OVERRIDES } from '../constants';\nimport { projectLogger } from '../logger';\nimport {\n TransactionEnvelopeType,\n type TransactionMeta,\n type TransactionParams,\n} from '../types';\n\nexport type UpdateGasRequest = {\n chainId: Hex;\n ethQuery: EthQuery;\n isCustomNetwork: boolean;\n isSimulationEnabled: boolean;\n txMeta: TransactionMeta;\n};\n\nexport const log = createModuleLogger(projectLogger, 'gas');\n\nexport const FIXED_GAS = '0x5208';\nexport const DEFAULT_GAS_MULTIPLIER = 1.5;\nexport const GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;\nexport const MAX_GAS_BLOCK_PERCENT = 90;\nexport const INTRINSIC_GAS = 21000;\n\nexport const DUMMY_AUTHORIZATION_SIGNATURE =\n '0x1111111111111111111111111111111111111111111111111111111111111111';\n\n/**\n * Populate the gas properties of the provided transaction meta.\n *\n * @param request - The request object including the necessary parameters.\n */\nexport async function updateGas(request: UpdateGasRequest) {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const [gas, simulationFails, gasLimitNoBuffer] = await getGas(request);\n\n txMeta.txParams.gas = gas;\n txMeta.simulationFails = simulationFails;\n txMeta.gasLimitNoBuffer = gasLimitNoBuffer;\n\n if (!initialParams.gas) {\n txMeta.originalGasEstimate = txMeta.txParams.gas;\n }\n\n if (!txMeta.defaultGasEstimates) {\n txMeta.defaultGasEstimates = {};\n }\n\n txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;\n}\n\n/**\n * Estimate the gas for the provided transaction parameters.\n * If the gas estimate fails, the fallback value is returned.\n *\n * @param options - The options object.\n * @param options.chainId - The chain ID of the transaction.\n * @param options.ethQuery - The EthQuery instance to interact with the network.\n * @param options.isSimulationEnabled - Whether the simulation is enabled.\n * @param options.txParams - The transaction parameters.\n * @returns The estimated gas and related info.\n */\nexport async function estimateGas({\n chainId,\n ethQuery,\n isSimulationEnabled,\n txParams,\n}: {\n chainId: Hex;\n ethQuery: EthQuery;\n isSimulationEnabled: boolean;\n txParams: TransactionParams;\n}) {\n const request = { ...txParams };\n const { authorizationList, data, from, value, to } = request;\n\n const { gasLimit: blockGasLimit, number: blockNumber } =\n await getLatestBlock(ethQuery);\n\n const blockGasLimitBN = hexToBN(blockGasLimit);\n\n const fallback = BNToHex(\n fractionBN(blockGasLimitBN, GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT, 100),\n );\n\n request.data = data ? add0x(data) : data;\n request.value = value || '0x0';\n\n request.authorizationList = normalizeAuthorizationList(\n request.authorizationList,\n chainId,\n );\n\n delete request.gasPrice;\n delete request.maxFeePerGas;\n delete request.maxPriorityFeePerGas;\n\n let estimatedGas = fallback;\n let simulationFails: TransactionMeta['simulationFails'];\n\n const isUpgradeWithDataToSelf =\n txParams.type === TransactionEnvelopeType.setCode &&\n authorizationList?.length &&\n data &&\n data !== '0x' &&\n from?.toLowerCase() === to?.toLowerCase();\n\n try {\n if (isSimulationEnabled && isUpgradeWithDataToSelf) {\n estimatedGas = await estimateGasUpgradeWithDataToSelf(\n request,\n ethQuery,\n chainId,\n );\n } else {\n estimatedGas = await query(ethQuery, 'estimateGas', [request]);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n simulationFails = {\n reason: error.message,\n errorKey: error.errorKey,\n debug: {\n blockNumber,\n blockGasLimit,\n },\n };\n\n log('Estimation failed', { ...simulationFails, fallback });\n }\n\n return {\n blockGasLimit,\n estimatedGas,\n simulationFails,\n };\n}\n\n/**\n * Add a buffer to the provided estimated gas.\n * The buffer is calculated based on the block gas limit and a multiplier.\n *\n * @param estimatedGas - The estimated gas.\n * @param blockGasLimit - The block gas limit.\n * @param multiplier - The multiplier to apply to the estimated gas.\n * @returns The gas with the buffer applied.\n */\nexport function addGasBuffer(\n estimatedGas: string,\n blockGasLimit: string,\n multiplier: number,\n) {\n const estimatedGasBN = hexToBN(estimatedGas);\n\n const maxGasBN = fractionBN(\n hexToBN(blockGasLimit),\n MAX_GAS_BLOCK_PERCENT,\n 100,\n );\n\n const paddedGasBN = fractionBN(estimatedGasBN, multiplier * 100, 100);\n\n if (estimatedGasBN.gt(maxGasBN)) {\n const estimatedGasHex = add0x(estimatedGas);\n log('Using estimated value', estimatedGasHex);\n return estimatedGasHex;\n }\n\n if (paddedGasBN.lt(maxGasBN)) {\n const paddedHex = add0x(BNToHex(paddedGasBN));\n log('Using padded estimate', paddedHex, multiplier);\n return paddedHex;\n }\n\n const maxHex = add0x(BNToHex(maxGasBN));\n log('Using 90% of block gas limit', maxHex);\n return maxHex;\n}\n\n/**\n * Determine the gas for the provided request.\n *\n * @param request - The request object including the necessary parameters.\n * @returns The final gas value and the estimate used.\n */\nasync function getGas(\n request: UpdateGasRequest,\n): Promise<[string, TransactionMeta['simulationFails']?, string?]> {\n const { chainId, isCustomNetwork, isSimulationEnabled, txMeta } = request;\n\n if (txMeta.txParams.gas) {\n log('Using value from request', txMeta.txParams.gas);\n return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];\n }\n\n if (await requiresFixedGas(request)) {\n log('Using fixed value', FIXED_GAS);\n return [FIXED_GAS, undefined, FIXED_GAS];\n }\n\n const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas({\n chainId: request.chainId,\n ethQuery: request.ethQuery,\n isSimulationEnabled,\n txParams: txMeta.txParams,\n });\n\n if (isCustomNetwork || simulationFails) {\n log(\n isCustomNetwork\n ? 'Using original estimate as custom network'\n : 'Using original fallback estimate as simulation failed',\n );\n return [estimatedGas, simulationFails, estimatedGas];\n }\n\n const bufferMultiplier =\n GAS_BUFFER_CHAIN_OVERRIDES[\n chainId as keyof typeof GAS_BUFFER_CHAIN_OVERRIDES\n ] ?? DEFAULT_GAS_MULTIPLIER;\n\n const bufferedGas = addGasBuffer(\n estimatedGas,\n blockGasLimit,\n bufferMultiplier,\n );\n\n return [bufferedGas, simulationFails, estimatedGas];\n}\n\n/**\n * Determine if the gas for the provided request should be fixed.\n *\n * @param options - The options object.\n * @param options.ethQuery - The EthQuery instance to interact with the network.\n * @param options.txMeta - The transaction meta object.\n * @param options.isCustomNetwork - Whether the network is a custom network.\n * @returns Whether the gas should be fixed.\n */\nasync function requiresFixedGas({\n ethQuery,\n txMeta,\n isCustomNetwork,\n}: UpdateGasRequest): Promise<boolean> {\n const {\n txParams: { to, data },\n } = txMeta;\n\n if (isCustomNetwork || !to || data) {\n return false;\n }\n\n const code = await getCode(ethQuery, to);\n\n return !code || code === '0x';\n}\n\n/**\n * Get the contract code for the provided address.\n *\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @param address - The address to get the code for.\n * @returns The contract code.\n */\nasync function getCode(\n ethQuery: EthQuery,\n address: string,\n): Promise<string | undefined> {\n return await query(ethQuery, 'getCode', [address]);\n}\n\n/**\n * Get the latest block from the network.\n *\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @returns The latest block number.\n */\nasync function getLatestBlock(\n ethQuery: EthQuery,\n): Promise<{ gasLimit: string; number: string }> {\n return await query(ethQuery, 'getBlockByNumber', ['latest', false]);\n}\n\n/**\n * Estimate the gas for a type 4 transaction.\n *\n * @param txParams - The transaction parameters.\n * @param ethQuery - The EthQuery instance to interact with the network.\n * @param chainId - The chain ID of the transaction.\n * @returns The estimated gas.\n */\nasync function estimateGasUpgradeWithDataToSelf(\n txParams: TransactionParams,\n ethQuery: EthQuery,\n chainId: Hex,\n) {\n const upgradeGas = await query(ethQuery, 'estimateGas', [\n {\n ...txParams,\n data: '0x',\n },\n ]);\n\n log('Upgrade only gas', upgradeGas);\n\n const delegationAddress = txParams.authorizationList?.[0].address as Hex;\n\n const executeGas = await simulateGas({\n chainId: chainId as Hex,\n delegationAddress,\n transaction: txParams,\n });\n\n log('Execute gas', executeGas);\n\n const total = BNToHex(\n hexToBN(upgradeGas).add(hexToBN(executeGas)).subn(INTRINSIC_GAS),\n );\n\n log('Total type 4 gas', total);\n\n return total;\n}\n\n/**\n * Simulate the required gas using the simulation API.\n *\n * @param options - The options object.\n * @param options.chainId - The chain ID of the transaction.\n * @param options.delegationAddress - The delegation address of the sender to mock.\n * @param options.transaction - The transaction parameters.\n * @returns The simulated gas.\n */\nasync function simulateGas({\n chainId,\n delegationAddress,\n transaction,\n}: {\n chainId: Hex;\n delegationAddress?: Hex;\n transaction: TransactionParams;\n}): Promise<Hex> {\n const response = await simulateTransactions(chainId, {\n transactions: [\n {\n to: transaction.to as Hex,\n from: transaction.from as Hex,\n data: transaction.data as Hex,\n value: transaction.value as Hex,\n },\n ],\n overrides: {\n [transaction.from as string]: {\n code:\n delegationAddress &&\n ((DELEGATION_PREFIX + remove0x(delegationAddress)) as Hex),\n },\n },\n });\n\n const gasUsed = response?.transactions?.[0].gasUsed;\n\n if (!gasUsed) {\n throw new Error('No simulated gas returned');\n }\n\n return gasUsed;\n}\n\n/**\n * Populate the authorization list with dummy values.\n *\n * @param authorizationList - The authorization list to prepare.\n * @param chainId - The chain ID to use.\n * @returns The authorization list with dummy values.\n */\nfunction normalizeAuthorizationList(\n authorizationList: TransactionParams['authorizationList'],\n chainId: Hex,\n) {\n return authorizationList?.map((authorization) => ({\n ...authorization,\n chainId: authorization.chainId ?? chainId,\n nonce: authorization.nonce ?? '0x1',\n r: authorization.r ?? DUMMY_AUTHORIZATION_SIGNATURE,\n s: authorization.s ?? DUMMY_AUTHORIZATION_SIGNATURE,\n yParity: authorization.yParity ?? '0x1',\n }));\n}\n"]}
@@ -1,11 +1,12 @@
1
1
  /// <reference types="debug" />
2
2
  import type EthQuery from "@metamask/eth-query";
3
3
  import type { Hex } from "@metamask/utils";
4
- import type { TransactionMeta, TransactionParams } from "../types.cjs";
4
+ import { type TransactionMeta, type TransactionParams } from "../types.cjs";
5
5
  export type UpdateGasRequest = {
6
+ chainId: Hex;
6
7
  ethQuery: EthQuery;
7
8
  isCustomNetwork: boolean;
8
- chainId: Hex;
9
+ isSimulationEnabled: boolean;
9
10
  txMeta: TransactionMeta;
10
11
  };
11
12
  export declare const log: import("debug").Debugger;
@@ -13,6 +14,8 @@ export declare const FIXED_GAS = "0x5208";
13
14
  export declare const DEFAULT_GAS_MULTIPLIER = 1.5;
14
15
  export declare const GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;
15
16
  export declare const MAX_GAS_BLOCK_PERCENT = 90;
17
+ export declare const INTRINSIC_GAS = 21000;
18
+ export declare const DUMMY_AUTHORIZATION_SIGNATURE = "0x1111111111111111111111111111111111111111111111111111111111111111";
16
19
  /**
17
20
  * Populate the gas properties of the provided transaction meta.
18
21
  *
@@ -23,11 +26,19 @@ export declare function updateGas(request: UpdateGasRequest): Promise<void>;
23
26
  * Estimate the gas for the provided transaction parameters.
24
27
  * If the gas estimate fails, the fallback value is returned.
25
28
  *
26
- * @param txParams - The transaction parameters.
27
- * @param ethQuery - The EthQuery instance to interact with the network.
29
+ * @param options - The options object.
30
+ * @param options.chainId - The chain ID of the transaction.
31
+ * @param options.ethQuery - The EthQuery instance to interact with the network.
32
+ * @param options.isSimulationEnabled - Whether the simulation is enabled.
33
+ * @param options.txParams - The transaction parameters.
28
34
  * @returns The estimated gas and related info.
29
35
  */
30
- export declare function estimateGas(txParams: TransactionParams, ethQuery: EthQuery): Promise<{
36
+ export declare function estimateGas({ chainId, ethQuery, isSimulationEnabled, txParams, }: {
37
+ chainId: Hex;
38
+ ethQuery: EthQuery;
39
+ isSimulationEnabled: boolean;
40
+ txParams: TransactionParams;
41
+ }): Promise<{
31
42
  blockGasLimit: string;
32
43
  estimatedGas: `0x${string}`;
33
44
  simulationFails: {
@@ -1 +1 @@
1
- {"version":3,"file":"gas.d.cts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAMA,OAAO,KAAK,QAAQ,4BAA4B;AAChD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAK3C,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,qBAAiB;AAEnE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,QAAQ,CAAC;IACnB,eAAe,EAAE,OAAO,CAAC;IACzB,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;CAC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AAS,CAAC,OAAO,EAAE,gBAAgB,iBAmBxD;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,WAAW,CAAC,EAChC,OAAO,EACP,QAAQ,EACR,mBAAmB,EACnB,QAAQ,GACT,EAAE;IACD,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,mBAAmB,EAAE,OAAO,CAAC;IAC7B,QAAQ,EAAE,iBAAiB,CAAC;CAC7B;;;;;;;;;;;GAgEA;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC1B,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,MAAM,iBA2BnB"}
@@ -1,11 +1,12 @@
1
1
  /// <reference types="debug" />
2
2
  import type EthQuery from "@metamask/eth-query";
3
3
  import type { Hex } from "@metamask/utils";
4
- import type { TransactionMeta, TransactionParams } from "../types.mjs";
4
+ import { type TransactionMeta, type TransactionParams } from "../types.mjs";
5
5
  export type UpdateGasRequest = {
6
+ chainId: Hex;
6
7
  ethQuery: EthQuery;
7
8
  isCustomNetwork: boolean;
8
- chainId: Hex;
9
+ isSimulationEnabled: boolean;
9
10
  txMeta: TransactionMeta;
10
11
  };
11
12
  export declare const log: import("debug").Debugger;
@@ -13,6 +14,8 @@ export declare const FIXED_GAS = "0x5208";
13
14
  export declare const DEFAULT_GAS_MULTIPLIER = 1.5;
14
15
  export declare const GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;
15
16
  export declare const MAX_GAS_BLOCK_PERCENT = 90;
17
+ export declare const INTRINSIC_GAS = 21000;
18
+ export declare const DUMMY_AUTHORIZATION_SIGNATURE = "0x1111111111111111111111111111111111111111111111111111111111111111";
16
19
  /**
17
20
  * Populate the gas properties of the provided transaction meta.
18
21
  *
@@ -23,11 +26,19 @@ export declare function updateGas(request: UpdateGasRequest): Promise<void>;
23
26
  * Estimate the gas for the provided transaction parameters.
24
27
  * If the gas estimate fails, the fallback value is returned.
25
28
  *
26
- * @param txParams - The transaction parameters.
27
- * @param ethQuery - The EthQuery instance to interact with the network.
29
+ * @param options - The options object.
30
+ * @param options.chainId - The chain ID of the transaction.
31
+ * @param options.ethQuery - The EthQuery instance to interact with the network.
32
+ * @param options.isSimulationEnabled - Whether the simulation is enabled.
33
+ * @param options.txParams - The transaction parameters.
28
34
  * @returns The estimated gas and related info.
29
35
  */
30
- export declare function estimateGas(txParams: TransactionParams, ethQuery: EthQuery): Promise<{
36
+ export declare function estimateGas({ chainId, ethQuery, isSimulationEnabled, txParams, }: {
37
+ chainId: Hex;
38
+ ethQuery: EthQuery;
39
+ isSimulationEnabled: boolean;
40
+ txParams: TransactionParams;
41
+ }): Promise<{
31
42
  blockGasLimit: string;
32
43
  estimatedGas: `0x${string}`;
33
44
  simulationFails: {
@@ -1 +1 @@
1
- {"version":3,"file":"gas.d.mts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";AAMA,OAAO,KAAK,QAAQ,4BAA4B;AAChD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAK3C,OAAO,KAAK,EAAE,eAAe,EAAE,iBAAiB,EAAE,qBAAiB;AAEnE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,QAAQ,CAAC;IACnB,eAAe,EAAE,OAAO,CAAC;IACzB,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,GAAG,0BAA2C,CAAC;AAE5D,eAAO,MAAM,SAAS,WAAW,CAAC;AAClC,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAC1C,eAAO,MAAM,mCAAmC,KAAK,CAAC;AACtD,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,iBAmBxD;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,iBAAiB,EAC3B,QAAQ,EAA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AE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,MAAM,iBA2BnB"}
@@ -1,12 +1,17 @@
1
1
  import { BNToHex, fractionBN, hexToBN, query } from "@metamask/controller-utils";
2
- import { add0x, createModuleLogger } from "@metamask/utils";
2
+ import { add0x, createModuleLogger, remove0x } from "@metamask/utils";
3
+ import { DELEGATION_PREFIX } from "./eip7702.mjs";
4
+ import { simulateTransactions } from "./simulation-api.mjs";
3
5
  import { GAS_BUFFER_CHAIN_OVERRIDES } from "../constants.mjs";
4
6
  import { projectLogger } from "../logger.mjs";
7
+ import { TransactionEnvelopeType } from "../types.mjs";
5
8
  export const log = createModuleLogger(projectLogger, 'gas');
6
9
  export const FIXED_GAS = '0x5208';
7
10
  export const DEFAULT_GAS_MULTIPLIER = 1.5;
8
11
  export const GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;
9
12
  export const MAX_GAS_BLOCK_PERCENT = 90;
13
+ export const INTRINSIC_GAS = 21000;
14
+ export const DUMMY_AUTHORIZATION_SIGNATURE = '0x1111111111111111111111111111111111111111111111111111111111111111';
10
15
  /**
11
16
  * Populate the gas properties of the provided transaction meta.
12
17
  *
@@ -31,25 +36,39 @@ export async function updateGas(request) {
31
36
  * Estimate the gas for the provided transaction parameters.
32
37
  * If the gas estimate fails, the fallback value is returned.
33
38
  *
34
- * @param txParams - The transaction parameters.
35
- * @param ethQuery - The EthQuery instance to interact with the network.
39
+ * @param options - The options object.
40
+ * @param options.chainId - The chain ID of the transaction.
41
+ * @param options.ethQuery - The EthQuery instance to interact with the network.
42
+ * @param options.isSimulationEnabled - Whether the simulation is enabled.
43
+ * @param options.txParams - The transaction parameters.
36
44
  * @returns The estimated gas and related info.
37
45
  */
38
- export async function estimateGas(txParams, ethQuery) {
46
+ export async function estimateGas({ chainId, ethQuery, isSimulationEnabled, txParams, }) {
39
47
  const request = { ...txParams };
40
- const { data, value } = request;
48
+ const { authorizationList, data, from, value, to } = request;
41
49
  const { gasLimit: blockGasLimit, number: blockNumber } = await getLatestBlock(ethQuery);
42
50
  const blockGasLimitBN = hexToBN(blockGasLimit);
43
51
  const fallback = BNToHex(fractionBN(blockGasLimitBN, GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT, 100));
44
52
  request.data = data ? add0x(data) : data;
45
53
  request.value = value || '0x0';
54
+ request.authorizationList = normalizeAuthorizationList(request.authorizationList, chainId);
46
55
  delete request.gasPrice;
47
56
  delete request.maxFeePerGas;
48
57
  delete request.maxPriorityFeePerGas;
49
58
  let estimatedGas = fallback;
50
59
  let simulationFails;
60
+ const isUpgradeWithDataToSelf = txParams.type === TransactionEnvelopeType.setCode &&
61
+ authorizationList?.length &&
62
+ data &&
63
+ data !== '0x' &&
64
+ from?.toLowerCase() === to?.toLowerCase();
51
65
  try {
52
- estimatedGas = await query(ethQuery, 'estimateGas', [request]);
66
+ if (isSimulationEnabled && isUpgradeWithDataToSelf) {
67
+ estimatedGas = await estimateGasUpgradeWithDataToSelf(request, ethQuery, chainId);
68
+ }
69
+ else {
70
+ estimatedGas = await query(ethQuery, 'estimateGas', [request]);
71
+ }
53
72
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
73
  }
55
74
  catch (error) {
@@ -103,7 +122,7 @@ export function addGasBuffer(estimatedGas, blockGasLimit, multiplier) {
103
122
  * @returns The final gas value and the estimate used.
104
123
  */
105
124
  async function getGas(request) {
106
- const { isCustomNetwork, chainId, txMeta } = request;
125
+ const { chainId, isCustomNetwork, isSimulationEnabled, txMeta } = request;
107
126
  if (txMeta.txParams.gas) {
108
127
  log('Using value from request', txMeta.txParams.gas);
109
128
  return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];
@@ -112,7 +131,12 @@ async function getGas(request) {
112
131
  log('Using fixed value', FIXED_GAS);
113
132
  return [FIXED_GAS, undefined, FIXED_GAS];
114
133
  }
115
- const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(txMeta.txParams, request.ethQuery);
134
+ const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas({
135
+ chainId: request.chainId,
136
+ ethQuery: request.ethQuery,
137
+ isSimulationEnabled,
138
+ txParams: txMeta.txParams,
139
+ });
116
140
  if (isCustomNetwork || simulationFails) {
117
141
  log(isCustomNetwork
118
142
  ? 'Using original estimate as custom network'
@@ -159,4 +183,80 @@ async function getCode(ethQuery, address) {
159
183
  async function getLatestBlock(ethQuery) {
160
184
  return await query(ethQuery, 'getBlockByNumber', ['latest', false]);
161
185
  }
186
+ /**
187
+ * Estimate the gas for a type 4 transaction.
188
+ *
189
+ * @param txParams - The transaction parameters.
190
+ * @param ethQuery - The EthQuery instance to interact with the network.
191
+ * @param chainId - The chain ID of the transaction.
192
+ * @returns The estimated gas.
193
+ */
194
+ async function estimateGasUpgradeWithDataToSelf(txParams, ethQuery, chainId) {
195
+ const upgradeGas = await query(ethQuery, 'estimateGas', [
196
+ {
197
+ ...txParams,
198
+ data: '0x',
199
+ },
200
+ ]);
201
+ log('Upgrade only gas', upgradeGas);
202
+ const delegationAddress = txParams.authorizationList?.[0].address;
203
+ const executeGas = await simulateGas({
204
+ chainId: chainId,
205
+ delegationAddress,
206
+ transaction: txParams,
207
+ });
208
+ log('Execute gas', executeGas);
209
+ const total = BNToHex(hexToBN(upgradeGas).add(hexToBN(executeGas)).subn(INTRINSIC_GAS));
210
+ log('Total type 4 gas', total);
211
+ return total;
212
+ }
213
+ /**
214
+ * Simulate the required gas using the simulation API.
215
+ *
216
+ * @param options - The options object.
217
+ * @param options.chainId - The chain ID of the transaction.
218
+ * @param options.delegationAddress - The delegation address of the sender to mock.
219
+ * @param options.transaction - The transaction parameters.
220
+ * @returns The simulated gas.
221
+ */
222
+ async function simulateGas({ chainId, delegationAddress, transaction, }) {
223
+ const response = await simulateTransactions(chainId, {
224
+ transactions: [
225
+ {
226
+ to: transaction.to,
227
+ from: transaction.from,
228
+ data: transaction.data,
229
+ value: transaction.value,
230
+ },
231
+ ],
232
+ overrides: {
233
+ [transaction.from]: {
234
+ code: delegationAddress &&
235
+ (DELEGATION_PREFIX + remove0x(delegationAddress)),
236
+ },
237
+ },
238
+ });
239
+ const gasUsed = response?.transactions?.[0].gasUsed;
240
+ if (!gasUsed) {
241
+ throw new Error('No simulated gas returned');
242
+ }
243
+ return gasUsed;
244
+ }
245
+ /**
246
+ * Populate the authorization list with dummy values.
247
+ *
248
+ * @param authorizationList - The authorization list to prepare.
249
+ * @param chainId - The chain ID to use.
250
+ * @returns The authorization list with dummy values.
251
+ */
252
+ function normalizeAuthorizationList(authorizationList, chainId) {
253
+ return authorizationList?.map((authorization) => ({
254
+ ...authorization,
255
+ chainId: authorization.chainId ?? chainId,
256
+ nonce: authorization.nonce ?? '0x1',
257
+ r: authorization.r ?? DUMMY_AUTHORIZATION_SIGNATURE,
258
+ s: authorization.s ?? DUMMY_AUTHORIZATION_SIGNATURE,
259
+ yParity: authorization.yParity ?? '0x1',
260
+ }));
261
+ }
162
262
  //# sourceMappingURL=gas.mjs.map