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