@metamask/bridge-status-controller 71.2.0 → 72.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/CHANGELOG.md +41 -1
  2. package/dist/bridge-status-controller-method-action-types.cjs.map +1 -1
  3. package/dist/bridge-status-controller-method-action-types.d.cts +5 -1
  4. package/dist/bridge-status-controller-method-action-types.d.cts.map +1 -1
  5. package/dist/bridge-status-controller-method-action-types.d.mts +5 -1
  6. package/dist/bridge-status-controller-method-action-types.d.mts.map +1 -1
  7. package/dist/bridge-status-controller-method-action-types.mjs.map +1 -1
  8. package/dist/bridge-status-controller.cjs +161 -366
  9. package/dist/bridge-status-controller.cjs.map +1 -1
  10. package/dist/bridge-status-controller.d.cts +27 -7
  11. package/dist/bridge-status-controller.d.cts.map +1 -1
  12. package/dist/bridge-status-controller.d.mts +27 -7
  13. package/dist/bridge-status-controller.d.mts.map +1 -1
  14. package/dist/bridge-status-controller.mjs +162 -370
  15. package/dist/bridge-status-controller.mjs.map +1 -1
  16. package/dist/index.cjs +4 -1
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +1 -0
  19. package/dist/index.d.cts.map +1 -1
  20. package/dist/index.d.mts +1 -0
  21. package/dist/index.d.mts.map +1 -1
  22. package/dist/index.mjs +1 -0
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/strategy/batch-sell-strategy.cjs +101 -0
  25. package/dist/strategy/batch-sell-strategy.cjs.map +1 -0
  26. package/dist/strategy/batch-sell-strategy.d.cts +10 -0
  27. package/dist/strategy/batch-sell-strategy.d.cts.map +1 -0
  28. package/dist/strategy/batch-sell-strategy.d.mts +10 -0
  29. package/dist/strategy/batch-sell-strategy.d.mts.map +1 -0
  30. package/dist/strategy/batch-sell-strategy.mjs +97 -0
  31. package/dist/strategy/batch-sell-strategy.mjs.map +1 -0
  32. package/dist/strategy/batch-strategy.cjs +64 -0
  33. package/dist/strategy/batch-strategy.cjs.map +1 -0
  34. package/dist/strategy/batch-strategy.d.cts +10 -0
  35. package/dist/strategy/batch-strategy.d.cts.map +1 -0
  36. package/dist/strategy/batch-strategy.d.mts +10 -0
  37. package/dist/strategy/batch-strategy.d.mts.map +1 -0
  38. package/dist/strategy/batch-strategy.mjs +60 -0
  39. package/dist/strategy/batch-strategy.mjs.map +1 -0
  40. package/dist/strategy/evm-strategy.cjs +149 -0
  41. package/dist/strategy/evm-strategy.cjs.map +1 -0
  42. package/dist/strategy/evm-strategy.d.cts +38 -0
  43. package/dist/strategy/evm-strategy.d.cts.map +1 -0
  44. package/dist/strategy/evm-strategy.d.mts +38 -0
  45. package/dist/strategy/evm-strategy.d.mts.map +1 -0
  46. package/dist/strategy/evm-strategy.mjs +143 -0
  47. package/dist/strategy/evm-strategy.mjs.map +1 -0
  48. package/dist/strategy/index.cjs +70 -0
  49. package/dist/strategy/index.cjs.map +1 -0
  50. package/dist/strategy/index.d.cts +12 -0
  51. package/dist/strategy/index.d.cts.map +1 -0
  52. package/dist/strategy/index.d.mts +12 -0
  53. package/dist/strategy/index.d.mts.map +1 -0
  54. package/dist/strategy/index.mjs +68 -0
  55. package/dist/strategy/index.mjs.map +1 -0
  56. package/dist/strategy/intent-strategy.cjs +160 -0
  57. package/dist/strategy/intent-strategy.cjs.map +1 -0
  58. package/dist/strategy/intent-strategy.d.cts +17 -0
  59. package/dist/strategy/intent-strategy.d.cts.map +1 -0
  60. package/dist/strategy/intent-strategy.d.mts +17 -0
  61. package/dist/strategy/intent-strategy.d.mts.map +1 -0
  62. package/dist/strategy/intent-strategy.mjs +156 -0
  63. package/dist/strategy/intent-strategy.mjs.map +1 -0
  64. package/dist/strategy/non-evm-strategy.cjs +80 -0
  65. package/dist/strategy/non-evm-strategy.cjs.map +1 -0
  66. package/dist/strategy/non-evm-strategy.d.cts +15 -0
  67. package/dist/strategy/non-evm-strategy.d.cts.map +1 -0
  68. package/dist/strategy/non-evm-strategy.d.mts +15 -0
  69. package/dist/strategy/non-evm-strategy.d.mts.map +1 -0
  70. package/dist/strategy/non-evm-strategy.mjs +76 -0
  71. package/dist/strategy/non-evm-strategy.mjs.map +1 -0
  72. package/dist/strategy/types.cjs +13 -0
  73. package/dist/strategy/types.cjs.map +1 -0
  74. package/dist/strategy/types.d.cts +77 -0
  75. package/dist/strategy/types.d.cts.map +1 -0
  76. package/dist/strategy/types.d.mts +77 -0
  77. package/dist/strategy/types.d.mts.map +1 -0
  78. package/dist/strategy/types.mjs +10 -0
  79. package/dist/strategy/types.mjs.map +1 -0
  80. package/dist/types.cjs.map +1 -1
  81. package/dist/types.d.cts +41 -6
  82. package/dist/types.d.cts.map +1 -1
  83. package/dist/types.d.mts +41 -6
  84. package/dist/types.d.mts.map +1 -1
  85. package/dist/types.mjs.map +1 -1
  86. package/dist/utils/bridge.cjs +5 -1
  87. package/dist/utils/bridge.cjs.map +1 -1
  88. package/dist/utils/bridge.d.cts +2 -2
  89. package/dist/utils/bridge.d.cts.map +1 -1
  90. package/dist/utils/bridge.d.mts +2 -2
  91. package/dist/utils/bridge.d.mts.map +1 -1
  92. package/dist/utils/bridge.mjs +3 -0
  93. package/dist/utils/bridge.mjs.map +1 -1
  94. package/dist/utils/history.cjs +72 -20
  95. package/dist/utils/history.cjs.map +1 -1
  96. package/dist/utils/history.d.cts +19 -5
  97. package/dist/utils/history.d.cts.map +1 -1
  98. package/dist/utils/history.d.mts +19 -5
  99. package/dist/utils/history.d.mts.map +1 -1
  100. package/dist/utils/history.mjs +69 -19
  101. package/dist/utils/history.mjs.map +1 -1
  102. package/dist/utils/metrics.cjs +10 -7
  103. package/dist/utils/metrics.cjs.map +1 -1
  104. package/dist/utils/metrics.d.cts +7 -6
  105. package/dist/utils/metrics.d.cts.map +1 -1
  106. package/dist/utils/metrics.d.mts +7 -6
  107. package/dist/utils/metrics.d.mts.map +1 -1
  108. package/dist/utils/metrics.mjs +10 -7
  109. package/dist/utils/metrics.mjs.map +1 -1
  110. package/dist/utils/trace.cjs +4 -4
  111. package/dist/utils/trace.cjs.map +1 -1
  112. package/dist/utils/trace.d.cts +2 -2
  113. package/dist/utils/trace.d.cts.map +1 -1
  114. package/dist/utils/trace.d.mts +2 -2
  115. package/dist/utils/trace.d.mts.map +1 -1
  116. package/dist/utils/trace.mjs +4 -4
  117. package/dist/utils/trace.mjs.map +1 -1
  118. package/dist/utils/transaction.cjs +223 -252
  119. package/dist/utils/transaction.cjs.map +1 -1
  120. package/dist/utils/transaction.d.cts +68 -147
  121. package/dist/utils/transaction.d.cts.map +1 -1
  122. package/dist/utils/transaction.d.mts +68 -147
  123. package/dist/utils/transaction.d.mts.map +1 -1
  124. package/dist/utils/transaction.mjs +214 -247
  125. package/dist/utils/transaction.mjs.map +1 -1
  126. package/package.json +5 -5
