@metamask/bridge-status-controller 71.2.1 → 72.0.1
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 +40 -1
- package/dist/bridge-status-controller-method-action-types.cjs.map +1 -1
- package/dist/bridge-status-controller-method-action-types.d.cts +5 -1
- package/dist/bridge-status-controller-method-action-types.d.cts.map +1 -1
- package/dist/bridge-status-controller-method-action-types.d.mts +5 -1
- package/dist/bridge-status-controller-method-action-types.d.mts.map +1 -1
- package/dist/bridge-status-controller-method-action-types.mjs.map +1 -1
- package/dist/bridge-status-controller.cjs +161 -366
- package/dist/bridge-status-controller.cjs.map +1 -1
- package/dist/bridge-status-controller.d.cts +27 -7
- package/dist/bridge-status-controller.d.cts.map +1 -1
- package/dist/bridge-status-controller.d.mts +27 -7
- package/dist/bridge-status-controller.d.mts.map +1 -1
- package/dist/bridge-status-controller.mjs +162 -370
- package/dist/bridge-status-controller.mjs.map +1 -1
- package/dist/index.cjs +4 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/strategy/batch-sell-strategy.cjs +103 -0
- package/dist/strategy/batch-sell-strategy.cjs.map +1 -0
- package/dist/strategy/batch-sell-strategy.d.cts +10 -0
- package/dist/strategy/batch-sell-strategy.d.cts.map +1 -0
- package/dist/strategy/batch-sell-strategy.d.mts +10 -0
- package/dist/strategy/batch-sell-strategy.d.mts.map +1 -0
- package/dist/strategy/batch-sell-strategy.mjs +99 -0
- package/dist/strategy/batch-sell-strategy.mjs.map +1 -0
- package/dist/strategy/batch-strategy.cjs +74 -0
- package/dist/strategy/batch-strategy.cjs.map +1 -0
- package/dist/strategy/batch-strategy.d.cts +10 -0
- package/dist/strategy/batch-strategy.d.cts.map +1 -0
- package/dist/strategy/batch-strategy.d.mts +10 -0
- package/dist/strategy/batch-strategy.d.mts.map +1 -0
- package/dist/strategy/batch-strategy.mjs +70 -0
- package/dist/strategy/batch-strategy.mjs.map +1 -0
- package/dist/strategy/evm-strategy.cjs +149 -0
- package/dist/strategy/evm-strategy.cjs.map +1 -0
- package/dist/strategy/evm-strategy.d.cts +38 -0
- package/dist/strategy/evm-strategy.d.cts.map +1 -0
- package/dist/strategy/evm-strategy.d.mts +38 -0
- package/dist/strategy/evm-strategy.d.mts.map +1 -0
- package/dist/strategy/evm-strategy.mjs +143 -0
- package/dist/strategy/evm-strategy.mjs.map +1 -0
- package/dist/strategy/index.cjs +70 -0
- package/dist/strategy/index.cjs.map +1 -0
- package/dist/strategy/index.d.cts +12 -0
- package/dist/strategy/index.d.cts.map +1 -0
- package/dist/strategy/index.d.mts +12 -0
- package/dist/strategy/index.d.mts.map +1 -0
- package/dist/strategy/index.mjs +68 -0
- package/dist/strategy/index.mjs.map +1 -0
- package/dist/strategy/intent-strategy.cjs +160 -0
- package/dist/strategy/intent-strategy.cjs.map +1 -0
- package/dist/strategy/intent-strategy.d.cts +17 -0
- package/dist/strategy/intent-strategy.d.cts.map +1 -0
- package/dist/strategy/intent-strategy.d.mts +17 -0
- package/dist/strategy/intent-strategy.d.mts.map +1 -0
- package/dist/strategy/intent-strategy.mjs +156 -0
- package/dist/strategy/intent-strategy.mjs.map +1 -0
- package/dist/strategy/non-evm-strategy.cjs +80 -0
- package/dist/strategy/non-evm-strategy.cjs.map +1 -0
- package/dist/strategy/non-evm-strategy.d.cts +15 -0
- package/dist/strategy/non-evm-strategy.d.cts.map +1 -0
- package/dist/strategy/non-evm-strategy.d.mts +15 -0
- package/dist/strategy/non-evm-strategy.d.mts.map +1 -0
- package/dist/strategy/non-evm-strategy.mjs +76 -0
- package/dist/strategy/non-evm-strategy.mjs.map +1 -0
- package/dist/strategy/types.cjs +13 -0
- package/dist/strategy/types.cjs.map +1 -0
- package/dist/strategy/types.d.cts +77 -0
- package/dist/strategy/types.d.cts.map +1 -0
- package/dist/strategy/types.d.mts +77 -0
- package/dist/strategy/types.d.mts.map +1 -0
- package/dist/strategy/types.mjs +10 -0
- package/dist/strategy/types.mjs.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +41 -6
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +41 -6
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/bridge.cjs +5 -1
- package/dist/utils/bridge.cjs.map +1 -1
- package/dist/utils/bridge.d.cts +2 -2
- package/dist/utils/bridge.d.cts.map +1 -1
- package/dist/utils/bridge.d.mts +2 -2
- package/dist/utils/bridge.d.mts.map +1 -1
- package/dist/utils/bridge.mjs +3 -0
- package/dist/utils/bridge.mjs.map +1 -1
- package/dist/utils/history.cjs +72 -20
- package/dist/utils/history.cjs.map +1 -1
- package/dist/utils/history.d.cts +19 -5
- package/dist/utils/history.d.cts.map +1 -1
- package/dist/utils/history.d.mts +19 -5
- package/dist/utils/history.d.mts.map +1 -1
- package/dist/utils/history.mjs +69 -19
- package/dist/utils/history.mjs.map +1 -1
- package/dist/utils/metrics.cjs +10 -7
- package/dist/utils/metrics.cjs.map +1 -1
- package/dist/utils/metrics.d.cts +7 -6
- package/dist/utils/metrics.d.cts.map +1 -1
- package/dist/utils/metrics.d.mts +7 -6
- package/dist/utils/metrics.d.mts.map +1 -1
- package/dist/utils/metrics.mjs +10 -7
- package/dist/utils/metrics.mjs.map +1 -1
- package/dist/utils/trace.cjs +4 -4
- package/dist/utils/trace.cjs.map +1 -1
- package/dist/utils/trace.d.cts +2 -2
- package/dist/utils/trace.d.cts.map +1 -1
- package/dist/utils/trace.d.mts +2 -2
- package/dist/utils/trace.d.mts.map +1 -1
- package/dist/utils/trace.mjs +4 -4
- package/dist/utils/trace.mjs.map +1 -1
- package/dist/utils/transaction.cjs +222 -253
- package/dist/utils/transaction.cjs.map +1 -1
- package/dist/utils/transaction.d.cts +68 -147
- package/dist/utils/transaction.d.cts.map +1 -1
- package/dist/utils/transaction.d.mts +68 -147
- package/dist/utils/transaction.d.mts.map +1 -1
- package/dist/utils/transaction.mjs +213 -248
- package/dist/utils/transaction.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1,80 +1,51 @@
|
|
|
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";
|
|
5
|
-
import { createProjectLogger } from "@metamask/utils";
|
|
6
|
-
import { BigNumber } from "bignumber.js";
|
|
4
|
+
import { GasFeeEstimateType, TransactionStatus, TransactionType } from "@metamask/transaction-controller";
|
|
5
|
+
import { createProjectLogger, isStrictHexString } from "@metamask/utils";
|
|
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
|
-
*
|
|
14
|
+
* For 7702 delegated transactions, check for delegation-specific fields
|
|
15
|
+
* These transactions might have authorizationList or delegationAddress
|
|
26
16
|
*
|
|
27
|
-
* @param
|
|
28
|
-
* @
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
+
// For gasless transactions with STX/sendBundle we keep disabling 7702
|
|
32
|
+
if (isDelegatedAccount && !gasIncluded) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
39
35
|
/**
|
|
40
|
-
*
|
|
36
|
+
* Explicitly return default instead of falsy value (see TransactionBatchRequest.disable7702)
|
|
41
37
|
*/
|
|
42
|
-
|
|
43
|
-
? new BigNumber(estimatedBaseFee, 10)
|
|
44
|
-
.times(10 ** 9)
|
|
45
|
-
.plus(maxPriorityFeePerGas, 16)
|
|
46
|
-
: undefined;
|
|
47
|
-
return {
|
|
48
|
-
baseAndPriorityFeePerGas,
|
|
49
|
-
maxFeePerGas,
|
|
50
|
-
maxPriorityFeePerGas,
|
|
51
|
-
};
|
|
38
|
+
return true;
|
|
52
39
|
};
|
|
53
|
-
export const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
export const hasNestedSwapTransactions = (txMeta) => {
|
|
41
|
+
return Boolean(txMeta?.nestedTransactions?.some((tx) => tx.type === TransactionType.swap));
|
|
42
|
+
};
|
|
43
|
+
export const getGasFeeEstimates = async (messenger, args) => {
|
|
44
|
+
const { estimates } = await messenger.call('TransactionController:estimateGasFee', args);
|
|
45
|
+
if (estimates?.type === GasFeeEstimateType.FeeMarket) {
|
|
46
|
+
return estimates[BRIDGE_PREFERRED_GAS_ESTIMATE];
|
|
59
47
|
}
|
|
60
|
-
|
|
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
|
-
};
|
|
48
|
+
return undefined;
|
|
78
49
|
};
|
|
79
50
|
export const getTransactions = (messenger) => {
|
|
80
51
|
return messenger.call('TransactionController:getState').transactions ?? [];
|
|
@@ -182,222 +153,216 @@ export const waitForTxConfirmation = async (messenger, txId, { timeoutMs = 5 * 6
|
|
|
182
153
|
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
183
154
|
}
|
|
184
155
|
};
|
|
185
|
-
export const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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),
|
|
156
|
+
export const toQuoteAndTxMetadata = ({ quoteResponse, isBridgeTx, }) => {
|
|
157
|
+
const tradeData = [];
|
|
158
|
+
const approvalTxType = isBridgeTx
|
|
159
|
+
? TransactionType.bridgeApproval
|
|
160
|
+
: TransactionType.swapApproval;
|
|
161
|
+
if (quoteResponse.resetApproval) {
|
|
162
|
+
tradeData.push({
|
|
163
|
+
quoteResponse,
|
|
164
|
+
tx: quoteResponse.resetApproval,
|
|
165
|
+
type: approvalTxType,
|
|
166
|
+
txFee: quoteResponse.quote.feeData[FeeType.TX_FEE],
|
|
229
167
|
});
|
|
230
168
|
}
|
|
231
|
-
if (approval) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
params: toBatchTxParams(skipGasFields, approval, gasFees),
|
|
169
|
+
if (quoteResponse.approval && isEvmTxData(quoteResponse.approval)) {
|
|
170
|
+
tradeData.push({
|
|
171
|
+
quoteResponse,
|
|
172
|
+
tx: quoteResponse.approval,
|
|
173
|
+
type: approvalTxType,
|
|
174
|
+
txFee: quoteResponse.quote.feeData[FeeType.TX_FEE],
|
|
238
175
|
});
|
|
239
176
|
}
|
|
240
|
-
|
|
241
|
-
|
|
177
|
+
tradeData.push({
|
|
178
|
+
quoteResponse,
|
|
179
|
+
tx: quoteResponse.trade,
|
|
242
180
|
type: isBridgeTx ? TransactionType.bridge : TransactionType.swap,
|
|
243
|
-
params: toBatchTxParams(skipGasFields, trade, gasFees),
|
|
244
181
|
assetsFiatValues: {
|
|
245
|
-
sending: sentAmount?.valueInCurrency?.toString(),
|
|
246
|
-
receiving: toTokenAmount?.valueInCurrency?.toString(),
|
|
182
|
+
sending: quoteResponse.sentAmount?.valueInCurrency?.toString(),
|
|
183
|
+
receiving: quoteResponse.toTokenAmount?.valueInCurrency?.toString(),
|
|
247
184
|
},
|
|
185
|
+
txFee: quoteResponse.quote.feeData[FeeType.TX_FEE],
|
|
248
186
|
});
|
|
249
|
-
|
|
250
|
-
disable7702,
|
|
251
|
-
isGasFeeIncluded: Boolean(gasIncluded7702),
|
|
252
|
-
isGasFeeSponsored: Boolean(gasSponsored),
|
|
253
|
-
networkClientId,
|
|
254
|
-
requireApproval,
|
|
255
|
-
origin: 'metamask',
|
|
256
|
-
from: trade.from,
|
|
257
|
-
isInternal: true,
|
|
258
|
-
transactions,
|
|
259
|
-
};
|
|
260
|
-
return transactionParams;
|
|
187
|
+
return tradeData;
|
|
261
188
|
};
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Build the trade+quote metadata array for the batch sell transaction
|
|
191
|
+
* This ties together the quote, the tx params and the txMeta after submission
|
|
192
|
+
*
|
|
193
|
+
* @param options - The options for the batch sell transaction
|
|
194
|
+
* @param options.quoteResponses - The quote responses for the batch sell transaction
|
|
195
|
+
* @param options.batchSellTrades - The batch sell trades for the batch sell transaction
|
|
196
|
+
* @returns The trade+quote metadata array for the batch sell transaction
|
|
197
|
+
*/
|
|
198
|
+
export const toQuoteAndTxMetadataBatch = ({ quoteResponses, batchSellTrades, }) => {
|
|
199
|
+
const tradeData = [];
|
|
200
|
+
const { transactions, gasIncluded7702, gasIncluded, gasSponsored = false, } = batchSellTrades;
|
|
201
|
+
for (const transaction of transactions) {
|
|
202
|
+
const { type, maxFeePerGas, maxPriorityFeePerGas, ...tx } = transaction;
|
|
203
|
+
// Match the trade or approval tx data with the quote response
|
|
204
|
+
const matchingQuoteResponse = quoteResponses.find(({ approval, trade }) => trade?.data.toLowerCase() === tx.data.toLowerCase() ||
|
|
205
|
+
approval?.data.toLowerCase() === tx.data.toLowerCase()) ?? quoteResponses[0];
|
|
206
|
+
// Include gasIncluded and gasIncluded7702 from the gasless batch
|
|
207
|
+
const normalizedQuote = {
|
|
208
|
+
...matchingQuoteResponse,
|
|
209
|
+
quote: {
|
|
210
|
+
...matchingQuoteResponse.quote,
|
|
211
|
+
gasIncluded,
|
|
212
|
+
gasIncluded7702,
|
|
213
|
+
gasSponsored,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
const commonTradeData = {
|
|
217
|
+
tx,
|
|
218
|
+
quoteResponse: normalizedQuote,
|
|
219
|
+
txFee: { maxFeePerGas, maxPriorityFeePerGas },
|
|
220
|
+
};
|
|
221
|
+
if (type === BatchSellTransactionType.TRADE) {
|
|
222
|
+
tradeData.push({
|
|
223
|
+
...commonTradeData,
|
|
224
|
+
type: TransactionType.swap,
|
|
225
|
+
assetsFiatValues: {
|
|
226
|
+
sending: matchingQuoteResponse.sentAmount?.valueInCurrency?.toString(),
|
|
227
|
+
receiving: matchingQuoteResponse.toTokenAmount?.valueInCurrency?.toString(),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
275
230
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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;
|
|
231
|
+
else {
|
|
232
|
+
tradeData.push({
|
|
233
|
+
...commonTradeData,
|
|
234
|
+
type: type === BatchSellTransactionType.APPROVAL
|
|
235
|
+
? TransactionType.swapApproval
|
|
236
|
+
: TransactionType.tokenMethodTransfer,
|
|
237
|
+
});
|
|
309
238
|
}
|
|
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
239
|
}
|
|
329
|
-
return
|
|
240
|
+
return tradeData;
|
|
330
241
|
};
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Appends the gas fee estimates for a transaction and normalizes the trade data
|
|
244
|
+
*
|
|
245
|
+
* @param messenger - The messenger for the gas fee estimates
|
|
246
|
+
* @param trade - the trade data to append gas fees to
|
|
247
|
+
* @param trade.chainId - ignored, use chainId instead
|
|
248
|
+
* @param trade.gasLimit - the gas limit to use for the gas fee estimates
|
|
249
|
+
* @param networkClientId - the network client ID to use for the gas fee estimates
|
|
250
|
+
* @param chainId - the chain ID to use for the gas fee estimates
|
|
251
|
+
* @param simulatedGasFeeLimits - either the txFee from the quote or the simulated gas fee limits for the batch sell
|
|
252
|
+
* @returns The gas fee estimates for the transaction
|
|
253
|
+
*/
|
|
254
|
+
export const toTransactionParams = async (messenger, { chainId: tradeChainId, gasLimit, ...trade }, networkClientId, chainId, simulatedGasFeeLimits) => {
|
|
255
|
+
const transactionParams = {
|
|
256
|
+
data: trade.data,
|
|
257
|
+
to: trade.to,
|
|
258
|
+
from: trade.from,
|
|
259
|
+
value: trade.value,
|
|
260
|
+
// Only add gas if it's truthy
|
|
261
|
+
gas: gasLimit ? toHex(gasLimit) : undefined,
|
|
262
|
+
};
|
|
263
|
+
// Use bridge-api's provided gas fee estimates
|
|
264
|
+
if (simulatedGasFeeLimits) {
|
|
337
265
|
return {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
266
|
+
...transactionParams,
|
|
267
|
+
// Sometimes estimates are hex, somethings numeric strings
|
|
268
|
+
maxFeePerGas: isStrictHexString(simulatedGasFeeLimits.maxFeePerGas)
|
|
269
|
+
? simulatedGasFeeLimits.maxFeePerGas
|
|
270
|
+
: toHex(simulatedGasFeeLimits.maxFeePerGas),
|
|
271
|
+
maxPriorityFeePerGas: isStrictHexString(simulatedGasFeeLimits.maxPriorityFeePerGas)
|
|
272
|
+
? simulatedGasFeeLimits.maxPriorityFeePerGas
|
|
273
|
+
: toHex(simulatedGasFeeLimits.maxPriorityFeePerGas),
|
|
341
274
|
};
|
|
342
275
|
}
|
|
343
|
-
|
|
276
|
+
// Get transaction's 1559 gas fee estimates
|
|
277
|
+
const gasFeeEstimates = await getGasFeeEstimates(messenger, {
|
|
344
278
|
transactionParams,
|
|
345
|
-
chainId,
|
|
346
279
|
networkClientId,
|
|
280
|
+
chainId,
|
|
347
281
|
});
|
|
348
282
|
return {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
283
|
+
...transactionParams,
|
|
284
|
+
maxFeePerGas: gasFeeEstimates?.maxFeePerGas,
|
|
285
|
+
maxPriorityFeePerGas: gasFeeEstimates?.maxPriorityFeePerGas,
|
|
352
286
|
};
|
|
353
287
|
};
|
|
354
|
-
|
|
355
|
-
|
|
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(), }) => {
|
|
288
|
+
export const getAddTransactionBatchParams = async ({ messenger, tradeData, requireApproval = false, isDelegatedAccount, ...addTransactionBatchParams }) => {
|
|
289
|
+
const trade = tradeData[0].tx;
|
|
371
290
|
const selectedAccount = getAccountByAddress(messenger, trade.from);
|
|
372
291
|
if (!selectedAccount) {
|
|
373
|
-
throw new Error('Failed to submit cross-chain swap transaction: unknown account in trade data');
|
|
292
|
+
throw new Error('Failed to submit cross-chain swap batch transaction: unknown account in trade data');
|
|
374
293
|
}
|
|
375
294
|
const hexChainId = formatChainIdToHex(trade.chainId);
|
|
376
295
|
const networkClientId = getNetworkClientIdByChainId(messenger, hexChainId);
|
|
377
|
-
const
|
|
378
|
-
|
|
296
|
+
const transactions = await Promise.all(tradeData.map(async ({ tx, txFee, assetsFiatValues, type }) => ({
|
|
297
|
+
params: await toTransactionParams(messenger, tx, networkClientId, hexChainId, txFee),
|
|
298
|
+
assetsFiatValues,
|
|
299
|
+
type,
|
|
300
|
+
})));
|
|
301
|
+
return {
|
|
379
302
|
networkClientId,
|
|
380
303
|
requireApproval,
|
|
381
|
-
type: transactionType,
|
|
382
304
|
origin: 'metamask',
|
|
305
|
+
from: selectedAccount.address,
|
|
383
306
|
isInternal: true,
|
|
307
|
+
transactions,
|
|
308
|
+
...addTransactionBatchParams,
|
|
384
309
|
};
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
310
|
+
};
|
|
311
|
+
export const findAllTransactionsInBatch = ({ messenger, batchId, tradeData, }) => {
|
|
312
|
+
// Filter for transactions with batchId
|
|
313
|
+
const txs = getTransactions(messenger).filter((tx) => tx.batchId === batchId);
|
|
314
|
+
return tradeData.map((tradeWithMetadata) => {
|
|
315
|
+
const { tx, type } = tradeWithMetadata;
|
|
316
|
+
return {
|
|
317
|
+
...tradeWithMetadata,
|
|
318
|
+
txMeta: txs.find((txMeta) => {
|
|
319
|
+
if (is7702Tx(txMeta)) {
|
|
320
|
+
// For 7702 transactions, we need to match based on transaction type
|
|
321
|
+
// since the data field might be different (batch execute call)
|
|
322
|
+
if (isTradeTx(type) && txMeta.type === TransactionType.batch) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
// Also check if it's an approval transaction for 7702
|
|
326
|
+
if (isApprovalTx(type) && txMeta.txParams.data === tx.data) {
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// Default matching logic for non-7702 transactions
|
|
331
|
+
if (txMeta.txParams.data === tx.data) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
return false;
|
|
335
|
+
}),
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* This is a workaround to update the tx type after submission. Batch txs are submitted with
|
|
341
|
+
* the "batch" type, but we need to update to swap/bridge for display purposes.
|
|
342
|
+
*
|
|
343
|
+
* @param params - The parameters for the transaction search
|
|
344
|
+
* @param params.messenger - The messenger to use for the transaction
|
|
345
|
+
* @param params.allTradesWithMetadata - The quote, tx data and type for each transaction in the batch
|
|
346
|
+
* @returns A list of transaction metas for each trade in the batch]
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* [
|
|
350
|
+
* {...tradeData[0], tradeMeta: TransactionMeta}
|
|
351
|
+
* {...tradeData[1], tradeMeta: TransactionMeta}
|
|
352
|
+
* {...tradeData[2], tradeMeta: TransactionMeta}
|
|
353
|
+
* {...tradeData[3], tradeMeta: TransactionMeta}
|
|
354
|
+
* ]
|
|
355
|
+
*/
|
|
356
|
+
export const updateTransactionsInBatch = ({ messenger, allTradesWithMetadata, }) => {
|
|
357
|
+
return allTradesWithMetadata.map((tradeWithMetadata) => {
|
|
358
|
+
const { txMeta, type } = tradeWithMetadata;
|
|
359
|
+
if (txMeta) {
|
|
360
|
+
// Update the tx type from batch to swap/bridge
|
|
361
|
+
updateTransaction(messenger, txMeta, { type }, `Update tx type to ${type}`);
|
|
362
|
+
const updatedTx = { ...txMeta, type };
|
|
363
|
+
return { ...tradeWithMetadata, txMeta: updatedTx, type };
|
|
364
|
+
}
|
|
365
|
+
return tradeWithMetadata;
|
|
366
|
+
});
|
|
402
367
|
};
|
|
403
368
|
//# sourceMappingURL=transaction.mjs.map
|