@metamask-previews/transaction-controller 52.2.0-preview-b881bfc9 → 52.2.0-preview-bf2d2c4c

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 (114) hide show
  1. package/CHANGELOG.md +0 -6
  2. package/dist/TransactionController.cjs +2 -13
  3. package/dist/TransactionController.cjs.map +1 -1
  4. package/dist/TransactionController.d.cts.map +1 -1
  5. package/dist/TransactionController.d.mts.map +1 -1
  6. package/dist/TransactionController.mjs +2 -13
  7. package/dist/TransactionController.mjs.map +1 -1
  8. package/dist/gas-flows/DefaultGasFeeFlow.cjs +3 -1
  9. package/dist/gas-flows/DefaultGasFeeFlow.cjs.map +1 -1
  10. package/dist/gas-flows/DefaultGasFeeFlow.d.cts +2 -2
  11. package/dist/gas-flows/DefaultGasFeeFlow.d.cts.map +1 -1
  12. package/dist/gas-flows/DefaultGasFeeFlow.d.mts +2 -2
  13. package/dist/gas-flows/DefaultGasFeeFlow.d.mts.map +1 -1
  14. package/dist/gas-flows/DefaultGasFeeFlow.mjs +3 -1
  15. package/dist/gas-flows/DefaultGasFeeFlow.mjs.map +1 -1
  16. package/dist/gas-flows/LineaGasFeeFlow.cjs +1 -1
  17. package/dist/gas-flows/LineaGasFeeFlow.cjs.map +1 -1
  18. package/dist/gas-flows/LineaGasFeeFlow.d.cts +1 -5
  19. package/dist/gas-flows/LineaGasFeeFlow.d.cts.map +1 -1
  20. package/dist/gas-flows/LineaGasFeeFlow.d.mts +1 -5
  21. package/dist/gas-flows/LineaGasFeeFlow.d.mts.map +1 -1
  22. package/dist/gas-flows/LineaGasFeeFlow.mjs +1 -1
  23. package/dist/gas-flows/LineaGasFeeFlow.mjs.map +1 -1
  24. package/dist/gas-flows/OptimismLayer1GasFeeFlow.cjs +1 -1
  25. package/dist/gas-flows/OptimismLayer1GasFeeFlow.cjs.map +1 -1
  26. package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.cts +1 -5
  27. package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.cts.map +1 -1
  28. package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.mts +1 -5
  29. package/dist/gas-flows/OptimismLayer1GasFeeFlow.d.mts.map +1 -1
  30. package/dist/gas-flows/OptimismLayer1GasFeeFlow.mjs +1 -1
  31. package/dist/gas-flows/OptimismLayer1GasFeeFlow.mjs.map +1 -1
  32. package/dist/gas-flows/OracleLayer1GasFeeFlow.cjs.map +1 -1
  33. package/dist/gas-flows/OracleLayer1GasFeeFlow.d.cts +1 -5
  34. package/dist/gas-flows/OracleLayer1GasFeeFlow.d.cts.map +1 -1
  35. package/dist/gas-flows/OracleLayer1GasFeeFlow.d.mts +1 -5
  36. package/dist/gas-flows/OracleLayer1GasFeeFlow.d.mts.map +1 -1
  37. package/dist/gas-flows/OracleLayer1GasFeeFlow.mjs.map +1 -1
  38. package/dist/gas-flows/ScrollLayer1GasFeeFlow.cjs +1 -1
  39. package/dist/gas-flows/ScrollLayer1GasFeeFlow.cjs.map +1 -1
  40. package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.cts +1 -5
  41. package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.cts.map +1 -1
  42. package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.mts +1 -5
  43. package/dist/gas-flows/ScrollLayer1GasFeeFlow.d.mts.map +1 -1
  44. package/dist/gas-flows/ScrollLayer1GasFeeFlow.mjs +1 -1
  45. package/dist/gas-flows/ScrollLayer1GasFeeFlow.mjs.map +1 -1
  46. package/dist/gas-flows/TestGasFeeFlow.cjs +1 -1
  47. package/dist/gas-flows/TestGasFeeFlow.cjs.map +1 -1
  48. package/dist/gas-flows/TestGasFeeFlow.d.cts +2 -2
  49. package/dist/gas-flows/TestGasFeeFlow.d.cts.map +1 -1
  50. package/dist/gas-flows/TestGasFeeFlow.d.mts +2 -2
  51. package/dist/gas-flows/TestGasFeeFlow.d.mts.map +1 -1
  52. package/dist/gas-flows/TestGasFeeFlow.mjs +1 -1
  53. package/dist/gas-flows/TestGasFeeFlow.mjs.map +1 -1
  54. package/dist/helpers/GasFeePoller.cjs +4 -9
  55. package/dist/helpers/GasFeePoller.cjs.map +1 -1
  56. package/dist/helpers/GasFeePoller.d.cts +1 -4
  57. package/dist/helpers/GasFeePoller.d.cts.map +1 -1
  58. package/dist/helpers/GasFeePoller.d.mts +1 -4
  59. package/dist/helpers/GasFeePoller.d.mts.map +1 -1
  60. package/dist/helpers/GasFeePoller.mjs +4 -9
  61. package/dist/helpers/GasFeePoller.mjs.map +1 -1
  62. package/dist/helpers/IncomingTransactionHelper.cjs +1 -2
  63. package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
  64. package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
  65. package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
  66. package/dist/helpers/IncomingTransactionHelper.mjs +1 -2
  67. package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
  68. package/dist/types.cjs.map +1 -1
  69. package/dist/types.d.cts +5 -18
  70. package/dist/types.d.cts.map +1 -1
  71. package/dist/types.d.mts +5 -18
  72. package/dist/types.d.mts.map +1 -1
  73. package/dist/types.mjs.map +1 -1
  74. package/dist/utils/feature-flags.cjs +2 -18
  75. package/dist/utils/feature-flags.cjs.map +1 -1
  76. package/dist/utils/feature-flags.d.cts +0 -16
  77. package/dist/utils/feature-flags.d.cts.map +1 -1
  78. package/dist/utils/feature-flags.d.mts +0 -16
  79. package/dist/utils/feature-flags.d.mts.map +1 -1
  80. package/dist/utils/feature-flags.mjs +1 -16
  81. package/dist/utils/feature-flags.mjs.map +1 -1
  82. package/dist/utils/gas-fees.cjs +3 -21
  83. package/dist/utils/gas-fees.cjs.map +1 -1
  84. package/dist/utils/gas-fees.d.cts +0 -14
  85. package/dist/utils/gas-fees.d.cts.map +1 -1
  86. package/dist/utils/gas-fees.d.mts +0 -14
  87. package/dist/utils/gas-fees.d.mts.map +1 -1
  88. package/dist/utils/gas-fees.mjs +2 -19
  89. package/dist/utils/gas-fees.mjs.map +1 -1
  90. package/dist/utils/gas-flow.cjs +2 -3
  91. package/dist/utils/gas-flow.cjs.map +1 -1
  92. package/dist/utils/gas-flow.d.cts +1 -3
  93. package/dist/utils/gas-flow.d.cts.map +1 -1
  94. package/dist/utils/gas-flow.d.mts +1 -3
  95. package/dist/utils/gas-flow.d.mts.map +1 -1
  96. package/dist/utils/gas-flow.mjs +2 -3
  97. package/dist/utils/gas-flow.mjs.map +1 -1
  98. package/dist/utils/layer1-gas-fee-flow.cjs +4 -9
  99. package/dist/utils/layer1-gas-fee-flow.cjs.map +1 -1
  100. package/dist/utils/layer1-gas-fee-flow.d.cts +1 -4
  101. package/dist/utils/layer1-gas-fee-flow.d.cts.map +1 -1
  102. package/dist/utils/layer1-gas-fee-flow.d.mts +1 -4
  103. package/dist/utils/layer1-gas-fee-flow.d.mts.map +1 -1
  104. package/dist/utils/layer1-gas-fee-flow.mjs +4 -9
  105. package/dist/utils/layer1-gas-fee-flow.mjs.map +1 -1
  106. package/package.json +1 -1
  107. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.cjs +0 -123
  108. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.cjs.map +0 -1
  109. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.cts +0 -28
  110. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.cts.map +0 -1
  111. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.mts +0 -28
  112. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.d.mts.map +0 -1
  113. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.mjs +0 -118
  114. package/dist/gas-flows/RandomisedEstimationsGasFeeFlow.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"layer1-gas-fee-flow.mjs","sourceRoot":"","sources":["../../src/utils/layer1-gas-fee-flow.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,E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;QACd,GAAG,CAAC,+BAA+B,EAAE,eAAe,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAChE,OAAO,SAAS,CAAC;KAClB;AACH,CAAC","sourcesContent":["import type { Provider } from '@metamask/network-controller';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type { Layer1GasFeeFlow, TransactionMeta } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'layer-1-gas-fee-flow');\n\nexport type UpdateLayer1GasFeeRequest = {\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n messenger: TransactionControllerMessenger;\n provider: Provider;\n transactionMeta: TransactionMeta;\n};\n\n/**\n * Updates the given transactionMeta with the layer 1 gas fee.\n *\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.provider - Provider used to create a new underlying EthQuery instance\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n */\nexport async function updateTransactionLayer1GasFee(\n request: UpdateLayer1GasFeeRequest,\n) {\n const layer1GasFee = await getTransactionLayer1GasFee(request);\n\n if (!layer1GasFee) {\n return;\n }\n\n const { transactionMeta } = request;\n\n transactionMeta.layer1GasFee = layer1GasFee;\n\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n}\n\n/**\n * Get the layer 1 gas fee flow for a transaction.\n *\n * @param transactionMeta - The transaction to get the layer 1 gas fee flow for.\n * @param layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @param messenger - The messenger instance.\n * @returns The layer 1 gas fee flow for the transaction, or undefined if none match.\n */\nfunction getLayer1GasFeeFlow(\n transactionMeta: TransactionMeta,\n layer1GasFeeFlows: Layer1GasFeeFlow[],\n messenger: TransactionControllerMessenger,\n): Layer1GasFeeFlow | undefined {\n return layer1GasFeeFlows.find((layer1GasFeeFlow) =>\n layer1GasFeeFlow.matchesTransaction({\n transactionMeta,\n messenger,\n }),\n );\n}\n\n/**\n * Get the layer 1 gas fee for a transaction.\n *\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @param request.provider - The provider to use to get the layer 1 gas fee.\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n * @param request.messenger - The messenger instance.\n * @returns The layer 1 gas fee.\n */\nexport async function getTransactionLayer1GasFee({\n layer1GasFeeFlows,\n messenger,\n provider,\n transactionMeta,\n}: UpdateLayer1GasFeeRequest): Promise<Hex | undefined> {\n const layer1GasFeeFlow = getLayer1GasFeeFlow(\n transactionMeta,\n layer1GasFeeFlows,\n messenger,\n );\n\n if (!layer1GasFeeFlow) {\n return undefined;\n }\n\n log(\n 'Found layer 1 gas fee flow',\n layer1GasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n\n try {\n const { layer1Fee } = await layer1GasFeeFlow.getLayer1Fee({\n provider,\n transactionMeta,\n });\n return layer1Fee;\n } catch (error) {\n log('Failed to get layer 1 gas fee', transactionMeta.id, error);\n return undefined;\n }\n}\n"]}
