@metamask-previews/transaction-controller 72.2.1-preview-a0d99e051 → 72.2.1-preview-060b37d94

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 CHANGED
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
  ### Added
11
11
 
12
12
  - Add optional `daFootprintGasScalar`, `operatorFeeScalar`, `operatorFeeConstant`, and `tokenRatio` fields to `TransactionReceipt` for OP Stack / Mantle Arsia receipts ([#9630](https://github.com/MetaMask/core/pull/9630))
13
+ - Estimate type-4 transactions with an authorization list as one outer transaction, instead of combining separate upgrade and execution estimates and subtracting 21,000 intrinsic gas, so the estimate includes the EIP-2780 runtime authorization charges ([#10783](https://github.com/MetaMask/core/pull/10783))
14
+ - If all authorizations are signed, use `eth_estimateGas` on the selected network for the exact transaction.
15
+ - Otherwise, send one Simulation API request with an `authorizationList` containing the authority of each entry: the sender for unsigned entries, and the recovered signer for pre-signed entries. Estimation fails if a signer cannot be recovered.
16
+ - If simulation is disabled, the chain is not supported by the Simulation API, or the simulation request fails or returns no gas limit, use `eth_estimateGas` on the selected network with unsigned authorizations omitted and the delegation of the sender set with a state override. Add 7,816 intrinsic gas per omitted authorization and, if the sender is not yet delegated, 35,190 gas once for the EIP-2780 delegation indicator charge. Keep pre-signed authorizations in the node request so their costs are included in its estimate.
17
+ - A simulated transaction error is recorded as `simulationFails` without falling back to the selected network.
13
18
  - Add support for Node.js 26 ([#10981](https://github.com/MetaMask/core/pull/10981))
14
19
 
15
20
  ### Changed
@@ -18,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
18
23
  - Bump `@metamask/core-backend` from `^12.1.0` to `^12.3.0` ([#10857](https://github.com/MetaMask/core/pull/10857), [#10876](https://github.com/MetaMask/core/pull/10876))
19
24
  - Bump `@metamask/base-controller` from `^10.0.0` to `^10.1.0` ([#10864](https://github.com/MetaMask/core/pull/10864))
20
25
  - Bump `@metamask/network-controller` from `^37.0.1` to `^37.1.1` ([#10864](https://github.com/MetaMask/core/pull/10864), [#10982](https://github.com/MetaMask/core/pull/10982))
26
+ - Bump `@metamask/utils` from `^12.0.0` to `^12.0.1` ([#11107](https://github.com/MetaMask/core/pull/11107))
21
27
 
22
28
  ### Fixed
23
29
 
@@ -17,8 +17,21 @@ export type EstimateGasBatchResult = {
17
17
  export declare const log: import("debug").Debugger;
18
18
  export declare const DEFAULT_GAS_MULTIPLIER = 1.5;
19
19
  export declare const MAX_GAS_BLOCK_PERCENT = 90;
20
- export declare const INTRINSIC_GAS = 21000;
21
20
  export declare const DUMMY_AUTHORIZATION_SIGNATURE = "0x1111111111111111111111111111111111111111111111111111111111111111";
21
+ /**
22
+ * EIP-2780 state gas charged at runtime when a valid authorization sets a
23
+ * delegation indicator for an authority that had none at the start of the
24
+ * transaction, at most once per authority.
25
+ *
26
+ * When the authority is the sender, this is the only runtime authorization
27
+ * charge: `ACCOUNT_WRITE` is covered by the transaction base cost, and the
28
+ * sender always exists in state as its nonce is incremented first.
29
+ */
30
+ export declare const EIP_2780_AUTHORIZATION_GAS = 35190;
31
+ /**
32
+ * Intrinsic gas cost for each authorization omitted from the node estimate.
33
+ */
34
+ export declare const EIP7280_AUTHORIZATION_INTRINSIC_GAS = 7816;
22
35
  /**
23
36
  * Populate the gas properties of the provided transaction meta.
24
37
  *
@@ -1 +1 @@
1
- {"version":3,"file":"gas.d.ts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,KAAK,EAAE,GAAG,EAAQ,MAAM,iBAAiB,CAAC;AAWjD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC;AAElF,OAAO,KAAK,EAEV,sBAAsB,EACtB,mBAAmB,EACnB,6BAA6B,EAC7B,4BAA4B,EAC5B,MAAM,EACN,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,EAAE,GAAG,CAAC;IACV,mBAAmB,EAAE,mBAAmB,CAAC;IACzC,YAAY,EAAE,6BAA6B,EAAE,CAAC;CAC/C,GAAG,OAAO,CAAC;IAAE,aAAa,EAAE,GAAG,CAAC;IAAC,SAAS,EAAE,GAAG,EAAE,CAAA;CAAE,CAAC,CA8CpD"}
package/dist/utils/gas.js CHANGED
@@ -4,15 +4,34 @@ import { BigNumber } from 'bignumber.js';
4
4
  import { simulateTransactions } from '../api/simulation-api.js';
5
5
  import { projectLogger } from '../logger.js';
6
6
  import { TransactionEnvelopeType } from '../types.js';
7
- import { DELEGATION_PREFIX, doesAccountSupportEIP7702, generateEIP7702BatchTransaction, } from './eip7702.js';
7
+ import { DELEGATION_PREFIX, doesAccountSupportEIP7702, generateEIP7702BatchTransaction, getAuthorizationAuthority, getDelegationAddress, isAuthorizationSigned, } from './eip7702.js';
8
8
  import { getGasEstimateBuffer, getGasEstimateFallback, } from './feature-flags.js';
9
9
  import { getChainId, rpcRequest } from './provider.js';
10
10
  import { decodeRevert } from './revert-reason.js';
11
11
  export const log = createModuleLogger(projectLogger, 'gas');
12
12
  export const DEFAULT_GAS_MULTIPLIER = 1.5;
13
13
  export const MAX_GAS_BLOCK_PERCENT = 90;
14
- export const INTRINSIC_GAS = 21000;
15
14
  export const DUMMY_AUTHORIZATION_SIGNATURE = '0x1111111111111111111111111111111111111111111111111111111111111111';
15
+ /**
16
+ * EIP-2780 state gas charged at runtime when a valid authorization sets a
17
+ * delegation indicator for an authority that had none at the start of the
18
+ * transaction, at most once per authority.
19
+ *
20
+ * When the authority is the sender, this is the only runtime authorization
21
+ * charge: `ACCOUNT_WRITE` is covered by the transaction base cost, and the
22
+ * sender always exists in state as its nonce is incremented first.
23
+ */
24
+ export const EIP_2780_AUTHORIZATION_GAS = 35190;
25
+ /**
26
+ * Intrinsic gas cost for each authorization omitted from the node estimate.
27
+ */
28
+ export const EIP7280_AUTHORIZATION_INTRINSIC_GAS = 7816;
29
+ /**
30
+ * Error for a simulated transaction that failed, as opposed to the simulation
31
+ * itself being unavailable.
32
+ */
33
+ class SimulatedTransactionError extends Error {
34
+ }
16
35
  /**
17
36
  * Populate the gas properties of the provided transaction meta.
18
37
  *
@@ -83,9 +102,21 @@ export async function estimateGas({ ignoreDelegationSignatures, isSimulationEnab
83
102
  Boolean(authorizationList?.length) &&
84
103
  Boolean(data) &&
85
104
  data !== '0x';
105
+ const type4AuthorizationList = txParams.type === TransactionEnvelopeType.setCode &&
106
+ authorizationList?.length
107
+ ? authorizationList
108
+ : undefined;
86
109
  try {
87
- if (isSimulationEnabled && isUpgradeWithData) {
88
- estimatedGas = await estimateGasUpgradeWithDataToSelf(request, messenger, networkClientId, chainId, getSimulationConfig);
110
+ if (type4AuthorizationList) {
111
+ estimatedGas = await estimateGasType4({
112
+ authorizationList: type4AuthorizationList,
113
+ chainId,
114
+ getSimulationConfig,
115
+ isSimulationEnabled,
116
+ messenger,
117
+ networkClientId,
118
+ txParams: request,
119
+ });
89
120
  }
90
121
  else if (ignoreDelegationSignatures && isSimulationEnabled) {
91
122
  estimatedGas = await simulateGas({
@@ -347,87 +378,146 @@ async function getLatestBlock(messenger, networkClientId) {
347
378
  }));
348
379
  }
349
380
  /**
350
- * Estimate the gas for a type 4 transaction.
381
+ * Estimate the gas for a type 4 transaction as one outer transaction.
351
382
  *
352
- * @param txParams - The transaction parameters.
353
- * @param messenger - The messenger instance for communication.
354
- * @param networkClientId - The network client ID.
355
- * @param chainId - The chain ID of the transaction.
356
- * @param getSimulationConfig - The function to get the simulation configuration.
383
+ * - If all authorizations are signed, the selected node estimates the exact
384
+ * transaction.
385
+ * - Otherwise, the Simulation API estimates the exact transaction, with the
386
+ * authority of each authorization so that it can apply any EIP-2780 runtime
387
+ * authorization charges.
388
+ * - If the Simulation API is disabled, unsupported or unavailable, the selected
389
+ * node estimates the transaction without unsigned authorizations and with the
390
+ * delegation of the sender overridden. Intrinsic gas is added for each omitted
391
+ * authorization, plus the EIP-2780 delegation charge if the sender is not yet
392
+ * delegated. A simulated transaction error is not retried using the node.
393
+ *
394
+ * @param options - The options object.
395
+ * @param options.authorizationList - The original authorization list of the transaction.
396
+ * @param options.chainId - The chain ID of the transaction.
397
+ * @param options.getSimulationConfig - The function to get the simulation configuration.
398
+ * @param options.isSimulationEnabled - Whether the simulation is enabled.
399
+ * @param options.messenger - The messenger instance for communication.
400
+ * @param options.networkClientId - The network client ID.
401
+ * @param options.txParams - The normalized transaction parameters.
357
402
  * @returns The estimated gas.
358
403
  */
359
- async function estimateGasUpgradeWithDataToSelf(txParams, messenger, networkClientId, chainId, getSimulationConfig) {
360
- const upgradeGas = (await rpcRequest({
361
- messenger,
362
- networkClientId,
363
- method: 'eth_estimateGas',
364
- params: [
365
- {
366
- ...txParams,
367
- to: txParams.from,
368
- data: '0x',
369
- },
370
- ],
371
- }));
372
- log('Upgrade only gas', upgradeGas);
373
- const delegationAddress = txParams.authorizationList?.[0].address;
374
- let executeGas;
375
- try {
376
- executeGas = await simulateGas({
377
- chainId,
378
- delegationAddress,
379
- getSimulationConfig,
380
- transaction: txParams,
381
- });
404
+ async function estimateGasType4({ authorizationList, chainId, getSimulationConfig, isSimulationEnabled, messenger, networkClientId, txParams, }) {
405
+ if (authorizationList.every(isAuthorizationSigned)) {
406
+ log('Estimating type 4 gas using node as all authorizations signed');
407
+ return await estimateGasNode(messenger, networkClientId, txParams);
382
408
  }
383
- catch (error) {
384
- log('Error while simulating data portion of upgrade', error);
385
- }
386
- if (executeGas === undefined) {
409
+ if (isSimulationEnabled) {
410
+ const simulationAuthorizationList = authorizationList.map((authorization) => {
411
+ const authority = getAuthorizationAuthority(authorization, txParams.from);
412
+ if (!authority) {
413
+ throw new Error('Cannot determine authority of signed authorization');
414
+ }
415
+ return { address: authorization.address, from: authority };
416
+ });
417
+ log('Estimating type 4 gas using simulation', simulationAuthorizationList);
387
418
  try {
388
- executeGas = await estimateGasNode(messenger, networkClientId, { ...txParams, authorizationList: undefined, type: undefined }, delegationAddress);
419
+ return await simulateGas({
420
+ authorizationList: simulationAuthorizationList,
421
+ chainId,
422
+ getSimulationConfig,
423
+ transaction: txParams,
424
+ });
389
425
  }
390
426
  catch (error) {
391
- log('Error while estimating data portion of upgrade', error);
392
- throw error;
427
+ if (error instanceof SimulatedTransactionError) {
428
+ throw error;
429
+ }
430
+ log('Simulation unavailable, estimating type 4 gas using node', error);
393
431
  }
394
- log('Success estimating data portion of upgrade', executeGas);
395
432
  }
396
- log('Execute gas', executeGas);
397
- const total = BNToHex(hexToBN(upgradeGas).add(hexToBN(executeGas)).subn(INTRINSIC_GAS));
398
- log('Total type 4 gas', total);
399
- return total;
433
+ return await estimateGasType4Node({
434
+ authorizationList,
435
+ messenger,
436
+ networkClientId,
437
+ txParams,
438
+ });
439
+ }
440
+ /**
441
+ * Estimate the gas for a type 4 transaction with unsigned authorizations using
442
+ * the selected node.
443
+ *
444
+ * Unsigned authorizations are omitted from the node request and their intrinsic
445
+ * gas is added manually per entry. Their authority is the sender, so the
446
+ * delegation of the sender is set with a state override, and the EIP-2780 charge
447
+ * for a new delegation indicator is added once if the sender is not yet
448
+ * delegated. Signed authorizations remain in the request and are charged by the
449
+ * node.
450
+ *
451
+ * @param options - The options object.
452
+ * @param options.authorizationList - The original authorization list of the transaction.
453
+ * @param options.messenger - The messenger instance for communication.
454
+ * @param options.networkClientId - The network client ID.
455
+ * @param options.txParams - The normalized transaction parameters.
456
+ * @returns The estimated gas including omitted authorization costs.
457
+ */
458
+ async function estimateGasType4Node({ authorizationList, messenger, networkClientId, txParams, }) {
459
+ const unsignedAuthorizations = authorizationList.filter((authorization) => !isAuthorizationSigned(authorization));
460
+ const signedAuthorizations = authorizationList.filter(isAuthorizationSigned);
461
+ const nodeTxParams = {
462
+ ...txParams,
463
+ authorizationList: signedAuthorizations.length
464
+ ? signedAuthorizations
465
+ : undefined,
466
+ type: signedAuthorizations.length ? txParams.type : undefined,
467
+ };
468
+ // Unsigned authorizations are signed by the sender, and the last valid
469
+ // authorization for an authority sets its delegation.
470
+ const delegationAddress = unsignedAuthorizations[unsignedAuthorizations.length - 1].address;
471
+ const [nodeGas, currentDelegationAddress] = await Promise.all([
472
+ estimateGasNode(messenger, networkClientId, nodeTxParams, delegationAddress),
473
+ getDelegationAddress(txParams.from, messenger, networkClientId),
474
+ ]);
475
+ const intrinsicGas = unsignedAuthorizations.length * EIP7280_AUTHORIZATION_INTRINSIC_GAS;
476
+ const delegationGas = currentDelegationAddress
477
+ ? 0
478
+ : EIP_2780_AUTHORIZATION_GAS;
479
+ log('Type 4 gas using node', {
480
+ currentDelegationAddress,
481
+ delegationGas,
482
+ intrinsicGas,
483
+ nodeGas,
484
+ });
485
+ return BNToHex(hexToBN(nodeGas).addn(intrinsicGas + delegationGas));
400
486
  }
401
487
  /**
402
488
  * Simulate the required gas using the simulation API.
403
489
  *
490
+ * If an authorization list is provided, it is included in the simulated
491
+ * transaction, a simulated transaction error is thrown as a
492
+ * `SimulatedTransactionError`, and the response must include a non-zero gas
493
+ * limit.
494
+ *
404
495
  * @param options - The options object.
496
+ * @param options.authorizationList - The authorization list with the authority of each entry.
405
497
  * @param options.chainId - The chain ID of the transaction.
406
- * @param options.delegationAddress - The delegation address of the sender to mock.
407
498
  * @param options.getSimulationConfig - The function to get the simulation configuration.
408
499
  * @param options.transaction - The transaction parameters.
409
500
  * @returns The simulated gas.
410
501
  */
411
- async function simulateGas({ chainId, delegationAddress, getSimulationConfig, transaction, }) {
502
+ async function simulateGas({ authorizationList, chainId, getSimulationConfig, transaction, }) {
412
503
  const response = await simulateTransactions(chainId, {
413
504
  getSimulationConfig,
414
505
  transactions: [
415
506
  {
507
+ ...(authorizationList ? { authorizationList } : {}),
416
508
  to: transaction.to,
417
509
  from: transaction.from,
418
510
  data: transaction.data,
419
511
  value: transaction.value,
420
512
  },
421
513
  ],
422
- overrides: {
423
- [transaction.from]: {
424
- code: delegationAddress &&
425
- (DELEGATION_PREFIX + remove0x(delegationAddress)),
426
- },
427
- },
428
514
  });
429
- const gasLimit = response?.transactions?.[0].gasLimit;
430
- if (!gasLimit) {
515
+ const simulatedTransaction = response?.transactions?.[0];
516
+ const gasLimit = simulatedTransaction?.gasLimit;
517
+ if (authorizationList && simulatedTransaction?.error) {
518
+ throw new SimulatedTransactionError(`Simulation failed: ${simulatedTransaction.error}`);
519
+ }
520
+ if (!gasLimit || (authorizationList && hexToBN(gasLimit).isZero())) {
431
521
  throw new Error('No simulated gas returned');
432
522
  }
433
523
  return gasLimit;
@@ -1 +1 @@
1
- {"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,OAAO,EACP,KAAK,GACN,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EACL,KAAK,EACL,kBAAkB,EAClB,iBAAiB,EACjB,QAAQ,GACT,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAYtD,OAAO,EACL,iBAAiB,EACjB,yBAAyB,EACzB,+BAA+B,GAChC,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAgBlD,MAAM,CAAC,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;AAE5D,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAC1C,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AACxC,MAAM,CAAC,MAAM,aAAa,GAAG,KAA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IAAI,EAAE,iBAAiB,GAAG,QAAQ,CAAC,iBAAiB,CAAC;aACtD;SACF,CAAC,CAAC;IACL,CAAC;IAED,OAAO,CAAC,MAAM,UAAU,CAAC;QACvB,SAAS;QACT,eAAe;QACf,MAAM,EAAE,iBAAiB;QACzB,MAAM;KACP,CAAC,CAAQ,CAAC;AACb,CAAC","sourcesContent":["import {\n BNToHex,\n fractionBN,\n hexToBN,\n toHex,\n} from '@metamask/controller-utils';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport {\n add0x,\n createModuleLogger,\n isStrictHexString,\n remove0x,\n} from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { simulateTransactions } from '../api/simulation-api.js';\nimport { projectLogger } from '../logger.js';\nimport type { TransactionControllerMessenger } from '../TransactionController.js';\nimport { TransactionEnvelopeType } from '../types.js';\nimport type {\n AuthorizationList,\n BatchTransactionParams,\n GetSimulationConfig,\n IsAtomicBatchSupportedRequest,\n IsAtomicBatchSupportedResult,\n Revert,\n TransactionBatchSingleRequest,\n TransactionMeta,\n TransactionParams,\n} from '../types.js';\nimport {\n DELEGATION_PREFIX,\n doesAccountSupportEIP7702,\n generateEIP7702BatchTransaction,\n} from './eip7702.js';\nimport {\n getGasEstimateBuffer,\n getGasEstimateFallback,\n} from './feature-flags.js';\nimport { getChainId, rpcRequest } from './provider.js';\nimport { decodeRevert } from './revert-reason.js';\n\nexport type UpdateGasRequest = {\n isCustomNetwork: boolean;\n isSimulationEnabled: boolean;\n getSimulationConfig: GetSimulationConfig;\n messenger: TransactionControllerMessenger;\n txMeta: TransactionMeta;\n};\n\nexport type EstimateGasBatchResult = {\n gasLimits: number[];\n requiresAuthorizationList?: true;\n totalGasLimit: number;\n};\n\nexport const log = createModuleLogger(projectLogger, 'gas');\n\nexport const DEFAULT_GAS_MULTIPLIER = 1.5;\nexport const MAX_GAS_BLOCK_PERCENT = 90;\nexport const INTRINSIC_GAS = 21000;\n\nexport const DUMMY_AUTHORIZATION_SIGNATURE =\n '0x1111111111111111111111111111111111111111111111111111111111111111';\n\n/**\n * Populate the gas properties of the provided transaction meta.\n *\n * @param request - The request object including the necessary parameters.\n */\nexport async function updateGas(request: UpdateGasRequest): Promise<void> {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const [gas, simulationFails, gasLimitNoBuffer, gasRevert] =\n await getGas(request);\n\n txMeta.txParams.gas = gas;\n txMeta.simulationFails = simulationFails;\n txMeta.gasLimitNoBuffer = gasLimitNoBuffer;\n\n if (gasRevert) {\n txMeta.revert = { ...txMeta.revert, gas: gasRevert };\n }\n\n if (!initialParams.gas) {\n txMeta.originalGasEstimate = txMeta.txParams.gas;\n }\n\n txMeta.defaultGasEstimates ??= {};\n txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;\n}\n\n/**\n * Estimate the gas for the provided transaction parameters.\n * If the gas estimate fails, the fallback value is returned.\n *\n * @param options - The options object.\n * @param options.ignoreDelegationSignatures - Ignore signature errors if submitting delegations to the DelegationManager.\n * @param options.isSimulationEnabled - Whether the simulation is enabled.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.messenger - The messenger instance for communication.\n * @param options.networkClientId - The network client ID.\n * @param options.txParams - The transaction parameters.\n * @returns The estimated gas and related info.\n */\nexport async function estimateGas({\n ignoreDelegationSignatures,\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams,\n}: {\n ignoreDelegationSignatures?: boolean;\n isSimulationEnabled: boolean;\n getSimulationConfig: GetSimulationConfig;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n txParams: TransactionParams;\n}): Promise<{\n blockGasLimit: string;\n estimatedGas: string;\n gasRevert?: Revert;\n isUpgradeWithData: boolean;\n simulationFails: TransactionMeta['simulationFails'];\n}> {\n const request = { ...txParams };\n const { authorizationList, data, value } = request;\n const chainId = getChainId({ messenger, networkClientId });\n\n if (ignoreDelegationSignatures && !isSimulationEnabled) {\n throw new Error(\n 'Gas estimation with ignored delegation signatures is not supported as simulation disabled',\n );\n }\n\n const { gasLimit: blockGasLimit, number: blockNumber } = await getLatestBlock(\n messenger,\n networkClientId,\n );\n\n const blockGasLimitBN = hexToBN(blockGasLimit);\n const { percentage, fixed, maxGasLimit } = getGasEstimateFallback(\n chainId,\n messenger,\n );\n\n const uncappedFallback = fixed\n ? toHex(fixed)\n : BNToHex(fractionBN(blockGasLimitBN, percentage, 100));\n\n // Clamp the fallback to the chain's per-transaction gas cap so it can never\n // exceed the gas limit the RPC will accept. Without this, a percentage of a\n // high block gas limit can overshoot the cap (e.g. 35% of Polygon's ~140M\n // block limit is ~49M, but the node caps a tx at ~33.5M and rejects it with\n // \"transaction gas limit too high\").\n const fallback =\n maxGasLimit !== undefined &&\n hexToBN(uncappedFallback).gt(hexToBN(toHex(maxGasLimit)))\n ? toHex(maxGasLimit)\n : uncappedFallback;\n\n log('Estimation fallback values', fallback);\n\n request.data = data ? add0x(data) : data;\n request.value = normalizeValue(value);\n\n request.authorizationList = normalizeAuthorizationList(\n request.authorizationList,\n chainId,\n );\n\n delete request.gasPrice;\n delete request.maxFeePerGas;\n delete request.maxPriorityFeePerGas;\n\n let estimatedGas = fallback;\n let simulationFails: TransactionMeta['simulationFails'];\n let gasRevert: Revert | undefined;\n\n const isUpgradeWithData =\n txParams.type === TransactionEnvelopeType.setCode &&\n Boolean(authorizationList?.length) &&\n Boolean(data) &&\n data !== '0x';\n\n try {\n if (isSimulationEnabled && isUpgradeWithData) {\n estimatedGas = await estimateGasUpgradeWithDataToSelf(\n request,\n messenger,\n networkClientId,\n chainId,\n getSimulationConfig,\n );\n } else if (ignoreDelegationSignatures && isSimulationEnabled) {\n estimatedGas = await simulateGas({\n chainId,\n getSimulationConfig,\n transaction: request,\n });\n } else {\n estimatedGas = await estimateGasNode(messenger, networkClientId, request);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n simulationFails = {\n reason: error.message,\n errorKey: error.errorKey,\n debug: {\n blockNumber,\n blockGasLimit,\n },\n };\n\n gasRevert = decodeRevert(error, 'gas');\n\n log('Estimation failed', { ...simulationFails, fallback, gasRevert });\n }\n\n return {\n blockGasLimit,\n estimatedGas,\n gasRevert,\n isUpgradeWithData,\n simulationFails,\n };\n}\n\n/**\n * Sum caller-provided gas limits across a batch.\n *\n * If every transaction in the batch already has a `gas` value, returns the\n * parsed per-tx limits and their sum. Otherwise returns `undefined`.\n *\n * Used by `estimateGasBatch`:\n * - non-7702 path: short-circuits simulation entirely when present.\n * - EIP-7702 path: used as a fallback when simulation fails — required for\n * callers that submit batches whose individual sub-calls cannot be simulated\n * standalone (e.g. predict-withdraw, where the batch's first sub-call\n * provides source-token balance to subsequent sub-calls). When 7702\n * simulation succeeds it is preferred since the bundled call has no per-tx\n * intrinsic gas cost and produces a tighter estimate.\n *\n * @param transactions - Batch transactions to inspect.\n * @returns Parsed gas limits and total when every transaction has gas; otherwise `undefined`.\n */\nexport function getProvidedBatchGasLimits(\n transactions: BatchTransactionParams[],\n): { gasLimits: number[]; totalGasLimit: number } | undefined {\n if (!transactions.every((transaction) => transaction.gas !== undefined)) {\n return undefined;\n }\n\n const gasLimits = transactions.map((transaction) =>\n new BigNumber(transaction.gas as Hex).toNumber(),\n );\n const totalGasLimit = gasLimits.reduce((acc, gasLimit) => acc + gasLimit, 0);\n\n return { gasLimits, totalGasLimit };\n}\n\nexport async function estimateGasBatch({\n from,\n getSimulationConfig,\n isAtomicBatchSupported,\n messenger,\n networkClientId,\n transactions,\n}: {\n from: Hex;\n getSimulationConfig: GetSimulationConfig;\n isAtomicBatchSupported: (\n request: IsAtomicBatchSupportedRequest,\n ) => Promise<IsAtomicBatchSupportedResult>;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n transactions: BatchTransactionParams[];\n}): Promise<EstimateGasBatchResult> {\n const chainId = getChainId({ messenger, networkClientId });\n\n const is7702Result = await isAtomicBatchSupported({\n address: from,\n chainIds: [chainId],\n });\n\n const accountSupports7702 = doesAccountSupportEIP7702(messenger, from);\n\n const chainResult = accountSupports7702\n ? is7702Result.find((result) => result.chainId === chainId)\n : undefined;\n\n const isUpgradeRequired = Boolean(chainResult && !chainResult.isSupported);\n\n if (isUpgradeRequired && !chainResult?.upgradeContractAddress) {\n throw new Error('Upgrade contract address not found');\n }\n\n if (chainResult) {\n const providedBatchGasLimits = getProvidedBatchGasLimits(transactions);\n\n const authorizationList = isUpgradeRequired\n ? [{ address: chainResult.upgradeContractAddress as Hex }]\n : undefined;\n\n const type = isUpgradeRequired\n ? TransactionEnvelopeType.setCode\n : undefined;\n\n const params: TransactionParams = {\n ...generateEIP7702BatchTransaction(from, transactions),\n authorizationList,\n from,\n type,\n };\n\n // Prefer real EIP-7702 simulation when it succeeds — the bundled call has\n // no per-tx intrinsic gas cost so the estimate is typically lower than\n // summing per-tx provided limits. Fall back to the provided sum when the\n // node-level simulation fails (e.g. predict-withdraw, where the batch's\n // first sub-call provides source-token balance to subsequent sub-calls).\n const { estimatedGas: gasLimitHex, simulationFails } = await estimateGas({\n isSimulationEnabled: true,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams: params,\n });\n\n if (simulationFails && providedBatchGasLimits) {\n log(\n 'EIP-7702 estimation failed, using batch parameter gas limits',\n providedBatchGasLimits,\n simulationFails,\n );\n return {\n gasLimits: [providedBatchGasLimits.totalGasLimit],\n ...(isUpgradeRequired ? { requiresAuthorizationList: true } : {}),\n totalGasLimit: providedBatchGasLimits.totalGasLimit,\n };\n }\n\n const totalGasLimit = new BigNumber(gasLimitHex).toNumber();\n\n log('Estimated EIP-7702 gas limit', totalGasLimit);\n\n return {\n gasLimits: [totalGasLimit],\n ...(isUpgradeRequired ? { requiresAuthorizationList: true } : {}),\n totalGasLimit,\n };\n }\n\n const providedBatchGasLimits = getProvidedBatchGasLimits(transactions);\n if (providedBatchGasLimits) {\n log('Using batch parameter gas limits', providedBatchGasLimits);\n return providedBatchGasLimits;\n }\n\n const { gasLimits: gasLimitsHex } = await simulateGasBatch({\n chainId,\n from,\n getSimulationConfig,\n transactions: transactions.map((transaction) => ({\n params: transaction,\n })),\n });\n\n const gasLimits = transactions.map((transaction, index) =>\n transaction.gas\n ? new BigNumber(transaction.gas).toNumber()\n : new BigNumber(gasLimitsHex[index]).toNumber(),\n );\n\n const totalGasLimit = gasLimits.reduce((acc, gasLimit) => acc + gasLimit, 0);\n\n log('Simulated batch gas limits', { totalGasLimit, gasLimits });\n\n return { totalGasLimit, gasLimits };\n}\n\n/**\n * Add a buffer to the provided estimated gas.\n * The buffer is calculated based on the block gas limit and a multiplier.\n *\n * @param estimatedGas - The estimated gas.\n * @param blockGasLimit - The block gas limit.\n * @param multiplier - The multiplier to apply to the estimated gas.\n * @returns The gas with the buffer applied.\n */\nexport function addGasBuffer(\n estimatedGas: string,\n blockGasLimit: string,\n multiplier: number,\n): string {\n const estimatedGasBN = hexToBN(estimatedGas);\n\n const maxGasBN = fractionBN(\n hexToBN(blockGasLimit),\n MAX_GAS_BLOCK_PERCENT,\n 100,\n );\n\n const paddedGasBN = fractionBN(estimatedGasBN, multiplier * 100, 100);\n\n if (estimatedGasBN.gt(maxGasBN)) {\n const estimatedGasHex = add0x(estimatedGas);\n log('Using estimated value', estimatedGasHex);\n return estimatedGasHex;\n }\n\n if (paddedGasBN.lt(maxGasBN)) {\n const paddedHex = add0x(BNToHex(paddedGasBN));\n log('Using padded estimate', paddedHex, multiplier);\n return paddedHex;\n }\n\n const maxHex = add0x(BNToHex(maxGasBN));\n log('Using 90% of block gas limit', maxHex);\n return maxHex;\n}\n\n/**\n * Simulate the required gas for a batch of transactions using the simulation API.\n *\n * @param options - The options object.\n * @param options.chainId - The chain ID of the transactions.\n * @param options.from - The address of the sender.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.transactions - The array of transactions within a batch request.\n * @returns An object containing the transactions with their gas limits and the total gas limit.\n */\nexport async function simulateGasBatch({\n chainId,\n from,\n getSimulationConfig,\n transactions,\n}: {\n chainId: Hex;\n from: Hex;\n getSimulationConfig: GetSimulationConfig;\n transactions: TransactionBatchSingleRequest[];\n}): Promise<{ totalGasLimit: Hex; gasLimits: Hex[] }> {\n try {\n const response = await simulateTransactions(chainId, {\n getSimulationConfig,\n transactions: transactions.map((transaction) => ({\n ...transaction.params,\n from,\n })),\n });\n\n if (response?.transactions?.length !== transactions.length) {\n throw new Error('Simulation response does not match transaction count');\n }\n\n return response.transactions.reduce<{\n totalGasLimit: Hex;\n gasLimits: Hex[];\n }>(\n (acc, transaction) => {\n const gasLimit = transaction?.gasLimit;\n\n if (!gasLimit) {\n throw new Error(\n 'No simulated gas returned for one of the transactions',\n );\n }\n\n acc.gasLimits.push(gasLimit);\n\n acc.totalGasLimit = BNToHex(\n hexToBN(acc.totalGasLimit).add(hexToBN(gasLimit)),\n );\n\n return acc;\n },\n {\n totalGasLimit: '0x0',\n gasLimits: [],\n },\n );\n } catch (error: unknown) {\n log('Error while simulating gas batch', error);\n throw new Error(\n 'Cannot estimate transaction batch total gas as simulation failed',\n );\n }\n}\n\n/**\n * Determine the gas for the provided request.\n *\n * @param request - The request object including the necessary parameters.\n * @returns The final gas value and the estimate used.\n */\nasync function getGas(\n request: UpdateGasRequest,\n): Promise<[string, TransactionMeta['simulationFails']?, string?, Revert?]> {\n const {\n isCustomNetwork,\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n txMeta,\n } = request;\n const { networkClientId } = txMeta;\n const chainId = getChainId({ messenger, networkClientId });\n const { disableGasBuffer } = txMeta;\n\n if (txMeta.txParams.gas) {\n log('Using value from request', txMeta.txParams.gas);\n return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];\n }\n\n const {\n blockGasLimit,\n estimatedGas,\n gasRevert,\n isUpgradeWithData,\n simulationFails,\n } = await estimateGas({\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams: txMeta.txParams,\n });\n\n log('Original estimated gas', estimatedGas);\n\n if (simulationFails) {\n log('Using original fallback estimate as simulation failed');\n }\n\n if (disableGasBuffer) {\n log('Gas buffer disabled');\n }\n\n if (simulationFails || disableGasBuffer) {\n return [estimatedGas, simulationFails, estimatedGas, gasRevert];\n }\n\n const bufferMultiplier = getGasEstimateBuffer({\n chainId,\n isCustomRPC: isCustomNetwork,\n isUpgradeWithData,\n messenger,\n });\n\n log('Buffer', bufferMultiplier);\n\n const bufferedGas = addGasBuffer(\n estimatedGas,\n blockGasLimit,\n bufferMultiplier,\n );\n\n log('Buffered gas', bufferedGas);\n\n return [bufferedGas, simulationFails, estimatedGas, gasRevert];\n}\n\n/**\n * Get the latest block from the network.\n *\n * @param messenger - The messenger instance for communication.\n * @param networkClientId - The network client ID.\n * @returns The latest block number.\n */\nasync function getLatestBlock(\n messenger: TransactionControllerMessenger,\n networkClientId: NetworkClientId,\n): Promise<{ gasLimit: string; number: string }> {\n return (await rpcRequest({\n messenger,\n networkClientId,\n method: 'eth_getBlockByNumber',\n params: ['latest', false],\n })) as { gasLimit: string; number: string };\n}\n\n/**\n * Estimate the gas for a type 4 transaction.\n *\n * @param txParams - The transaction parameters.\n * @param messenger - The messenger instance for communication.\n * @param networkClientId - The network client ID.\n * @param chainId - The chain ID of the transaction.\n * @param getSimulationConfig - The function to get the simulation configuration.\n * @returns The estimated gas.\n */\nasync function estimateGasUpgradeWithDataToSelf(\n txParams: TransactionParams,\n messenger: TransactionControllerMessenger,\n networkClientId: NetworkClientId,\n chainId: Hex,\n getSimulationConfig: GetSimulationConfig,\n): Promise<Hex> {\n const upgradeGas = (await rpcRequest({\n messenger,\n networkClientId,\n method: 'eth_estimateGas',\n params: [\n {\n ...txParams,\n to: txParams.from,\n data: '0x',\n },\n ],\n })) as Hex;\n\n log('Upgrade only gas', upgradeGas);\n\n const delegationAddress = txParams.authorizationList?.[0].address as Hex;\n\n let executeGas: Hex | undefined;\n\n try {\n executeGas = await simulateGas({\n chainId,\n delegationAddress,\n getSimulationConfig,\n transaction: txParams,\n });\n } catch (error: unknown) {\n log('Error while simulating data portion of upgrade', error);\n }\n\n if (executeGas === undefined) {\n try {\n executeGas = await estimateGasNode(\n messenger,\n networkClientId,\n { ...txParams, authorizationList: undefined, type: undefined },\n delegationAddress,\n );\n } catch (error: unknown) {\n log('Error while estimating data portion of upgrade', error);\n throw error;\n }\n\n log('Success estimating data portion of upgrade', executeGas);\n }\n\n log('Execute gas', executeGas);\n\n const total = BNToHex(\n hexToBN(upgradeGas).add(hexToBN(executeGas)).subn(INTRINSIC_GAS),\n );\n\n log('Total type 4 gas', total);\n\n return total;\n}\n\n/**\n * Simulate the required gas using the simulation API.\n *\n * @param options - The options object.\n * @param options.chainId - The chain ID of the transaction.\n * @param options.delegationAddress - The delegation address of the sender to mock.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.transaction - The transaction parameters.\n * @returns The simulated gas.\n */\nasync function simulateGas({\n chainId,\n delegationAddress,\n getSimulationConfig,\n transaction,\n}: {\n chainId: Hex;\n delegationAddress?: Hex;\n getSimulationConfig: GetSimulationConfig;\n transaction: TransactionParams;\n}): Promise<Hex> {\n const response = await simulateTransactions(chainId, {\n getSimulationConfig,\n transactions: [\n {\n to: transaction.to as Hex,\n from: transaction.from as Hex,\n data: transaction.data as Hex,\n value: transaction.value as Hex,\n },\n ],\n overrides: {\n [transaction.from]: {\n code:\n delegationAddress &&\n ((DELEGATION_PREFIX + remove0x(delegationAddress)) as Hex),\n },\n },\n });\n\n const gasLimit = response?.transactions?.[0].gasLimit;\n\n if (!gasLimit) {\n throw new Error('No simulated gas returned');\n }\n\n return gasLimit;\n}\n\n/**\n * Populate the authorization list with dummy values.\n *\n * @param authorizationList - The authorization list to prepare.\n * @param chainId - The chain ID to use.\n * @returns The authorization list with dummy values.\n */\nfunction normalizeAuthorizationList(\n authorizationList: AuthorizationList | undefined,\n chainId: Hex,\n): AuthorizationList | undefined {\n return authorizationList?.map((authorization) => ({\n ...authorization,\n chainId: authorization.chainId ?? chainId,\n nonce: authorization.nonce ?? '0x1',\n r: authorization.r ?? DUMMY_AUTHORIZATION_SIGNATURE,\n s: authorization.s ?? DUMMY_AUTHORIZATION_SIGNATURE,\n yParity: authorization.yParity ?? '0x1',\n }));\n}\n\n/**\n * Normalize the value to a canonical hex quantity with no leading zero digits.\n * Some RPC nodes (e.g. Go's `hexutil`) reject quantities such as `0x00` or\n * `0x0de0b6b3a7640000`, failing gas estimation entirely.\n *\n * @param value - The transaction value to normalize.\n * @returns The normalized transaction value.\n */\nfunction normalizeValue(value: string | undefined): string {\n const valueOrDefault = value ?? '0x0';\n\n if (!isStrictHexString(valueOrDefault)) {\n return valueOrDefault;\n }\n\n const stripped = remove0x(valueOrDefault).replace(/^0+/u, '');\n\n return stripped.length === 0 ? '0x0' : add0x(stripped);\n}\n\n/**\n * Estimate the gas for a transaction using the `eth_estimateGas` method.\n *\n * @param messenger - The messenger instance for communication.\n * @param networkClientId - The network client ID.\n * @param txParams - The transaction parameters.\n * @param delegationAddress - The delegation address of the sender to mock.\n * @returns The estimated gas as a hex string.\n */\nasync function estimateGasNode(\n messenger: TransactionControllerMessenger,\n networkClientId: NetworkClientId,\n txParams: TransactionParams,\n delegationAddress?: Hex,\n): Promise<Hex> {\n const { from } = txParams;\n const params = [txParams] as Json[];\n\n if (delegationAddress) {\n params.push('latest');\n\n params.push({\n [from]: {\n code: DELEGATION_PREFIX + remove0x(delegationAddress),\n },\n });\n }\n\n return (await rpcRequest({\n messenger,\n networkClientId,\n method: 'eth_estimateGas',\n params,\n })) as Hex;\n}\n"]}
1
+ {"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,OAAO,EACP,KAAK,GACN,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EACL,KAAK,EACL,kBAAkB,EAClB,iBAAiB,EACjB,QAAQ,GACT,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,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AAS;QACT,eAAe;QACf,MAAM,EAAE,iBAAiB;QACzB,MAAM;KACP,CAAC,CAAQ,CAAC;AACb,CAAC","sourcesContent":["import {\n BNToHex,\n fractionBN,\n hexToBN,\n toHex,\n} from '@metamask/controller-utils';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport {\n add0x,\n createModuleLogger,\n isStrictHexString,\n remove0x,\n} from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { simulateTransactions } from '../api/simulation-api.js';\nimport type { SimulationRequestTransaction } from '../api/simulation-api.js';\nimport { projectLogger } from '../logger.js';\nimport type { TransactionControllerMessenger } from '../TransactionController.js';\nimport { TransactionEnvelopeType } from '../types.js';\nimport type {\n AuthorizationList,\n BatchTransactionParams,\n GetSimulationConfig,\n IsAtomicBatchSupportedRequest,\n IsAtomicBatchSupportedResult,\n Revert,\n TransactionBatchSingleRequest,\n TransactionMeta,\n TransactionParams,\n} from '../types.js';\nimport {\n DELEGATION_PREFIX,\n doesAccountSupportEIP7702,\n generateEIP7702BatchTransaction,\n getAuthorizationAuthority,\n getDelegationAddress,\n isAuthorizationSigned,\n} from './eip7702.js';\nimport {\n getGasEstimateBuffer,\n getGasEstimateFallback,\n} from './feature-flags.js';\nimport { getChainId, rpcRequest } from './provider.js';\nimport { decodeRevert } from './revert-reason.js';\n\nexport type UpdateGasRequest = {\n isCustomNetwork: boolean;\n isSimulationEnabled: boolean;\n getSimulationConfig: GetSimulationConfig;\n messenger: TransactionControllerMessenger;\n txMeta: TransactionMeta;\n};\n\nexport type EstimateGasBatchResult = {\n gasLimits: number[];\n requiresAuthorizationList?: true;\n totalGasLimit: number;\n};\n\nexport const log = createModuleLogger(projectLogger, 'gas');\n\nexport const DEFAULT_GAS_MULTIPLIER = 1.5;\nexport const MAX_GAS_BLOCK_PERCENT = 90;\n\nexport const DUMMY_AUTHORIZATION_SIGNATURE =\n '0x1111111111111111111111111111111111111111111111111111111111111111';\n\n/**\n * EIP-2780 state gas charged at runtime when a valid authorization sets a\n * delegation indicator for an authority that had none at the start of the\n * transaction, at most once per authority.\n *\n * When the authority is the sender, this is the only runtime authorization\n * charge: `ACCOUNT_WRITE` is covered by the transaction base cost, and the\n * sender always exists in state as its nonce is incremented first.\n */\nexport const EIP_2780_AUTHORIZATION_GAS = 35190;\n\n/**\n * Intrinsic gas cost for each authorization omitted from the node estimate.\n */\nexport const EIP7280_AUTHORIZATION_INTRINSIC_GAS = 7816;\n\n/**\n * Error for a simulated transaction that failed, as opposed to the simulation\n * itself being unavailable.\n */\nclass SimulatedTransactionError extends Error {}\n\n/**\n * Populate the gas properties of the provided transaction meta.\n *\n * @param request - The request object including the necessary parameters.\n */\nexport async function updateGas(request: UpdateGasRequest): Promise<void> {\n const { txMeta } = request;\n const initialParams = { ...txMeta.txParams };\n\n const [gas, simulationFails, gasLimitNoBuffer, gasRevert] =\n await getGas(request);\n\n txMeta.txParams.gas = gas;\n txMeta.simulationFails = simulationFails;\n txMeta.gasLimitNoBuffer = gasLimitNoBuffer;\n\n if (gasRevert) {\n txMeta.revert = { ...txMeta.revert, gas: gasRevert };\n }\n\n if (!initialParams.gas) {\n txMeta.originalGasEstimate = txMeta.txParams.gas;\n }\n\n txMeta.defaultGasEstimates ??= {};\n txMeta.defaultGasEstimates.gas = txMeta.txParams.gas;\n}\n\n/**\n * Estimate the gas for the provided transaction parameters.\n * If the gas estimate fails, the fallback value is returned.\n *\n * @param options - The options object.\n * @param options.ignoreDelegationSignatures - Ignore signature errors if submitting delegations to the DelegationManager.\n * @param options.isSimulationEnabled - Whether the simulation is enabled.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.messenger - The messenger instance for communication.\n * @param options.networkClientId - The network client ID.\n * @param options.txParams - The transaction parameters.\n * @returns The estimated gas and related info.\n */\nexport async function estimateGas({\n ignoreDelegationSignatures,\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams,\n}: {\n ignoreDelegationSignatures?: boolean;\n isSimulationEnabled: boolean;\n getSimulationConfig: GetSimulationConfig;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n txParams: TransactionParams;\n}): Promise<{\n blockGasLimit: string;\n estimatedGas: string;\n gasRevert?: Revert;\n isUpgradeWithData: boolean;\n simulationFails: TransactionMeta['simulationFails'];\n}> {\n const request = { ...txParams };\n const { authorizationList, data, value } = request;\n const chainId = getChainId({ messenger, networkClientId });\n\n if (ignoreDelegationSignatures && !isSimulationEnabled) {\n throw new Error(\n 'Gas estimation with ignored delegation signatures is not supported as simulation disabled',\n );\n }\n\n const { gasLimit: blockGasLimit, number: blockNumber } = await getLatestBlock(\n messenger,\n networkClientId,\n );\n\n const blockGasLimitBN = hexToBN(blockGasLimit);\n const { percentage, fixed, maxGasLimit } = getGasEstimateFallback(\n chainId,\n messenger,\n );\n\n const uncappedFallback = fixed\n ? toHex(fixed)\n : BNToHex(fractionBN(blockGasLimitBN, percentage, 100));\n\n // Clamp the fallback to the chain's per-transaction gas cap so it can never\n // exceed the gas limit the RPC will accept. Without this, a percentage of a\n // high block gas limit can overshoot the cap (e.g. 35% of Polygon's ~140M\n // block limit is ~49M, but the node caps a tx at ~33.5M and rejects it with\n // \"transaction gas limit too high\").\n const fallback =\n maxGasLimit !== undefined &&\n hexToBN(uncappedFallback).gt(hexToBN(toHex(maxGasLimit)))\n ? toHex(maxGasLimit)\n : uncappedFallback;\n\n log('Estimation fallback values', fallback);\n\n request.data = data ? add0x(data) : data;\n request.value = normalizeValue(value);\n\n request.authorizationList = normalizeAuthorizationList(\n request.authorizationList,\n chainId,\n );\n\n delete request.gasPrice;\n delete request.maxFeePerGas;\n delete request.maxPriorityFeePerGas;\n\n let estimatedGas = fallback;\n let simulationFails: TransactionMeta['simulationFails'];\n let gasRevert: Revert | undefined;\n\n const isUpgradeWithData =\n txParams.type === TransactionEnvelopeType.setCode &&\n Boolean(authorizationList?.length) &&\n Boolean(data) &&\n data !== '0x';\n\n const type4AuthorizationList =\n txParams.type === TransactionEnvelopeType.setCode &&\n authorizationList?.length\n ? authorizationList\n : undefined;\n\n try {\n if (type4AuthorizationList) {\n estimatedGas = await estimateGasType4({\n authorizationList: type4AuthorizationList,\n chainId,\n getSimulationConfig,\n isSimulationEnabled,\n messenger,\n networkClientId,\n txParams: request,\n });\n } else if (ignoreDelegationSignatures && isSimulationEnabled) {\n estimatedGas = await simulateGas({\n chainId,\n getSimulationConfig,\n transaction: request,\n });\n } else {\n estimatedGas = await estimateGasNode(messenger, networkClientId, request);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n simulationFails = {\n reason: error.message,\n errorKey: error.errorKey,\n debug: {\n blockNumber,\n blockGasLimit,\n },\n };\n\n gasRevert = decodeRevert(error, 'gas');\n\n log('Estimation failed', { ...simulationFails, fallback, gasRevert });\n }\n\n return {\n blockGasLimit,\n estimatedGas,\n gasRevert,\n isUpgradeWithData,\n simulationFails,\n };\n}\n\n/**\n * Sum caller-provided gas limits across a batch.\n *\n * If every transaction in the batch already has a `gas` value, returns the\n * parsed per-tx limits and their sum. Otherwise returns `undefined`.\n *\n * Used by `estimateGasBatch`:\n * - non-7702 path: short-circuits simulation entirely when present.\n * - EIP-7702 path: used as a fallback when simulation fails — required for\n * callers that submit batches whose individual sub-calls cannot be simulated\n * standalone (e.g. predict-withdraw, where the batch's first sub-call\n * provides source-token balance to subsequent sub-calls). When 7702\n * simulation succeeds it is preferred since the bundled call has no per-tx\n * intrinsic gas cost and produces a tighter estimate.\n *\n * @param transactions - Batch transactions to inspect.\n * @returns Parsed gas limits and total when every transaction has gas; otherwise `undefined`.\n */\nexport function getProvidedBatchGasLimits(\n transactions: BatchTransactionParams[],\n): { gasLimits: number[]; totalGasLimit: number } | undefined {\n if (!transactions.every((transaction) => transaction.gas !== undefined)) {\n return undefined;\n }\n\n const gasLimits = transactions.map((transaction) =>\n new BigNumber(transaction.gas as Hex).toNumber(),\n );\n const totalGasLimit = gasLimits.reduce((acc, gasLimit) => acc + gasLimit, 0);\n\n return { gasLimits, totalGasLimit };\n}\n\nexport async function estimateGasBatch({\n from,\n getSimulationConfig,\n isAtomicBatchSupported,\n messenger,\n networkClientId,\n transactions,\n}: {\n from: Hex;\n getSimulationConfig: GetSimulationConfig;\n isAtomicBatchSupported: (\n request: IsAtomicBatchSupportedRequest,\n ) => Promise<IsAtomicBatchSupportedResult>;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n transactions: BatchTransactionParams[];\n}): Promise<EstimateGasBatchResult> {\n const chainId = getChainId({ messenger, networkClientId });\n\n const is7702Result = await isAtomicBatchSupported({\n address: from,\n chainIds: [chainId],\n });\n\n const accountSupports7702 = doesAccountSupportEIP7702(messenger, from);\n\n const chainResult = accountSupports7702\n ? is7702Result.find((result) => result.chainId === chainId)\n : undefined;\n\n const isUpgradeRequired = Boolean(chainResult && !chainResult.isSupported);\n\n if (isUpgradeRequired && !chainResult?.upgradeContractAddress) {\n throw new Error('Upgrade contract address not found');\n }\n\n if (chainResult) {\n const providedBatchGasLimits = getProvidedBatchGasLimits(transactions);\n\n const authorizationList = isUpgradeRequired\n ? [{ address: chainResult.upgradeContractAddress as Hex }]\n : undefined;\n\n const type = isUpgradeRequired\n ? TransactionEnvelopeType.setCode\n : undefined;\n\n const params: TransactionParams = {\n ...generateEIP7702BatchTransaction(from, transactions),\n authorizationList,\n from,\n type,\n };\n\n // Prefer real EIP-7702 simulation when it succeeds — the bundled call has\n // no per-tx intrinsic gas cost so the estimate is typically lower than\n // summing per-tx provided limits. Fall back to the provided sum when the\n // node-level simulation fails (e.g. predict-withdraw, where the batch's\n // first sub-call provides source-token balance to subsequent sub-calls).\n const { estimatedGas: gasLimitHex, simulationFails } = await estimateGas({\n isSimulationEnabled: true,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams: params,\n });\n\n if (simulationFails && providedBatchGasLimits) {\n log(\n 'EIP-7702 estimation failed, using batch parameter gas limits',\n providedBatchGasLimits,\n simulationFails,\n );\n return {\n gasLimits: [providedBatchGasLimits.totalGasLimit],\n ...(isUpgradeRequired ? { requiresAuthorizationList: true } : {}),\n totalGasLimit: providedBatchGasLimits.totalGasLimit,\n };\n }\n\n const totalGasLimit = new BigNumber(gasLimitHex).toNumber();\n\n log('Estimated EIP-7702 gas limit', totalGasLimit);\n\n return {\n gasLimits: [totalGasLimit],\n ...(isUpgradeRequired ? { requiresAuthorizationList: true } : {}),\n totalGasLimit,\n };\n }\n\n const providedBatchGasLimits = getProvidedBatchGasLimits(transactions);\n if (providedBatchGasLimits) {\n log('Using batch parameter gas limits', providedBatchGasLimits);\n return providedBatchGasLimits;\n }\n\n const { gasLimits: gasLimitsHex } = await simulateGasBatch({\n chainId,\n from,\n getSimulationConfig,\n transactions: transactions.map((transaction) => ({\n params: transaction,\n })),\n });\n\n const gasLimits = transactions.map((transaction, index) =>\n transaction.gas\n ? new BigNumber(transaction.gas).toNumber()\n : new BigNumber(gasLimitsHex[index]).toNumber(),\n );\n\n const totalGasLimit = gasLimits.reduce((acc, gasLimit) => acc + gasLimit, 0);\n\n log('Simulated batch gas limits', { totalGasLimit, gasLimits });\n\n return { totalGasLimit, gasLimits };\n}\n\n/**\n * Add a buffer to the provided estimated gas.\n * The buffer is calculated based on the block gas limit and a multiplier.\n *\n * @param estimatedGas - The estimated gas.\n * @param blockGasLimit - The block gas limit.\n * @param multiplier - The multiplier to apply to the estimated gas.\n * @returns The gas with the buffer applied.\n */\nexport function addGasBuffer(\n estimatedGas: string,\n blockGasLimit: string,\n multiplier: number,\n): string {\n const estimatedGasBN = hexToBN(estimatedGas);\n\n const maxGasBN = fractionBN(\n hexToBN(blockGasLimit),\n MAX_GAS_BLOCK_PERCENT,\n 100,\n );\n\n const paddedGasBN = fractionBN(estimatedGasBN, multiplier * 100, 100);\n\n if (estimatedGasBN.gt(maxGasBN)) {\n const estimatedGasHex = add0x(estimatedGas);\n log('Using estimated value', estimatedGasHex);\n return estimatedGasHex;\n }\n\n if (paddedGasBN.lt(maxGasBN)) {\n const paddedHex = add0x(BNToHex(paddedGasBN));\n log('Using padded estimate', paddedHex, multiplier);\n return paddedHex;\n }\n\n const maxHex = add0x(BNToHex(maxGasBN));\n log('Using 90% of block gas limit', maxHex);\n return maxHex;\n}\n\n/**\n * Simulate the required gas for a batch of transactions using the simulation API.\n *\n * @param options - The options object.\n * @param options.chainId - The chain ID of the transactions.\n * @param options.from - The address of the sender.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.transactions - The array of transactions within a batch request.\n * @returns An object containing the transactions with their gas limits and the total gas limit.\n */\nexport async function simulateGasBatch({\n chainId,\n from,\n getSimulationConfig,\n transactions,\n}: {\n chainId: Hex;\n from: Hex;\n getSimulationConfig: GetSimulationConfig;\n transactions: TransactionBatchSingleRequest[];\n}): Promise<{ totalGasLimit: Hex; gasLimits: Hex[] }> {\n try {\n const response = await simulateTransactions(chainId, {\n getSimulationConfig,\n transactions: transactions.map((transaction) => ({\n ...transaction.params,\n from,\n })),\n });\n\n if (response?.transactions?.length !== transactions.length) {\n throw new Error('Simulation response does not match transaction count');\n }\n\n return response.transactions.reduce<{\n totalGasLimit: Hex;\n gasLimits: Hex[];\n }>(\n (acc, transaction) => {\n const gasLimit = transaction?.gasLimit;\n\n if (!gasLimit) {\n throw new Error(\n 'No simulated gas returned for one of the transactions',\n );\n }\n\n acc.gasLimits.push(gasLimit);\n\n acc.totalGasLimit = BNToHex(\n hexToBN(acc.totalGasLimit).add(hexToBN(gasLimit)),\n );\n\n return acc;\n },\n {\n totalGasLimit: '0x0',\n gasLimits: [],\n },\n );\n } catch (error: unknown) {\n log('Error while simulating gas batch', error);\n throw new Error(\n 'Cannot estimate transaction batch total gas as simulation failed',\n );\n }\n}\n\n/**\n * Determine the gas for the provided request.\n *\n * @param request - The request object including the necessary parameters.\n * @returns The final gas value and the estimate used.\n */\nasync function getGas(\n request: UpdateGasRequest,\n): Promise<[string, TransactionMeta['simulationFails']?, string?, Revert?]> {\n const {\n isCustomNetwork,\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n txMeta,\n } = request;\n const { networkClientId } = txMeta;\n const chainId = getChainId({ messenger, networkClientId });\n const { disableGasBuffer } = txMeta;\n\n if (txMeta.txParams.gas) {\n log('Using value from request', txMeta.txParams.gas);\n return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];\n }\n\n const {\n blockGasLimit,\n estimatedGas,\n gasRevert,\n isUpgradeWithData,\n simulationFails,\n } = await estimateGas({\n isSimulationEnabled,\n getSimulationConfig,\n messenger,\n networkClientId,\n txParams: txMeta.txParams,\n });\n\n log('Original estimated gas', estimatedGas);\n\n if (simulationFails) {\n log('Using original fallback estimate as simulation failed');\n }\n\n if (disableGasBuffer) {\n log('Gas buffer disabled');\n }\n\n if (simulationFails || disableGasBuffer) {\n return [estimatedGas, simulationFails, estimatedGas, gasRevert];\n }\n\n const bufferMultiplier = getGasEstimateBuffer({\n chainId,\n isCustomRPC: isCustomNetwork,\n isUpgradeWithData,\n messenger,\n });\n\n log('Buffer', bufferMultiplier);\n\n const bufferedGas = addGasBuffer(\n estimatedGas,\n blockGasLimit,\n bufferMultiplier,\n );\n\n log('Buffered gas', bufferedGas);\n\n return [bufferedGas, simulationFails, estimatedGas, gasRevert];\n}\n\n/**\n * Get the latest block from the network.\n *\n * @param messenger - The messenger instance for communication.\n * @param networkClientId - The network client ID.\n * @returns The latest block number.\n */\nasync function getLatestBlock(\n messenger: TransactionControllerMessenger,\n networkClientId: NetworkClientId,\n): Promise<{ gasLimit: string; number: string }> {\n return (await rpcRequest({\n messenger,\n networkClientId,\n method: 'eth_getBlockByNumber',\n params: ['latest', false],\n })) as { gasLimit: string; number: string };\n}\n\n/**\n * Estimate the gas for a type 4 transaction as one outer transaction.\n *\n * - If all authorizations are signed, the selected node estimates the exact\n * transaction.\n * - Otherwise, the Simulation API estimates the exact transaction, with the\n * authority of each authorization so that it can apply any EIP-2780 runtime\n * authorization charges.\n * - If the Simulation API is disabled, unsupported or unavailable, the selected\n * node estimates the transaction without unsigned authorizations and with the\n * delegation of the sender overridden. Intrinsic gas is added for each omitted\n * authorization, plus the EIP-2780 delegation charge if the sender is not yet\n * delegated. A simulated transaction error is not retried using the node.\n *\n * @param options - The options object.\n * @param options.authorizationList - The original authorization list of the transaction.\n * @param options.chainId - The chain ID of the transaction.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.isSimulationEnabled - Whether the simulation is enabled.\n * @param options.messenger - The messenger instance for communication.\n * @param options.networkClientId - The network client ID.\n * @param options.txParams - The normalized transaction parameters.\n * @returns The estimated gas.\n */\nasync function estimateGasType4({\n authorizationList,\n chainId,\n getSimulationConfig,\n isSimulationEnabled,\n messenger,\n networkClientId,\n txParams,\n}: {\n authorizationList: AuthorizationList;\n chainId: Hex;\n getSimulationConfig: GetSimulationConfig;\n isSimulationEnabled: boolean;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n txParams: TransactionParams;\n}): Promise<Hex> {\n if (authorizationList.every(isAuthorizationSigned)) {\n log('Estimating type 4 gas using node as all authorizations signed');\n return await estimateGasNode(messenger, networkClientId, txParams);\n }\n\n if (isSimulationEnabled) {\n const simulationAuthorizationList = authorizationList.map(\n (authorization) => {\n const authority = getAuthorizationAuthority(\n authorization,\n txParams.from,\n );\n\n if (!authority) {\n throw new Error('Cannot determine authority of signed authorization');\n }\n\n return { address: authorization.address, from: authority };\n },\n );\n\n log('Estimating type 4 gas using simulation', simulationAuthorizationList);\n\n try {\n return await simulateGas({\n authorizationList: simulationAuthorizationList,\n chainId,\n getSimulationConfig,\n transaction: txParams,\n });\n } catch (error) {\n if (error instanceof SimulatedTransactionError) {\n throw error;\n }\n\n log('Simulation unavailable, estimating type 4 gas using node', error);\n }\n }\n\n return await estimateGasType4Node({\n authorizationList,\n messenger,\n networkClientId,\n txParams,\n });\n}\n\n/**\n * Estimate the gas for a type 4 transaction with unsigned authorizations using\n * the selected node.\n *\n * Unsigned authorizations are omitted from the node request and their intrinsic\n * gas is added manually per entry. Their authority is the sender, so the\n * delegation of the sender is set with a state override, and the EIP-2780 charge\n * for a new delegation indicator is added once if the sender is not yet\n * delegated. Signed authorizations remain in the request and are charged by the\n * node.\n *\n * @param options - The options object.\n * @param options.authorizationList - The original authorization list of the transaction.\n * @param options.messenger - The messenger instance for communication.\n * @param options.networkClientId - The network client ID.\n * @param options.txParams - The normalized transaction parameters.\n * @returns The estimated gas including omitted authorization costs.\n */\nasync function estimateGasType4Node({\n authorizationList,\n messenger,\n networkClientId,\n txParams,\n}: {\n authorizationList: AuthorizationList;\n messenger: TransactionControllerMessenger;\n networkClientId: NetworkClientId;\n txParams: TransactionParams;\n}): Promise<Hex> {\n const unsignedAuthorizations = authorizationList.filter(\n (authorization) => !isAuthorizationSigned(authorization),\n );\n const signedAuthorizations = authorizationList.filter(isAuthorizationSigned);\n const nodeTxParams = {\n ...txParams,\n authorizationList: signedAuthorizations.length\n ? signedAuthorizations\n : undefined,\n type: signedAuthorizations.length ? txParams.type : undefined,\n };\n\n // Unsigned authorizations are signed by the sender, and the last valid\n // authorization for an authority sets its delegation.\n const delegationAddress =\n unsignedAuthorizations[unsignedAuthorizations.length - 1].address;\n\n const [nodeGas, currentDelegationAddress] = await Promise.all([\n estimateGasNode(\n messenger,\n networkClientId,\n nodeTxParams,\n delegationAddress,\n ),\n getDelegationAddress(txParams.from as Hex, messenger, networkClientId),\n ]);\n\n const intrinsicGas =\n unsignedAuthorizations.length * EIP7280_AUTHORIZATION_INTRINSIC_GAS;\n const delegationGas = currentDelegationAddress\n ? 0\n : EIP_2780_AUTHORIZATION_GAS;\n\n log('Type 4 gas using node', {\n currentDelegationAddress,\n delegationGas,\n intrinsicGas,\n nodeGas,\n });\n\n return BNToHex(hexToBN(nodeGas).addn(intrinsicGas + delegationGas));\n}\n\n/**\n * Simulate the required gas using the simulation API.\n *\n * If an authorization list is provided, it is included in the simulated\n * transaction, a simulated transaction error is thrown as a\n * `SimulatedTransactionError`, and the response must include a non-zero gas\n * limit.\n *\n * @param options - The options object.\n * @param options.authorizationList - The authorization list with the authority of each entry.\n * @param options.chainId - The chain ID of the transaction.\n * @param options.getSimulationConfig - The function to get the simulation configuration.\n * @param options.transaction - The transaction parameters.\n * @returns The simulated gas.\n */\nasync function simulateGas({\n authorizationList,\n chainId,\n getSimulationConfig,\n transaction,\n}: {\n authorizationList?: SimulationRequestTransaction['authorizationList'];\n chainId: Hex;\n getSimulationConfig: GetSimulationConfig;\n transaction: TransactionParams;\n}): Promise<Hex> {\n const response = await simulateTransactions(chainId, {\n getSimulationConfig,\n transactions: [\n {\n ...(authorizationList ? { authorizationList } : {}),\n to: transaction.to as Hex,\n from: transaction.from as Hex,\n data: transaction.data as Hex,\n value: transaction.value as Hex,\n },\n ],\n });\n\n const simulatedTransaction = response?.transactions?.[0];\n const gasLimit = simulatedTransaction?.gasLimit;\n\n if (authorizationList && simulatedTransaction?.error) {\n throw new SimulatedTransactionError(\n `Simulation failed: ${simulatedTransaction.error}`,\n );\n }\n\n if (!gasLimit || (authorizationList && hexToBN(gasLimit).isZero())) {\n throw new Error('No simulated gas returned');\n }\n\n return gasLimit;\n}\n\n/**\n * Populate the authorization list with dummy values.\n *\n * @param authorizationList - The authorization list to prepare.\n * @param chainId - The chain ID to use.\n * @returns The authorization list with dummy values.\n */\nfunction normalizeAuthorizationList(\n authorizationList: AuthorizationList | undefined,\n chainId: Hex,\n): AuthorizationList | undefined {\n return authorizationList?.map((authorization) => ({\n ...authorization,\n chainId: authorization.chainId ?? chainId,\n nonce: authorization.nonce ?? '0x1',\n r: authorization.r ?? DUMMY_AUTHORIZATION_SIGNATURE,\n s: authorization.s ?? DUMMY_AUTHORIZATION_SIGNATURE,\n yParity: authorization.yParity ?? '0x1',\n }));\n}\n\n/**\n * Normalize the value to a canonical hex quantity with no leading zero digits.\n * Some RPC nodes (e.g. Go's `hexutil`) reject quantities such as `0x00` or\n * `0x0de0b6b3a7640000`, failing gas estimation entirely.\n *\n * @param value - The transaction value to normalize.\n * @returns The normalized transaction value.\n */\nfunction normalizeValue(value: string | undefined): string {\n const valueOrDefault = value ?? '0x0';\n\n if (!isStrictHexString(valueOrDefault)) {\n return valueOrDefault;\n }\n\n const stripped = remove0x(valueOrDefault).replace(/^0+/u, '');\n\n return stripped.length === 0 ? '0x0' : add0x(stripped);\n}\n\n/**\n * Estimate the gas for a transaction using the `eth_estimateGas` method.\n *\n * @param messenger - The messenger instance for communication.\n * @param networkClientId - The network client ID.\n * @param txParams - The transaction parameters.\n * @param delegationAddress - The delegation address of the sender to mock.\n * @returns The estimated gas as a hex string.\n */\nasync function estimateGasNode(\n messenger: TransactionControllerMessenger,\n networkClientId: NetworkClientId,\n txParams: TransactionParams,\n delegationAddress?: Hex,\n): Promise<Hex> {\n const { from } = txParams;\n const params = [txParams] as Json[];\n\n if (delegationAddress) {\n params.push('latest');\n\n params.push({\n [from]: {\n code: DELEGATION_PREFIX + remove0x(delegationAddress),\n },\n });\n }\n\n return (await rpcRequest({\n messenger,\n networkClientId,\n method: 'eth_estimateGas',\n params,\n })) as Hex;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/transaction-controller",
3
- "version": "72.2.1-preview-a0d99e051",
3
+ "version": "72.2.1-preview-060b37d94",
4
4
  "description": "Stores transactions alongside their periodically updated statuses and manages interactions such as approval and cancellation",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -69,7 +69,7 @@
69
69
  "@metamask/nonce-tracker": "^6.1.0",
70
70
  "@metamask/remote-feature-flag-controller": "^7.0.0",
71
71
  "@metamask/rpc-errors": "^7.0.3",
72
- "@metamask/utils": "^12.0.0",
72
+ "@metamask/utils": "^12.0.1",
73
73
  "async-mutex": "^0.5.0",
74
74
  "bignumber.js": "^9.1.2",
75
75
  "bn.js": "^5.2.5",