@metamask/bridge-controller 63.2.0 → 64.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 (90) hide show
  1. package/CHANGELOG.md +28 -1
  2. package/dist/bridge-controller.cjs +60 -34
  3. package/dist/bridge-controller.cjs.map +1 -1
  4. package/dist/bridge-controller.d.cts +0 -8
  5. package/dist/bridge-controller.d.cts.map +1 -1
  6. package/dist/bridge-controller.d.mts +0 -8
  7. package/dist/bridge-controller.d.mts.map +1 -1
  8. package/dist/bridge-controller.mjs +62 -36
  9. package/dist/bridge-controller.mjs.map +1 -1
  10. package/dist/constants/swaps.cjs +57 -58
  11. package/dist/constants/swaps.cjs.map +1 -1
  12. package/dist/constants/swaps.d.cts +0 -1
  13. package/dist/constants/swaps.d.cts.map +1 -1
  14. package/dist/constants/swaps.d.mts +0 -1
  15. package/dist/constants/swaps.d.mts.map +1 -1
  16. package/dist/constants/swaps.mjs +6 -7
  17. package/dist/constants/swaps.mjs.map +1 -1
  18. package/dist/constants/tokens.cjs +15 -16
  19. package/dist/constants/tokens.cjs.map +1 -1
  20. package/dist/constants/tokens.d.cts +3 -3
  21. package/dist/constants/tokens.d.cts.map +1 -1
  22. package/dist/constants/tokens.d.mts +3 -3
  23. package/dist/constants/tokens.d.mts.map +1 -1
  24. package/dist/constants/tokens.mjs +2 -3
  25. package/dist/constants/tokens.mjs.map +1 -1
  26. package/dist/index.cjs +4 -4
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +4 -4
  29. package/dist/index.d.cts.map +1 -1
  30. package/dist/index.d.mts +4 -4
  31. package/dist/index.d.mts.map +1 -1
  32. package/dist/index.mjs +4 -4
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/selectors.d.cts +702 -0
  35. package/dist/selectors.d.cts.map +1 -1
  36. package/dist/selectors.d.mts +702 -0
  37. package/dist/selectors.d.mts.map +1 -1
  38. package/dist/types.cjs +2 -1
  39. package/dist/types.cjs.map +1 -1
  40. package/dist/types.d.cts +12 -3
  41. package/dist/types.d.cts.map +1 -1
  42. package/dist/types.d.mts +12 -3
  43. package/dist/types.d.mts.map +1 -1
  44. package/dist/types.mjs +2 -1
  45. package/dist/types.mjs.map +1 -1
  46. package/dist/utils/bridge.cjs +28 -23
  47. package/dist/utils/bridge.cjs.map +1 -1
  48. package/dist/utils/bridge.d.cts +11 -10
  49. package/dist/utils/bridge.d.cts.map +1 -1
  50. package/dist/utils/bridge.d.mts +11 -10
  51. package/dist/utils/bridge.d.mts.map +1 -1
  52. package/dist/utils/bridge.mjs +26 -21
  53. package/dist/utils/bridge.mjs.map +1 -1
  54. package/dist/utils/fetch.cjs +22 -1
  55. package/dist/utils/fetch.cjs.map +1 -1
  56. package/dist/utils/fetch.d.cts.map +1 -1
  57. package/dist/utils/fetch.d.mts.map +1 -1
  58. package/dist/utils/fetch.mjs +22 -1
  59. package/dist/utils/fetch.mjs.map +1 -1
  60. package/dist/utils/metrics/types.cjs.map +1 -1
  61. package/dist/utils/metrics/types.d.cts +1 -1
  62. package/dist/utils/metrics/types.d.cts.map +1 -1
  63. package/dist/utils/metrics/types.d.mts +1 -1
  64. package/dist/utils/metrics/types.d.mts.map +1 -1
  65. package/dist/utils/metrics/types.mjs.map +1 -1
  66. package/dist/utils/quote-fees.cjs +1 -4
  67. package/dist/utils/quote-fees.cjs.map +1 -1
  68. package/dist/utils/quote-fees.d.cts +1 -1
  69. package/dist/utils/quote-fees.d.cts.map +1 -1
  70. package/dist/utils/quote-fees.d.mts +1 -1
  71. package/dist/utils/quote-fees.d.mts.map +1 -1
  72. package/dist/utils/quote-fees.mjs +1 -4
  73. package/dist/utils/quote-fees.mjs.map +1 -1
  74. package/dist/utils/quote.cjs +8 -3
  75. package/dist/utils/quote.cjs.map +1 -1
  76. package/dist/utils/quote.d.cts +1 -1
  77. package/dist/utils/quote.d.cts.map +1 -1
  78. package/dist/utils/quote.d.mts +1 -1
  79. package/dist/utils/quote.d.mts.map +1 -1
  80. package/dist/utils/quote.mjs +8 -3
  81. package/dist/utils/quote.mjs.map +1 -1
  82. package/dist/utils/swaps.cjs +45 -1
  83. package/dist/utils/swaps.cjs.map +1 -1
  84. package/dist/utils/swaps.d.cts +20 -0
  85. package/dist/utils/swaps.d.cts.map +1 -1
  86. package/dist/utils/swaps.d.mts +20 -0
  87. package/dist/utils/swaps.d.mts.map +1 -1
  88. package/dist/utils/swaps.mjs +43 -0
  89. package/dist/utils/swaps.mjs.map +1 -1
  90. package/package.json +4 -4
@@ -9,18 +9,21 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _BridgeController_instances, _BridgeController_abortController, _BridgeController_quotesFirstFetched, _BridgeController_clientId, _BridgeController_clientVersion, _BridgeController_getLayer1GasFee, _BridgeController_fetchFn, _BridgeController_trackMetaMetricsFn, _BridgeController_trace, _BridgeController_config, _BridgeController_trackResponseValidationFailures, _BridgeController_getExchangeRateSources, _BridgeController_fetchAssetExchangeRates, _BridgeController_hasSufficientBalance, _BridgeController_fetchBridgeQuotes, _BridgeController_handleQuoteStreaming, _BridgeController_setMinimumBalanceForRentExemptionInLamports, _BridgeController_getMultichainSelectedAccount, _BridgeController_getNetworkClientByChainId, _BridgeController_getRequestMetadata, _BridgeController_getQuoteFetchData, _BridgeController_getEventProperties, _BridgeController_trackInputChangedEvents;
12
+ var _BridgeController_instances, _BridgeController_abortController, _BridgeController_quotesFirstFetched, _BridgeController_clientId, _BridgeController_clientVersion, _BridgeController_getLayer1GasFee, _BridgeController_fetchFn, _BridgeController_trackMetaMetricsFn, _BridgeController_trace, _BridgeController_config, _BridgeController_trackResponseValidationFailures, _BridgeController_getExchangeRateSources, _BridgeController_fetchAssetExchangeRates, _BridgeController_hasInsufficientBalance, _BridgeController_shouldResetApproval, _BridgeController_fetchBridgeQuotes, _BridgeController_handleQuoteStreaming, _BridgeController_setMinimumBalanceForRentExemptionInLamports, _BridgeController_getMultichainSelectedAccount, _BridgeController_getNetworkClientByChainId, _BridgeController_getRequestMetadata, _BridgeController_getQuoteFetchData, _BridgeController_getEventProperties, _BridgeController_trackInputChangedEvents, _BridgeController_getUSDTMainnetAllowance;
13
+ import { BigNumber } from "@ethersproject/bignumber";
13
14
  import { Contract } from "@ethersproject/contracts";
14
15
  import { Web3Provider } from "@ethersproject/providers";
15
16
  import { abiERC20 } from "@metamask/metamask-eth-abis";
16
17
  import { StaticIntervalPollingController } from "@metamask/polling-controller";
17
- import { BRIDGE_CONTROLLER_NAME, BRIDGE_PROD_API_BASE_URL, DEFAULT_BRIDGE_CONTROLLER_STATE, METABRIDGE_CHAIN_TO_ADDRESS_MAP, REFRESH_INTERVAL_MS } from "./constants/bridge.mjs";
18
+ import { BRIDGE_CONTROLLER_NAME, BRIDGE_PROD_API_BASE_URL, DEFAULT_BRIDGE_CONTROLLER_STATE, METABRIDGE_ETHEREUM_ADDRESS, REFRESH_INTERVAL_MS } from "./constants/bridge.mjs";
19
+ import { CHAIN_IDS } from "./constants/chains.mjs";
20
+ import { SWAPS_CONTRACT_ADDRESSES } from "./constants/swaps.mjs";
18
21
  import { TraceName } from "./constants/traces.mjs";
19
22
  import { selectIsAssetExchangeRateInState } from "./selectors.mjs";
20
23
  import { RequestStatus } from "./types.mjs";
21
24
  import { getAssetIdsForToken, toExchangeRates } from "./utils/assets.mjs";
22
25
  import { hasSufficientBalance } from "./utils/balance.mjs";
23
- import { getDefaultBridgeControllerState, isCrossChain, isNonEvmChainId, isSolanaChainId } from "./utils/bridge.mjs";
26
+ import { getDefaultBridgeControllerState, isCrossChain, isEthUsdt, isNonEvmChainId, isSolanaChainId } from "./utils/bridge.mjs";
24
27
  import { formatAddressToCaipReference, formatChainIdToCaip, formatChainIdToHex } from "./utils/caip-formatters.mjs";