1
+ {"version":3,"file":"layer1-gas-fee-flow.mjs","sourceRoot":"","sources":["../../src/utils/layer1-gas-fee-flow.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAG1C,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,sBAAsB,CAAC,CAAC;AAQtE;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAkC;IAElC,MAAM,YAAY,GAAG,MAAM,0BAA0B,CAAC,OAAO,CAAC,CAAC;IAE/D,IAAI,CAAC,YAAY,EAAE;QACjB,OAAO;KACR;IAED,MAAM,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC;IAEpC,eAAe,CAAC,YAAY,GAAG,YAAY,CAAC;IAE5C,GAAG,CAAC,yBAAyB,EAAE,YAAY,EAAE,eAAe,CAAC,EAAE,CAAC,CAAC;AACnE,CAAC;AAED;;;;;;GAMG;AACH,SAAS,mBAAmB,CAC1B,eAAgC,EAChC,iBAAqC;IAErC,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC,gBAAgB,EAAE,EAAE,CACjD,gBAAgB,CAAC,kBAAkB,CAAC,eAAe,CAAC,CACrD,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,EAC/C,iBAAiB,EACjB,QAAQ,EACR,eAAe,GACW;IAC1B,MAAM,gBAAgB,GAAG,mBAAmB,CAC1C,eAAe,EACf,iBAAiB,CAClB,CAAC;IAEF,IAAI,CAAC,gBAAgB,EAAE;QACrB,OAAO,SAAS,CAAC;KAClB;IAED,GAAG,CACD,4BAA4B,EAC5B,gBAAgB,CAAC,WAAW,CAAC,IAAI,EACjC,eAAe,CAAC,EAAE,CACnB,CAAC;IAEF,IAAI;QACF,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,gBAAgB,CAAC,YAAY,CAAC;YACxD,QAAQ;YACR,eAAe;SAChB,CAAC,CAAC;QACH,OAAO,SAAS,CAAC;KAClB;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,CAAC,+BAA+B,EAAE,eAAe,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAChE,OAAO,SAAS,CAAC;KAClB;AACH,CAAC","sourcesContent":["import type { Provider } from '@metamask/network-controller';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type { Layer1GasFeeFlow, TransactionMeta } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'layer-1-gas-fee-flow');\n\nexport type UpdateLayer1GasFeeRequest = {\n layer1GasFeeFlows: Layer1GasFeeFlow[];\n provider: Provider;\n transactionMeta: TransactionMeta;\n};\n\n/**\n * Updates the given transactionMeta with the layer 1 gas fee.\n *\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.provider - Provider used to create a new underlying EthQuery instance\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n */\nexport async function updateTransactionLayer1GasFee(\n request: UpdateLayer1GasFeeRequest,\n) {\n const layer1GasFee = await getTransactionLayer1GasFee(request);\n\n if (!layer1GasFee) {\n return;\n }\n\n const { transactionMeta } = request;\n\n transactionMeta.layer1GasFee = layer1GasFee;\n\n log('Updated layer 1 gas fee', layer1GasFee, transactionMeta.id);\n}\n\n/**\n * Get the layer 1 gas fee flow for a transaction.\n *\n * @param transactionMeta - The transaction to get the layer 1 gas fee flow for.\n * @param layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @returns The layer 1 gas fee flow for the transaction, or undefined if none match.\n */\nfunction getLayer1GasFeeFlow(\n transactionMeta: TransactionMeta,\n layer1GasFeeFlows: Layer1GasFeeFlow[],\n): Layer1GasFeeFlow | undefined {\n return layer1GasFeeFlows.find((layer1GasFeeFlow) =>\n layer1GasFeeFlow.matchesTransaction(transactionMeta),\n );\n}\n\n/**\n * Get the layer 1 gas fee for a transaction.\n *\n * @param request - The request to use when getting the layer 1 gas fee.\n * @param request.layer1GasFeeFlows - The layer 1 gas fee flows to search.\n * @param request.provider - The provider to use to get the layer 1 gas fee.\n * @param request.transactionMeta - The transaction to get the layer 1 gas fee for.\n * @returns The layer 1 gas fee.\n */\nexport async function getTransactionLayer1GasFee({\n layer1GasFeeFlows,\n provider,\n transactionMeta,\n}: UpdateLayer1GasFeeRequest): Promise<Hex | undefined> {\n const layer1GasFeeFlow = getLayer1GasFeeFlow(\n transactionMeta,\n layer1GasFeeFlows,\n );\n\n if (!layer1GasFeeFlow) {\n return undefined;\n }\n\n log(\n 'Found layer 1 gas fee flow',\n layer1GasFeeFlow.constructor.name,\n transactionMeta.id,\n );\n\n try {\n const { layer1Fee } = await layer1GasFeeFlow.getLayer1Fee({\n provider,\n transactionMeta,\n });\n return layer1Fee;\n } catch (error) {\n log('Failed to get layer 1 gas fee', transactionMeta.id, error);\n return undefined;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/transaction-controller",
3
- "version": "52.2.0-preview-b881bfc9",
3
+ "version": "52.2.0-preview-bf2d2c4c",
4
4
  "description": "Stores transactions alongside their periodically updated statuses and manages interactions such as approval and cancellation",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -1,123 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var _RandomisedEstimationsGasFeeFlow_instances, _RandomisedEstimationsGasFeeFlow_getDefaultGasFees, _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel;
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.randomiseDecimalGWEIAndConvertToHex = exports.RandomisedEstimationsGasFeeFlow = void 0;
10
- const gas_fee_controller_1 = require("@metamask/gas-fee-controller");
11
- const utils_1 = require("@metamask/utils");
12
- const DefaultGasFeeFlow_1 = require("./DefaultGasFeeFlow.cjs");
13
- const logger_1 = require("../logger.cjs");
14
- const types_1 = require("../types.cjs");
15
- const feature_flags_1 = require("../utils/feature-flags.cjs");
16
- const gas_fees_1 = require("../utils/gas-fees.cjs");
17
- const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'randomised-estimation-gas-fee-flow');
18
- const DEFAULT_PRESERVE_NUMBER_OF_DIGITS = 2;
19
- /**
20
- * Implementation of a gas fee flow that randomises the last digits of gas fee estimations
21
- */
22
- class RandomisedEstimationsGasFeeFlow {
23
- constructor() {
24
- _RandomisedEstimationsGasFeeFlow_instances.add(this);
25
- }
26
- matchesTransaction({ transactionMeta, messenger, }) {
27
- const { chainId } = transactionMeta;
28
- const gasFeeRandomisation = (0, feature_flags_1.getGasFeeRandomisation)(messenger);
29
- const randomisedGasFeeDigits = gasFeeRandomisation.randomisedGasFeeDigits[chainId];
30
- return randomisedGasFeeDigits !== undefined;
31
- }
32
- async getGasFees(request) {
33
- try {
34
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees).call(this, request);
35
- }
36
- catch (error) {
37
- log('Using default flow as fallback due to error', error);
38
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getDefaultGasFees).call(this, request);
39
- }
40
- }
41
- }
42
- exports.RandomisedEstimationsGasFeeFlow = RandomisedEstimationsGasFeeFlow;
43
- _RandomisedEstimationsGasFeeFlow_instances = new WeakSet(), _RandomisedEstimationsGasFeeFlow_getDefaultGasFees = async function _RandomisedEstimationsGasFeeFlow_getDefaultGasFees(request) {
44
- return new DefaultGasFeeFlow_1.DefaultGasFeeFlow().getGasFees(request);
45
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees = async function _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees(request) {
46
- const { messenger, gasFeeControllerData, transactionMeta } = request;
47
- const { gasEstimateType, gasFeeEstimates } = gasFeeControllerData;
48
- const gasFeeRandomisation = (0, feature_flags_1.getGasFeeRandomisation)(messenger);
49
- const randomisedGasFeeDigits = gasFeeRandomisation.randomisedGasFeeDigits[transactionMeta.chainId];
50
- const preservedNumberOfDigits = gasFeeRandomisation.preservedNumberOfDigits ??
51
- DEFAULT_PRESERVE_NUMBER_OF_DIGITS;
52
- if (gasEstimateType === gas_fee_controller_1.GAS_ESTIMATE_TYPES.FEE_MARKET) {
53
- log('Randomising fee market estimates', gasFeeEstimates);
54
- const randomisedFeeMarketEstimates = __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates).call(this, gasFeeEstimates, randomisedGasFeeDigits, preservedNumberOfDigits);
55
- log('Added randomised fee market estimates', randomisedFeeMarketEstimates);
56
- return {
57
- estimates: randomisedFeeMarketEstimates,
58
- };
59
- }
60
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getDefaultGasFees).call(this, request);
61
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates = function _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates(gasFeeEstimates, lastNDigits, preservedNumberOfDigits) {
62
- const levels = Object.values(types_1.GasFeeEstimateLevel).reduce((result, level) => ({
63
- ...result,
64
- [level]: __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel).call(this, gasFeeEstimates, level, lastNDigits, preservedNumberOfDigits),
65
- }), {});
66
- return {
67
- type: types_1.GasFeeEstimateType.FeeMarket,
68
- ...levels,
69
- };
70
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel = function _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel(gasFeeEstimates, level, lastNDigits, preservedNumberOfDigits) {
71
- return {
72
- maxFeePerGas: (0, gas_fees_1.gweiDecimalToWeiHex)(gasFeeEstimates[level].suggestedMaxFeePerGas),
73
- // Only priority fee is randomised
74
- maxPriorityFeePerGas: randomiseDecimalGWEIAndConvertToHex(gasFeeEstimates[level].suggestedMaxPriorityFeePerGas, lastNDigits, preservedNumberOfDigits),
75
- };
76
- };
77
- /**
78
- * Generates a random number with the specified number of digits that is greater than or equal to the given minimum value.
79
- *
80
- * @param digitCount - The number of digits the random number should have
81
- * @param minValue - The minimum value the random number should have
82
- * @returns A random number with the specified number of digits
83
- */
84
- function generateRandomDigits(digitCount, minValue) {
85
- const multiplier = 10 ** digitCount;
86
- return minValue + Math.floor(Math.random() * (multiplier - minValue));
87
- }
88
- /**
89
- * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.
90
- *
91
- * This function preserves the more significant digits while randomizing only the least significant ones,
92
- * ensuring that fees remain close to the original estimation while providing randomisation.
93
- * The randomisation is performed in Wei units for more precision.
94
- *
95
- * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)
96
- * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise
97
- * @param preservedNumberOfDigits - The number of most significant digits to preserve
98
- * @returns The randomised value converted to Wei in hexadecimal format
99
- */
100
- function randomiseDecimalGWEIAndConvertToHex(gweiDecimalValue, numberOfDigitsToRandomizeAtTheEnd, preservedNumberOfDigits) {
101
- const weiDecimalValue = (0, gas_fees_1.gweiDecimalToWeiDecimal)(gweiDecimalValue);
102
- const decimalLength = weiDecimalValue.length;
103
- // Determine how many digits to randomise while preserving the PRESERVE_NUMBER_OF_DIGITS
104
- const effectiveDigitsToRandomise = Math.min(numberOfDigitsToRandomizeAtTheEnd, decimalLength - preservedNumberOfDigits);
105
- // Handle the case when the value is 0 or too small
106
- if (Number(weiDecimalValue) === 0 || effectiveDigitsToRandomise <= 0) {
107
- return `0x${Number(weiDecimalValue).toString(16)}`;
108
- }
109
- // Use string manipulation to get the base part (significant digits)
110
- const significantDigitsCount = decimalLength - effectiveDigitsToRandomise;
111
- const significantDigits = weiDecimalValue.slice(0, significantDigitsCount);
112
- // Get the original ending digits using string manipulation
113
- const endingDigits = weiDecimalValue.slice(-effectiveDigitsToRandomise);
114
- const originalEndingDigits = Number(endingDigits);
115
- // Generate random digits that are greater than or equal to the original ending digits
116
- const randomEndingDigits = generateRandomDigits(effectiveDigitsToRandomise, originalEndingDigits);
117
- const basePart = BigInt(significantDigits + '0'.repeat(effectiveDigitsToRandomise));
118
- const randomisedWeiDecimal = basePart + BigInt(randomEndingDigits);
119
- const hexRandomisedWei = `0x${randomisedWeiDecimal.toString(16)}`;
120
- return (0, utils_1.add0x)(hexRandomisedWei);
121
- }
122
- exports.randomiseDecimalGWEIAndConvertToHex = randomiseDecimalGWEIAndConvertToHex;
123
- //# sourceMappingURL=RandomisedEstimationsGasFeeFlow.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RandomisedEstimationsGasFeeFlow.cjs","sourceRoot":"","sources":["../../src/gas-flows/RandomisedEstimationsGasFeeFlow.ts"],"names":[],"mappings":";;;;;;;;;AACA,qEAAkE;AAClE,2CAAsE;AAEtE,+DAAwD;AACxD,0CAA0C;AAU1C,wCAAmE;AACnE,8DAAgE;AAChE,oDAG2B;AAE3B,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAC5B,sBAAa,EACb,oCAAoC,CACrC,CAAC;AAEF,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAE5C;;GAEG;AACH,MAAa,+BAA+B;IAA5C;;IA+GA,CAAC;IA9GC,kBAAkB,CAAC,EACjB,eAAe,EACf,SAAS,GAIV;QACC,MAAM,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;QAEpC,MAAM,mBAAmB,GAAG,IAAA,sCAAsB,EAAC,SAAS,CAAC,CAAC;QAE9D,MAAM,sBAAsB,GAC1B,mBAAmB,CAAC,sBAAsB,CAAC,OAAO,CAAC,CAAC;QAEtD,OAAO,sBAAsB,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAA0B;QACzC,IAAI;YACF,OAAO,MAAM,uBAAA,IAAI,yGAAsB,MAA1B,IAAI,EAAuB,OAAO,CAAC,CAAC;SAClD;QAAC,OAAO,KAAK,EAAE;YACd,GAAG,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;YAC1D,OAAO,MAAM,uBAAA,IAAI,sGAAmB,MAAvB,IAAI,EAAoB,OAAO,CAAC,CAAC;SAC/C;IACH,CAAC;CAs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CAAC;IAC1E,MAAM,iBAAiB,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,sBAAsB,CAAC,CAAC;IAE3E,2DAA2D;IAC3D,MAAM,YAAY,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,0BAA0B,CAAC,CAAC;IACxE,MAAM,oBAAoB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAElD,sFAAsF;IACtF,MAAM,kBAAkB,GAAG,oBAAoB,CAC7C,0BAA0B,EAC1B,oBAAoB,CACrB,CAAC;IAEF,MAAM,QAAQ,GAAG,MAAM,CACrB,iBAAiB,GAAG,GAAG,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAC3D,CAAC;IACF,MAAM,oBAAoB,GAAG,QAAQ,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;IAEnE,MAAM,gBAAgB,GAAG,KAAK,oBAAoB,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IAElE,OAAO,IAAA,aAAK,EAAC,gBAAgB,CAAC,CAAC;AACjC,CAAC;AAzCD,kFAyCC","sourcesContent":["import type { GasFeeEstimates as FeeMarketGasPriceEstimate } from '@metamask/gas-fee-controller';\nimport { GAS_ESTIMATE_TYPES } from '@metamask/gas-fee-controller';\nimport { add0x, createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { DefaultGasFeeFlow } from './DefaultGasFeeFlow';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n FeeMarketGasFeeEstimateForLevel,\n FeeMarketGasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasFeeFlowResponse,\n TransactionMeta,\n} from '../types';\nimport { GasFeeEstimateLevel, GasFeeEstimateType } from '../types';\nimport { getGasFeeRandomisation } from '../utils/feature-flags';\nimport {\n gweiDecimalToWeiDecimal,\n gweiDecimalToWeiHex,\n} from '../utils/gas-fees';\n\nconst log = createModuleLogger(\n projectLogger,\n 'randomised-estimation-gas-fee-flow',\n);\n\nconst DEFAULT_PRESERVE_NUMBER_OF_DIGITS = 2;\n\n/**\n * Implementation of a gas fee flow that randomises the last digits of gas fee estimations\n */\nexport class RandomisedEstimationsGasFeeFlow implements GasFeeFlow {\n matchesTransaction({\n transactionMeta,\n messenger,\n }: {\n transactionMeta: TransactionMeta;\n messenger: TransactionControllerMessenger;\n }): boolean {\n const { chainId } = transactionMeta;\n\n const gasFeeRandomisation = getGasFeeRandomisation(messenger);\n\n const randomisedGasFeeDigits =\n gasFeeRandomisation.randomisedGasFeeDigits[chainId];\n\n return randomisedGasFeeDigits !== undefined;\n }\n\n async getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse> {\n try {\n return await this.#getRandomisedGasFees(request);\n } catch (error) {\n log('Using default flow as fallback due to error', error);\n return await this.#getDefaultGasFees(request);\n }\n }\n\n async #getDefaultGasFees(\n request: GasFeeFlowRequest,\n ): Promise<GasFeeFlowResponse> {\n return new DefaultGasFeeFlow().getGasFees(request);\n }\n\n async #getRandomisedGasFees(\n request: GasFeeFlowRequest,\n ): Promise<GasFeeFlowResponse> {\n const { messenger, gasFeeControllerData, transactionMeta } = request;\n const { gasEstimateType, gasFeeEstimates } = gasFeeControllerData;\n\n const gasFeeRandomisation = getGasFeeRandomisation(messenger);\n\n const randomisedGasFeeDigits =\n gasFeeRandomisation.randomisedGasFeeDigits[transactionMeta.chainId];\n\n const preservedNumberOfDigits =\n gasFeeRandomisation.preservedNumberOfDigits ??\n DEFAULT_PRESERVE_NUMBER_OF_DIGITS;\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n log('Randomising fee market estimates', gasFeeEstimates);\n const randomisedFeeMarketEstimates =\n this.#getRandomisedFeeMarketEstimates(\n gasFeeEstimates,\n randomisedGasFeeDigits,\n preservedNumberOfDigits,\n );\n log(\n 'Added randomised fee market estimates',\n randomisedFeeMarketEstimates,\n );\n\n return {\n estimates: randomisedFeeMarketEstimates,\n };\n }\n\n return await this.#getDefaultGasFees(request);\n }\n\n #getRandomisedFeeMarketEstimates(\n gasFeeEstimates: FeeMarketGasPriceEstimate,\n lastNDigits: number,\n preservedNumberOfDigits: number,\n ): FeeMarketGasFeeEstimates {\n const levels = Object.values(GasFeeEstimateLevel).reduce(\n (result, level) => ({\n ...result,\n [level]: this.#getRandomisedFeeMarketLevel(\n gasFeeEstimates,\n level,\n lastNDigits,\n preservedNumberOfDigits,\n ),\n }),\n {} as Omit<FeeMarketGasFeeEstimates, 'type'>,\n );\n\n return {\n type: GasFeeEstimateType.FeeMarket,\n ...levels,\n };\n }\n\n #getRandomisedFeeMarketLevel(\n gasFeeEstimates: FeeMarketGasPriceEstimate,\n level: GasFeeEstimateLevel,\n lastNDigits: number,\n preservedNumberOfDigits: number,\n ): FeeMarketGasFeeEstimateForLevel {\n return {\n maxFeePerGas: gweiDecimalToWeiHex(\n gasFeeEstimates[level].suggestedMaxFeePerGas,\n ),\n // Only priority fee is randomised\n maxPriorityFeePerGas: randomiseDecimalGWEIAndConvertToHex(\n gasFeeEstimates[level].suggestedMaxPriorityFeePerGas,\n lastNDigits,\n preservedNumberOfDigits,\n ),\n };\n }\n}\n\n/**\n * Generates a random number with the specified number of digits that is greater than or equal to the given minimum value.\n *\n * @param digitCount - The number of digits the random number should have\n * @param minValue - The minimum value the random number should have\n * @returns A random number with the specified number of digits\n */\nfunction generateRandomDigits(digitCount: number, minValue: number): number {\n const multiplier = 10 ** digitCount;\n return minValue + Math.floor(Math.random() * (multiplier - minValue));\n}\n\n/**\n * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.\n *\n * This function preserves the more significant digits while randomizing only the least significant ones,\n * ensuring that fees remain close to the original estimation while providing randomisation.\n * The randomisation is performed in Wei units for more precision.\n *\n * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)\n * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise\n * @param preservedNumberOfDigits - The number of most significant digits to preserve\n * @returns The randomised value converted to Wei in hexadecimal format\n */\nexport function randomiseDecimalGWEIAndConvertToHex(\n gweiDecimalValue: string | number,\n numberOfDigitsToRandomizeAtTheEnd: number,\n preservedNumberOfDigits: number,\n): Hex {\n const weiDecimalValue = gweiDecimalToWeiDecimal(gweiDecimalValue);\n const decimalLength = weiDecimalValue.length;\n\n // Determine how many digits to randomise while preserving the PRESERVE_NUMBER_OF_DIGITS\n const effectiveDigitsToRandomise = Math.min(\n numberOfDigitsToRandomizeAtTheEnd,\n decimalLength - preservedNumberOfDigits,\n );\n\n // Handle the case when the value is 0 or too small\n if (Number(weiDecimalValue) === 0 || effectiveDigitsToRandomise <= 0) {\n return `0x${Number(weiDecimalValue).toString(16)}` as Hex;\n }\n\n // Use string manipulation to get the base part (significant digits)\n const significantDigitsCount = decimalLength - effectiveDigitsToRandomise;\n const significantDigits = weiDecimalValue.slice(0, significantDigitsCount);\n\n // Get the original ending digits using string manipulation\n const endingDigits = weiDecimalValue.slice(-effectiveDigitsToRandomise);\n const originalEndingDigits = Number(endingDigits);\n\n // Generate random digits that are greater than or equal to the original ending digits\n const randomEndingDigits = generateRandomDigits(\n effectiveDigitsToRandomise,\n originalEndingDigits,\n );\n\n const basePart = BigInt(\n significantDigits + '0'.repeat(effectiveDigitsToRandomise),\n );\n const randomisedWeiDecimal = basePart + BigInt(randomEndingDigits);\n\n const hexRandomisedWei = `0x${randomisedWeiDecimal.toString(16)}`;\n\n return add0x(hexRandomisedWei);\n}\n"]}
@@ -1,28 +0,0 @@
1
- import { type Hex } from "@metamask/utils";
2
- import type { TransactionControllerMessenger } from "../TransactionController.cjs";
3
- import type { GasFeeFlow, GasFeeFlowRequest, GasFeeFlowResponse, TransactionMeta } from "../types.cjs";
4
- /**
5
- * Implementation of a gas fee flow that randomises the last digits of gas fee estimations
6
- */
7
- export declare class RandomisedEstimationsGasFeeFlow implements GasFeeFlow {
8
- #private;
9
- matchesTransaction({ transactionMeta, messenger, }: {
10
- transactionMeta: TransactionMeta;
11
- messenger: TransactionControllerMessenger;
12
- }): boolean;
13
- getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse>;
14
- }
15
- /**
16
- * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.
17
- *
18
- * This function preserves the more significant digits while randomizing only the least significant ones,
19
- * ensuring that fees remain close to the original estimation while providing randomisation.
20
- * The randomisation is performed in Wei units for more precision.
21
- *
22
- * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)
23
- * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise
24
- * @param preservedNumberOfDigits - The number of most significant digits to preserve
25
- * @returns The randomised value converted to Wei in hexadecimal format
26
- */
27
- export declare function randomiseDecimalGWEIAndConvertToHex(gweiDecimalValue: string | number, numberOfDigitsToRandomizeAtTheEnd: number, preservedNumberOfDigits: number): Hex;
28
- //# sourceMappingURL=RandomisedEstimationsGasFeeFlow.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RandomisedEstimationsGasFeeFlow.d.cts","sourceRoot":"","sources":["../../src/gas-flows/RandomisedEstimationsGasFeeFlow.ts"],"names":[],"mappings":"AAEA,OAAO,EAA6B,KAAK,GAAG,EAAE,wBAAwB;AAItE,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAGV,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EAChB,qBAAiB;AAelB;;GAEG;AACH,qBAAa,+BAAgC,YAAW,UAAU;;IAChE,kBAAkB,CAAC,EACjB,eAAe,EACf,SAAS,GACV,EAAE;QACD,eAAe,EAAE,eAAe,CAAC;QACjC,SAAS,EAAE,8BAA8B,CAAC;KAC3C,GAAG,OAAO;IAWL,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CA6F1E;AAcD;;;;;;;;;;;GAWG;AACH,wBAAgB,mCAAmC,CACjD,gBAAgB,EAAE,MAAM,GAAG,MAAM,EACjC,iCAAiC,EAAE,MAAM,EACzC,uBAAuB,EAAE,MAAM,GAC9B,GAAG,CAqCL"}
@@ -1,28 +0,0 @@
1
- import { type Hex } from "@metamask/utils";
2
- import type { TransactionControllerMessenger } from "../TransactionController.mjs";
3
- import type { GasFeeFlow, GasFeeFlowRequest, GasFeeFlowResponse, TransactionMeta } from "../types.mjs";
4
- /**
5
- * Implementation of a gas fee flow that randomises the last digits of gas fee estimations
6
- */
7
- export declare class RandomisedEstimationsGasFeeFlow implements GasFeeFlow {
8
- #private;
9
- matchesTransaction({ transactionMeta, messenger, }: {
10
- transactionMeta: TransactionMeta;
11
- messenger: TransactionControllerMessenger;
12
- }): boolean;
13
- getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse>;
14
- }
15
- /**
16
- * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.
17
- *
18
- * This function preserves the more significant digits while randomizing only the least significant ones,
19
- * ensuring that fees remain close to the original estimation while providing randomisation.
20
- * The randomisation is performed in Wei units for more precision.
21
- *
22
- * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)
23
- * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise
24
- * @param preservedNumberOfDigits - The number of most significant digits to preserve
25
- * @returns The randomised value converted to Wei in hexadecimal format
26
- */
27
- export declare function randomiseDecimalGWEIAndConvertToHex(gweiDecimalValue: string | number, numberOfDigitsToRandomizeAtTheEnd: number, preservedNumberOfDigits: number): Hex;
28
- //# sourceMappingURL=RandomisedEstimationsGasFeeFlow.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RandomisedEstimationsGasFeeFlow.d.mts","sourceRoot":"","sources":["../../src/gas-flows/RandomisedEstimationsGasFeeFlow.ts"],"names":[],"mappings":"AAEA,OAAO,EAA6B,KAAK,GAAG,EAAE,wBAAwB;AAItE,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAGV,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EAChB,qBAAiB;AAelB;;GAEG;AACH,qBAAa,+BAAgC,YAAW,UAAU;;IAChE,kBAAkB,CAAC,EACjB,eAAe,EACf,SAAS,GACV,EAAE;QACD,eAAe,EAAE,eAAe,CAAC;QACjC,SAAS,EAAE,8BAA8B,CAAC;KAC3C,GAAG,OAAO;IAWL,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CA6F1E;AAcD;;;;;;;;;;;GAWG;AACH,wBAAgB,mCAAmC,CACjD,gBAAgB,EAAE,MAAM,GAAG,MAAM,EACjC,iCAAiC,EAAE,MAAM,EACzC,uBAAuB,EAAE,MAAM,GAC9B,GAAG,CAqCL"}
@@ -1,118 +0,0 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var _RandomisedEstimationsGasFeeFlow_instances, _RandomisedEstimationsGasFeeFlow_getDefaultGasFees, _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel;
7
- import { GAS_ESTIMATE_TYPES } from "@metamask/gas-fee-controller";
8
- import { add0x, createModuleLogger } from "@metamask/utils";
9
- import { DefaultGasFeeFlow } from "./DefaultGasFeeFlow.mjs";
10
- import { projectLogger } from "../logger.mjs";
11
- import { GasFeeEstimateLevel, GasFeeEstimateType } from "../types.mjs";
12
- import { getGasFeeRandomisation } from "../utils/feature-flags.mjs";
13
- import { gweiDecimalToWeiDecimal, gweiDecimalToWeiHex } from "../utils/gas-fees.mjs";
14
- const log = createModuleLogger(projectLogger, 'randomised-estimation-gas-fee-flow');
15
- const DEFAULT_PRESERVE_NUMBER_OF_DIGITS = 2;
16
- /**
17
- * Implementation of a gas fee flow that randomises the last digits of gas fee estimations
18
- */
19
- export class RandomisedEstimationsGasFeeFlow {
20
- constructor() {
21
- _RandomisedEstimationsGasFeeFlow_instances.add(this);
22
- }
23
- matchesTransaction({ transactionMeta, messenger, }) {
24
- const { chainId } = transactionMeta;
25
- const gasFeeRandomisation = getGasFeeRandomisation(messenger);
26
- const randomisedGasFeeDigits = gasFeeRandomisation.randomisedGasFeeDigits[chainId];
27
- return randomisedGasFeeDigits !== undefined;
28
- }
29
- async getGasFees(request) {
30
- try {
31
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees).call(this, request);
32
- }
33
- catch (error) {
34
- log('Using default flow as fallback due to error', error);
35
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getDefaultGasFees).call(this, request);
36
- }
37
- }
38
- }
39
- _RandomisedEstimationsGasFeeFlow_instances = new WeakSet(), _RandomisedEstimationsGasFeeFlow_getDefaultGasFees = async function _RandomisedEstimationsGasFeeFlow_getDefaultGasFees(request) {
40
- return new DefaultGasFeeFlow().getGasFees(request);
41
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees = async function _RandomisedEstimationsGasFeeFlow_getRandomisedGasFees(request) {
42
- const { messenger, gasFeeControllerData, transactionMeta } = request;
43
- const { gasEstimateType, gasFeeEstimates } = gasFeeControllerData;
44
- const gasFeeRandomisation = getGasFeeRandomisation(messenger);
45
- const randomisedGasFeeDigits = gasFeeRandomisation.randomisedGasFeeDigits[transactionMeta.chainId];
46
- const preservedNumberOfDigits = gasFeeRandomisation.preservedNumberOfDigits ??
47
- DEFAULT_PRESERVE_NUMBER_OF_DIGITS;
48
- if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {
49
- log('Randomising fee market estimates', gasFeeEstimates);
50
- const randomisedFeeMarketEstimates = __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates).call(this, gasFeeEstimates, randomisedGasFeeDigits, preservedNumberOfDigits);
51
- log('Added randomised fee market estimates', randomisedFeeMarketEstimates);
52
- return {
53
- estimates: randomisedFeeMarketEstimates,
54
- };
55
- }
56
- return await __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getDefaultGasFees).call(this, request);
57
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates = function _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketEstimates(gasFeeEstimates, lastNDigits, preservedNumberOfDigits) {
58
- const levels = Object.values(GasFeeEstimateLevel).reduce((result, level) => ({
59
- ...result,
60
- [level]: __classPrivateFieldGet(this, _RandomisedEstimationsGasFeeFlow_instances, "m", _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel).call(this, gasFeeEstimates, level, lastNDigits, preservedNumberOfDigits),
61
- }), {});
62
- return {
63
- type: GasFeeEstimateType.FeeMarket,
64
- ...levels,
65
- };
66
- }, _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel = function _RandomisedEstimationsGasFeeFlow_getRandomisedFeeMarketLevel(gasFeeEstimates, level, lastNDigits, preservedNumberOfDigits) {
67
- return {
68
- maxFeePerGas: gweiDecimalToWeiHex(gasFeeEstimates[level].suggestedMaxFeePerGas),
69
- // Only priority fee is randomised
70
- maxPriorityFeePerGas: randomiseDecimalGWEIAndConvertToHex(gasFeeEstimates[level].suggestedMaxPriorityFeePerGas, lastNDigits, preservedNumberOfDigits),
71
- };
72
- };
73
- /**
74
- * Generates a random number with the specified number of digits that is greater than or equal to the given minimum value.
75
- *
76
- * @param digitCount - The number of digits the random number should have
77
- * @param minValue - The minimum value the random number should have
78
- * @returns A random number with the specified number of digits
79
- */
80
- function generateRandomDigits(digitCount, minValue) {
81
- const multiplier = 10 ** digitCount;
82
- return minValue + Math.floor(Math.random() * (multiplier - minValue));
83
- }
84
- /**
85
- * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.
86
- *
87
- * This function preserves the more significant digits while randomizing only the least significant ones,
88
- * ensuring that fees remain close to the original estimation while providing randomisation.
89
- * The randomisation is performed in Wei units for more precision.
90
- *
91
- * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)
92
- * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise
93
- * @param preservedNumberOfDigits - The number of most significant digits to preserve
94
- * @returns The randomised value converted to Wei in hexadecimal format
95
- */
96
- export function randomiseDecimalGWEIAndConvertToHex(gweiDecimalValue, numberOfDigitsToRandomizeAtTheEnd, preservedNumberOfDigits) {
97
- const weiDecimalValue = gweiDecimalToWeiDecimal(gweiDecimalValue);
98
- const decimalLength = weiDecimalValue.length;
99
- // Determine how many digits to randomise while preserving the PRESERVE_NUMBER_OF_DIGITS
100
- const effectiveDigitsToRandomise = Math.min(numberOfDigitsToRandomizeAtTheEnd, decimalLength - preservedNumberOfDigits);
101
- // Handle the case when the value is 0 or too small
102
- if (Number(weiDecimalValue) === 0 || effectiveDigitsToRandomise <= 0) {
103
- return `0x${Number(weiDecimalValue).toString(16)}`;
104
- }
105
- // Use string manipulation to get the base part (significant digits)
106
- const significantDigitsCount = decimalLength - effectiveDigitsToRandomise;
107
- const significantDigits = weiDecimalValue.slice(0, significantDigitsCount);
108
- // Get the original ending digits using string manipulation
109
- const endingDigits = weiDecimalValue.slice(-effectiveDigitsToRandomise);
110
- const originalEndingDigits = Number(endingDigits);
111
- // Generate random digits that are greater than or equal to the original ending digits
112
- const randomEndingDigits = generateRandomDigits(effectiveDigitsToRandomise, originalEndingDigits);
113
- const basePart = BigInt(significantDigits + '0'.repeat(effectiveDigitsToRandomise));
114
- const randomisedWeiDecimal = basePart + BigInt(randomEndingDigits);
115
- const hexRandomisedWei = `0x${randomisedWeiDecimal.toString(16)}`;
116
- return add0x(hexRandomisedWei);
117
- }
118
- //# sourceMappingURL=RandomisedEstimationsGasFeeFlow.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RandomisedEstimationsGasFeeFlow.mjs","sourceRoot":"","sources":["../../src/gas-flows/RandomisedEstimationsGasFeeFlow.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,kBAAkB,EAAE,qCAAqC;AAClE,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAY,wBAAwB;AAEtE,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AACxD,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAU1C,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,qBAAiB;AACnE,OAAO,EAAE,sBAAsB,EAAE,mCAA+B;AAChE,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACpB,8BAA0B;AAE3B,MAAM,GAAG,GAAG,kBAAkB,CAC5B,aAAa,EACb,oCAAoC,CACrC,CAAC;AAEF,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,+BAA+B;IAA5C;;IA+GA,CAAC;IA9GC,kBAAkB,CAAC,EACjB,eAAe,EACf,SAAS,GAIV;QACC,MAAM,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;QAEpC,MAAM,mBAAmB,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAC;QAE9D,MAAM,sBAAsB,GAC1B,mBAAmB,CAAC,sBAAsB,CAAC,OAAO,CAAC,CAAC;QAEtD,OAAO,sBAAsB,KAAK,SAAS,CAAC;IAC9C,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAA0B;QACzC,IAAI;YACF,OAAO,MAAM,uBAAA,IAAI,yGAAsB,MAA1B,IAAI,EAAuB,OAAO,CAAC,CAAC;SAClD;QAAC,OAAO,KAAK,EAAE;YACd,GAAG,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC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CAAC;KAC3D;IAED,oEAAoE;IACpE,MAAM,sBAAsB,GAAG,aAAa,GAAG,0BAA0B,CAAC;IAC1E,MAAM,iBAAiB,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,sBAAsB,CAAC,CAAC;IAE3E,2DAA2D;IAC3D,MAAM,YAAY,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,0BAA0B,CAAC,CAAC;IACxE,MAAM,oBAAoB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAElD,sFAAsF;IACtF,MAAM,kBAAkB,GAAG,oBAAoB,CAC7C,0BAA0B,EAC1B,oBAAoB,CACrB,CAAC;IAEF,MAAM,QAAQ,GAAG,MAAM,CACrB,iBAAiB,GAAG,GAAG,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAC3D,CAAC;IACF,MAAM,oBAAoB,GAAG,QAAQ,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;IAEnE,MAAM,gBAAgB,GAAG,KAAK,oBAAoB,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IAElE,OAAO,KAAK,CAAC,gBAAgB,CAAC,CAAC;AACjC,CAAC","sourcesContent":["import type { GasFeeEstimates as FeeMarketGasPriceEstimate } from '@metamask/gas-fee-controller';\nimport { GAS_ESTIMATE_TYPES } from '@metamask/gas-fee-controller';\nimport { add0x, createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { DefaultGasFeeFlow } from './DefaultGasFeeFlow';\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n FeeMarketGasFeeEstimateForLevel,\n FeeMarketGasFeeEstimates,\n GasFeeFlow,\n GasFeeFlowRequest,\n GasFeeFlowResponse,\n TransactionMeta,\n} from '../types';\nimport { GasFeeEstimateLevel, GasFeeEstimateType } from '../types';\nimport { getGasFeeRandomisation } from '../utils/feature-flags';\nimport {\n gweiDecimalToWeiDecimal,\n gweiDecimalToWeiHex,\n} from '../utils/gas-fees';\n\nconst log = createModuleLogger(\n projectLogger,\n 'randomised-estimation-gas-fee-flow',\n);\n\nconst DEFAULT_PRESERVE_NUMBER_OF_DIGITS = 2;\n\n/**\n * Implementation of a gas fee flow that randomises the last digits of gas fee estimations\n */\nexport class RandomisedEstimationsGasFeeFlow implements GasFeeFlow {\n matchesTransaction({\n transactionMeta,\n messenger,\n }: {\n transactionMeta: TransactionMeta;\n messenger: TransactionControllerMessenger;\n }): boolean {\n const { chainId } = transactionMeta;\n\n const gasFeeRandomisation = getGasFeeRandomisation(messenger);\n\n const randomisedGasFeeDigits =\n gasFeeRandomisation.randomisedGasFeeDigits[chainId];\n\n return randomisedGasFeeDigits !== undefined;\n }\n\n async getGasFees(request: GasFeeFlowRequest): Promise<GasFeeFlowResponse> {\n try {\n return await this.#getRandomisedGasFees(request);\n } catch (error) {\n log('Using default flow as fallback due to error', error);\n return await this.#getDefaultGasFees(request);\n }\n }\n\n async #getDefaultGasFees(\n request: GasFeeFlowRequest,\n ): Promise<GasFeeFlowResponse> {\n return new DefaultGasFeeFlow().getGasFees(request);\n }\n\n async #getRandomisedGasFees(\n request: GasFeeFlowRequest,\n ): Promise<GasFeeFlowResponse> {\n const { messenger, gasFeeControllerData, transactionMeta } = request;\n const { gasEstimateType, gasFeeEstimates } = gasFeeControllerData;\n\n const gasFeeRandomisation = getGasFeeRandomisation(messenger);\n\n const randomisedGasFeeDigits =\n gasFeeRandomisation.randomisedGasFeeDigits[transactionMeta.chainId];\n\n const preservedNumberOfDigits =\n gasFeeRandomisation.preservedNumberOfDigits ??\n DEFAULT_PRESERVE_NUMBER_OF_DIGITS;\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n log('Randomising fee market estimates', gasFeeEstimates);\n const randomisedFeeMarketEstimates =\n this.#getRandomisedFeeMarketEstimates(\n gasFeeEstimates,\n randomisedGasFeeDigits,\n preservedNumberOfDigits,\n );\n log(\n 'Added randomised fee market estimates',\n randomisedFeeMarketEstimates,\n );\n\n return {\n estimates: randomisedFeeMarketEstimates,\n };\n }\n\n return await this.#getDefaultGasFees(request);\n }\n\n #getRandomisedFeeMarketEstimates(\n gasFeeEstimates: FeeMarketGasPriceEstimate,\n lastNDigits: number,\n preservedNumberOfDigits: number,\n ): FeeMarketGasFeeEstimates {\n const levels = Object.values(GasFeeEstimateLevel).reduce(\n (result, level) => ({\n ...result,\n [level]: this.#getRandomisedFeeMarketLevel(\n gasFeeEstimates,\n level,\n lastNDigits,\n preservedNumberOfDigits,\n ),\n }),\n {} as Omit<FeeMarketGasFeeEstimates, 'type'>,\n );\n\n return {\n type: GasFeeEstimateType.FeeMarket,\n ...levels,\n };\n }\n\n #getRandomisedFeeMarketLevel(\n gasFeeEstimates: FeeMarketGasPriceEstimate,\n level: GasFeeEstimateLevel,\n lastNDigits: number,\n preservedNumberOfDigits: number,\n ): FeeMarketGasFeeEstimateForLevel {\n return {\n maxFeePerGas: gweiDecimalToWeiHex(\n gasFeeEstimates[level].suggestedMaxFeePerGas,\n ),\n // Only priority fee is randomised\n maxPriorityFeePerGas: randomiseDecimalGWEIAndConvertToHex(\n gasFeeEstimates[level].suggestedMaxPriorityFeePerGas,\n lastNDigits,\n preservedNumberOfDigits,\n ),\n };\n }\n}\n\n/**\n * Generates a random number with the specified number of digits that is greater than or equal to the given minimum value.\n *\n * @param digitCount - The number of digits the random number should have\n * @param minValue - The minimum value the random number should have\n * @returns A random number with the specified number of digits\n */\nfunction generateRandomDigits(digitCount: number, minValue: number): number {\n const multiplier = 10 ** digitCount;\n return minValue + Math.floor(Math.random() * (multiplier - minValue));\n}\n\n/**\n * Randomises the least significant digits of a decimal gas fee value and converts it to a hexadecimal Wei value.\n *\n * This function preserves the more significant digits while randomizing only the least significant ones,\n * ensuring that fees remain close to the original estimation while providing randomisation.\n * The randomisation is performed in Wei units for more precision.\n *\n * @param gweiDecimalValue - The original gas fee value in Gwei (decimal)\n * @param numberOfDigitsToRandomizeAtTheEnd - The number of least significant digits to randomise\n * @param preservedNumberOfDigits - The number of most significant digits to preserve\n * @returns The randomised value converted to Wei in hexadecimal format\n */\nexport function randomiseDecimalGWEIAndConvertToHex(\n gweiDecimalValue: string | number,\n numberOfDigitsToRandomizeAtTheEnd: number,\n preservedNumberOfDigits: number,\n): Hex {\n const weiDecimalValue = gweiDecimalToWeiDecimal(gweiDecimalValue);\n const decimalLength = weiDecimalValue.length;\n\n // Determine how many digits to randomise while preserving the PRESERVE_NUMBER_OF_DIGITS\n const effectiveDigitsToRandomise = Math.min(\n numberOfDigitsToRandomizeAtTheEnd,\n decimalLength - preservedNumberOfDigits,\n );\n\n // Handle the case when the value is 0 or too small\n if (Number(weiDecimalValue) === 0 || effectiveDigitsToRandomise <= 0) {\n return `0x${Number(weiDecimalValue).toString(16)}` as Hex;\n }\n\n // Use string manipulation to get the base part (significant digits)\n const significantDigitsCount = decimalLength - effectiveDigitsToRandomise;\n const significantDigits = weiDecimalValue.slice(0, significantDigitsCount);\n\n // Get the original ending digits using string manipulation\n const endingDigits = weiDecimalValue.slice(-effectiveDigitsToRandomise);\n const originalEndingDigits = Number(endingDigits);\n\n // Generate random digits that are greater than or equal to the original ending digits\n const randomEndingDigits = generateRandomDigits(\n effectiveDigitsToRandomise,\n originalEndingDigits,\n );\n\n const basePart = BigInt(\n significantDigits + '0'.repeat(effectiveDigitsToRandomise),\n );\n const randomisedWeiDecimal = basePart + BigInt(randomEndingDigits);\n\n const hexRandomisedWei = `0x${randomisedWeiDecimal.toString(16)}`;\n\n return add0x(hexRandomisedWei);\n}\n"]}