@metamask/transaction-controller 29.0.0 → 29.0.2

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.
@@ -4,7 +4,7 @@
4
4
  var _chunkZNZEJDOEjs = require('./chunk-ZNZEJDOE.js');
5
5
 
6
6
 
7
- var _chunkX3KACH5Pjs = require('./chunk-X3KACH5P.js');
7
+ var _chunkIT3SYNZ6js = require('./chunk-IT3SYNZ6.js');
8
8
 
9
9
 
10
10
  var _chunkSD6CWFDFjs = require('./chunk-SD6CWFDF.js');
@@ -234,7 +234,7 @@ var TransactionController = class extends _basecontroller.BaseController {
234
234
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getGlobalChainId);
235
235
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _isCustomNetwork);
236
236
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _internalEvents, new (0, _events.EventEmitter)());
237
- this.inProcessOfSigning = /* @__PURE__ */ new Set();
237
+ this.approvingTransactionIds = /* @__PURE__ */ new Set();
238
238
  this.mutex = new (0, _asyncmutex.Mutex)();
239
239
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _incomingTransactionOptions, void 0);
240
240
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _pendingTransactionOptions, void 0);
@@ -725,9 +725,7 @@ var TransactionController = class extends _basecontroller.BaseController {
725
725
  _chunkUGFBA4GVjs.validateGasValues.call(void 0, gasValues);
726
726
  }
727
727
  _chunkS6VGOPUYjs.projectLogger.call(void 0, "Creating speed up transaction", transactionId, gasValues);
728
- const transactionMeta = this.state.transactions.find(
729
- ({ id }) => id === transactionId
730
- );
728
+ const transactionMeta = this.getTransaction(transactionId);
731
729
  if (!transactionMeta) {
732
730
  return;
733
731
  }
@@ -776,7 +774,7 @@ var TransactionController = class extends _basecontroller.BaseController {
776
774
  unsignedEthTx,
777
775
  transactionMeta.txParams.from
778
776
  );
779
- const transactionMetaWithRsv = await this.updateTransactionMetaRSV(
777
+ const transactionMetaWithRsv = this.updateTransactionMetaRSV(
780
778
  transactionMeta,
781
779
  signedTx
782
780
  );
@@ -1215,10 +1213,10 @@ var TransactionController = class extends _basecontroller.BaseController {
1215
1213
  common
1216
1214
  });
1217
1215
  const initialTxAsSerializedHex = _util.bufferToHex.call(void 0, initialTxAsEthTx.serialize());