@@ -1,88 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.submitEvmTransaction = exports.addTransactionBatch = exports.findAndUpdateTransactionsInBatch = exports.getAddTransactionBatchParams = exports.toBatchTxParams = exports.waitForTxConfirmation = exports.handleMobileHardwareWalletDelay = exports.handleApprovalDelay = exports.addSyntheticTransaction = exports.generateActionId = exports.addTransaction = exports.checkIsDelegatedAccount = exports.updateTransaction = exports.getTransactionMetaByHash = exports.getTransactionMetaById = exports.getTransactions = exports.calculateGasFees = exports.getTxGasEstimates = exports.getGasFeeEstimates = exports.isCrossChainTx = void 0;
3
+ exports.updateTransactionsInBatch = exports.findAllTransactionsInBatch = exports.getAddTransactionBatchParams = exports.toTransactionParams = exports.toQuoteAndTxMetadataBatch = exports.toQuoteAndTxMetadata = exports.waitForTxConfirmation = exports.handleMobileHardwareWalletDelay = exports.handleApprovalDelay = exports.addSyntheticTransaction = exports.generateActionId = exports.addTransaction = exports.checkIsDelegatedAccount = exports.updateTransaction = exports.getTransactionMetaByHash = exports.getTransactionMetaById = exports.getTransactions = exports.getGasFeeEstimates = exports.hasNestedSwapTransactions = exports.shouldDisable7702 = exports.is7702Tx = exports.isCrossChainTx = exports.isTradeTx = exports.isApprovalTx = void 0;
4
4
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
5
5
  const bridge_controller_1 = require("@metamask/bridge-controller");
