@metamask-previews/transaction-controller 62.7.0-preview-42084fe4 → 62.7.0-preview-7bc2d97e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/TransactionController.cjs +23 -72
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +12 -13
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +12 -13
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +23 -72
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.cjs +37 -25
- package/dist/helpers/PendingTransactionTracker.cjs.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.cts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.d.mts.map +1 -1
- package/dist/helpers/PendingTransactionTracker.mjs +38 -26
- package/dist/helpers/PendingTransactionTracker.mjs.map +1 -1
- package/dist/index.cjs +1 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +5 -4
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +5 -4
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/feature-flags.cjs +4 -1
- package/dist/utils/feature-flags.cjs.map +1 -1
- package/dist/utils/feature-flags.d.cts +5 -0
- package/dist/utils/feature-flags.d.cts.map +1 -1
- package/dist/utils/feature-flags.d.mts +5 -0
- package/dist/utils/feature-flags.d.mts.map +1 -1
- package/dist/utils/feature-flags.mjs +4 -1
- package/dist/utils/feature-flags.mjs.map +1 -1
- package/package.json +3 -3
- package/dist/utils/history.cjs +0 -164
- package/dist/utils/history.cjs.map +0 -1
- package/dist/utils/history.d.cts +0 -29
- package/dist/utils/history.d.cts.map +0 -1
- package/dist/utils/history.d.mts +0 -29
- package/dist/utils/history.d.mts.map +0 -1
- package/dist/utils/history.mjs +0 -164
- package/dist/utils/history.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.d.mts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAK3C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;
|
|
1
|
+
{"version":3,"file":"feature-flags.d.mts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAK3C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAU/E;;GAEG;AACH,oBAAY,WAAW;IACrB,OAAO,2BAA2B;IAClC,SAAS,6BAA6B;IACtC,oBAAoB,wCAAwC;IAC5D,YAAY,+BAA+B;CAC5C;AAED,KAAK,mBAAmB,GAAG;IACzB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,+DAA+D;IAC/D,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE;QACtB;;;;WAIG;QACH,SAAS,CAAC,EAAE,MAAM,CAChB,GAAG,EACH;YACE,qCAAqC;YACrC,OAAO,EAAE,GAAG,CAAC;YAEb,qDAAqD;YACrD,SAAS,EAAE,GAAG,CAAC;SAChB,EAAE,CACJ,CAAC;QAEF,sDAAsD;QACtD,eAAe,CAAC,EAAE,GAAG,EAAE,CAAC;KACzB,CAAC;IAEF;;;OAGG;IACH,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE;QACxB,uDAAuD;QACvD,OAAO,CAAC,EAAE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;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,GAAG,EACZ,SAAS,EAAE,8BAA8B,GACxC;IACD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB,CAgBA;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,OAAO,EACP,WAAW,EACX,uBAAuB,EACvB,SAAS,GACV,EAAE;IACD,OAAO,EAAE,GAAG,CAAC;IACb,WAAW,EAAE,OAAO,CAAC;IACrB,uBAAuB,EAAE,OAAO,CAAC;IACjC,SAAS,EAAE,8BAA8B,CAAC;CAC3C,GAAG,MAAM,CAsBT;AAED;;;;;;GAMG;AACH,wBAAgB,sCAAsC,CACpD,SAAS,EAAE,8BAA8B,GACxC,MAAM,CAOR;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,GAAG,EACZ,SAAS,EAAE,8BAA8B,GACxC,MAAM,GAAG,SAAS,CAUpB"}
|
|
@@ -5,6 +5,7 @@ import { projectLogger } from "../logger.mjs";
|
|
|
5
5
|
const DEFAULT_BATCH_SIZE_LIMIT = 10;
|
|
6
6
|
const DEFAULT_ACCELERATED_POLLING_COUNT_MAX = 10;
|
|
7
7
|
const DEFAULT_ACCELERATED_POLLING_INTERVAL_MS = 3 * 1000;
|
|
8
|
+
const DEFAULT_BLOCK_TIME = 12 * 1000;
|
|
8
9
|
const DEFAULT_GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;
|
|
9
10
|
const DEFAULT_GAS_ESTIMATE_BUFFER = 1;
|
|
10
11
|
const DEFAULT_INCOMING_TRANSACTIONS_POLLING_INTERVAL_MS = 1000 * 60 * 4; // 4 Minutes
|
|
@@ -83,7 +84,9 @@ export function getAcceleratedPollingParams(chainId, messenger) {
|
|
|
83
84
|
const intervalMs = acceleratedPollingParams?.perChainConfig?.[chainId]?.intervalMs ??
|
|
84
85
|
acceleratedPollingParams?.defaultIntervalMs ??
|
|
85
86
|
DEFAULT_ACCELERATED_POLLING_INTERVAL_MS;
|
|
86
|
-
|
|
87
|
+
const blockTime = acceleratedPollingParams?.perChainConfig?.[chainId]?.blockTime ??
|
|
88
|
+
DEFAULT_BLOCK_TIME;
|
|
89
|
+
return { blockTime, countMax, intervalMs };
|
|
87
90
|
}
|
|
88
91
|
/**
|
|
89
92
|
* Retrieves the gas fee randomisation parameters.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAGrD,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAC/C,OAAO,EAAE,kBAAkB,EAAE,oBAAgB;AAC7C,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,wBAAwB,GAAG,EAAE,CAAC;AACpC,MAAM,qCAAqC,GAAG,EAAE,CAAC;AACjD,MAAM,uCAAuC,GAAG,CAAC,GAAG,IAAI,CAAC;AACzD,MAAM,2CAA2C,GAAG,EAAE,CAAC;AACvD,MAAM,2BAA2B,GAAG,CAAC,CAAC;AACtC,MAAM,iDAAiD,GAAG,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,YAAY;AAErF;;GAEG;AACH,MAAM,CAAN,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,iDAAkC,CAAA;IAClC,qDAAsC,CAAA;IACtC,2EAA4D,CAAA;IAC5D,0DAA2C,CAAA;AAC7C,CAAC,EALW,WAAW,KAAX,WAAW,QAKtB;AA0JD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,SAAyC;IAEzC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;IAChD,OAAO,YAAY,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,eA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,OAAO,CAC9B,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 } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { padHexToEvenLength } from './utils';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\nconst DEFAULT_ACCELERATED_POLLING_COUNT_MAX = 10;\nconst DEFAULT_ACCELERATED_POLLING_INTERVAL_MS = 3 * 1000;\nconst DEFAULT_GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;\nconst DEFAULT_GAS_ESTIMATE_BUFFER = 1;\nconst DEFAULT_INCOMING_TRANSACTIONS_POLLING_INTERVAL_MS = 1000 * 60 * 4; // 4 Minutes\n\n/**\n * Feature flags supporting the transaction controller.\n */\nexport enum FeatureFlag {\n EIP7702 = 'confirmations_eip_7702',\n GasBuffer = 'confirmations_gas_buffer',\n IncomingTransactions = 'confirmations_incoming_transactions',\n Transactions = 'confirmations_transactions',\n}\n\ntype GasEstimateFallback = {\n /**\n * The fixed gas estimate fallback for a transaction.\n */\n fixed?: number;\n\n /**\n * The percentage multiplier gas estimate fallback for a transaction.\n */\n percentage?: number;\n};\n\nexport type TransactionControllerFeatureFlags = {\n /** Feature flags to support EIP-7702 / type-4 transactions. */\n [FeatureFlag.EIP7702]?: {\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 /**\n * Buffers added to gas limit estimations.\n * Values are multipliers such as `1.5` meaning 150% of the original gas limit.\n */\n [FeatureFlag.GasBuffer]?: {\n /** Fallback buffer for all chains and transactions. */\n default?: number;\n\n /**\n * Buffer for included network RPCs only and not those added by user.\n * Takes priority over `default`.\n */\n included?: number;\n\n /** Buffers for specific chains. */\n perChainConfig?: {\n [chainId: Hex]: {\n /**\n * Buffer for the chain for all transactions.\n * Takes priority over non-chain `included`.\n */\n base?: number;\n\n /**\n * Buffer if network RPC is included and not added by user.\n * Takes priority over `base`.\n */\n included?: number;\n\n /**\n * Buffer for the chain for EIP-7702 / type 4 transactions only.\n * Only if `data` included and `to` matches `from`.\n * Takes priority over `included` and `base`.\n */\n eip7702?: number;\n };\n };\n };\n\n /** Incoming transaction configuration. */\n [FeatureFlag.IncomingTransactions]?: {\n /** Interval between requests to accounts API to retrieve incoming transactions. */\n pollingIntervalMs?: number;\n };\n\n /** Miscellaneous feature flags to support the transaction controller. */\n [FeatureFlag.Transactions]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n\n /**\n * Accelerated polling is used to speed up the polling process for\n * transactions that are not yet confirmed.\n */\n acceleratedPolling?: {\n /** Accelerated polling parameters on a per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: {\n /**\n * Maximum number of polling requests that can be made in a row, before\n * the normal polling resumes.\n */\n countMax?: number;\n\n /** Interval between polling requests in milliseconds. */\n intervalMs?: number;\n };\n };\n\n /** Default `countMax` in case no chain-specific parameter is set. */\n defaultCountMax?: number;\n\n /** Default `intervalMs` in case no chain-specific parameter is set. */\n defaultIntervalMs?: number;\n };\n\n gasFeeRandomisation?: {\n /** Randomised gas fee digits per chainId. */\n randomisedGasFeeDigits?: Record<Hex, number>;\n\n /** Number of digits to preserve for randomised gas fee digits. */\n preservedNumberOfDigits?: number;\n };\n\n /** Gas estimate fallback is used as a fallback in case of failure to obtain the gas estimate values. */\n gasEstimateFallback?: {\n /** Gas estimate fallback per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: GasEstimateFallback;\n };\n\n /**\n * Default gas estimate fallback.\n * This value is used when no specific gas estimate fallback is found for a chain ID.\n */\n default?: GasEstimateFallback;\n };\n\n /**\n * Number of attempts to wait before automatically marking a transaction as failed\n * if it has no receipt status and hash is not found on the network.\n */\n timeoutAttempts?: {\n /** Automatic fail threshold on a per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: number;\n };\n\n /**\n * Default automatic fail threshold.\n * This value is used when no specific threshold is found for a chain ID.\n */\n default?: number;\n };\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?.[FeatureFlag.EIP7702]?.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?.[FeatureFlag.EIP7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, padHexToEvenLength(chainId) as Hex],\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?.[FeatureFlag.Transactions]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the accelerated polling parameters for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The accelerated polling parameters: `countMax` and `intervalMs`.\n */\nexport function getAcceleratedPollingParams(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): { countMax: number; intervalMs: number } {\n const featureFlags = getFeatureFlags(messenger);\n\n const acceleratedPollingParams =\n featureFlags?.[FeatureFlag.Transactions]?.acceleratedPolling;\n\n const countMax =\n acceleratedPollingParams?.perChainConfig?.[chainId]?.countMax ??\n acceleratedPollingParams?.defaultCountMax ??\n DEFAULT_ACCELERATED_POLLING_COUNT_MAX;\n\n const intervalMs =\n acceleratedPollingParams?.perChainConfig?.[chainId]?.intervalMs ??\n acceleratedPollingParams?.defaultIntervalMs ??\n DEFAULT_ACCELERATED_POLLING_INTERVAL_MS;\n\n return { countMax, intervalMs };\n}\n\n/**\n * Retrieves the gas fee randomisation parameters.\n *\n * @param messenger - The controller messenger instance.\n * @returns The gas fee randomisation parameters.\n */\nexport function getGasFeeRandomisation(\n messenger: TransactionControllerMessenger,\n): {\n randomisedGasFeeDigits: Record<Hex, number>;\n preservedNumberOfDigits: number | undefined;\n} {\n const featureFlags = getFeatureFlags(messenger);\n\n const gasFeeRandomisation =\n featureFlags?.[FeatureFlag.Transactions]?.gasFeeRandomisation ?? {};\n\n return {\n randomisedGasFeeDigits: gasFeeRandomisation.randomisedGasFeeDigits ?? {},\n preservedNumberOfDigits: gasFeeRandomisation.preservedNumberOfDigits,\n };\n}\n\n/**\n * Retrieves the gas estimate fallback for a given chain ID.\n * Defaults to the default gas estimate fallback if not set.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The gas estimate fallback.\n */\nexport function getGasEstimateFallback(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): {\n fixed?: number;\n percentage: number;\n} {\n const featureFlags = getFeatureFlags(messenger);\n\n const gasEstimateFallbackFlags =\n featureFlags?.[FeatureFlag.Transactions]?.gasEstimateFallback;\n\n const chainFlags = gasEstimateFallbackFlags?.perChainConfig?.[chainId];\n\n const percentage =\n chainFlags?.percentage ??\n gasEstimateFallbackFlags?.default?.percentage ??\n DEFAULT_GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT;\n\n const fixed = chainFlags?.fixed ?? gasEstimateFallbackFlags?.default?.fixed;\n\n return { fixed, percentage };\n}\n\n/**\n * Retrieves the gas buffers for a given chain ID.\n *\n * @param request - The request object.\n * @param request.chainId - The chain ID.\n * @param request.isCustomRPC - Whether the network RPC is added by the user.\n * @param request.isUpgradeWithDataToSelf - Whether the transaction is an EIP-7702 upgrade with data to self.\n * @param request.messenger - The controller messenger instance.\n * @returns The gas buffers.\n */\nexport function getGasEstimateBuffer({\n chainId,\n isCustomRPC,\n isUpgradeWithDataToSelf,\n messenger,\n}: {\n chainId: Hex;\n isCustomRPC: boolean;\n isUpgradeWithDataToSelf: boolean;\n messenger: TransactionControllerMessenger;\n}): number {\n const featureFlags = getFeatureFlags(messenger);\n const gasBufferFlags = featureFlags?.[FeatureFlag.GasBuffer];\n const chainFlags = gasBufferFlags?.perChainConfig?.[chainId];\n const chainIncludedRPCBuffer = isCustomRPC ? undefined : chainFlags?.included;\n\n const defaultIncludedRPCBuffer = isCustomRPC\n ? undefined\n : gasBufferFlags?.included;\n\n const upgradeBuffer = isUpgradeWithDataToSelf\n ? chainFlags?.eip7702\n : undefined;\n\n return (\n upgradeBuffer ??\n chainIncludedRPCBuffer ??\n chainFlags?.base ??\n defaultIncludedRPCBuffer ??\n gasBufferFlags?.default ??\n DEFAULT_GAS_ESTIMATE_BUFFER\n );\n}\n\n/**\n * Retrieves the incoming transactions polling interval.\n * Defaults to 4 minutes if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The incoming transactions polling interval in milliseconds.\n */\nexport function getIncomingTransactionsPollingInterval(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n\n return (\n featureFlags?.[FeatureFlag.IncomingTransactions]?.pollingIntervalMs ??\n DEFAULT_INCOMING_TRANSACTIONS_POLLING_INTERVAL_MS\n );\n}\n\n/**\n * Retrieves the number of attempts to wait before automatically marking a transaction as dropped\n * if it has no receipt status.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The threshold number of attempts, or undefined if the feature is disabled.\n */\nexport function getTimeoutAttempts(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): number | undefined {\n const featureFlags = getFeatureFlags(messenger);\n\n const timeoutAttemptsFlags =\n featureFlags?.[FeatureFlag.Transactions]?.timeoutAttempts;\n\n return (\n timeoutAttemptsFlags?.perChainConfig?.[chainId] ??\n timeoutAttemptsFlags?.default\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,EAAE,wBAAwB;AA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AC;AAC3D,CAAC","sourcesContent":["import { createModuleLogger } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { isValidSignature } from './signature';\nimport { padHexToEvenLength } from './utils';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\n\nconst DEFAULT_BATCH_SIZE_LIMIT = 10;\nconst DEFAULT_ACCELERATED_POLLING_COUNT_MAX = 10;\nconst DEFAULT_ACCELERATED_POLLING_INTERVAL_MS = 3 * 1000;\nconst DEFAULT_BLOCK_TIME = 12 * 1000;\nconst DEFAULT_GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT = 35;\nconst DEFAULT_GAS_ESTIMATE_BUFFER = 1;\nconst DEFAULT_INCOMING_TRANSACTIONS_POLLING_INTERVAL_MS = 1000 * 60 * 4; // 4 Minutes\n\n/**\n * Feature flags supporting the transaction controller.\n */\nexport enum FeatureFlag {\n EIP7702 = 'confirmations_eip_7702',\n GasBuffer = 'confirmations_gas_buffer',\n IncomingTransactions = 'confirmations_incoming_transactions',\n Transactions = 'confirmations_transactions',\n}\n\ntype GasEstimateFallback = {\n /**\n * The fixed gas estimate fallback for a transaction.\n */\n fixed?: number;\n\n /**\n * The percentage multiplier gas estimate fallback for a transaction.\n */\n percentage?: number;\n};\n\nexport type TransactionControllerFeatureFlags = {\n /** Feature flags to support EIP-7702 / type-4 transactions. */\n [FeatureFlag.EIP7702]?: {\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 /**\n * Buffers added to gas limit estimations.\n * Values are multipliers such as `1.5` meaning 150% of the original gas limit.\n */\n [FeatureFlag.GasBuffer]?: {\n /** Fallback buffer for all chains and transactions. */\n default?: number;\n\n /**\n * Buffer for included network RPCs only and not those added by user.\n * Takes priority over `default`.\n */\n included?: number;\n\n /** Buffers for specific chains. */\n perChainConfig?: {\n [chainId: Hex]: {\n /**\n * Buffer for the chain for all transactions.\n * Takes priority over non-chain `included`.\n */\n base?: number;\n\n /**\n * Buffer if network RPC is included and not added by user.\n * Takes priority over `base`.\n */\n included?: number;\n\n /**\n * Buffer for the chain for EIP-7702 / type 4 transactions only.\n * Only if `data` included and `to` matches `from`.\n * Takes priority over `included` and `base`.\n */\n eip7702?: number;\n };\n };\n };\n\n /** Incoming transaction configuration. */\n [FeatureFlag.IncomingTransactions]?: {\n /** Interval between requests to accounts API to retrieve incoming transactions. */\n pollingIntervalMs?: number;\n };\n\n /** Miscellaneous feature flags to support the transaction controller. */\n [FeatureFlag.Transactions]?: {\n /** Maximum number of transactions that can be in an external batch. */\n batchSizeLimit?: number;\n\n /**\n * Accelerated polling is used to speed up the polling process for\n * transactions that are not yet confirmed.\n */\n acceleratedPolling?: {\n /** Accelerated polling parameters on a per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: {\n /**\n * Block time in milliseconds.\n */\n blockTime?: number;\n\n /**\n * Maximum number of polling requests that can be made in a row, before\n * the normal polling resumes.\n */\n countMax?: number;\n\n /** Interval between polling requests in milliseconds. */\n intervalMs?: number;\n };\n };\n\n /** Default `countMax` in case no chain-specific parameter is set. */\n defaultCountMax?: number;\n\n /** Default `intervalMs` in case no chain-specific parameter is set. */\n defaultIntervalMs?: number;\n };\n\n gasFeeRandomisation?: {\n /** Randomised gas fee digits per chainId. */\n randomisedGasFeeDigits?: Record<Hex, number>;\n\n /** Number of digits to preserve for randomised gas fee digits. */\n preservedNumberOfDigits?: number;\n };\n\n /** Gas estimate fallback is used as a fallback in case of failure to obtain the gas estimate values. */\n gasEstimateFallback?: {\n /** Gas estimate fallback per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: GasEstimateFallback;\n };\n\n /**\n * Default gas estimate fallback.\n * This value is used when no specific gas estimate fallback is found for a chain ID.\n */\n default?: GasEstimateFallback;\n };\n\n /**\n * Number of attempts to wait before automatically marking a transaction as failed\n * if it has no receipt status and hash is not found on the network.\n */\n timeoutAttempts?: {\n /** Automatic fail threshold on a per-chain basis. */\n perChainConfig?: {\n [chainId: Hex]: number;\n };\n\n /**\n * Default automatic fail threshold.\n * This value is used when no specific threshold is found for a chain ID.\n */\n default?: number;\n };\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?.[FeatureFlag.EIP7702]?.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?.[FeatureFlag.EIP7702]?.contracts?.[\n chainId.toLowerCase() as Hex\n ] ?? [];\n\n return contracts\n .filter((contract) =>\n isValidSignature(\n [contract.address, padHexToEvenLength(chainId) as Hex],\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?.[FeatureFlag.Transactions]?.batchSizeLimit ??\n DEFAULT_BATCH_SIZE_LIMIT\n );\n}\n\n/**\n * Retrieves the accelerated polling parameters for a given chain ID.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The accelerated polling parameters: `countMax` and `intervalMs`.\n */\nexport function getAcceleratedPollingParams(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): { blockTime: number; countMax: number; intervalMs: number } {\n const featureFlags = getFeatureFlags(messenger);\n\n const acceleratedPollingParams =\n featureFlags?.[FeatureFlag.Transactions]?.acceleratedPolling;\n\n const countMax =\n acceleratedPollingParams?.perChainConfig?.[chainId]?.countMax ??\n acceleratedPollingParams?.defaultCountMax ??\n DEFAULT_ACCELERATED_POLLING_COUNT_MAX;\n\n const intervalMs =\n acceleratedPollingParams?.perChainConfig?.[chainId]?.intervalMs ??\n acceleratedPollingParams?.defaultIntervalMs ??\n DEFAULT_ACCELERATED_POLLING_INTERVAL_MS;\n\n const blockTime =\n acceleratedPollingParams?.perChainConfig?.[chainId]?.blockTime ??\n DEFAULT_BLOCK_TIME;\n\n return { blockTime, countMax, intervalMs };\n}\n\n/**\n * Retrieves the gas fee randomisation parameters.\n *\n * @param messenger - The controller messenger instance.\n * @returns The gas fee randomisation parameters.\n */\nexport function getGasFeeRandomisation(\n messenger: TransactionControllerMessenger,\n): {\n randomisedGasFeeDigits: Record<Hex, number>;\n preservedNumberOfDigits: number | undefined;\n} {\n const featureFlags = getFeatureFlags(messenger);\n\n const gasFeeRandomisation =\n featureFlags?.[FeatureFlag.Transactions]?.gasFeeRandomisation ?? {};\n\n return {\n randomisedGasFeeDigits: gasFeeRandomisation.randomisedGasFeeDigits ?? {},\n preservedNumberOfDigits: gasFeeRandomisation.preservedNumberOfDigits,\n };\n}\n\n/**\n * Retrieves the gas estimate fallback for a given chain ID.\n * Defaults to the default gas estimate fallback if not set.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The gas estimate fallback.\n */\nexport function getGasEstimateFallback(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): {\n fixed?: number;\n percentage: number;\n} {\n const featureFlags = getFeatureFlags(messenger);\n\n const gasEstimateFallbackFlags =\n featureFlags?.[FeatureFlag.Transactions]?.gasEstimateFallback;\n\n const chainFlags = gasEstimateFallbackFlags?.perChainConfig?.[chainId];\n\n const percentage =\n chainFlags?.percentage ??\n gasEstimateFallbackFlags?.default?.percentage ??\n DEFAULT_GAS_ESTIMATE_FALLBACK_BLOCK_PERCENT;\n\n const fixed = chainFlags?.fixed ?? gasEstimateFallbackFlags?.default?.fixed;\n\n return { fixed, percentage };\n}\n\n/**\n * Retrieves the gas buffers for a given chain ID.\n *\n * @param request - The request object.\n * @param request.chainId - The chain ID.\n * @param request.isCustomRPC - Whether the network RPC is added by the user.\n * @param request.isUpgradeWithDataToSelf - Whether the transaction is an EIP-7702 upgrade with data to self.\n * @param request.messenger - The controller messenger instance.\n * @returns The gas buffers.\n */\nexport function getGasEstimateBuffer({\n chainId,\n isCustomRPC,\n isUpgradeWithDataToSelf,\n messenger,\n}: {\n chainId: Hex;\n isCustomRPC: boolean;\n isUpgradeWithDataToSelf: boolean;\n messenger: TransactionControllerMessenger;\n}): number {\n const featureFlags = getFeatureFlags(messenger);\n const gasBufferFlags = featureFlags?.[FeatureFlag.GasBuffer];\n const chainFlags = gasBufferFlags?.perChainConfig?.[chainId];\n const chainIncludedRPCBuffer = isCustomRPC ? undefined : chainFlags?.included;\n\n const defaultIncludedRPCBuffer = isCustomRPC\n ? undefined\n : gasBufferFlags?.included;\n\n const upgradeBuffer = isUpgradeWithDataToSelf\n ? chainFlags?.eip7702\n : undefined;\n\n return (\n upgradeBuffer ??\n chainIncludedRPCBuffer ??\n chainFlags?.base ??\n defaultIncludedRPCBuffer ??\n gasBufferFlags?.default ??\n DEFAULT_GAS_ESTIMATE_BUFFER\n );\n}\n\n/**\n * Retrieves the incoming transactions polling interval.\n * Defaults to 4 minutes if not set.\n *\n * @param messenger - The controller messenger instance.\n * @returns The incoming transactions polling interval in milliseconds.\n */\nexport function getIncomingTransactionsPollingInterval(\n messenger: TransactionControllerMessenger,\n): number {\n const featureFlags = getFeatureFlags(messenger);\n\n return (\n featureFlags?.[FeatureFlag.IncomingTransactions]?.pollingIntervalMs ??\n DEFAULT_INCOMING_TRANSACTIONS_POLLING_INTERVAL_MS\n );\n}\n\n/**\n * Retrieves the number of attempts to wait before automatically marking a transaction as dropped\n * if it has no receipt status.\n *\n * @param chainId - The chain ID.\n * @param messenger - The controller messenger instance.\n * @returns The threshold number of attempts, or undefined if the feature is disabled.\n */\nexport function getTimeoutAttempts(\n chainId: Hex,\n messenger: TransactionControllerMessenger,\n): number | undefined {\n const featureFlags = getFeatureFlags(messenger);\n\n const timeoutAttemptsFlags =\n featureFlags?.[FeatureFlag.Transactions]?.timeoutAttempts;\n\n return (\n timeoutAttemptsFlags?.perChainConfig?.[chainId] ??\n timeoutAttemptsFlags?.default\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/transaction-controller",
|
|
3
|
-
"version": "62.7.0-preview-
|
|
3
|
+
"version": "62.7.0-preview-7bc2d97e",
|
|
4
4
|
"description": "Stores transactions alongside their periodically updated statuses and manages interactions such as approval and cancellation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"@metamask/metamask-eth-abis": "^3.1.1",
|
|
66
66
|
"@metamask/network-controller": "^27.1.0",
|
|
67
67
|
"@metamask/nonce-tracker": "^6.0.0",
|
|
68
|
-
"@metamask/remote-feature-flag-controller": "^
|
|
68
|
+
"@metamask/remote-feature-flag-controller": "^4.0.0",
|
|
69
69
|
"@metamask/rpc-errors": "^7.0.2",
|
|
70
|
-
"@metamask/utils": "^11.
|
|
70
|
+
"@metamask/utils": "^11.9.0",
|
|
71
71
|
"async-mutex": "^0.5.0",
|
|
72
72
|
"bignumber.js": "^9.1.2",
|
|
73
73
|
"bn.js": "^5.2.1",
|
package/dist/utils/history.cjs
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.updateTransactionHistory = exports.addInitialHistorySnapshot = exports.DISPLAYED_TRANSACTION_HISTORY_PATHS = exports.MAX_TRANSACTION_HISTORY_LENGTH = void 0;
|
|
7
|
-
const fast_json_patch_1 = __importDefault(require("fast-json-patch"));
|
|
8
|
-
const lodash_1 = require("lodash");
|
|
9
|
-
/**
|
|
10
|
-
* The maximum allowed length of the `transaction.history` property.
|
|
11
|
-
*/
|
|
12
|
-
exports.MAX_TRANSACTION_HISTORY_LENGTH = 100;
|
|
13
|
-
/**
|
|
14
|
-
* A list of trarnsaction history paths that may be used for display. These entries will not be
|
|
15
|
-
* compressed.
|
|
16
|
-
*/
|
|
17
|
-
exports.DISPLAYED_TRANSACTION_HISTORY_PATHS = [
|
|
18
|
-
'/status',
|
|
19
|
-
'/txParams/gasPrice',
|
|
20
|
-
'/txParams/gas',
|
|
21
|
-
'/estimatedBaseFee',
|
|
22
|
-
'/blockTimestamp',
|
|
23
|
-
];
|
|
24
|
-
/**
|
|
25
|
-
* Build a new version of the provided transaction with an initial history
|
|
26
|
-
* entry, which is just a snapshot of the transaction.
|
|
27
|
-
*
|
|
28
|
-
* @param transactionMeta - TransactionMeta to add initial history snapshot to.
|
|
29
|
-
* @returns A copy of `transactionMeta` with a new `history` property.
|
|
30
|
-
*/
|
|
31
|
-
function addInitialHistorySnapshot(transactionMeta) {
|
|
32
|
-
const snapshot = snapshotFromTransactionMeta(transactionMeta);
|
|
33
|
-
return (0, lodash_1.merge)({}, transactionMeta, { history: [snapshot] });
|
|
34
|
-
}
|
|
35
|
-
exports.addInitialHistorySnapshot = addInitialHistorySnapshot;
|
|
36
|
-
/**
|
|
37
|
-
* Builds a new version of the transaction with a new history entry if
|
|
38
|
-
* it has a `history` property, or just returns the transaction.
|
|
39
|
-
*
|
|
40
|
-
* @param transactionMeta - TransactionMeta to add history entry to.
|
|
41
|
-
* @param note - Note to add to history entry.
|
|
42
|
-
* @returns A copy of `transactionMeta` with a new `history` entry if it has an
|
|
43
|
-
* existing non-empty `history` array.
|
|
44
|
-
*/
|
|
45
|
-
function updateTransactionHistory(transactionMeta, note) {
|
|
46
|
-
if (!transactionMeta.history) {
|
|
47
|
-
return transactionMeta;
|
|
48
|
-
}
|
|
49
|
-
const currentState = snapshotFromTransactionMeta(transactionMeta);
|
|
50
|
-
const previousState = replayHistory(transactionMeta.history);
|
|
51
|
-
const newHistoryEntry = generateHistoryEntry(previousState, currentState, note);
|
|
52
|
-
if (newHistoryEntry.length === 0) {
|
|
53
|
-
return transactionMeta;
|
|
54
|
-
}
|
|
55
|
-
// Casts required here because this list has two separate types of entries:
|
|
56
|
-
// TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first
|
|
57
|
-
// entry, but TypeScript loses that type information when `slice` is called for some reason.
|
|
58
|
-
let updatedHistory = [
|
|
59
|
-
...transactionMeta.history,
|
|
60
|
-
newHistoryEntry,
|
|
61
|
-
];
|
|
62
|
-
if (updatedHistory.length > exports.MAX_TRANSACTION_HISTORY_LENGTH) {
|
|
63
|
-
updatedHistory = compressTransactionHistory(updatedHistory);
|
|
64
|
-
}
|
|
65
|
-
return (0, lodash_1.merge)({}, transactionMeta, {
|
|
66
|
-
history: updatedHistory,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
exports.updateTransactionHistory = updateTransactionHistory;
|
|
70
|
-
/**
|
|
71
|
-
* Compress the transaction history, if it is possible to do so without compressing entries used
|
|
72
|
-
* for display. History entries are merged together to make room for a single new entry.
|
|
73
|
-
*
|
|
74
|
-
* @param transactionHistory - The transaction history to compress.
|
|
75
|
-
* @returns A compressed transaction history.
|
|
76
|
-
*/
|
|
77
|
-
function compressTransactionHistory(transactionHistory) {
|
|
78
|
-
const initialEntry = transactionHistory[0];
|
|
79
|
-
// Casts required here because this list has two separate types of entries:
|
|
80
|
-
// TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first
|
|
81
|
-
// entry, but TypeScript loses that type information when `slice` is called for some reason.
|
|
82
|
-
const historyEntries = transactionHistory.slice(1);
|
|
83
|
-
const firstNonDisplayedEntryIndex = historyEntries.findIndex((historyEntry) => {
|
|
84
|
-
return !historyEntry.some(({ path }) => exports.DISPLAYED_TRANSACTION_HISTORY_PATHS.includes(path));
|
|
85
|
-
});
|
|
86
|
-
// If no non-displayed entry is found, let history exceed max size.
|
|
87
|
-
// TODO: Move data used for display to another property, so that we can more reliably limit
|
|
88
|
-
// history size or remove it altogether.
|
|
89
|
-
if (firstNonDisplayedEntryIndex === -1) {
|
|
90
|
-
return transactionHistory;
|
|
91
|
-
}
|
|
92
|
-
// If a non-displayed entry is found that we can remove, merge it with another entry.
|
|
93
|
-
// The entry we're merging with might be a "displayed" entry, but that's OK, merging more changes
|
|
94
|
-
// in does not break our display logic.
|
|
95
|
-
const mergeTargetEntryIndex =
|
|
96
|
-
// Merge with previous entry if there is no next entry.
|
|
97
|
-
// We default to merging with next because the next entry might also be non-displayed, so it
|
|
98
|
-
// might be removed in a future trim, saving more space.
|
|
99
|
-
firstNonDisplayedEntryIndex === historyEntries.length - 1
|
|
100
|
-
? firstNonDisplayedEntryIndex - 1
|
|
101
|
-
: firstNonDisplayedEntryIndex + 1;
|
|
102
|
-
const firstIndexToMerge = Math.min(firstNonDisplayedEntryIndex, mergeTargetEntryIndex);
|
|
103
|
-
const firstEntryToMerge = historyEntries[firstIndexToMerge];
|
|
104
|
-
const secondEntryToMerge = historyEntries[firstIndexToMerge + 1];
|
|
105
|
-
const beforeMergeState = replayHistory([
|
|
106
|
-
initialEntry,
|
|
107
|
-
...historyEntries.slice(0, firstIndexToMerge),
|
|
108
|
-
]);
|
|
109
|
-
const afterMergeState = replayHistory([
|
|
110
|
-
beforeMergeState,
|
|
111
|
-
firstEntryToMerge,
|
|
112
|
-
secondEntryToMerge,
|
|
113
|
-
]);
|
|
114
|
-
const mergedHistoryEntry = generateHistoryEntry(beforeMergeState, afterMergeState, `${String(firstEntryToMerge[0].note)}, ${String(secondEntryToMerge[0].note)}`);
|
|
115
|
-
historyEntries.splice(firstIndexToMerge, 2, mergedHistoryEntry);
|
|
116
|
-
return [initialEntry, ...historyEntries];
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Generates a history entry from the previous and new transaction metadata.
|
|
120
|
-
*
|
|
121
|
-
* @param previousState - The previous transaction metadata.
|
|
122
|
-
* @param currentState - The new transaction metadata.
|
|
123
|
-
* @param note - A note for the transaction metada update.
|
|
124
|
-
* @returns An array of history operation.
|
|
125
|
-
*/
|
|
126
|
-
function generateHistoryEntry(
|
|
127
|
-
// TODO: Replace `any` with type
|
|
128
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
|
-
previousState, currentState, note) {
|
|
130
|
-
const historyOperationsEntry = fast_json_patch_1.default.compare(previousState, currentState);
|
|
131
|
-
// Add a note to the first operation, since it breaks if we append it to the entry
|
|
132
|
-
if (historyOperationsEntry[0]) {
|
|
133
|
-
if (note) {
|
|
134
|
-
historyOperationsEntry[0].note = note;
|
|
135
|
-
}
|
|
136
|
-
historyOperationsEntry[0].timestamp = Date.now();
|
|
137
|
-
}
|
|
138
|
-
return historyOperationsEntry;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Recovers previous transactionMeta from passed history array.
|
|
142
|
-
*
|
|
143
|
-
* @param transactionHistory - The transaction metadata to replay.
|
|
144
|
-
* @returns The transaction metadata.
|
|
145
|
-
*/
|
|
146
|
-
function replayHistory(transactionHistory) {
|
|
147
|
-
const shortHistory = (0, lodash_1.cloneDeep)(transactionHistory);
|
|
148
|
-
return shortHistory.reduce(
|
|
149
|
-
// TODO: Replace `any` with type
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
151
|
-
(val, entry) => fast_json_patch_1.default.applyPatch(val, entry).newDocument);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Clone the transaction meta data without the history property.
|
|
155
|
-
*
|
|
156
|
-
* @param transactionMeta - The transaction metadata to snapshot.
|
|
157
|
-
* @returns A deep clone of transaction metadata without history property.
|
|
158
|
-
*/
|
|
159
|
-
function snapshotFromTransactionMeta(transactionMeta) {
|
|
160
|
-
const snapshot = { ...transactionMeta };
|
|
161
|
-
delete snapshot.history;
|
|
162
|
-
return (0, lodash_1.cloneDeep)(snapshot);
|
|
163
|
-
}
|
|
164
|
-
//# sourceMappingURL=history.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"history.cjs","sourceRoot":"","sources":["../../src/utils/history.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAyC;AACzC,mCAA0C;AAQ1C;;GAEG;AACU,QAAA,8BAA8B,GAAG,GAAG,CAAC;AAElD;;;GAGG;AACU,QAAA,mCAAmC,GAAG;IACjD,SAAS;IACT,oBAAoB;IACpB,eAAe;IACf,mBAAmB;IACnB,iBAAiB;CAClB,CAAC;AAEF;;;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["import jsonDiffer from 'fast-json-patch';\nimport { cloneDeep, merge } from 'lodash';\n\nimport type {\n TransactionHistory,\n TransactionHistoryEntry,\n TransactionMeta,\n} from '../types';\n\n/**\n * The maximum allowed length of the `transaction.history` property.\n */\nexport const MAX_TRANSACTION_HISTORY_LENGTH = 100;\n\n/**\n * A list of trarnsaction history paths that may be used for display. These entries will not be\n * compressed.\n */\nexport const DISPLAYED_TRANSACTION_HISTORY_PATHS = [\n '/status',\n '/txParams/gasPrice',\n '/txParams/gas',\n '/estimatedBaseFee',\n '/blockTimestamp',\n];\n\n/**\n * Build a new version of the provided transaction with an initial history\n * entry, which is just a snapshot of the transaction.\n *\n * @param transactionMeta - TransactionMeta to add initial history snapshot to.\n * @returns A copy of `transactionMeta` with a new `history` property.\n */\nexport function addInitialHistorySnapshot(\n transactionMeta: TransactionMeta,\n): TransactionMeta {\n const snapshot = snapshotFromTransactionMeta(transactionMeta);\n return merge({}, transactionMeta, { history: [snapshot] });\n}\n\n/**\n * Builds a new version of the transaction with a new history entry if\n * it has a `history` property, or just returns the transaction.\n *\n * @param transactionMeta - TransactionMeta to add history entry to.\n * @param note - Note to add to history entry.\n * @returns A copy of `transactionMeta` with a new `history` entry if it has an\n * existing non-empty `history` array.\n */\nexport function updateTransactionHistory(\n transactionMeta: TransactionMeta,\n note: string,\n): TransactionMeta {\n if (!transactionMeta.history) {\n return transactionMeta;\n }\n\n const currentState = snapshotFromTransactionMeta(transactionMeta);\n const previousState = replayHistory(transactionMeta.history);\n const newHistoryEntry = generateHistoryEntry(\n previousState,\n currentState,\n note,\n );\n\n if (newHistoryEntry.length === 0) {\n return transactionMeta;\n }\n\n // Casts required here because this list has two separate types of entries:\n // TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first\n // entry, but TypeScript loses that type information when `slice` is called for some reason.\n let updatedHistory = [\n ...transactionMeta.history,\n newHistoryEntry,\n ] as TransactionHistory;\n\n if (updatedHistory.length > MAX_TRANSACTION_HISTORY_LENGTH) {\n updatedHistory = compressTransactionHistory(updatedHistory);\n }\n\n return merge({}, transactionMeta, {\n history: updatedHistory,\n });\n}\n\n/**\n * Compress the transaction history, if it is possible to do so without compressing entries used\n * for display. History entries are merged together to make room for a single new entry.\n *\n * @param transactionHistory - The transaction history to compress.\n * @returns A compressed transaction history.\n */\nfunction compressTransactionHistory(\n transactionHistory: TransactionHistory,\n): TransactionHistory {\n const initialEntry = transactionHistory[0];\n // Casts required here because this list has two separate types of entries:\n // TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first\n // entry, but TypeScript loses that type information when `slice` is called for some reason.\n const historyEntries = transactionHistory.slice(\n 1,\n ) as TransactionHistoryEntry[];\n\n const firstNonDisplayedEntryIndex = historyEntries.findIndex(\n (historyEntry) => {\n return !historyEntry.some(({ path }) =>\n DISPLAYED_TRANSACTION_HISTORY_PATHS.includes(path),\n );\n },\n );\n\n // If no non-displayed entry is found, let history exceed max size.\n // TODO: Move data used for display to another property, so that we can more reliably limit\n // history size or remove it altogether.\n if (firstNonDisplayedEntryIndex === -1) {\n return transactionHistory;\n }\n\n // If a non-displayed entry is found that we can remove, merge it with another entry.\n // The entry we're merging with might be a \"displayed\" entry, but that's OK, merging more changes\n // in does not break our display logic.\n const mergeTargetEntryIndex =\n // Merge with previous entry if there is no next entry.\n // We default to merging with next because the next entry might also be non-displayed, so it\n // might be removed in a future trim, saving more space.\n firstNonDisplayedEntryIndex === historyEntries.length - 1\n ? firstNonDisplayedEntryIndex - 1\n : firstNonDisplayedEntryIndex + 1;\n const firstIndexToMerge = Math.min(\n firstNonDisplayedEntryIndex,\n mergeTargetEntryIndex,\n );\n const firstEntryToMerge = historyEntries[firstIndexToMerge];\n const secondEntryToMerge = historyEntries[firstIndexToMerge + 1];\n\n const beforeMergeState = replayHistory([\n initialEntry,\n ...historyEntries.slice(0, firstIndexToMerge),\n ]);\n const afterMergeState = replayHistory([\n beforeMergeState,\n firstEntryToMerge,\n secondEntryToMerge,\n ]);\n const mergedHistoryEntry = generateHistoryEntry(\n beforeMergeState,\n afterMergeState,\n `${String(firstEntryToMerge[0].note)}, ${String(\n secondEntryToMerge[0].note,\n )}`,\n );\n\n historyEntries.splice(firstIndexToMerge, 2, mergedHistoryEntry);\n return [initialEntry, ...historyEntries];\n}\n\n/**\n * Generates a history entry from the previous and new transaction metadata.\n *\n * @param previousState - The previous transaction metadata.\n * @param currentState - The new transaction metadata.\n * @param note - A note for the transaction metada update.\n * @returns An array of history operation.\n */\nfunction generateHistoryEntry(\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousState: any,\n currentState: TransactionMeta,\n note: string,\n): TransactionHistoryEntry {\n const historyOperationsEntry = jsonDiffer.compare(\n previousState,\n currentState,\n ) as TransactionHistoryEntry;\n // Add a note to the first operation, since it breaks if we append it to the entry\n if (historyOperationsEntry[0]) {\n if (note) {\n historyOperationsEntry[0].note = note;\n }\n historyOperationsEntry[0].timestamp = Date.now();\n }\n return historyOperationsEntry;\n}\n\n/**\n * Recovers previous transactionMeta from passed history array.\n *\n * @param transactionHistory - The transaction metadata to replay.\n * @returns The transaction metadata.\n */\nfunction replayHistory(\n transactionHistory: TransactionHistory,\n): TransactionMeta {\n const shortHistory = cloneDeep(transactionHistory);\n return shortHistory.reduce(\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (val, entry: any) => jsonDiffer.applyPatch(val, entry).newDocument,\n ) as TransactionMeta;\n}\n\n/**\n * Clone the transaction meta data without the history property.\n *\n * @param transactionMeta - The transaction metadata to snapshot.\n * @returns A deep clone of transaction metadata without history property.\n */\nfunction snapshotFromTransactionMeta(\n transactionMeta: TransactionMeta,\n): TransactionMeta {\n const snapshot = { ...transactionMeta };\n delete snapshot.history;\n return cloneDeep(snapshot);\n}\n"]}
|
package/dist/utils/history.d.cts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { TransactionMeta } from "../types.cjs";
|
|
2
|
-
/**
|
|
3
|
-
* The maximum allowed length of the `transaction.history` property.
|
|
4
|
-
*/
|
|
5
|
-
export declare const MAX_TRANSACTION_HISTORY_LENGTH = 100;
|
|
6
|
-
/**
|
|
7
|
-
* A list of trarnsaction history paths that may be used for display. These entries will not be
|
|
8
|
-
* compressed.
|
|
9
|
-
*/
|
|
10
|
-
export declare const DISPLAYED_TRANSACTION_HISTORY_PATHS: string[];
|
|
11
|
-
/**
|
|
12
|
-
* Build a new version of the provided transaction with an initial history
|
|
13
|
-
* entry, which is just a snapshot of the transaction.
|
|
14
|
-
*
|
|
15
|
-
* @param transactionMeta - TransactionMeta to add initial history snapshot to.
|
|
16
|
-
* @returns A copy of `transactionMeta` with a new `history` property.
|
|
17
|
-
*/
|
|
18
|
-
export declare function addInitialHistorySnapshot(transactionMeta: TransactionMeta): TransactionMeta;
|
|
19
|
-
/**
|
|
20
|
-
* Builds a new version of the transaction with a new history entry if
|
|
21
|
-
* it has a `history` property, or just returns the transaction.
|
|
22
|
-
*
|
|
23
|
-
* @param transactionMeta - TransactionMeta to add history entry to.
|
|
24
|
-
* @param note - Note to add to history entry.
|
|
25
|
-
* @returns A copy of `transactionMeta` with a new `history` entry if it has an
|
|
26
|
-
* existing non-empty `history` array.
|
|
27
|
-
*/
|
|
28
|
-
export declare function updateTransactionHistory(transactionMeta: TransactionMeta, note: string): TransactionMeta;
|
|
29
|
-
//# sourceMappingURL=history.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"history.d.cts","sourceRoot":"","sources":["../../src/utils/history.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAGV,eAAe,EAChB,qBAAiB;AAElB;;GAEG;AACH,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAElD;;;GAGG;AACH,eAAO,MAAM,mCAAmC,UAM/C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACvC,eAAe,EAAE,eAAe,GAC/B,eAAe,CAGjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,eAAe,EAAE,eAAe,EAChC,IAAI,EAAE,MAAM,GACX,eAAe,CAgCjB"}
|
package/dist/utils/history.d.mts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { TransactionMeta } from "../types.mjs";
|
|
2
|
-
/**
|
|
3
|
-
* The maximum allowed length of the `transaction.history` property.
|
|
4
|
-
*/
|
|
5
|
-
export declare const MAX_TRANSACTION_HISTORY_LENGTH = 100;
|
|
6
|
-
/**
|
|
7
|
-
* A list of trarnsaction history paths that may be used for display. These entries will not be
|
|
8
|
-
* compressed.
|
|
9
|
-
*/
|
|
10
|
-
export declare const DISPLAYED_TRANSACTION_HISTORY_PATHS: string[];
|
|
11
|
-
/**
|
|
12
|
-
* Build a new version of the provided transaction with an initial history
|
|
13
|
-
* entry, which is just a snapshot of the transaction.
|
|
14
|
-
*
|
|
15
|
-
* @param transactionMeta - TransactionMeta to add initial history snapshot to.
|
|
16
|
-
* @returns A copy of `transactionMeta` with a new `history` property.
|
|
17
|
-
*/
|
|
18
|
-
export declare function addInitialHistorySnapshot(transactionMeta: TransactionMeta): TransactionMeta;
|
|
19
|
-
/**
|
|
20
|
-
* Builds a new version of the transaction with a new history entry if
|
|
21
|
-
* it has a `history` property, or just returns the transaction.
|
|
22
|
-
*
|
|
23
|
-
* @param transactionMeta - TransactionMeta to add history entry to.
|
|
24
|
-
* @param note - Note to add to history entry.
|
|
25
|
-
* @returns A copy of `transactionMeta` with a new `history` entry if it has an
|
|
26
|
-
* existing non-empty `history` array.
|
|
27
|
-
*/
|
|
28
|
-
export declare function updateTransactionHistory(transactionMeta: TransactionMeta, note: string): TransactionMeta;
|
|
29
|
-
//# sourceMappingURL=history.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"history.d.mts","sourceRoot":"","sources":["../../src/utils/history.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAGV,eAAe,EAChB,qBAAiB;AAElB;;GAEG;AACH,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAElD;;;GAGG;AACH,eAAO,MAAM,mCAAmC,UAM/C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACvC,eAAe,EAAE,eAAe,GAC/B,eAAe,CAGjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,eAAe,EAAE,eAAe,EAChC,IAAI,EAAE,MAAM,GACX,eAAe,CAgCjB"}
|
package/dist/utils/history.mjs
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
function $importDefault(module) {
|
|
2
|
-
if (module?.__esModule) {
|
|
3
|
-
return module.default;
|
|
4
|
-
}
|
|
5
|
-
return module;
|
|
6
|
-
}
|
|
7
|
-
import $jsonDiffer from "fast-json-patch";
|
|
8
|
-
const jsonDiffer = $importDefault($jsonDiffer);
|
|
9
|
-
import $lodash from "lodash";
|
|
10
|
-
const { cloneDeep, merge } = $lodash;
|
|
11
|
-
/**
|
|
12
|
-
* The maximum allowed length of the `transaction.history` property.
|
|
13
|
-
*/
|
|
14
|
-
export const MAX_TRANSACTION_HISTORY_LENGTH = 100;
|
|
15
|
-
/**
|
|
16
|
-
* A list of trarnsaction history paths that may be used for display. These entries will not be
|
|
17
|
-
* compressed.
|
|
18
|
-
*/
|
|
19
|
-
export const DISPLAYED_TRANSACTION_HISTORY_PATHS = [
|
|
20
|
-
'/status',
|
|
21
|
-
'/txParams/gasPrice',
|
|
22
|
-
'/txParams/gas',
|
|
23
|
-
'/estimatedBaseFee',
|
|
24
|
-
'/blockTimestamp',
|
|
25
|
-
];
|
|
26
|
-
/**
|
|
27
|
-
* Build a new version of the provided transaction with an initial history
|
|
28
|
-
* entry, which is just a snapshot of the transaction.
|
|
29
|
-
*
|
|
30
|
-
* @param transactionMeta - TransactionMeta to add initial history snapshot to.
|
|
31
|
-
* @returns A copy of `transactionMeta` with a new `history` property.
|
|
32
|
-
*/
|
|
33
|
-
export function addInitialHistorySnapshot(transactionMeta) {
|
|
34
|
-
const snapshot = snapshotFromTransactionMeta(transactionMeta);
|
|
35
|
-
return merge({}, transactionMeta, { history: [snapshot] });
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Builds a new version of the transaction with a new history entry if
|
|
39
|
-
* it has a `history` property, or just returns the transaction.
|
|
40
|
-
*
|
|
41
|
-
* @param transactionMeta - TransactionMeta to add history entry to.
|
|
42
|
-
* @param note - Note to add to history entry.
|
|
43
|
-
* @returns A copy of `transactionMeta` with a new `history` entry if it has an
|
|
44
|
-
* existing non-empty `history` array.
|
|
45
|
-
*/
|
|
46
|
-
export function updateTransactionHistory(transactionMeta, note) {
|
|
47
|
-
if (!transactionMeta.history) {
|
|
48
|
-
return transactionMeta;
|
|
49
|
-
}
|
|
50
|
-
const currentState = snapshotFromTransactionMeta(transactionMeta);
|
|
51
|
-
const previousState = replayHistory(transactionMeta.history);
|
|
52
|
-
const newHistoryEntry = generateHistoryEntry(previousState, currentState, note);
|
|
53
|
-
if (newHistoryEntry.length === 0) {
|
|
54
|
-
return transactionMeta;
|
|
55
|
-
}
|
|
56
|
-
// Casts required here because this list has two separate types of entries:
|
|
57
|
-
// TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first
|
|
58
|
-
// entry, but TypeScript loses that type information when `slice` is called for some reason.
|
|
59
|
-
let updatedHistory = [
|
|
60
|
-
...transactionMeta.history,
|
|
61
|
-
newHistoryEntry,
|
|
62
|
-
];
|
|
63
|
-
if (updatedHistory.length > MAX_TRANSACTION_HISTORY_LENGTH) {
|
|
64
|
-
updatedHistory = compressTransactionHistory(updatedHistory);
|
|
65
|
-
}
|
|
66
|
-
return merge({}, transactionMeta, {
|
|
67
|
-
history: updatedHistory,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Compress the transaction history, if it is possible to do so without compressing entries used
|
|
72
|
-
* for display. History entries are merged together to make room for a single new entry.
|
|
73
|
-
*
|
|
74
|
-
* @param transactionHistory - The transaction history to compress.
|
|
75
|
-
* @returns A compressed transaction history.
|
|
76
|
-
*/
|
|
77
|
-
function compressTransactionHistory(transactionHistory) {
|
|
78
|
-
const initialEntry = transactionHistory[0];
|
|
79
|
-
// Casts required here because this list has two separate types of entries:
|
|
80
|
-
// TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first
|
|
81
|
-
// entry, but TypeScript loses that type information when `slice` is called for some reason.
|
|
82
|
-
const historyEntries = transactionHistory.slice(1);
|
|
83
|
-
const firstNonDisplayedEntryIndex = historyEntries.findIndex((historyEntry) => {
|
|
84
|
-
return !historyEntry.some(({ path }) => DISPLAYED_TRANSACTION_HISTORY_PATHS.includes(path));
|
|
85
|
-
});
|
|
86
|
-
// If no non-displayed entry is found, let history exceed max size.
|
|
87
|
-
// TODO: Move data used for display to another property, so that we can more reliably limit
|
|
88
|
-
// history size or remove it altogether.
|
|
89
|
-
if (firstNonDisplayedEntryIndex === -1) {
|
|
90
|
-
return transactionHistory;
|
|
91
|
-
}
|
|
92
|
-
// If a non-displayed entry is found that we can remove, merge it with another entry.
|
|
93
|
-
// The entry we're merging with might be a "displayed" entry, but that's OK, merging more changes
|
|
94
|
-
// in does not break our display logic.
|
|
95
|
-
const mergeTargetEntryIndex =
|
|
96
|
-
// Merge with previous entry if there is no next entry.
|
|
97
|
-
// We default to merging with next because the next entry might also be non-displayed, so it
|
|
98
|
-
// might be removed in a future trim, saving more space.
|
|
99
|
-
firstNonDisplayedEntryIndex === historyEntries.length - 1
|
|
100
|
-
? firstNonDisplayedEntryIndex - 1
|
|
101
|
-
: firstNonDisplayedEntryIndex + 1;
|
|
102
|
-
const firstIndexToMerge = Math.min(firstNonDisplayedEntryIndex, mergeTargetEntryIndex);
|
|
103
|
-
const firstEntryToMerge = historyEntries[firstIndexToMerge];
|
|
104
|
-
const secondEntryToMerge = historyEntries[firstIndexToMerge + 1];
|
|
105
|
-
const beforeMergeState = replayHistory([
|
|
106
|
-
initialEntry,
|
|
107
|
-
...historyEntries.slice(0, firstIndexToMerge),
|
|
108
|
-
]);
|
|
109
|
-
const afterMergeState = replayHistory([
|
|
110
|
-
beforeMergeState,
|
|
111
|
-
firstEntryToMerge,
|
|
112
|
-
secondEntryToMerge,
|
|
113
|
-
]);
|
|
114
|
-
const mergedHistoryEntry = generateHistoryEntry(beforeMergeState, afterMergeState, `${String(firstEntryToMerge[0].note)}, ${String(secondEntryToMerge[0].note)}`);
|
|
115
|
-
historyEntries.splice(firstIndexToMerge, 2, mergedHistoryEntry);
|
|
116
|
-
return [initialEntry, ...historyEntries];
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Generates a history entry from the previous and new transaction metadata.
|
|
120
|
-
*
|
|
121
|
-
* @param previousState - The previous transaction metadata.
|
|
122
|
-
* @param currentState - The new transaction metadata.
|
|
123
|
-
* @param note - A note for the transaction metada update.
|
|
124
|
-
* @returns An array of history operation.
|
|
125
|
-
*/
|
|
126
|
-
function generateHistoryEntry(
|
|
127
|
-
// TODO: Replace `any` with type
|
|
128
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
|
-
previousState, currentState, note) {
|
|
130
|
-
const historyOperationsEntry = jsonDiffer.compare(previousState, currentState);
|
|
131
|
-
// Add a note to the first operation, since it breaks if we append it to the entry
|
|
132
|
-
if (historyOperationsEntry[0]) {
|
|
133
|
-
if (note) {
|
|
134
|
-
historyOperationsEntry[0].note = note;
|
|
135
|
-
}
|
|
136
|
-
historyOperationsEntry[0].timestamp = Date.now();
|
|
137
|
-
}
|
|
138
|
-
return historyOperationsEntry;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Recovers previous transactionMeta from passed history array.
|
|
142
|
-
*
|
|
143
|
-
* @param transactionHistory - The transaction metadata to replay.
|
|
144
|
-
* @returns The transaction metadata.
|
|
145
|
-
*/
|
|
146
|
-
function replayHistory(transactionHistory) {
|
|
147
|
-
const shortHistory = cloneDeep(transactionHistory);
|
|
148
|
-
return shortHistory.reduce(
|
|
149
|
-
// TODO: Replace `any` with type
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
151
|
-
(val, entry) => jsonDiffer.applyPatch(val, entry).newDocument);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Clone the transaction meta data without the history property.
|
|
155
|
-
*
|
|
156
|
-
* @param transactionMeta - The transaction metadata to snapshot.
|
|
157
|
-
* @returns A deep clone of transaction metadata without history property.
|
|
158
|
-
*/
|
|
159
|
-
function snapshotFromTransactionMeta(transactionMeta) {
|
|
160
|
-
const snapshot = { ...transactionMeta };
|
|
161
|
-
delete snapshot.history;
|
|
162
|
-
return cloneDeep(snapshot);
|
|
163
|
-
}
|
|
164
|
-
//# sourceMappingURL=history.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"history.mjs","sourceRoot":"","sources":["../../src/utils/history.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,WAAU,wBAAwB;;;;AASzC;;GAEG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,CAAC;AAElD;;;GAGG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG;IACjD,SAAS;IACT,oBAAoB;IACpB,eAAe;IACf,mBAAmB;IACnB,iBAAiB;CAClB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB,CACvC,eAAgC;IAEhC,MAAM,QAAQ,GAAG,2BAA2B,CAAC,eAAe,CAAC,CAAC;IAC9D,OAAO,KAAK,CAAC,EAAE,EAAE,eAAe,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,wBAAwB,CACtC,eAAgC,EAChC,IAAY;IAEZ,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,CAAC;QAC7B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,YAAY,GAAG,2BAA2B,CAAC,eAAe,CAAC,CAAC;IAClE,MA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C;QAC9B,IAAI,IAAI,EAAE,CAAC;YACT,sBAAsB,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC;QACxC,CAAC;QACD,sBAAsB,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACnD,CAAC;IACD,OAAO,sBAAsB,CAAC;AAChC,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CACpB,kBAAsC;IAEtC,MAAM,YAAY,GAAG,SAAS,CAAC,kBAAkB,CAAC,CAAC;IACnD,OAAO,YAAY,CAAC,MAAM;IACxB,gCAAgC;IAChC,8DAA8D;IAC9D,CAAC,GAAG,EAAE,KAAU,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,WAAW,CAChD,CAAC;AACvB,CAAC;AAED;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,eAAgC;IAEhC,MAAM,QAAQ,GAAG,EAAE,GAAG,eAAe,EAAE,CAAC;IACxC,OAAO,QAAQ,CAAC,OAAO,CAAC;IACxB,OAAO,SAAS,CAAC,QAAQ,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["import jsonDiffer from 'fast-json-patch';\nimport { cloneDeep, merge } from 'lodash';\n\nimport type {\n TransactionHistory,\n TransactionHistoryEntry,\n TransactionMeta,\n} from '../types';\n\n/**\n * The maximum allowed length of the `transaction.history` property.\n */\nexport const MAX_TRANSACTION_HISTORY_LENGTH = 100;\n\n/**\n * A list of trarnsaction history paths that may be used for display. These entries will not be\n * compressed.\n */\nexport const DISPLAYED_TRANSACTION_HISTORY_PATHS = [\n '/status',\n '/txParams/gasPrice',\n '/txParams/gas',\n '/estimatedBaseFee',\n '/blockTimestamp',\n];\n\n/**\n * Build a new version of the provided transaction with an initial history\n * entry, which is just a snapshot of the transaction.\n *\n * @param transactionMeta - TransactionMeta to add initial history snapshot to.\n * @returns A copy of `transactionMeta` with a new `history` property.\n */\nexport function addInitialHistorySnapshot(\n transactionMeta: TransactionMeta,\n): TransactionMeta {\n const snapshot = snapshotFromTransactionMeta(transactionMeta);\n return merge({}, transactionMeta, { history: [snapshot] });\n}\n\n/**\n * Builds a new version of the transaction with a new history entry if\n * it has a `history` property, or just returns the transaction.\n *\n * @param transactionMeta - TransactionMeta to add history entry to.\n * @param note - Note to add to history entry.\n * @returns A copy of `transactionMeta` with a new `history` entry if it has an\n * existing non-empty `history` array.\n */\nexport function updateTransactionHistory(\n transactionMeta: TransactionMeta,\n note: string,\n): TransactionMeta {\n if (!transactionMeta.history) {\n return transactionMeta;\n }\n\n const currentState = snapshotFromTransactionMeta(transactionMeta);\n const previousState = replayHistory(transactionMeta.history);\n const newHistoryEntry = generateHistoryEntry(\n previousState,\n currentState,\n note,\n );\n\n if (newHistoryEntry.length === 0) {\n return transactionMeta;\n }\n\n // Casts required here because this list has two separate types of entries:\n // TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first\n // entry, but TypeScript loses that type information when `slice` is called for some reason.\n let updatedHistory = [\n ...transactionMeta.history,\n newHistoryEntry,\n ] as TransactionHistory;\n\n if (updatedHistory.length > MAX_TRANSACTION_HISTORY_LENGTH) {\n updatedHistory = compressTransactionHistory(updatedHistory);\n }\n\n return merge({}, transactionMeta, {\n history: updatedHistory,\n });\n}\n\n/**\n * Compress the transaction history, if it is possible to do so without compressing entries used\n * for display. History entries are merged together to make room for a single new entry.\n *\n * @param transactionHistory - The transaction history to compress.\n * @returns A compressed transaction history.\n */\nfunction compressTransactionHistory(\n transactionHistory: TransactionHistory,\n): TransactionHistory {\n const initialEntry = transactionHistory[0];\n // Casts required here because this list has two separate types of entries:\n // TransactionMeta and TransactionHistoryEntry. The only TransactionMeta is the first\n // entry, but TypeScript loses that type information when `slice` is called for some reason.\n const historyEntries = transactionHistory.slice(\n 1,\n ) as TransactionHistoryEntry[];\n\n const firstNonDisplayedEntryIndex = historyEntries.findIndex(\n (historyEntry) => {\n return !historyEntry.some(({ path }) =>\n DISPLAYED_TRANSACTION_HISTORY_PATHS.includes(path),\n );\n },\n );\n\n // If no non-displayed entry is found, let history exceed max size.\n // TODO: Move data used for display to another property, so that we can more reliably limit\n // history size or remove it altogether.\n if (firstNonDisplayedEntryIndex === -1) {\n return transactionHistory;\n }\n\n // If a non-displayed entry is found that we can remove, merge it with another entry.\n // The entry we're merging with might be a \"displayed\" entry, but that's OK, merging more changes\n // in does not break our display logic.\n const mergeTargetEntryIndex =\n // Merge with previous entry if there is no next entry.\n // We default to merging with next because the next entry might also be non-displayed, so it\n // might be removed in a future trim, saving more space.\n firstNonDisplayedEntryIndex === historyEntries.length - 1\n ? firstNonDisplayedEntryIndex - 1\n : firstNonDisplayedEntryIndex + 1;\n const firstIndexToMerge = Math.min(\n firstNonDisplayedEntryIndex,\n mergeTargetEntryIndex,\n );\n const firstEntryToMerge = historyEntries[firstIndexToMerge];\n const secondEntryToMerge = historyEntries[firstIndexToMerge + 1];\n\n const beforeMergeState = replayHistory([\n initialEntry,\n ...historyEntries.slice(0, firstIndexToMerge),\n ]);\n const afterMergeState = replayHistory([\n beforeMergeState,\n firstEntryToMerge,\n secondEntryToMerge,\n ]);\n const mergedHistoryEntry = generateHistoryEntry(\n beforeMergeState,\n afterMergeState,\n `${String(firstEntryToMerge[0].note)}, ${String(\n secondEntryToMerge[0].note,\n )}`,\n );\n\n historyEntries.splice(firstIndexToMerge, 2, mergedHistoryEntry);\n return [initialEntry, ...historyEntries];\n}\n\n/**\n * Generates a history entry from the previous and new transaction metadata.\n *\n * @param previousState - The previous transaction metadata.\n * @param currentState - The new transaction metadata.\n * @param note - A note for the transaction metada update.\n * @returns An array of history operation.\n */\nfunction generateHistoryEntry(\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousState: any,\n currentState: TransactionMeta,\n note: string,\n): TransactionHistoryEntry {\n const historyOperationsEntry = jsonDiffer.compare(\n previousState,\n currentState,\n ) as TransactionHistoryEntry;\n // Add a note to the first operation, since it breaks if we append it to the entry\n if (historyOperationsEntry[0]) {\n if (note) {\n historyOperationsEntry[0].note = note;\n }\n historyOperationsEntry[0].timestamp = Date.now();\n }\n return historyOperationsEntry;\n}\n\n/**\n * Recovers previous transactionMeta from passed history array.\n *\n * @param transactionHistory - The transaction metadata to replay.\n * @returns The transaction metadata.\n */\nfunction replayHistory(\n transactionHistory: TransactionHistory,\n): TransactionMeta {\n const shortHistory = cloneDeep(transactionHistory);\n return shortHistory.reduce(\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (val, entry: any) => jsonDiffer.applyPatch(val, entry).newDocument,\n ) as TransactionMeta;\n}\n\n/**\n * Clone the transaction meta data without the history property.\n *\n * @param transactionMeta - The transaction metadata to snapshot.\n * @returns A deep clone of transaction metadata without history property.\n */\nfunction snapshotFromTransactionMeta(\n transactionMeta: TransactionMeta,\n): TransactionMeta {\n const snapshot = { ...transactionMeta };\n delete snapshot.history;\n return cloneDeep(snapshot);\n}\n"]}
|