1218
- if (this.inProcessOfSigning.has(initialTxAsSerializedHex)) {
1216
+ if (this.approvingTransactionIds.has(initialTxAsSerializedHex)) {
1219
1217
  return "";
1220
1218
  }
1221
- this.inProcessOfSigning.add(initialTxAsSerializedHex);
1219
+ this.approvingTransactionIds.add(initialTxAsSerializedHex);
1222
1220
  let rawTransactions, nonceLock;
1223
1221
  try {
1224
1222
  const fromAddress = initialTx.from;
@@ -1239,7 +1237,7 @@ var TransactionController = class extends _basecontroller.BaseController {
1239
1237
  throw err;
1240
1238
  } finally {
1241
1239
  nonceLock?.releaseLock();
1242
- this.inProcessOfSigning.delete(initialTxAsSerializedHex);
1240
+ this.approvingTransactionIds.delete(initialTxAsSerializedHex);
1243
1241
  }
1244
1242
  return rawTransactions;
1245
1243
  }
@@ -1280,10 +1278,10 @@ var TransactionController = class extends _basecontroller.BaseController {
1280
1278
  transactionMeta,
1281
1279
  _lodash.pickBy.call(void 0, { hash, status })
1282
1280
  );
1283
- if (status === "submitted" /* submitted */) {
1281
+ if (updatedTransactionMeta.status === "submitted" /* submitted */) {
1284
1282
  updatedTransactionMeta.submittedTime = (/* @__PURE__ */ new Date()).getTime();
1285
1283
  }
1286
- if (status === "failed" /* failed */) {
1284
+ if (updatedTransactionMeta.status === "failed" /* failed */) {
1287
1285
  updatedTransactionMeta.error = _chunkUGFBA4GVjs.normalizeTxError.call(void 0, new Error(errorMessage));
1288
1286
  }
1289
1287
  this.updateTransaction(
@@ -1639,68 +1637,62 @@ var TransactionController = class extends _basecontroller.BaseController {
1639
1637
  * @param transactionId - The ID of the transaction to approve.
1640
1638
  */
1641
1639
  async approveTransaction(transactionId) {
1642
- const { transactions } = this.state;
1643
- const releaseLock = await this.mutex.acquire();
1644
- const index = transactions.findIndex(({ id }) => transactionId === id);
1645
- const transactionMeta = transactions[index];
1646
- const updatedTransactionMeta = _lodash.cloneDeep.call(void 0, transactionMeta);
1647
- const {
1648
- txParams: { from },
1649
- networkClientId
1650
- } = transactionMeta;
1651
- let releaseNonceLock;
1640
+ const cleanupTasks = new Array();
1641
+ cleanupTasks.push(await this.mutex.acquire());
1642
+ let transactionMeta = this.getTransactionOrThrow(transactionId);
1652
1643
  try {
1653
1644
  if (!this.sign) {
1654
- releaseLock();
1655
1645
  this.failTransaction(
1656
1646
  transactionMeta,
1657
1647
  new Error("No sign method defined.")
1658
1648
  );
1659
1649
  return "not-approved" /* NotApproved */;
1660
1650
  } else if (!transactionMeta.chainId) {
1661
- releaseLock();
1662
1651
  this.failTransaction(transactionMeta, new Error("No chainId defined."));
1663
1652
  return "not-approved" /* NotApproved */;
1664
1653
  }
1665
- if (this.inProcessOfSigning.has(transactionId)) {
1654
+ if (this.approvingTransactionIds.has(transactionId)) {
1666
1655
  _chunkS6VGOPUYjs.projectLogger.call(void 0, "Skipping approval as signing in progress", transactionId);
1667
1656
  return "not-approved" /* NotApproved */;
1668
1657
  }
1658
+ this.approvingTransactionIds.add(transactionId);
1659
+ cleanupTasks.push(
1660
+ () => this.approvingTransactionIds.delete(transactionId)
1661
+ );
1669
1662
  const [nonce, releaseNonce] = await _chunkZNZEJDOEjs.getNextNonce.call(void 0,
1670
1663
  transactionMeta,
1671
- (address) => _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _multichainTrackingHelper).getNonceLock(address, networkClientId)
1664
+ (address) => _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _multichainTrackingHelper).getNonceLock(
1665
+ address,
1666
+ transactionMeta.networkClientId
1667
+ )
1672
1668
  );
1673
- releaseNonceLock = releaseNonce;
1674
- updatedTransactionMeta.status = "approved" /* approved */;
1675
- updatedTransactionMeta.txParams = {
1676
- ...updatedTransactionMeta.txParams,
1677
- nonce,
1678
- chainId: transactionMeta.chainId
1679
- };
1680
- const baseTxParams = {
1681
- ...updatedTransactionMeta.txParams,
1682
- gasLimit: updatedTransactionMeta.txParams.gas
1683
- };
1684
- this.updateTransaction(
1685
- updatedTransactionMeta,
1686
- "TransactionController#approveTransaction - Transaction approved"
1687
- );
1688
- this.onTransactionStatusChange(updatedTransactionMeta);
1689
- const isEIP1559 = _chunkUGFBA4GVjs.isEIP1559Transaction.call(void 0, updatedTransactionMeta.txParams);
1690
- const txParams = isEIP1559 ? {
1691
- ...baseTxParams,
1692
- estimatedBaseFee: updatedTransactionMeta.txParams.estimatedBaseFee,
1693
- type: "0x2" /* feeMarket */
1694
- } : baseTxParams;
1669
+ releaseNonce && cleanupTasks.push(releaseNonce);
1670
+ transactionMeta = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateTransactionInternal, updateTransactionInternal_fn).call(this, {
1671
+ transactionId,
1672
+ note: "TransactionController#approveTransaction - Transaction approved"
1673
+ }, (draftTxMeta) => {
1674
+ const { txParams, chainId } = draftTxMeta;
1675
+ draftTxMeta.status = "approved" /* approved */;
1676
+ draftTxMeta.txParams = {
1677
+ ...txParams,
1678
+ nonce,
1679
+ chainId,
1680
+ gasLimit: txParams.gas,
1681
+ ..._chunkUGFBA4GVjs.isEIP1559Transaction.call(void 0, txParams) && {
1682
+ type: "0x2" /* feeMarket */
1683
+ }
1684
+ };
1685
+ });
1686
+ this.onTransactionStatusChange(transactionMeta);
1695
1687
  const rawTx = await this.signTransaction(
1696
- updatedTransactionMeta,
1697
- txParams
1688
+ transactionMeta,
1689
+ transactionMeta.txParams
1698
1690
  );
1699
- if (!this.beforePublish(updatedTransactionMeta)) {
1691
+ if (!this.beforePublish(transactionMeta)) {
1700
1692
  _chunkS6VGOPUYjs.projectLogger.call(void 0, "Skipping publishing transaction based on hook");
1701
1693
  this.messagingSystem.publish(
1702
1694
  `${controllerName}:transactionPublishingSkipped`,
1703
- updatedTransactionMeta
1695
+ transactionMeta
1704
1696
  );
1705
1697
  return "skipped-via-before-publish-hook" /* SkippedViaBeforePublishHook */;
1706
1698
  }
@@ -1711,51 +1703,50 @@ var TransactionController = class extends _basecontroller.BaseController {
1711
1703
  networkClientId: transactionMeta.networkClientId,
1712
1704
  chainId: transactionMeta.chainId
1713
1705
  });
1714
- if (transactionMeta.type === "swap" /* swap */) {
1706
+ let preTxBalance;
1707
+ const shouldUpdatePreTxBalance = transactionMeta.type === "swap" /* swap */;
1708
+ if (shouldUpdatePreTxBalance) {
1715
1709
  _chunkS6VGOPUYjs.projectLogger.call(void 0, "Determining pre-transaction balance");
1716
- const preTxBalance = await _controllerutils.query.call(void 0, ethQuery, "getBalance", [from]);
1717
- updatedTransactionMeta.preTxBalance = preTxBalance;
1718
- _chunkS6VGOPUYjs.projectLogger.call(void 0,
1719
- "Updated pre-transaction balance",
1720
- updatedTransactionMeta.preTxBalance
1721
- );
1710
+ preTxBalance = await _controllerutils.query.call(void 0, ethQuery, "getBalance", [
1711
+ transactionMeta.txParams.from
1712
+ ]);
1722
1713
  }
1723
- _chunkS6VGOPUYjs.projectLogger.call(void 0, "Publishing transaction", txParams);
1714
+ _chunkS6VGOPUYjs.projectLogger.call(void 0, "Publishing transaction", transactionMeta.txParams);
1724
1715
  let { transactionHash: hash } = await this.publish(
1725
- updatedTransactionMeta,
1716
+ transactionMeta,
1726
1717
  rawTx
1727
1718
  );
1728
1719
  if (hash === void 0) {
1729
1720
  hash = await this.publishTransaction(ethQuery, rawTx);
1730
1721
  }
1731
1722
  _chunkS6VGOPUYjs.projectLogger.call(void 0, "Publish successful", hash);
1732
- updatedTransactionMeta.hash = hash;
1733
- updatedTransactionMeta.status = "submitted" /* submitted */;
1734
- updatedTransactionMeta.submittedTime = (/* @__PURE__ */ new Date()).getTime();
1735
- this.updateTransaction(
1736
- updatedTransactionMeta,
1737
- "TransactionController#approveTransaction - Transaction submitted"
1738
- );
1723
+ transactionMeta = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateTransactionInternal, updateTransactionInternal_fn).call(this, {
1724
+ transactionId,
1725
+ note: "TransactionController#approveTransaction - Transaction submitted"
1726
+ }, (draftTxMeta) => {
1727
+ draftTxMeta.hash = hash;
1728
+ draftTxMeta.status = "submitted" /* submitted */;
1729
+ draftTxMeta.submittedTime = (/* @__PURE__ */ new Date()).getTime();
1730
+ if (shouldUpdatePreTxBalance) {
1731
+ draftTxMeta.preTxBalance = preTxBalance;
1732
+ _chunkS6VGOPUYjs.projectLogger.call(void 0, "Updated pre-transaction balance", preTxBalance);
1733
+ }
1734
+ });
1739
1735
  this.messagingSystem.publish(`${controllerName}:transactionSubmitted`, {
1740
- transactionMeta: updatedTransactionMeta
1736
+ transactionMeta
1741
1737
  });
1742
1738
  this.messagingSystem.publish(
1743
1739
  `${controllerName}:transactionFinished`,
1744
- updatedTransactionMeta
1745
- );
1746
- _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _internalEvents).emit(
1747
- `${updatedTransactionMeta.id}:finished`,
1748
- updatedTransactionMeta
1740
+ transactionMeta
1749
1741
  );
1750
- this.onTransactionStatusChange(updatedTransactionMeta);
1742
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _internalEvents).emit(`${transactionId}:finished`, transactionMeta);
1743
+ this.onTransactionStatusChange(transactionMeta);
1751
1744
  return "approved" /* Approved */;
1752
1745
  } catch (error) {
1753
1746
  this.failTransaction(transactionMeta, error);
1754
1747
  return "not-approved" /* NotApproved */;
1755
1748
  } finally {
1756
- this.inProcessOfSigning.delete(transactionId);
1757
- releaseNonceLock?.();
1758
- releaseLock();
1749
+ cleanupTasks.forEach((task) => task());
1759
1750
  }
1760
1751
  }
1761
1752
  async publishTransaction(ethQuery, rawTransaction) {
@@ -1877,6 +1868,15 @@ var TransactionController = class extends _basecontroller.BaseController {
1877
1868
  const { transactions } = this.state;
1878
1869
  return transactions.find(({ id }) => id === transactionId);
1879
1870
  }
1871
+ getTransactionOrThrow(transactionId, errorMessagePrefix = "TransactionController") {
1872
+ const txMeta = this.getTransaction(transactionId);
1873
+ if (!txMeta) {
1874
+ throw new Error(
1875
+ `${errorMessagePrefix}: No transaction found with id ${transactionId}`
1876
+ );
1877
+ }
1878
+ return txMeta;
1879
+ }
1880
1880
  getApprovalId(txMeta) {
1881
1881
  return String(txMeta.id);
1882
1882
  }
@@ -2085,8 +2085,9 @@ var TransactionController = class extends _basecontroller.BaseController {
2085
2085
  *
2086
2086
  * @param transactionMeta - The TransactionMeta object to update.
2087
2087
  * @param signedTx - The encompassing type for all transaction types containing r, s, and v values.
2088
+ * @returns The updated TransactionMeta object.
2088
2089
  */
2089
- async updateTransactionMetaRSV(transactionMeta, signedTx) {
2090
+ updateTransactionMetaRSV(transactionMeta, signedTx) {
2090
2091
  const transactionMetaWithRsv = _lodash.cloneDeep.call(void 0, transactionMeta);
2091
2092
  for (const key of ["r", "s", "v"]) {
2092
2093
  const value = signedTx[key];
@@ -2108,7 +2109,7 @@ var TransactionController = class extends _basecontroller.BaseController {
2108
2109
  transactionMeta.chainId,
2109
2110
  txParams
2110
2111
  );
2111
- this.inProcessOfSigning.add(transactionMeta.id);
2112
+ this.approvingTransactionIds.add(transactionMeta.id);
2112
2113
  const signedTx = await new Promise((resolve, reject) => {
2113
2114
  this.sign?.(
2114
2115
  unsignedEthTx,
@@ -2134,7 +2135,7 @@ var TransactionController = class extends _basecontroller.BaseController {
2134
2135
  return void 0;
2135
2136
  }
2136
2137
  const transactionMetaWithRsv = {
2137
- ...await this.updateTransactionMetaRSV(transactionMeta, signedTx),
2138
+ ...this.updateTransactionMetaRSV(transactionMeta, signedTx),
2138
2139
  status: "signed" /* signed */
2139
2140
  };
2140
2141
  this.updateTransaction(
@@ -2413,6 +2414,7 @@ updateTransactionInternal_fn = function({
2413
2414
  if (updatedTransactionParams.length > 0) {
2414
2415
  _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _onTransactionParamsUpdated, onTransactionParamsUpdated_fn).call(this, transactionMeta, updatedTransactionParams);
2415
2416
  }
2417
+ return transactionMeta;
2416
2418
  };
2417
2419
  _checkIfTransactionParamsUpdated = new WeakSet();
2418
2420
  checkIfTransactionParamsUpdated_fn = function(newTransactionMeta) {
@@ -2458,7 +2460,7 @@ updateSimulationData_fn = async function(transactionMeta) {
2458
2460
  _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateTransactionInternal, updateTransactionInternal_fn).call(this, { transactionId, skipHistory: true }, (txMeta) => {
2459
2461
  txMeta.simulationData = void 0;
2460
2462
  });
2461
- simulationData = await _chunkX3KACH5Pjs.getSimulationData.call(void 0, {
2463
+ simulationData = await _chunkIT3SYNZ6js.getSimulationData.call(void 0, {
2462
2464
  chainId,
2463
2465
  from,
2464
2466
  to,
@@ -2547,4 +2549,4 @@ isCustomNetwork_fn = function(networkClientId) {
2547
2549
 
2548
2550
 
2549
2551
  exports.HARDFORK = HARDFORK; exports.CANCEL_RATE = CANCEL_RATE; exports.SPEED_UP_RATE = SPEED_UP_RATE; exports.ApprovalState = ApprovalState; exports.TransactionController = TransactionController;
2550
- //# sourceMappingURL=chunk-N55SEKK7.js.map
2552
+ //# sourceMappingURL=chunk-IXUOEWWL.js.map