6
6
  const controller_utils_1 = require("@metamask/controller-utils");
7
7
  const transaction_controller_1 = require("@metamask/transaction-controller");
8
8
  const utils_1 = require("@metamask/utils");
9
- const bignumber_js_1 = require("bignumber.js");
10
9
  const constants_1 = require("../constants.cjs");
11
10
  const accounts_1 = require("./accounts.cjs");
12
11
  const network_1 = require("./network.cjs");
13
12
  const isApprovalTx = (type) => type === transaction_controller_1.TransactionType.bridgeApproval ||
14
13
  type === transaction_controller_1.TransactionType.swapApproval;
14
+ exports.isApprovalTx = isApprovalTx;
15
15
  const isTradeTx = (type) => type === transaction_controller_1.TransactionType.bridge || type === transaction_controller_1.TransactionType.swap;
16
- const isCrossChainTx = (type) => isTradeTx(type) || isApprovalTx(type);
16
+ exports.isTradeTx = isTradeTx;
17
+ const isCrossChainTx = (type) => (0, exports.isTradeTx)(type) || (0, exports.isApprovalTx)(type);
17
18
  exports.isCrossChainTx = isCrossChainTx;
18
- const getGasFeeEstimates = async (messenger, args) => {
19
- const { estimates } = await messenger.call('TransactionController:estimateGasFee', args);
20
- if (bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE in estimates &&
21
- typeof estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE] === 'object' &&
22
- 'maxFeePerGas' in estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE] &&
23
- 'maxPriorityFeePerGas' in estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE]) {
24
- return estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE];
25
- }
26
- return {};
27
- };
28
- exports.getGasFeeEstimates = getGasFeeEstimates;
29
19
  /**
30
- * Get the gas fee estimates for a transaction
20
+ * For 7702 delegated transactions, check for delegation-specific fields
21
+ * These transactions might have authorizationList or delegationAddress
31
22
  *
32
- * @param messenger - The messenger for the gas fee estimates
33
- * @param estimateGasFeeParams - The parameters for the {@link TransactionController.estimateGasFee} method
34
-
35
- * @returns The gas fee estimates for the transaction
23
+ * @param tx - The transaction meta
24
+ * @returns Whether the transaction is a 7702 transaction
36
25
  */
37
- const getTxGasEstimates = async (messenger, estimateGasFeeParams) => {
38
- const { gasFeeEstimates } = messenger.call('GasFeeController:getState');
39
- const estimatedBaseFee = 'estimatedBaseFee' in gasFeeEstimates
40
- ? gasFeeEstimates.estimatedBaseFee
41
- : '0';
42
- // Get transaction's 1559 gas fee estimates
43
- const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getGasFeeEstimates)(messenger, estimateGasFeeParams);
26
+ const is7702Tx = (tx) => {
27
+ return ((Array.isArray(tx.txParams.authorizationList) &&
28
+ tx.txParams.authorizationList.length > 0) ||
29
+ Boolean(tx.delegationAddress));
30
+ };
31
+ exports.is7702Tx = is7702Tx;
32
+ const shouldDisable7702 = (gasIncluded7702 = false, gasIncluded = false, isDelegatedAccount = false) => {
33
+ // Enable 7702 batching when the quote includes gasless 7702 support
34
+ if (gasIncluded7702) {
35
+ return false;
36
+ }
37
+ // Enable batching when the account is already delegated (to avoid the in-flight transaction limit for delegated accounts)
38
+ if (isDelegatedAccount) {
39
+ return false;
40
+ }
41
+ // For gasless transactions with STX/sendBundle we keep disabling 7702
42
+ if (gasIncluded) {
43
+ return true;
44
+ }
44
45
  /**
45
- * @deprecated this is unused
46
+ * Explicitly return default instead of falsy value (see TransactionBatchRequest.disable7702)
46
47
  */
47
- const baseAndPriorityFeePerGas = maxPriorityFeePerGas
48
- ? new bignumber_js_1.BigNumber(estimatedBaseFee, 10)
49
- .times(10 ** 9)
50
- .plus(maxPriorityFeePerGas, 16)
51
- : undefined;
52
- return {
53
- baseAndPriorityFeePerGas,
54
- maxFeePerGas,
55
- maxPriorityFeePerGas,
56
- };
48
+ return true;
57
49
  };