25
28
  import { getBridgeFeatureFlags, hasMinimumRequiredVersion } from "./utils/feature-flags.mjs";
26
29
  import { fetchAssetPrices, fetchBridgeQuotes, fetchBridgeQuoteStream } from "./utils/fetch.mjs";
@@ -127,6 +130,7 @@ export class BridgeController extends StaticIntervalPollingController() {
127
130
  ? undefined
128
131
  : __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getNetworkClientByChainId).call(this, formatChainIdToHex(updatedQuoteRequest.srcChainId))?.configuration;
129
132
  let insufficientBal;
133
+ let resetApproval = Boolean(paramsToUpdate.resetApproval);
130
134
  if (isSrcChainNonEVM) {
131
135
  // If the source chain is not an EVM network, use value from params
132
136
  insufficientBal = paramsToUpdate.insufficientBal;
@@ -141,16 +145,11 @@ export class BridgeController extends StaticIntervalPollingController() {
141
145
  this.update((state) => {
142
146
  state.quotesLoadingStatus = RequestStatus.LOADING;
143
147
  });
144
- try {
145
- // Otherwise query the src token balance from the RPC provider
146
- insufficientBal =
147
- paramsToUpdate.insufficientBal ??
148
- !(await __classPrivateFieldGet(this, _BridgeController_hasSufficientBalance, "f").call(this, updatedQuoteRequest));
149
- }
150
- catch (error) {
151
- console.warn('Failed to fetch balance', error);
152
- insufficientBal = true;
153
- }
148
+ resetApproval = await __classPrivateFieldGet(this, _BridgeController_shouldResetApproval, "f").call(this, updatedQuoteRequest);
149
+ // Otherwise query the src token balance from the RPC provider
150
+ insufficientBal =
151
+ paramsToUpdate.insufficientBal ??
152
+ (await __classPrivateFieldGet(this, _BridgeController_hasInsufficientBalance, "f").call(this, updatedQuoteRequest));
154
153
  }
155
154
  // Set refresh rate based on the source chain before starting polling
156
155
  this.setChainIntervalLength();
@@ -158,6 +157,7 @@ export class BridgeController extends StaticIntervalPollingController() {
158
157
  updatedQuoteRequest: {
159
158
  ...updatedQuoteRequest,
160
159
  insufficientBal,
160
+ resetApproval,
161
161
  },
162
162
  context,
163
163
  });
@@ -178,10 +178,11 @@ export class BridgeController extends StaticIntervalPollingController() {
178
178
  const quoteRequestOverrides = featureId
179
179
  ? bridgeFeatureFlags.quoteRequestOverrides?.[featureId]
180
180
  : undefined;
181
+ const resetApproval = await __classPrivateFieldGet(this, _BridgeController_shouldResetApproval, "f").call(this, quoteRequest);
181
182
  // If quoteRequestOverrides is specified, merge it with the quoteRequest
182
183
  const { quotes: baseQuotes, validationFailures } = await fetchBridgeQuotes(quoteRequestOverrides
183
- ? { ...quoteRequest, ...quoteRequestOverrides }
184
- : quoteRequest, abortSignal, __classPrivateFieldGet(this, _BridgeController_clientId, "f"), __classPrivateFieldGet(this, _BridgeController_fetchFn, "f"), __classPrivateFieldGet(this, _BridgeController_config, "f").customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL, featureId, __classPrivateFieldGet(this, _BridgeController_clientVersion, "f"));
184
+ ? { ...quoteRequest, ...quoteRequestOverrides, resetApproval }
185
+ : { ...quoteRequest, resetApproval }, abortSignal, __classPrivateFieldGet(this, _BridgeController_clientId, "f"), __classPrivateFieldGet(this, _BridgeController_fetchFn, "f"), __classPrivateFieldGet(this, _BridgeController_config, "f").customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL, featureId, __classPrivateFieldGet(this, _BridgeController_clientVersion, "f"));
185
186
  __classPrivateFieldGet(this, _BridgeController_trackResponseValidationFailures, "f").call(this, validationFailures);
186
187
  const quotesWithFees = await appendFeesToQuotes(baseQuotes, this.messenger, __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f"), __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this, quoteRequest.walletAddress));
187
188
  return sortQuotes(quotesWithFees, featureId);
@@ -245,15 +246,40 @@ export class BridgeController extends StaticIntervalPollingController() {
245
246
  };
246
247
  });
247
248
  });
248
- _BridgeController_hasSufficientBalance.set(this, async (quoteRequest) => {
249
- const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);
250
- const provider = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getNetworkClientByChainId).call(this, srcChainIdInHex)?.provider;
251
- const normalizedSrcTokenAddress = formatAddressToCaipReference(quoteRequest.srcTokenAddress);
252
- return (provider &&
253
- normalizedSrcTokenAddress &&
254
- quoteRequest.srcTokenAmount &&
255
- srcChainIdInHex &&
256
- (await hasSufficientBalance(provider, quoteRequest.walletAddress, normalizedSrcTokenAddress, quoteRequest.srcTokenAmount, srcChainIdInHex)));
249
+ _BridgeController_hasInsufficientBalance.set(this, async (quoteRequest) => {
250
+ try {
251
+ const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);
252
+ const provider = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getNetworkClientByChainId).call(this, srcChainIdInHex)?.provider;
253
+ const normalizedSrcTokenAddress = formatAddressToCaipReference(quoteRequest.srcTokenAddress);
254
+ return !(provider &&
255
+ normalizedSrcTokenAddress &&
256
+ quoteRequest.srcTokenAmount &&
257
+ srcChainIdInHex &&
258
+ (await hasSufficientBalance(provider, quoteRequest.walletAddress, normalizedSrcTokenAddress, quoteRequest.srcTokenAmount, srcChainIdInHex)));
259
+ }
260
+ catch (error) {
261
+ console.warn('Failed to set insufficientBal', error);
262
+ // Fall back to true so the backend returns quotes
263
+ return true;
264
+ }
265
+ });
266
+ _BridgeController_shouldResetApproval.set(this, async (quoteRequest) => {
267
+ if (isNonEvmChainId(quoteRequest.srcChainId)) {
268
+ return false;
269
+ }
270
+ try {
271
+ const normalizedSrcTokenAddress = formatAddressToCaipReference(quoteRequest.srcTokenAddress);
272
+ if (isEthUsdt(quoteRequest.srcChainId, normalizedSrcTokenAddress)) {
273
+ const allowance = BigNumber.from(await __classPrivateFieldGet(this, _BridgeController_getUSDTMainnetAllowance, "f").call(this, quoteRequest.walletAddress, normalizedSrcTokenAddress, quoteRequest.destChainId));
274
+ return allowance.lt(quoteRequest.srcTokenAmount) && allowance.gt(0);
275
+ }
276
+ return false;
277
+ }
278
+ catch (error) {
279
+ console.warn('Failed to set resetApproval', error);
280
+ // Fall back to true so the backend returns quotes
281
+ return true;
282
+ }
257
283
  });
258
284
  this.stopPollingForQuotes = (reason, context) => {
259
285
  this.stopAllPolling();
@@ -324,7 +350,7 @@ export class BridgeController extends StaticIntervalPollingController() {
324
350
  const selectedAccount = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this, updatedQuoteRequest.walletAddress);
325
351
  // This call is not awaited to prevent blocking quote fetching if the snap takes too long to respond
326
352
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
327
- __classPrivateFieldGet(this, _BridgeController_setMinimumBalanceForRentExemptionInLamports, "f").call(this, updatedQuoteRequest.srcChainId, selectedAccount.metadata?.snap?.id);
353
+ __classPrivateFieldGet(this, _BridgeController_setMinimumBalanceForRentExemptionInLamports, "f").call(this, updatedQuoteRequest.srcChainId, selectedAccount?.metadata?.snap?.id);
328
354
  // Use SSE if enabled and return early
329
355
  if (shouldStream) {
330
356
  await __classPrivateFieldGet(this, _BridgeController_handleQuoteStreaming, "f").call(this, updatedQuoteRequest, selectedAccount);
@@ -594,21 +620,25 @@ export class BridgeController extends StaticIntervalPollingController() {
594
620
  };
595
621
  /**
596
622
  *
597
- * @param contractAddress - The address of the ERC20 token contract
598
- * @param chainId - The hex chain ID of the bridge network
623
+ * @param walletAddress - The address of the account to get the allowance for
624
+ * @param contractAddress - The address of the ERC20 token contract on mainnet
625
+ * @param destinationChainId - The chain ID of the destination network
599
626
  * @returns The atomic allowance of the ERC20 token contract
600
627
  */
601
- this.getBridgeERC20Allowance = async (contractAddress, chainId) => {
602
- const networkClient = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getNetworkClientByChainId).call(this, chainId);
628
+ _BridgeController_getUSDTMainnetAllowance.set(this, async (walletAddress, contractAddress, destinationChainId) => {
629
+ const networkClient = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getNetworkClientByChainId).call(this, CHAIN_IDS.MAINNET);
603
630
  const provider = networkClient?.provider;
604
631
  if (!provider) {
605
632
  throw new Error('No provider found');
606
633
  }
607
634
  const ethersProvider = new Web3Provider(provider);
608
635
  const contract = new Contract(contractAddress, abiERC20, ethersProvider);
609
- const allowance = await contract.allowance(this.state.quoteRequest.walletAddress, METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId]);
636
+ const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destinationChainId)
637
+ ? METABRIDGE_ETHEREUM_ADDRESS
638
+ : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];
639
+ const allowance = await contract.allowance(walletAddress, spenderAddress);
610
640
  return allowance.toString();