58
- exports.getTxGasEstimates = getTxGasEstimates;
59
- const calculateGasFees = async (skipGasFields, messenger, { chainId: _, gasLimit, ...trade }, networkClientId, chainId, txFee) => {
60
- if (skipGasFields) {
61
- return {};
62
- }
63
- if (txFee) {
64
- return { ...txFee, gas: gasLimit?.toString() };
50
+ exports.shouldDisable7702 = shouldDisable7702;
51
+ const hasNestedSwapTransactions = (txMeta) => {
52
+ return Boolean(txMeta?.nestedTransactions?.some((tx) => tx.type === transaction_controller_1.TransactionType.swap));
53
+ };
54
+ exports.hasNestedSwapTransactions = hasNestedSwapTransactions;
55
+ const getGasFeeEstimates = async (messenger, args) => {
56
+ const { estimates } = await messenger.call('TransactionController:estimateGasFee', args);
57
+ if (estimates?.type === transaction_controller_1.GasFeeEstimateType.FeeMarket) {
58
+ return estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE];
65
59
  }
66
- const transactionParams = {
67
- ...trade,
68
- gas: gasLimit?.toString(),
69
- data: trade.data,
70
- to: trade.to,
71
- value: trade.value,
72
- };
73
- const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getTxGasEstimates)(messenger, {
74
- transactionParams,
75
- networkClientId,
76
- chainId,
77
- });
78
- const maxGasLimit = (0, controller_utils_1.toHex)(transactionParams.gas ?? 0);
79
- return {
80
- maxFeePerGas,
81
- maxPriorityFeePerGas,
82
- gas: maxGasLimit,
83
- };
60
+ return undefined;
84
61
  };
85
- exports.calculateGasFees = calculateGasFees;
62
+ exports.getGasFeeEstimates = getGasFeeEstimates;
86
63
  const getTransactions = (messenger) => {
87
64
  return messenger.call('TransactionController:getState').transactions ?? [];
88
65
  };
@@ -200,227 +177,221 @@ const waitForTxConfirmation = async (messenger, txId, { timeoutMs = 5 * 60000, p
200
177
  }
201
178
  };
202
179
  exports.waitForTxConfirmation = waitForTxConfirmation;
203
- const toBatchTxParams = (skipGasFields, { chainId, gasLimit, ...trade }, { maxFeePerGas, maxPriorityFeePerGas, gas, }) => {
204
- const params = {
180
+ const toQuoteAndTxMetadata = ({ quoteResponse, isBridgeTx, }) => {
181
+ const tradeData = [];
182
+ const approvalTxType = isBridgeTx
183
+ ? transaction_controller_1.TransactionType.bridgeApproval
184
+ : transaction_controller_1.TransactionType.swapApproval;
185
+ if (quoteResponse.resetApproval) {
186
+ tradeData.push({
187
+ quoteResponse,
188
+ tx: quoteResponse.resetApproval,
189
+ type: approvalTxType,
190
+ txFee: quoteResponse.quote.feeData[bridge_controller_1.FeeType.TX_FEE],
191
+ });
192
+ }
193
+ if (quoteResponse.approval && (0, bridge_controller_1.isEvmTxData)(quoteResponse.approval)) {
194
+ tradeData.push({
195
+ quoteResponse,
196
+ tx: quoteResponse.approval,
197
+ type: approvalTxType,
198
+ txFee: quoteResponse.quote.feeData[bridge_controller_1.FeeType.TX_FEE],
199
+ });
200
+ }
201
+ tradeData.push({
202
+ quoteResponse,
203
+ tx: quoteResponse.trade,
204
+ type: isBridgeTx ? transaction_controller_1.TransactionType.bridge : transaction_controller_1.TransactionType.swap,
205
+ assetsFiatValues: {
206
+ sending: quoteResponse.sentAmount?.valueInCurrency?.toString(),
207
+ receiving: quoteResponse.toTokenAmount?.valueInCurrency?.toString(),
208
+ },
209
+ txFee: quoteResponse.quote.feeData[bridge_controller_1.FeeType.TX_FEE],
210
+ });
211
+ return tradeData;
212
+ };
213
+ exports.toQuoteAndTxMetadata = toQuoteAndTxMetadata;
214
+ /**
215
+ * Build the trade+quote metadata array for the batch sell transaction
216
+ * This ties together the quote, the tx params and the txMeta after submission
217
+ *
218
+ * @param options - The options for the batch sell transaction
219
+ * @param options.quoteResponses - The quote responses for the batch sell transaction
220
+ * @param options.batchSellTrades - The batch sell trades for the batch sell transaction
221
+ * @returns The trade+quote metadata array for the batch sell transaction
222
+ */
223
+ const toQuoteAndTxMetadataBatch = ({ quoteResponses, batchSellTrades, }) => {
224
+ const tradeData = [];
225
+ const { transactions, gasIncluded7702, gasIncluded, gasSponsored = false, } = batchSellTrades;
226
+ for (const transaction of transactions) {
227
+ const { type, maxFeePerGas, maxPriorityFeePerGas, ...tx } = transaction;
228
+ // Match the trade or approval tx data with the quote response
229
+ const matchingQuoteResponse = quoteResponses.find(({ approval, trade }) => trade?.data.toLowerCase() === tx.data.toLowerCase() ||
230
+ approval?.data.toLowerCase() === tx.data.toLowerCase()) ?? quoteResponses[0];
231
+ // Include gasIncluded and gasIncluded7702 from the gasless batch
232
+ const normalizedQuote = {
233
+ ...matchingQuoteResponse,
234
+ quote: {
235
+ ...matchingQuoteResponse.quote,
236
+ gasIncluded,
237
+ gasIncluded7702,
238
+ gasSponsored,
239
+ },
240
+ };
241
+ const commonTradeData = {
242
+ tx,
243
+ quoteResponse: normalizedQuote,
244
+ txFee: { maxFeePerGas, maxPriorityFeePerGas },
245
+ };
246
+ if (type === bridge_controller_1.BatchSellTransactionType.TRADE) {
247
+ tradeData.push({
248
+ ...commonTradeData,
249
+ type: transaction_controller_1.TransactionType.swap,
250
+ assetsFiatValues: {
251
+ sending: matchingQuoteResponse.sentAmount?.valueInCurrency?.toString(),
252
+ receiving: matchingQuoteResponse.toTokenAmount?.valueInCurrency?.toString(),
253
+ },
254
+ });
255
+ }
256
+ else {
257
+ tradeData.push({
258
+ ...commonTradeData,
259
+ type: type === bridge_controller_1.BatchSellTransactionType.APPROVAL
260
+ ? transaction_controller_1.TransactionType.swapApproval
261
+ : transaction_controller_1.TransactionType.tokenMethodTransfer,
262
+ });
263
+ }
264
+ }
265
+ return tradeData;
266
+ };
267
+ exports.toQuoteAndTxMetadataBatch = toQuoteAndTxMetadataBatch;
268
+ /**
269
+ * Appends the gas fee estimates for a transaction and normalizes the trade data
270
+ *
271
+ * @param messenger - The messenger for the gas fee estimates
272
+ * @param trade - the trade data to append gas fees to
273
+ * @param trade.chainId - ignored, use chainId instead
274
+ * @param trade.gasLimit - the gas limit to use for the gas fee estimates
275
+ * @param networkClientId - the network client ID to use for the gas fee estimates
276
+ * @param chainId - the chain ID to use for the gas fee estimates
277
+ * @param simulatedGasFeeLimits - either the txFee from the quote or the simulated gas fee limits for the batch sell
278
+ * @returns The gas fee estimates for the transaction
279
+ */
280
+ const toTransactionParams = async (messenger, { chainId: tradeChainId, gasLimit, ...trade }, networkClientId, chainId, simulatedGasFeeLimits) => {
281
+ const normalizedTrade = {
205
282
  ...trade,
206
283
  data: trade.data,
207
284
  to: trade.to,
285
+ from: trade.from,
208
286
  value: trade.value,
209
287
  };
210
- if (skipGasFields) {
211
- return params;
288
+ const transactionParams = {
289
+ ...normalizedTrade,
290
+ // Only add gasLimit and gas if they're truthy
291
+ gas: gasLimit ? (0, controller_utils_1.toHex)(gasLimit) : undefined,
292
+ };
293
+ // Use bridge-api's provided gas fee estimates
294
+ if (simulatedGasFeeLimits) {
295
+ return {
296
+ ...transactionParams,
297
+ maxFeePerGas: (0, controller_utils_1.toHex)(simulatedGasFeeLimits.maxFeePerGas),
298
+ maxPriorityFeePerGas: (0, controller_utils_1.toHex)(simulatedGasFeeLimits.maxPriorityFeePerGas),
299
+ };
212
300
  }
301
+ // Get transaction's 1559 gas fee estimates
302
+ const gasFeeEstimates = await (0, exports.getGasFeeEstimates)(messenger, {
303
+ transactionParams,
304
+ networkClientId,
305
+ chainId,
306
+ });
213
307
  return {
214
- ...params,
215
- gas: (0, controller_utils_1.toHex)(gas ?? 0),
216
- maxFeePerGas: (0, controller_utils_1.toHex)(maxFeePerGas ?? 0),
217
- maxPriorityFeePerGas: (0, controller_utils_1.toHex)(maxPriorityFeePerGas ?? 0),
308
+ ...transactionParams,
309
+ maxFeePerGas: gasFeeEstimates?.maxFeePerGas,
310
+ maxPriorityFeePerGas: gasFeeEstimates?.maxPriorityFeePerGas,
218
311
  };
219
312
  };
220
- exports.toBatchTxParams = toBatchTxParams;
221
- const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, resetApproval, trade, quoteResponse: { quote: { feeData: { txFee }, gasIncluded, gasIncluded7702, gasSponsored, }, sentAmount, toTokenAmount, }, requireApproval = false, isDelegatedAccount = false, }) => {
222
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
223
- const isGasless = gasIncluded || gasIncluded7702;
313
+ exports.toTransactionParams = toTransactionParams;
314
+ const getAddTransactionBatchParams = async ({ messenger, tradeData, requireApproval = false, isDelegatedAccount, ...addTransactionBatchParams }) => {
315
+ const trade = tradeData[0].tx;
224
316
  const selectedAccount = (0, accounts_1.getAccountByAddress)(messenger, trade.from);
225
317
  if (!selectedAccount) {
226
318
  throw new Error('Failed to submit cross-chain swap batch transaction: unknown account in trade data');
227
319
  }
228
320
  const hexChainId = (0, bridge_controller_1.formatChainIdToHex)(trade.chainId);
229
321
  const networkClientId = (0, network_1.getNetworkClientIdByChainId)(messenger, hexChainId);
230
- // Gas fields should be omitted only when gas is sponsored via 7702
231
- const skipGasFields = gasIncluded7702 === true;
232
- // Enable 7702 batching when the quote includes gasless 7702 support,
233
- // or when the account is already delegated (to avoid the in-flight
234
- // transaction limit for delegated accounts)
235
- let disable7702 = !skipGasFields && !isDelegatedAccount;
236
- // For gasless transactions with STX/sendBundle we keep disabling 7702.
237
- if (gasIncluded && !gasIncluded7702) {
238
- disable7702 = true;
239
- }
240
- const transactions = [];
241
- if (resetApproval) {
242
- const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, resetApproval, networkClientId, hexChainId, isGasless ? txFee : undefined);
243
- transactions.push({
244
- type: isBridgeTx
245
- ? transaction_controller_1.TransactionType.bridgeApproval
246
- : transaction_controller_1.TransactionType.swapApproval,
247
- params: (0, exports.toBatchTxParams)(skipGasFields, resetApproval, gasFees),
248
- });
249
- }
250
- if (approval) {
251
- const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, approval, networkClientId, hexChainId, isGasless ? txFee : undefined);
252
- transactions.push({
253
- type: isBridgeTx
254
- ? transaction_controller_1.TransactionType.bridgeApproval
255
- : transaction_controller_1.TransactionType.swapApproval,
256
- params: (0, exports.toBatchTxParams)(skipGasFields, approval, gasFees),
257
- });
258
- }
259
- const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, trade, networkClientId, hexChainId, isGasless ? txFee : undefined);
260
- transactions.push({
261
- type: isBridgeTx ? transaction_controller_1.TransactionType.bridge : transaction_controller_1.TransactionType.swap,
262
- params: (0, exports.toBatchTxParams)(skipGasFields, trade, gasFees),
263
- assetsFiatValues: {
264
- sending: sentAmount?.valueInCurrency?.toString(),
265
- receiving: toTokenAmount?.valueInCurrency?.toString(),
266
- },
267
- });
268
- const transactionParams = {
269
- disable7702,
270
- isGasFeeIncluded: Boolean(gasIncluded7702),
271
- isGasFeeSponsored: Boolean(gasSponsored),
322
+ const transactions = await Promise.all(tradeData.map(async ({ tx, txFee, assetsFiatValues, type }) => ({
323
+ params: await (0, exports.toTransactionParams)(messenger, tx, networkClientId, hexChainId, txFee),
324
+ assetsFiatValues,
325
+ type,
326
+ })));
327
+ return {
272
328
  networkClientId,
273
329
  requireApproval,
274
330
  origin: 'metamask',
331
+ from: selectedAccount.address,
275
332
  isInternal: true,
276
- from: trade.from,
277
333
  transactions,
334
+ ...addTransactionBatchParams,
278
335
  };
279
- return transactionParams;
280
336
  };