611
- };
641
+ });
612
642
  this.setIntervalLength(REFRESH_INTERVAL_MS);
613
643
  __classPrivateFieldSet(this, _BridgeController_abortController, new AbortController(), "f");
614
644
  __classPrivateFieldSet(this, _BridgeController_getLayer1GasFee, getLayer1GasFee, "f");
@@ -622,21 +652,17 @@ export class BridgeController extends StaticIntervalPollingController() {
622
652
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`, this.setChainIntervalLength.bind(this));
623
653
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`, this.updateBridgeQuoteRequestParams.bind(this));
624
654
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:resetState`, this.resetState.bind(this));
625
- this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`, this.getBridgeERC20Allowance.bind(this));
626
655
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`, this.trackUnifiedSwapBridgeEvent.bind(this));
627
656
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:stopPollingForQuotes`, this.stopPollingForQuotes.bind(this));
628
657
  this.messenger.registerActionHandler(`${BRIDGE_CONTROLLER_NAME}:fetchQuotes`, this.fetchQuotes.bind(this));
629
658
  }
630
659
  }
631
- _BridgeController_abortController = new WeakMap(), _BridgeController_quotesFirstFetched = new WeakMap(), _BridgeController_clientId = new WeakMap(), _BridgeController_clientVersion = new WeakMap(), _BridgeController_getLayer1GasFee = new WeakMap(), _BridgeController_fetchFn = new WeakMap(), _BridgeController_trackMetaMetricsFn = new WeakMap(), _BridgeController_trace = new WeakMap(), _BridgeController_config = new WeakMap(), _BridgeController_trackResponseValidationFailures = new WeakMap(), _BridgeController_getExchangeRateSources = new WeakMap(), _BridgeController_fetchAssetExchangeRates = new WeakMap(), _BridgeController_hasSufficientBalance = new WeakMap(), _BridgeController_fetchBridgeQuotes = new WeakMap(), _BridgeController_handleQuoteStreaming = new WeakMap(), _BridgeController_setMinimumBalanceForRentExemptionInLamports = new WeakMap(), _BridgeController_getRequestMetadata = new WeakMap(), _BridgeController_getQuoteFetchData = new WeakMap(), _BridgeController_getEventProperties = new WeakMap(), _BridgeController_trackInputChangedEvents = new WeakMap(), _BridgeController_instances = new WeakSet(), _BridgeController_getMultichainSelectedAccount = function _BridgeController_getMultichainSelectedAccount(walletAddress) {
660
+ _BridgeController_abortController = new WeakMap(), _BridgeController_quotesFirstFetched = new WeakMap(), _BridgeController_clientId = new WeakMap(), _BridgeController_clientVersion = new WeakMap(), _BridgeController_getLayer1GasFee = new WeakMap(), _BridgeController_fetchFn = new WeakMap(), _BridgeController_trackMetaMetricsFn = new WeakMap(), _BridgeController_trace = new WeakMap(), _BridgeController_config = new WeakMap(), _BridgeController_trackResponseValidationFailures = new WeakMap(), _BridgeController_getExchangeRateSources = new WeakMap(), _BridgeController_fetchAssetExchangeRates = new WeakMap(), _BridgeController_hasInsufficientBalance = new WeakMap(), _BridgeController_shouldResetApproval = new WeakMap(), _BridgeController_fetchBridgeQuotes = new WeakMap(), _BridgeController_handleQuoteStreaming = new WeakMap(), _BridgeController_setMinimumBalanceForRentExemptionInLamports = new WeakMap(), _BridgeController_getRequestMetadata = new WeakMap(), _BridgeController_getQuoteFetchData = new WeakMap(), _BridgeController_getEventProperties = new WeakMap(), _BridgeController_trackInputChangedEvents = new WeakMap(), _BridgeController_getUSDTMainnetAllowance = new WeakMap(), _BridgeController_instances = new WeakSet(), _BridgeController_getMultichainSelectedAccount = function _BridgeController_getMultichainSelectedAccount(walletAddress) {
632
661
  const addressToUse = walletAddress ?? this.state.quoteRequest.walletAddress;
633
662
  if (!addressToUse) {
634
663
  throw new Error('Account address is required');
635
664
  }
636
665
  const selectedAccount = this.messenger.call('AccountsController:getAccountByAddress', addressToUse);
637
- if (!selectedAccount) {
638
- throw new Error('Account not found');
639
- }
640
666
  return selectedAccount;
641
667
  }, _BridgeController_getNetworkClientByChainId = function _BridgeController_getNetworkClientByChainId(chainId) {
642
668
  const networkClientId = this.messenger.call('NetworkController:findNetworkClientIdByChainId', chainId);
@@ -1 +1 @@
1
- {"version":3,"file":"bridge-controller.mjs","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,YAAY,EAAE,iCAAiC;AAIxD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AACvD,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAK/E,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,+BAA+B,EAC/B,+BAA+B,EAC/B,mBAAmB,EACpB,+BAA2B;AAC5B,OAAO,EAAE,SAAS,EAAE,+BAA2B;AAC/C,OAAO,EAAE,gCAAgC,EAAE,wBAAoB;AAE/D,OAAO,EAQL,aAAa,EACd,oBAAgB;AACjB,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,2BAAuB;AACtE,OAAO,EAAE,oBAAoB,EAAE,4BAAwB;AACvD,OAAO,EACL,+BAA+B,EAC/B,YAAY,EACZ,eAAe,EACf,eAAe,EAChB,2BAAuB;AACxB,OAAO,EACL,4BAA4B,EAC5B,mBAAmB,EACnB,kBAAkB,EACnB,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,yBAAyB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C,qBAAqB,CAClC,GAAG,sBAAsB,uBAAuB,EAChD,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,CACrC,CAAC;QACF,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAClC,GAAG,sBAAsB,cAAc,EACvC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAC5B,CAAC;IACJ,CAAC;CA+uBF;2sCAtOG,aAAoD;IAEpD,MAAM,YAAY,GAAG,aAAa,IAAI,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,aAAa,CAAC;IAC5E,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IACD,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,wCAAwC,EACxC,YAAY,CACb,CAAC;IACF,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,eAAe,CAAC;AACzB,CAAC,qGAE0B,OAAY;IACrC,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,gDAAgD,EAChD,OAAO,CACR,CAAC;IACF,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,wCAAwC,OAAO,EAAE,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACvC,wCAAwC,EACxC,eAAe,CAChB,CAAC;IACF,OAAO,aAAa,CAAC;AACvB,CAAC","sourcesContent":["import type { BigNumber } from '@ethersproject/bignumber';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport type { StateMetadata } from '@metamask/base-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionController } from '@metamask/transaction-controller';\nimport type { CaipAssetType, Hex } from '@metamask/utils';\n\nimport type { BridgeClientId } from './constants/bridge';\nimport {\n BRIDGE_CONTROLLER_NAME,\n BRIDGE_PROD_API_BASE_URL,\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP,\n REFRESH_INTERVAL_MS,\n} from './constants/bridge';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type NonEvmFees,\n type QuoteResponse,\n type BridgeControllerState,\n type BridgeControllerMessenger,\n type FetchFunction,\n RequestStatus,\n} from './types';\nimport { getAssetIdsForToken, toExchangeRates } from './utils/assets';\nimport { hasSufficientBalance } from './utils/balance';\nimport {\n getDefaultBridgeControllerState,\n isCrossChain,\n isNonEvmChainId,\n isSolanaChainId,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport {\n getBridgeFeatureFlags,\n hasMinimumRequiredVersion,\n} from './utils/feature-flags';\nimport {\n fetchAssetPrices,\n fetchBridgeQuotes,\n fetchBridgeQuoteStream,\n} from './utils/fetch';\nimport {\n AbortReason,\n MetricsActionType,\n UnifiedSwapBridgeEventName,\n} from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest, sortQuotes } from './utils/quote';\nimport { appendFeesToQuotes } from './utils/quote-fees';\nimport { getMinimumBalanceForRentExemptionInLamports } from './utils/snaps';\nimport type { FeatureId } from './utils/validators';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotes: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesInitialLoadTime: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesLastFetched: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesLoadingStatus: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quoteFetchError: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesRefreshCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n assetExchangeRates: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n minimumBalanceForRentExemptionInLamports: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param updatedQuoteRequest - The updated quote request\n * @param context - The context contains properties that can't be populated by the\n * controller and need to be provided by the client for analytics\n */\ntype BridgePollingInput = {\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuotesError\n >[UnifiedSwapBridgeEventName.QuotesError] &\n Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuotesRequested\n >[UnifiedSwapBridgeEventName.QuotesRequested];\n};\n\nexport class BridgeController extends StaticIntervalPollingController<BridgePollingInput>()<\n typeof BRIDGE_CONTROLLER_NAME,\n BridgeControllerState,\n BridgeControllerMessenger\n> {\n #abortController: AbortController | undefined;\n\n #quotesFirstFetched: number | undefined;\n\n readonly #clientId: BridgeClientId;\n\n readonly #clientVersion: string;\n\n readonly #getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;\n\n readonly #fetchFn: FetchFunction;\n\n readonly #trackMetaMetricsFn: <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n properties: CrossChainSwapsEventProperties<T>,\n ) => void;\n\n readonly #trace: TraceCallback;\n\n readonly #config: {\n customBridgeApiBaseUrl?: string;\n };\n\n constructor({\n messenger,\n state,\n clientId,\n clientVersion,\n getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\n clientVersion: string;\n getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;\n fetchFn: FetchFunction;\n config?: {\n customBridgeApiBaseUrl?: string;\n };\n trackMetaMetricsFn: <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n properties: CrossChainSwapsEventProperties<T>,\n ) => void;\n traceFn?: TraceCallback;\n }) {\n super({\n name: BRIDGE_CONTROLLER_NAME,\n metadata,\n messenger,\n state: {\n ...getDefaultBridgeControllerState(),\n ...state,\n },\n });\n\n this.setIntervalLength(REFRESH_INTERVAL_MS);\n\n this.#abortController = new AbortController();\n this.#getLayer1GasFee = getLayer1GasFee;\n this.#clientId = clientId;\n this.#clientVersion = clientVersion;\n this.#fetchFn = fetchFn;\n this.#trackMetaMetricsFn = trackMetaMetricsFn;\n this.#config = config ?? {};\n this.#trace = traceFn ?? (((_request, fn) => fn?.()) as TraceCallback);\n\n // Register action handlers\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`,\n this.getBridgeERC20Allowance.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:stopPollingForQuotes`,\n this.stopPollingForQuotes.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:fetchQuotes`,\n this.fetchQuotes.bind(this),\n );\n }\n\n _executePoll = async (pollingInput: BridgePollingInput) => {\n await this.#fetchBridgeQuotes(pollingInput);\n };\n\n updateBridgeQuoteRequestParams = async (\n paramsToUpdate: Partial<GenericQuoteRequest> & {\n walletAddress: GenericQuoteRequest['walletAddress'];\n },\n context: BridgePollingInput['context'],\n ) => {\n this.#trackInputChangedEvents(paramsToUpdate);\n this.resetState(AbortReason.QuoteRequestUpdated);\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\n });\n\n await this.#fetchAssetExchangeRates(updatedQuoteRequest).catch((error) =>\n console.warn('Failed to fetch asset exchange rates', error),\n );\n\n if (isValidQuoteRequest(updatedQuoteRequest)) {\n this.#quotesFirstFetched = Date.now();\n const isSrcChainNonEVM = isNonEvmChainId(updatedQuoteRequest.srcChainId);\n const providerConfig = isSrcChainNonEVM\n ? undefined\n : this.#getNetworkClientByChainId(\n formatChainIdToHex(updatedQuoteRequest.srcChainId),\n )?.configuration;\n\n let insufficientBal: boolean | undefined;\n if (isSrcChainNonEVM) {\n // If the source chain is not an EVM network, use value from params\n insufficientBal = paramsToUpdate.insufficientBal;\n } else if (providerConfig?.rpcUrl?.includes('tenderly')) {\n // If the rpcUrl is a tenderly fork (e2e tests), set insufficientBal=true\n // The bridge-api filters out quotes if the balance on mainnet is insufficient so this override allows quotes to always be returned\n insufficientBal = true;\n } else {\n // Set loading status if RPC calls are made before the quotes are fetched\n this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n });\n try {\n // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n !(await this.#hasSufficientBalance(updatedQuoteRequest));\n } catch (error) {\n console.warn('Failed to fetch balance', error);\n insufficientBal = true;\n }\n }\n\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n },\n context,\n });\n }\n };\n\n /**\n * Fetches quotes for specified request without updating the controller state\n * This method does not start polling for quotes and does not emit UnifiedSwapBridge events\n *\n * @param quoteRequest - The parameters for quote requests to fetch\n * @param abortSignal - The abort signal to cancel all the requests\n * @param featureId - The feature ID that maps to quoteParam overrides from LD\n * @returns A list of validated quotes\n */\n fetchQuotes = async (\n quoteRequest: GenericQuoteRequest,\n abortSignal: AbortSignal | null = null,\n featureId: FeatureId | null = null,\n ): Promise<(QuoteResponse & L1GasFees & NonEvmFees)[]> => {\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messenger);\n // If featureId is specified, retrieve the quoteRequestOverrides for that featureId\n const quoteRequestOverrides = featureId\n ? bridgeFeatureFlags.quoteRequestOverrides?.[featureId]\n : undefined;\n\n // If quoteRequestOverrides is specified, merge it with the quoteRequest\n const { quotes: baseQuotes, validationFailures } = await fetchBridgeQuotes(\n quoteRequestOverrides\n ? { ...quoteRequest, ...quoteRequestOverrides }\n : quoteRequest,\n abortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n featureId,\n this.#clientVersion,\n );\n\n this.#trackResponseValidationFailures(validationFailures);\n\n const quotesWithFees = await appendFeesToQuotes(\n baseQuotes,\n this.messenger,\n this.#getLayer1GasFee,\n this.#getMultichainSelectedAccount(quoteRequest.walletAddress),\n );\n\n return sortQuotes(quotesWithFees, featureId);\n };\n\n readonly #trackResponseValidationFailures = (\n validationFailures: string[],\n ) => {\n if (validationFailures.length === 0) {\n return;\n }\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesValidationFailed,\n {\n failures: validationFailures,\n },\n );\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messenger.call('MultichainAssetsRatesController:getState'),\n ...this.messenger.call('CurrencyRateController:getState'),\n ...this.messenger.call('TokenRatesController:getState'),\n ...this.state,\n };\n };\n\n /**\n * Fetches the exchange rates for the assets in the quote request if they are not already in the state\n * In addition to the selected tokens, this also fetches the native asset for the source and destination chains\n *\n * @param quoteRequest - The quote request\n * @param quoteRequest.srcChainId - The source chain ID\n * @param quoteRequest.srcTokenAddress - The source token address\n * @param quoteRequest.destChainId - The destination chain ID\n * @param quoteRequest.destTokenAddress - The destination token address\n */\n readonly #fetchAssetExchangeRates = async ({\n srcChainId,\n srcTokenAddress,\n destChainId,\n destTokenAddress,\n }: Partial<GenericQuoteRequest>) => {\n const assetIds: Set<CaipAssetType> = new Set([]);\n const exchangeRateSources = this.#getExchangeRateSources();\n if (\n srcTokenAddress &&\n srcChainId &&\n !selectIsAssetExchangeRateInState(\n exchangeRateSources,\n srcChainId,\n srcTokenAddress,\n )\n ) {\n getAssetIdsForToken(srcTokenAddress, srcChainId).forEach((assetId) =>\n assetIds.add(assetId),\n );\n }\n if (\n destTokenAddress &&\n destChainId &&\n !selectIsAssetExchangeRateInState(\n exchangeRateSources,\n destChainId,\n destTokenAddress,\n )\n ) {\n getAssetIdsForToken(destTokenAddress, destChainId).forEach((assetId) =>\n assetIds.add(assetId),\n );\n }\n\n const currency = this.messenger.call(\n 'CurrencyRateController:getState',\n ).currentCurrency;\n\n if (assetIds.size === 0) {\n return;\n }\n\n const pricesByAssetId = await fetchAssetPrices({\n assetIds,\n currencies: new Set([currency]),\n clientId: this.#clientId,\n clientVersion: this.#clientVersion,\n fetchFn: this.#fetchFn,\n signal: this.#abortController?.signal,\n });\n const exchangeRates = toExchangeRates(currency, pricesByAssetId);\n this.update((state) => {\n state.assetExchangeRates = {\n ...state.assetExchangeRates,\n ...exchangeRates,\n };\n });\n };\n\n readonly #hasSufficientBalance = async (\n quoteRequest: GenericQuoteRequest,\n ) => {\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider = this.#getNetworkClientByChainId(srcChainIdInHex)?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return (\n provider &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n quoteRequest.walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n };\n\n stopPollingForQuotes = (\n reason?: AbortReason,\n context?: RequiredEventContextFromClient[UnifiedSwapBridgeEventName.QuotesReceived],\n ) => {\n this.stopAllPolling();\n // If polling is stopped before quotes finish loading, track QuotesReceived\n if (this.state.quotesLoadingStatus === RequestStatus.LOADING && context) {\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesReceived,\n context,\n );\n }\n // Clears quotes list in state\n this.#abortController?.abort(reason);\n };\n\n resetState = (reason = AbortReason.ResetState) => {\n this.stopPollingForQuotes(reason);\n this.update((state) => {\n // Cannot do direct assignment to state, i.e. state = {... }, need to manually assign each field\n state.quoteRequest = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest;\n state.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n state.quotesLastFetched =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLastFetched;\n state.quotesLoadingStatus =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLoadingStatus;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n state.quotesRefreshCount =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesRefreshCount;\n state.assetExchangeRates =\n DEFAULT_BRIDGE_CONTROLLER_STATE.assetExchangeRates;\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n /**\n * Sets the interval length based on the source chain\n */\n setChainIntervalLength = () => {\n const { state } = this;\n const { srcChainId } = state.quoteRequest;\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messenger);\n\n const refreshRateOverride = srcChainId\n ? bridgeFeatureFlags.chains[formatChainIdToCaip(srcChainId)]?.refreshRate\n : undefined;\n const defaultRefreshRate = bridgeFeatureFlags.refreshRate;\n this.setIntervalLength(refreshRateOverride ?? defaultRefreshRate);\n };\n\n readonly #fetchBridgeQuotes = async ({\n updatedQuoteRequest,\n context,\n }: BridgePollingInput) => {\n this.#abortController?.abort('New quote request');\n this.#abortController = new AbortController();\n\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesRequested,\n context,\n );\n\n const { sse, maxRefreshCount } = getBridgeFeatureFlags(this.messenger);\n const shouldStream =\n sse?.enabled &&\n hasMinimumRequiredVersion(this.#clientVersion, sse.minimumVersion);\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n state.quotesLastFetched = Date.now();\n state.quotesLoadingStatus = RequestStatus.LOADING;\n });\n\n try {\n await this.#trace(\n {\n name: isCrossChain(\n updatedQuoteRequest.srcChainId,\n updatedQuoteRequest.destChainId,\n )\n ? TraceName.BridgeQuotesFetched\n : TraceName.SwapQuotesFetched,\n data: {\n srcChainId: formatChainIdToCaip(updatedQuoteRequest.srcChainId),\n destChainId: formatChainIdToCaip(updatedQuoteRequest.destChainId),\n },\n },\n async () => {\n const selectedAccount = this.#getMultichainSelectedAccount(\n updatedQuoteRequest.walletAddress,\n );\n // This call is not awaited to prevent blocking quote fetching if the snap takes too long to respond\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n selectedAccount.metadata?.snap?.id,\n );\n // Use SSE if enabled and return early\n if (shouldStream) {\n await this.#handleQuoteStreaming(\n updatedQuoteRequest,\n selectedAccount,\n );\n return;\n }\n // Otherwise use regular fetch\n const quotes = await this.fetchQuotes(\n updatedQuoteRequest,\n this.#abortController?.signal,\n );\n this.update((state) => {\n // Set the initial load time if this is the first fetch\n if (\n state.quotesRefreshCount ===\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesRefreshCount &&\n this.#quotesFirstFetched\n ) {\n state.quotesInitialLoadTime =\n Date.now() - this.#quotesFirstFetched;\n }\n state.quotes = quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\n },\n );\n } catch (error) {\n // Reset the quotes list if the fetch fails to avoid showing stale quotes\n this.update((state) => {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n // Ignore abort errors\n if (\n (error as Error).toString().includes('AbortError') ||\n (error as Error).toString().includes('FetchRequestCanceledException') ||\n [\n AbortReason.ResetState,\n AbortReason.NewQuoteRequest,\n AbortReason.QuoteRequestUpdated,\n AbortReason.TransactionSubmitted,\n ].includes(error as AbortReason)\n ) {\n // Exit the function early to prevent other state updates\n return;\n }\n\n // Update loading status and error message\n this.update((state) => {\n // The error object reference is not guaranteed to exist on mobile so reading\n // the message directly could cause an error.\n let errorMessage;\n try {\n errorMessage =\n (error as Error)?.message ?? (error as Error).toString();\n } catch {\n // Intentionally empty\n } finally {\n state.quoteFetchError = errorMessage ?? 'Unknown error';\n }\n state.quotesLoadingStatus = RequestStatus.ERROR;\n });\n // Track event and log error\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesError,\n context,\n );\n console.log(\n `Failed to ${shouldStream ? 'stream' : 'fetch'} bridge quotes`,\n error,\n );\n }\n\n // Update refresh count after fetching, validation and fee calculation have completed\n this.update((state) => {\n state.quotesRefreshCount += 1;\n });\n // Stop polling if the maximum number of refreshes has been reached\n if (\n updatedQuoteRequest.insufficientBal ||\n (!updatedQuoteRequest.insufficientBal &&\n this.state.quotesRefreshCount >= maxRefreshCount)\n ) {\n this.stopAllPolling();\n }\n };\n\n readonly #handleQuoteStreaming = async (\n updatedQuoteRequest: GenericQuoteRequest,\n selectedAccount: InternalAccount,\n ) => {\n /**\n * Tracks the number of valid quotes received from the current stream, which is used\n * to determine when to clear the quotes list and set the initial load time\n */\n let validQuotesCounter = 0;\n /**\n * Tracks all pending promises from appendFeesToQuotes calls to ensure they complete\n * before setting quotesLoadingStatus to FETCHED\n */\n const pendingFeeAppendPromises = new Set<Promise<void>>();\n\n await fetchBridgeQuoteStream(\n this.#fetchFn,\n updatedQuoteRequest,\n this.#abortController?.signal,\n this.#clientId,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n {\n onValidationFailure: this.#trackResponseValidationFailures,\n onValidQuoteReceived: async (quote: QuoteResponse) => {\n const feeAppendPromise = (async () => {\n const quotesWithFees = await appendFeesToQuotes(\n [quote],\n this.messenger,\n this.#getLayer1GasFee,\n selectedAccount,\n );\n if (quotesWithFees.length > 0) {\n validQuotesCounter += 1;\n }\n this.update((state) => {\n // Clear previous quotes and quotes load time when first quote in the current\n // polling loop is received\n // This enables clients to continue showing the previous quotes while new\n // quotes are loading\n // Note: If there are no valid quotes until the 2nd fetch, quotesInitialLoadTime will be > refreshRate\n if (validQuotesCounter === 1) {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n if (!state.quotesInitialLoadTime && this.#quotesFirstFetched) {\n // Set the initial load time after the first quote is received\n state.quotesInitialLoadTime =\n Date.now() - this.#quotesFirstFetched;\n }\n }\n state.quotes = [...state.quotes, ...quotesWithFees];\n });\n })();\n pendingFeeAppendPromises.add(feeAppendPromise);\n feeAppendPromise\n .catch((error) => {\n // Catch errors to prevent them from breaking stream processing\n // If appendFeesToQuotes throws, the state update never happens, so no invalid entry is added\n console.error('Error appending fees to quote', error);\n })\n .finally(() => {\n pendingFeeAppendPromises.delete(feeAppendPromise);\n });\n // Await the promise to ensure errors are caught and handled before continuing\n // The promise is also tracked in pendingFeeAppendPromises for onClose to wait for\n await feeAppendPromise;\n },\n onClose: async () => {\n // Wait for all pending appendFeesToQuotes operations to complete\n // before setting quotesLoadingStatus to FETCHED\n await Promise.allSettled(Array.from(pendingFeeAppendPromises));\n this.update((state) => {\n // If there are no valid quotes in the current stream, clear the quotes list\n // to remove quotes from the previous stream\n if (validQuotesCounter === 0) {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n }\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\n },\n },\n this.#clientVersion,\n );\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n snapId?: string,\n ) => {\n if (!isSolanaChainId(srcChainId) || !snapId) {\n return;\n }\n const minimumBalanceForRentExemptionInLamports =\n await getMinimumBalanceForRentExemptionInLamports(snapId, this.messenger);\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n minimumBalanceForRentExemptionInLamports;\n });\n };\n\n #getMultichainSelectedAccount(\n walletAddress?: GenericQuoteRequest['walletAddress'],\n ) {\n const addressToUse = walletAddress ?? this.state.quoteRequest.walletAddress;\n if (!addressToUse) {\n throw new Error('Account address is required');\n }\n const selectedAccount = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n addressToUse,\n );\n if (!selectedAccount) {\n throw new Error('Account not found');\n }\n return selectedAccount;\n }\n\n #getNetworkClientByChainId(chainId: Hex) {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n chainId,\n );\n if (!networkClientId) {\n throw new Error(`No network client found for chainId: ${chainId}`);\n }\n const networkClient = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n | 'stx_enabled'\n | 'usd_amount_source'\n | 'security_warnings'\n | 'is_hardware_wallet'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact' | 'can_submit'\n > => {\n return {\n quotes_count: this.state.quotes.length,\n quotes_list: this.state.quotes.map(({ quote }) =>\n formatProviderLabel(quote),\n ),\n initial_load_time_all_quotes: this.state.quotesInitialLoadTime ?? 0,\n };\n };\n\n readonly #getEventProperties = <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n propertiesFromClient: Pick<RequiredEventContextFromClient, T>[T],\n ): CrossChainSwapsEventProperties<T> => {\n const baseProperties = {\n ...propertiesFromClient,\n action_type: MetricsActionType.SWAPBRIDGE_V1,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesValidationFailed:\n return {\n ...getRequestParams(this.state.quoteRequest),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesError:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n error_message: this.state.quoteFetchError,\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.AllQuotesOpened:\n case UnifiedSwapBridgeEventName.AllQuotesSorted:\n case UnifiedSwapBridgeEventName.QuoteSelected:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.Failed: {\n // Populate the properties that the error occurred before the tx was submitted\n return {\n ...baseProperties,\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...propertiesFromClient,\n };\n }\n case UnifiedSwapBridgeEventName.AssetDetailTooltipClicked:\n return baseProperties;\n // These events may be published after the bridge-controller state is reset\n // So the BridgeStatusController populates all the properties\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n return propertiesFromClient;\n case UnifiedSwapBridgeEventName.InputChanged:\n default:\n return baseProperties;\n }\n };\n\n readonly #trackInputChangedEvents = (\n paramsToUpdate: Partial<GenericQuoteRequest>,\n ) => {\n Object.entries(paramsToUpdate).forEach(([key, value]) => {\n const inputKey = toInputChangedPropertyKey[key as keyof QuoteRequest];\n const inputValue =\n toInputChangedPropertyValue[key as keyof QuoteRequest]?.(\n paramsToUpdate,\n );\n if (\n inputKey &&\n inputValue !== undefined &&\n value !== this.state.quoteRequest[key as keyof GenericQuoteRequest]\n ) {\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.InputChanged,\n {\n input: inputKey,\n input_value: inputValue,\n },\n );\n }\n });\n };\n\n /**\n * This method tracks cross-chain swaps events\n *\n * @param eventName - The name of the event to track\n * @param propertiesFromClient - Properties that can't be calculated from the event name and need to be provided by the client\n * @example\n * this.trackUnifiedSwapBridgeEvent(UnifiedSwapBridgeEventName.ActionOpened, {\n * location: MetaMetricsSwapsEventSource.MainView,\n * });\n */\n trackUnifiedSwapBridgeEvent = <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n propertiesFromClient: Pick<RequiredEventContextFromClient, T>[T],\n ) => {\n try {\n const combinedPropertiesForEvent = this.#getEventProperties<T>(\n eventName,\n propertiesFromClient,\n );\n\n this.#trackMetaMetricsFn(eventName, combinedPropertiesForEvent);\n } catch (error) {\n console.error(\n `Error tracking cross-chain swaps MetaMetrics event ${eventName}`,\n error,\n );\n }\n };\n\n /**\n *\n * @param contractAddress - The address of the ERC20 token contract\n * @param chainId - The hex chain ID of the bridge network\n * @returns The atomic allowance of the ERC20 token contract\n */\n getBridgeERC20Allowance = async (\n contractAddress: string,\n chainId: Hex,\n ): Promise<string> => {\n const networkClient = this.#getNetworkClientByChainId(chainId);\n const provider = networkClient?.provider;\n if (!provider) {\n throw new Error('No provider found');\n }\n\n const ethersProvider = new Web3Provider(provider);\n const contract = new Contract(contractAddress, abiERC20, ethersProvider);\n const allowance: BigNumber = await contract.allowance(\n this.state.quoteRequest.walletAddress,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId],\n );\n return allowance.toString();\n };\n}\n"]}
1
+ {"version":3,"file":"bridge-controller.mjs","sourceR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AAwC,EACxC,YAAY,CACb,CAAC;IACF,OAAO,eAAe,CAAC;AACzB,CAAC,qGAE0B,OAAY;IACrC,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,gDAAgD,EAChD,OAAO,CACR,CAAC;IACF,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,wCAAwC,OAAO,EAAE,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACvC,wCAAwC,EACxC,eAAe,CAChB,CAAC;IACF,OAAO,aAAa,CAAC;AACvB,CAAC","sourcesContent":["import { BigNumber } from '@ethersproject/bignumber';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport type { StateMetadata } from '@metamask/base-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionController } from '@metamask/transaction-controller';\nimport type { CaipAssetType, Hex } from '@metamask/utils';\n\nimport type { BridgeClientId } from './constants/bridge';\nimport {\n BRIDGE_CONTROLLER_NAME,\n BRIDGE_PROD_API_BASE_URL,\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n METABRIDGE_ETHEREUM_ADDRESS,\n REFRESH_INTERVAL_MS,\n} from './constants/bridge';\nimport { CHAIN_IDS } from './constants/chains';\nimport { SWAPS_CONTRACT_ADDRESSES } from './constants/swaps';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type NonEvmFees,\n type QuoteResponse,\n type BridgeControllerState,\n type BridgeControllerMessenger,\n type FetchFunction,\n RequestStatus,\n} from './types';\nimport { getAssetIdsForToken, toExchangeRates } from './utils/assets';\nimport { hasSufficientBalance } from './utils/balance';\nimport {\n getDefaultBridgeControllerState,\n isCrossChain,\n isEthUsdt,\n isNonEvmChainId,\n isSolanaChainId,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport {\n getBridgeFeatureFlags,\n hasMinimumRequiredVersion,\n} from './utils/feature-flags';\nimport {\n fetchAssetPrices,\n fetchBridgeQuotes,\n fetchBridgeQuoteStream,\n} from './utils/fetch';\nimport {\n AbortReason,\n MetricsActionType,\n UnifiedSwapBridgeEventName,\n} from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest, sortQuotes } from './utils/quote';\nimport { appendFeesToQuotes } from './utils/quote-fees';\nimport { getMinimumBalanceForRentExemptionInLamports } from './utils/snaps';\nimport type { FeatureId } from './utils/validators';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotes: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesInitialLoadTime: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesLastFetched: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesLoadingStatus: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quoteFetchError: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n quotesRefreshCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n assetExchangeRates: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n minimumBalanceForRentExemptionInLamports: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param updatedQuoteRequest - The updated quote request\n * @param context - The context contains properties that can't be populated by the\n * controller and need to be provided by the client for analytics\n */\ntype BridgePollingInput = {\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuotesError\n >[UnifiedSwapBridgeEventName.QuotesError] &\n Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuotesRequested\n >[UnifiedSwapBridgeEventName.QuotesRequested];\n};\n\nexport class BridgeController extends StaticIntervalPollingController<BridgePollingInput>()<\n typeof BRIDGE_CONTROLLER_NAME,\n BridgeControllerState,\n BridgeControllerMessenger\n> {\n #abortController: AbortController | undefined;\n\n #quotesFirstFetched: number | undefined;\n\n readonly #clientId: BridgeClientId;\n\n readonly #clientVersion: string;\n\n readonly #getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;\n\n readonly #fetchFn: FetchFunction;\n\n readonly #trackMetaMetricsFn: <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n properties: CrossChainSwapsEventProperties<T>,\n ) => void;\n\n readonly #trace: TraceCallback;\n\n readonly #config: {\n customBridgeApiBaseUrl?: string;\n };\n\n constructor({\n messenger,\n state,\n clientId,\n clientVersion,\n getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\n clientVersion: string;\n getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;\n fetchFn: FetchFunction;\n config?: {\n customBridgeApiBaseUrl?: string;\n };\n trackMetaMetricsFn: <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n properties: CrossChainSwapsEventProperties<T>,\n ) => void;\n traceFn?: TraceCallback;\n }) {\n super({\n name: BRIDGE_CONTROLLER_NAME,\n metadata,\n messenger,\n state: {\n ...getDefaultBridgeControllerState(),\n ...state,\n },\n });\n\n this.setIntervalLength(REFRESH_INTERVAL_MS);\n\n this.#abortController = new AbortController();\n this.#getLayer1GasFee = getLayer1GasFee;\n this.#clientId = clientId;\n this.#clientVersion = clientVersion;\n this.#fetchFn = fetchFn;\n this.#trackMetaMetricsFn = trackMetaMetricsFn;\n this.#config = config ?? {};\n this.#trace = traceFn ?? (((_request, fn) => fn?.()) as TraceCallback);\n\n // Register action handlers\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:stopPollingForQuotes`,\n this.stopPollingForQuotes.bind(this),\n );\n this.messenger.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:fetchQuotes`,\n this.fetchQuotes.bind(this),\n );\n }\n\n _executePoll = async (pollingInput: BridgePollingInput) => {\n await this.#fetchBridgeQuotes(pollingInput);\n };\n\n updateBridgeQuoteRequestParams = async (\n paramsToUpdate: Partial<GenericQuoteRequest> & {\n walletAddress: GenericQuoteRequest['walletAddress'];\n },\n context: BridgePollingInput['context'],\n ) => {\n this.#trackInputChangedEvents(paramsToUpdate);\n this.resetState(AbortReason.QuoteRequestUpdated);\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\n });\n\n await this.#fetchAssetExchangeRates(updatedQuoteRequest).catch((error) =>\n console.warn('Failed to fetch asset exchange rates', error),\n );\n\n if (isValidQuoteRequest(updatedQuoteRequest)) {\n this.#quotesFirstFetched = Date.now();\n const isSrcChainNonEVM = isNonEvmChainId(updatedQuoteRequest.srcChainId);\n const providerConfig = isSrcChainNonEVM\n ? undefined\n : this.#getNetworkClientByChainId(\n formatChainIdToHex(updatedQuoteRequest.srcChainId),\n )?.configuration;\n\n let insufficientBal: boolean | undefined;\n let resetApproval: boolean = Boolean(paramsToUpdate.resetApproval);\n if (isSrcChainNonEVM) {\n // If the source chain is not an EVM network, use value from params\n insufficientBal = paramsToUpdate.insufficientBal;\n } else if (providerConfig?.rpcUrl?.includes('tenderly')) {\n // If the rpcUrl is a tenderly fork (e2e tests), set insufficientBal=true\n // The bridge-api filters out quotes if the balance on mainnet is insufficient so this override allows quotes to always be returned\n insufficientBal = true;\n } else {\n // Set loading status if RPC calls are made before the quotes are fetched\n this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n });\n resetApproval = await this.#shouldResetApproval(updatedQuoteRequest);\n // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n (await this.#hasInsufficientBalance(updatedQuoteRequest));\n }\n\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n resetApproval,\n },\n context,\n });\n }\n };\n\n /**\n * Fetches quotes for specified request without updating the controller state\n * This method does not start polling for quotes and does not emit UnifiedSwapBridge events\n *\n * @param quoteRequest - The parameters for quote requests to fetch\n * @param abortSignal - The abort signal to cancel all the requests\n * @param featureId - The feature ID that maps to quoteParam overrides from LD\n * @returns A list of validated quotes\n */\n fetchQuotes = async (\n quoteRequest: GenericQuoteRequest,\n abortSignal: AbortSignal | null = null,\n featureId: FeatureId | null = null,\n ): Promise<(QuoteResponse & L1GasFees & NonEvmFees)[]> => {\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messenger);\n // If featureId is specified, retrieve the quoteRequestOverrides for that featureId\n const quoteRequestOverrides = featureId\n ? bridgeFeatureFlags.quoteRequestOverrides?.[featureId]\n : undefined;\n const resetApproval = await this.#shouldResetApproval(quoteRequest);\n\n // If quoteRequestOverrides is specified, merge it with the quoteRequest\n const { quotes: baseQuotes, validationFailures } = await fetchBridgeQuotes(\n quoteRequestOverrides\n ? { ...quoteRequest, ...quoteRequestOverrides, resetApproval }\n : { ...quoteRequest, resetApproval },\n abortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n featureId,\n this.#clientVersion,\n );\n\n this.#trackResponseValidationFailures(validationFailures);\n\n const quotesWithFees = await appendFeesToQuotes(\n baseQuotes,\n this.messenger,\n this.#getLayer1GasFee,\n this.#getMultichainSelectedAccount(quoteRequest.walletAddress),\n );\n\n return sortQuotes(quotesWithFees, featureId);\n };\n\n readonly #trackResponseValidationFailures = (\n validationFailures: string[],\n ) => {\n if (validationFailures.length === 0) {\n return;\n }\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesValidationFailed,\n {\n failures: validationFailures,\n },\n );\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messenger.call('MultichainAssetsRatesController:getState'),\n ...this.messenger.call('CurrencyRateController:getState'),\n ...this.messenger.call('TokenRatesController:getState'),\n ...this.state,\n };\n };\n\n /**\n * Fetches the exchange rates for the assets in the quote request if they are not already in the state\n * In addition to the selected tokens, this also fetches the native asset for the source and destination chains\n *\n * @param quoteRequest - The quote request\n * @param quoteRequest.srcChainId - The source chain ID\n * @param quoteRequest.srcTokenAddress - The source token address\n * @param quoteRequest.destChainId - The destination chain ID\n * @param quoteRequest.destTokenAddress - The destination token address\n */\n readonly #fetchAssetExchangeRates = async ({\n srcChainId,\n srcTokenAddress,\n destChainId,\n destTokenAddress,\n }: Partial<GenericQuoteRequest>) => {\n const assetIds: Set<CaipAssetType> = new Set([]);\n const exchangeRateSources = this.#getExchangeRateSources();\n if (\n srcTokenAddress &&\n srcChainId &&\n !selectIsAssetExchangeRateInState(\n exchangeRateSources,\n srcChainId,\n srcTokenAddress,\n )\n ) {\n getAssetIdsForToken(srcTokenAddress, srcChainId).forEach((assetId) =>\n assetIds.add(assetId),\n );\n }\n if (\n destTokenAddress &&\n destChainId &&\n !selectIsAssetExchangeRateInState(\n exchangeRateSources,\n destChainId,\n destTokenAddress,\n )\n ) {\n getAssetIdsForToken(destTokenAddress, destChainId).forEach((assetId) =>\n assetIds.add(assetId),\n );\n }\n\n const currency = this.messenger.call(\n 'CurrencyRateController:getState',\n ).currentCurrency;\n\n if (assetIds.size === 0) {\n return;\n }\n\n const pricesByAssetId = await fetchAssetPrices({\n assetIds,\n currencies: new Set([currency]),\n clientId: this.#clientId,\n clientVersion: this.#clientVersion,\n fetchFn: this.#fetchFn,\n signal: this.#abortController?.signal,\n });\n const exchangeRates = toExchangeRates(currency, pricesByAssetId);\n this.update((state) => {\n state.assetExchangeRates = {\n ...state.assetExchangeRates,\n ...exchangeRates,\n };\n });\n };\n\n readonly #hasInsufficientBalance = async (\n quoteRequest: GenericQuoteRequest,\n ) => {\n try {\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider =\n this.#getNetworkClientByChainId(srcChainIdInHex)?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return !(\n provider &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n quoteRequest.walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n } catch (error) {\n console.warn('Failed to set insufficientBal', error);\n // Fall back to true so the backend returns quotes\n return true;\n }\n };\n\n readonly #shouldResetApproval = async (quoteRequest: GenericQuoteRequest) => {\n if (isNonEvmChainId(quoteRequest.srcChainId)) {\n return false;\n }\n try {\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n if (isEthUsdt(quoteRequest.srcChainId, normalizedSrcTokenAddress)) {\n const allowance = BigNumber.from(\n await this.#getUSDTMainnetAllowance(\n quoteRequest.walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.destChainId,\n ),\n );\n return allowance.lt(quoteRequest.srcTokenAmount) && allowance.gt(0);\n }\n return false;\n } catch (error) {\n console.warn('Failed to set resetApproval', error);\n // Fall back to true so the backend returns quotes\n return true;\n }\n };\n\n stopPollingForQuotes = (\n reason?: AbortReason,\n context?: RequiredEventContextFromClient[UnifiedSwapBridgeEventName.QuotesReceived],\n ) => {\n this.stopAllPolling();\n // If polling is stopped before quotes finish loading, track QuotesReceived\n if (this.state.quotesLoadingStatus === RequestStatus.LOADING && context) {\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesReceived,\n context,\n );\n }\n // Clears quotes list in state\n this.#abortController?.abort(reason);\n };\n\n resetState = (reason = AbortReason.ResetState) => {\n this.stopPollingForQuotes(reason);\n this.update((state) => {\n // Cannot do direct assignment to state, i.e. state = {... }, need to manually assign each field\n state.quoteRequest = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest;\n state.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n state.quotesLastFetched =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLastFetched;\n state.quotesLoadingStatus =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesLoadingStatus;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n state.quotesRefreshCount =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesRefreshCount;\n state.assetExchangeRates =\n DEFAULT_BRIDGE_CONTROLLER_STATE.assetExchangeRates;\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n /**\n * Sets the interval length based on the source chain\n */\n setChainIntervalLength = () => {\n const { state } = this;\n const { srcChainId } = state.quoteRequest;\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messenger);\n\n const refreshRateOverride = srcChainId\n ? bridgeFeatureFlags.chains[formatChainIdToCaip(srcChainId)]?.refreshRate\n : undefined;\n const defaultRefreshRate = bridgeFeatureFlags.refreshRate;\n this.setIntervalLength(refreshRateOverride ?? defaultRefreshRate);\n };\n\n readonly #fetchBridgeQuotes = async ({\n updatedQuoteRequest,\n context,\n }: BridgePollingInput) => {\n this.#abortController?.abort('New quote request');\n this.#abortController = new AbortController();\n\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesRequested,\n context,\n );\n\n const { sse, maxRefreshCount } = getBridgeFeatureFlags(this.messenger);\n const shouldStream =\n sse?.enabled &&\n hasMinimumRequiredVersion(this.#clientVersion, sse.minimumVersion);\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n state.quotesLastFetched = Date.now();\n state.quotesLoadingStatus = RequestStatus.LOADING;\n });\n\n try {\n await this.#trace(\n {\n name: isCrossChain(\n updatedQuoteRequest.srcChainId,\n updatedQuoteRequest.destChainId,\n )\n ? TraceName.BridgeQuotesFetched\n : TraceName.SwapQuotesFetched,\n data: {\n srcChainId: formatChainIdToCaip(updatedQuoteRequest.srcChainId),\n destChainId: formatChainIdToCaip(updatedQuoteRequest.destChainId),\n },\n },\n async () => {\n const selectedAccount = this.#getMultichainSelectedAccount(\n updatedQuoteRequest.walletAddress,\n );\n // This call is not awaited to prevent blocking quote fetching if the snap takes too long to respond\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n selectedAccount?.metadata?.snap?.id,\n );\n // Use SSE if enabled and return early\n if (shouldStream) {\n await this.#handleQuoteStreaming(\n updatedQuoteRequest,\n selectedAccount,\n );\n return;\n }\n // Otherwise use regular fetch\n const quotes = await this.fetchQuotes(\n updatedQuoteRequest,\n this.#abortController?.signal,\n );\n this.update((state) => {\n // Set the initial load time if this is the first fetch\n if (\n state.quotesRefreshCount ===\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesRefreshCount &&\n this.#quotesFirstFetched\n ) {\n state.quotesInitialLoadTime =\n Date.now() - this.#quotesFirstFetched;\n }\n state.quotes = quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\n },\n );\n } catch (error) {\n // Reset the quotes list if the fetch fails to avoid showing stale quotes\n this.update((state) => {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n // Ignore abort errors\n if (\n (error as Error).toString().includes('AbortError') ||\n (error as Error).toString().includes('FetchRequestCanceledException') ||\n [\n AbortReason.ResetState,\n AbortReason.NewQuoteRequest,\n AbortReason.QuoteRequestUpdated,\n AbortReason.TransactionSubmitted,\n ].includes(error as AbortReason)\n ) {\n // Exit the function early to prevent other state updates\n return;\n }\n\n // Update loading status and error message\n this.update((state) => {\n // The error object reference is not guaranteed to exist on mobile so reading\n // the message directly could cause an error.\n let errorMessage;\n try {\n errorMessage =\n (error as Error)?.message ?? (error as Error).toString();\n } catch {\n // Intentionally empty\n } finally {\n state.quoteFetchError = errorMessage ?? 'Unknown error';\n }\n state.quotesLoadingStatus = RequestStatus.ERROR;\n });\n // Track event and log error\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuotesError,\n context,\n );\n console.log(\n `Failed to ${shouldStream ? 'stream' : 'fetch'} bridge quotes`,\n error,\n );\n }\n\n // Update refresh count after fetching, validation and fee calculation have completed\n this.update((state) => {\n state.quotesRefreshCount += 1;\n });\n // Stop polling if the maximum number of refreshes has been reached\n if (\n updatedQuoteRequest.insufficientBal ||\n (!updatedQuoteRequest.insufficientBal &&\n this.state.quotesRefreshCount >= maxRefreshCount)\n ) {\n this.stopAllPolling();\n }\n };\n\n readonly #handleQuoteStreaming = async (\n updatedQuoteRequest: GenericQuoteRequest,\n selectedAccount?: InternalAccount,\n ) => {\n /**\n * Tracks the number of valid quotes received from the current stream, which is used\n * to determine when to clear the quotes list and set the initial load time\n */\n let validQuotesCounter = 0;\n /**\n * Tracks all pending promises from appendFeesToQuotes calls to ensure they complete\n * before setting quotesLoadingStatus to FETCHED\n */\n const pendingFeeAppendPromises = new Set<Promise<void>>();\n\n await fetchBridgeQuoteStream(\n this.#fetchFn,\n updatedQuoteRequest,\n this.#abortController?.signal,\n this.#clientId,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n {\n onValidationFailure: this.#trackResponseValidationFailures,\n onValidQuoteReceived: async (quote: QuoteResponse) => {\n const feeAppendPromise = (async () => {\n const quotesWithFees = await appendFeesToQuotes(\n [quote],\n this.messenger,\n this.#getLayer1GasFee,\n selectedAccount,\n );\n if (quotesWithFees.length > 0) {\n validQuotesCounter += 1;\n }\n this.update((state) => {\n // Clear previous quotes and quotes load time when first quote in the current\n // polling loop is received\n // This enables clients to continue showing the previous quotes while new\n // quotes are loading\n // Note: If there are no valid quotes until the 2nd fetch, quotesInitialLoadTime will be > refreshRate\n if (validQuotesCounter === 1) {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n if (!state.quotesInitialLoadTime && this.#quotesFirstFetched) {\n // Set the initial load time after the first quote is received\n state.quotesInitialLoadTime =\n Date.now() - this.#quotesFirstFetched;\n }\n }\n state.quotes = [...state.quotes, ...quotesWithFees];\n });\n })();\n pendingFeeAppendPromises.add(feeAppendPromise);\n feeAppendPromise\n .catch((error) => {\n // Catch errors to prevent them from breaking stream processing\n // If appendFeesToQuotes throws, the state update never happens, so no invalid entry is added\n console.error('Error appending fees to quote', error);\n })\n .finally(() => {\n pendingFeeAppendPromises.delete(feeAppendPromise);\n });\n // Await the promise to ensure errors are caught and handled before continuing\n // The promise is also tracked in pendingFeeAppendPromises for onClose to wait for\n await feeAppendPromise;\n },\n onClose: async () => {\n // Wait for all pending appendFeesToQuotes operations to complete\n // before setting quotesLoadingStatus to FETCHED\n await Promise.allSettled(Array.from(pendingFeeAppendPromises));\n this.update((state) => {\n // If there are no valid quotes in the current stream, clear the quotes list\n // to remove quotes from the previous stream\n if (validQuotesCounter === 0) {\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n }\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\n },\n },\n this.#clientVersion,\n );\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n snapId?: string,\n ) => {\n if (!isSolanaChainId(srcChainId) || !snapId) {\n return;\n }\n const minimumBalanceForRentExemptionInLamports =\n await getMinimumBalanceForRentExemptionInLamports(snapId, this.messenger);\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n minimumBalanceForRentExemptionInLamports;\n });\n };\n\n #getMultichainSelectedAccount(\n walletAddress?: GenericQuoteRequest['walletAddress'],\n ) {\n const addressToUse = walletAddress ?? this.state.quoteRequest.walletAddress;\n if (!addressToUse) {\n throw new Error('Account address is required');\n }\n const selectedAccount = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n addressToUse,\n );\n return selectedAccount;\n }\n\n #getNetworkClientByChainId(chainId: Hex) {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n chainId,\n );\n if (!networkClientId) {\n throw new Error(`No network client found for chainId: ${chainId}`);\n }\n const networkClient = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n | 'stx_enabled'\n | 'usd_amount_source'\n | 'security_warnings'\n | 'is_hardware_wallet'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact' | 'can_submit'\n > => {\n return {\n quotes_count: this.state.quotes.length,\n quotes_list: this.state.quotes.map(({ quote }) =>\n formatProviderLabel(quote),\n ),\n initial_load_time_all_quotes: this.state.quotesInitialLoadTime ?? 0,\n };\n };\n\n readonly #getEventProperties = <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n propertiesFromClient: Pick<RequiredEventContextFromClient, T>[T],\n ): CrossChainSwapsEventProperties<T> => {\n const baseProperties = {\n ...propertiesFromClient,\n action_type: MetricsActionType.SWAPBRIDGE_V1,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesValidationFailed:\n return {\n ...getRequestParams(this.state.quoteRequest),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesError:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n error_message: this.state.quoteFetchError,\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.AllQuotesOpened:\n case UnifiedSwapBridgeEventName.AllQuotesSorted:\n case UnifiedSwapBridgeEventName.QuoteSelected:\n return {\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.Failed: {\n // Populate the properties that the error occurred before the tx was submitted\n return {\n ...baseProperties,\n ...getRequestParams(this.state.quoteRequest),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...propertiesFromClient,\n };\n }\n case UnifiedSwapBridgeEventName.AssetDetailTooltipClicked:\n return baseProperties;\n // These events may be published after the bridge-controller state is reset\n // So the BridgeStatusController populates all the properties\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n return propertiesFromClient;\n case UnifiedSwapBridgeEventName.InputChanged:\n default:\n return baseProperties;\n }\n };\n\n readonly #trackInputChangedEvents = (\n paramsToUpdate: Partial<GenericQuoteRequest>,\n ) => {\n Object.entries(paramsToUpdate).forEach(([key, value]) => {\n const inputKey = toInputChangedPropertyKey[key as keyof QuoteRequest];\n const inputValue =\n toInputChangedPropertyValue[key as keyof QuoteRequest]?.(\n paramsToUpdate,\n );\n if (\n inputKey &&\n inputValue !== undefined &&\n value !== this.state.quoteRequest[key as keyof GenericQuoteRequest]\n ) {\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.InputChanged,\n {\n input: inputKey,\n input_value: inputValue,\n },\n );\n }\n });\n };\n\n /**\n * This method tracks cross-chain swaps events\n *\n * @param eventName - The name of the event to track\n * @param propertiesFromClient - Properties that can't be calculated from the event name and need to be provided by the client\n * @example\n * this.trackUnifiedSwapBridgeEvent(UnifiedSwapBridgeEventName.ActionOpened, {\n * location: MetaMetricsSwapsEventSource.MainView,\n * });\n */\n trackUnifiedSwapBridgeEvent = <\n T extends\n (typeof UnifiedSwapBridgeEventName)[keyof typeof UnifiedSwapBridgeEventName],\n >(\n eventName: T,\n propertiesFromClient: Pick<RequiredEventContextFromClient, T>[T],\n ) => {\n try {\n const combinedPropertiesForEvent = this.#getEventProperties<T>(\n eventName,\n propertiesFromClient,\n );\n\n this.#trackMetaMetricsFn(eventName, combinedPropertiesForEvent);\n } catch (error) {\n console.error(\n `Error tracking cross-chain swaps MetaMetrics event ${eventName}`,\n error,\n );\n }\n };\n\n /**\n *\n * @param walletAddress - The address of the account to get the allowance for\n * @param contractAddress - The address of the ERC20 token contract on mainnet\n * @param destinationChainId - The chain ID of the destination network\n * @returns The atomic allowance of the ERC20 token contract\n */\n readonly #getUSDTMainnetAllowance = async (\n walletAddress: string,\n contractAddress: string,\n destinationChainId: GenericQuoteRequest['destChainId'],\n ): Promise<string> => {\n const networkClient = this.#getNetworkClientByChainId(CHAIN_IDS.MAINNET);\n const provider = networkClient?.provider;\n if (!provider) {\n throw new Error('No provider found');\n }\n\n const ethersProvider = new Web3Provider(provider);\n const contract = new Contract(contractAddress, abiERC20, ethersProvider);\n const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destinationChainId)\n ? METABRIDGE_ETHEREUM_ADDRESS\n : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];\n const allowance: BigNumber = await contract.allowance(\n walletAddress,\n spenderAddress,\n );\n return allowance.toString();\n };\n}\n"]}