281
337
  exports.getAddTransactionBatchParams = getAddTransactionBatchParams;
282
- const findAndUpdateTransactionsInBatch = ({ messenger, batchId, txDataByType, }) => {
283
- const txs = (0, exports.getTransactions)(messenger);
284
- const txBatch = {
285
- approvalMeta: undefined,
286
- tradeMeta: undefined,
287
- };
288
- // This is a workaround to update the tx type after the tx is signed
289
- // TODO: remove this once the tx type for batch txs is preserved in the tx controller
290
- const txEntries = Object.entries(txDataByType);
291
- txEntries.forEach(([txType, txData]) => {
292
- // Skip types not present in the batch (e.g. swap entry is undefined for bridge txs)
293
- if (txData === undefined) {
294
- return;
295
- }
296
- // Find transaction by batchId and either matching data or delegation characteristics
297
- const txMeta = txs.find((tx) => {
298
- if (tx.batchId !== batchId) {
299
- return false;
300
- }
301
- // For 7702 delegated transactions, check for delegation-specific fields
302
- // These transactions might have authorizationList or delegationAddress
303
- const is7702Transaction = (Array.isArray(tx.txParams.authorizationList) &&
304
- tx.txParams.authorizationList.length > 0) ||
305
- Boolean(tx.delegationAddress);
306
- if (is7702Transaction) {
307
- // For 7702 transactions, we need to match based on transaction type
308
- // since the data field might be different (batch execute call)
309
- if (isTradeTx(txType) && tx.type === transaction_controller_1.TransactionType.batch) {
310
- return true;
338
+ const findAllTransactionsInBatch = ({ messenger, batchId, tradeData, }) => {
339
+ // Filter for transactions with batchId
340
+ const txs = (0, exports.getTransactions)(messenger).filter((tx) => tx.batchId === batchId);
341
+ return tradeData.map((tradeWithMetadata) => {
342
+ const { tx, type } = tradeWithMetadata;
343
+ return {
344
+ ...tradeWithMetadata,
345
+ txMeta: txs.find((txMeta) => {
346
+ if ((0, exports.is7702Tx)(txMeta)) {
347
+ // For 7702 transactions, we need to match based on transaction type
348
+ // since the data field might be different (batch execute call)
349
+ if ((0, exports.isTradeTx)(type) && txMeta.type === transaction_controller_1.TransactionType.batch) {
350
+ return true;
351
+ }
352
+ // Also check if it's an approval transaction for 7702
353
+ if ((0, exports.isApprovalTx)(type) && txMeta.txParams.data === tx.data) {
354
+ return true;
355
+ }
311
356
  }
312
- // Also check if it's an approval transaction for 7702
313
- if (isApprovalTx(txType) && tx.txParams.data === txData) {
357
+ // Default matching logic for non-7702 transactions
358
+ if (txMeta.txParams.data === tx.data) {
314
359
  return true;
315
360
  }
316
- }
317
- // Default matching logic for non-7702 transactions
318
- return tx.txParams.data === txData;
319
- });
320
- if (txMeta) {
321
- const updatedTx = { ...txMeta, type: txType };
322
- (0, exports.updateTransaction)(messenger, txMeta, { type: txType }, `Update tx type to ${txType}`);
323
- const txTypes = [
324
- transaction_controller_1.TransactionType.bridgeApproval,
325
- transaction_controller_1.TransactionType.swapApproval,
326
- ];
327
- txBatch[txTypes.includes(txType) ? 'approvalMeta' : 'tradeMeta'] =
328
- updatedTx;
329
- }
330
- });
331
- return txBatch;
332
- };
333
- exports.findAndUpdateTransactionsInBatch = findAndUpdateTransactionsInBatch;
334
- const addTransactionBatch = async (messenger, addTransactionBatchFn, ...args) => {
335
- const txDataByType = {
336
- [transaction_controller_1.TransactionType.bridgeApproval]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.bridgeApproval)?.params.data,
337
- [transaction_controller_1.TransactionType.swapApproval]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.swapApproval)?.params.data,
338
- [transaction_controller_1.TransactionType.bridge]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.bridge)?.params.data,
339
- [transaction_controller_1.TransactionType.swap]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.swap)?.params.data,
340
- };
341
- const { batchId } = await addTransactionBatchFn(...args);
342
- const { approvalMeta, tradeMeta } = (0, exports.findAndUpdateTransactionsInBatch)({
343
- messenger,
344
- batchId,
345
- txDataByType,
346
- });
347
- if (!tradeMeta) {
348
- throw new Error('Failed to update cross-chain swap transaction batch: tradeMeta not found');
349
- }
350
- return { approvalMeta, tradeMeta };
351
- };
352
- exports.addTransactionBatch = addTransactionBatch;
353
- // TODO rename
354
- const getGasFeesForSubmission = async (messenger, transactionParams, networkClientId, chainId, txFee) => {
355
- const { gas } = transactionParams;
356
- // If txFee is provided (gasIncluded case), use the quote's gas fees
357
- // Convert to hex since txFee values from the quote are decimal strings
358
- if (txFee) {
359
- return {
360
- maxFeePerGas: (0, controller_utils_1.toHex)(txFee.maxFeePerGas),
361
- maxPriorityFeePerGas: (0, controller_utils_1.toHex)(txFee.maxPriorityFeePerGas),
362
- gas: gas ? (0, controller_utils_1.toHex)(gas) : undefined,
361
+ return false;
362
+ }),
363
363
  };
364
- }
365
- const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getTxGasEstimates)(messenger, {
366
- transactionParams,
367
- chainId,
368
- networkClientId,
369
364
  });
370
- return {
371
- maxFeePerGas,
372
- maxPriorityFeePerGas,
373
- gas: gas ? (0, controller_utils_1.toHex)(gas) : undefined,
374
- };
375
365
  };
366
+ exports.findAllTransactionsInBatch = findAllTransactionsInBatch;
376
367
  /**
377
- * Submits an EVM transaction to the TransactionController
368
+ * This is a workaround to update the tx type after submission. Batch txs are submitted with
369
+ * the "batch" type, but we need to update to swap/bridge for display purposes.
378
370
  *
379
- * @param params - The parameters for the transaction
380
- * @param params.transactionType - The type of transaction to submit
381
- * @param params.trade - The trade data to confirm
382
- * @param params.requireApproval - Whether to require approval for the transaction
383
- * @param params.txFee - Optional gas fee parameters from the quote (used when gasIncluded is true)
384
- * @param params.txFee.maxFeePerGas - The maximum fee per gas from the quote
385
- * @param params.txFee.maxPriorityFeePerGas - The maximum priority fee per gas from the quote
386
- * @param params.actionId - Optional actionId for pre-submission history (if not provided, one is generated)
371
+ * @param params - The parameters for the transaction search
387
372
  * @param params.messenger - The messenger to use for the transaction
388
- * @returns The transaction meta
373
+ * @param params.allTradesWithMetadata - The quote, tx data and type for each transaction in the batch
374
+ * @returns A list of transaction metas for each trade in the batch]
375
+ *
376
+ * @example
377
+ * [
378
+ * {...tradeData[0], tradeMeta: TransactionMeta}
379
+ * {...tradeData[1], tradeMeta: TransactionMeta}
380
+ * {...tradeData[2], tradeMeta: TransactionMeta}
381
+ * {...tradeData[3], tradeMeta: TransactionMeta}
382
+ * ]
389
383
  */
390
- const submitEvmTransaction = async ({ messenger, trade, transactionType, requireApproval = false, txFee,
391
- // Use provided actionId (for pre-submission history) or generate one
392
- actionId = (0, exports.generateActionId)(), }) => {
393
- const selectedAccount = (0, accounts_1.getAccountByAddress)(messenger, trade.from);
394
- if (!selectedAccount) {
395
- throw new Error('Failed to submit cross-chain swap transaction: unknown account in trade data');
396
- }
397
- const hexChainId = (0, bridge_controller_1.formatChainIdToHex)(trade.chainId);
398
- const networkClientId = (0, network_1.getNetworkClientIdByChainId)(messenger, hexChainId);
399
- const requestOptions = {
400
- actionId,
401
- networkClientId,
402
- requireApproval,
403
- type: transactionType,
404
- origin: 'metamask',
405
- isInternal: true,
406
- };
407
- // Exclude gasLimit from trade to avoid type issues (it can be null)
408
- const { gasLimit: tradeGasLimit, ...tradeWithoutGasLimit } = trade;
409
- const transactionParams = {
410
- ...tradeWithoutGasLimit,
411
- chainId: hexChainId,
412
- // Only add gasLimit and gas if they're valid (not undefined/null/zero)
413
- ...(tradeGasLimit &&
414
- tradeGasLimit !== 0 && {
415
- gasLimit: tradeGasLimit.toString(),
416
- gas: tradeGasLimit.toString(),
417
- }),
418
- };
419
- const transactionParamsWithMaxGas = {
420
- ...transactionParams,
421
- ...(await getGasFeesForSubmission(messenger, transactionParams, networkClientId, hexChainId, txFee)),
422
- };
423
- return await (0, exports.addTransaction)(messenger, transactionParamsWithMaxGas, requestOptions);
384
+ const updateTransactionsInBatch = ({ messenger, allTradesWithMetadata, }) => {
385
+ return allTradesWithMetadata.map((tradeWithMetadata) => {
386
+ const { txMeta, type } = tradeWithMetadata;
387
+ if (txMeta) {
388
+ // Update the tx type from batch to swap/bridge
389
+ (0, exports.updateTransaction)(messenger, txMeta, { type }, `Update tx type to ${type}`);
390
+ const updatedTx = { ...txMeta, type };
391
+ return { ...tradeWithMetadata, txMeta: updatedTx, type };
392
+ }
393
+ return tradeWithMetadata;
394
+ });
424
395
  };
425
- exports.submitEvmTransaction = submitEvmTransaction;
396
+ exports.updateTransactionsInBatch = updateTransactionsInBatch;
426
397
  //# sourceMappingURL=transaction.cjs.map