@metamask-previews/bridge-controller 32.0.0-preview-510657d5 → 32.1.0-preview-d2bf8ff
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 +15 -1
- package/dist/bridge-controller.cjs +51 -28
- package/dist/bridge-controller.cjs.map +1 -1
- package/dist/bridge-controller.d.cts +3 -6
- package/dist/bridge-controller.d.cts.map +1 -1
- package/dist/bridge-controller.d.mts +3 -6
- package/dist/bridge-controller.d.mts.map +1 -1
- package/dist/bridge-controller.mjs +51 -28
- package/dist/bridge-controller.mjs.map +1 -1
- package/dist/utils/fetch.cjs +15 -1
- package/dist/utils/fetch.cjs.map +1 -1
- package/dist/utils/fetch.d.cts.map +1 -1
- package/dist/utils/fetch.d.mts.map +1 -1
- package/dist/utils/fetch.mjs +15 -1
- package/dist/utils/fetch.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -7,9 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [32.1.0]
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Include all invalid quote properties in sentry logs ([#5913](https://github.com/MetaMask/core/pull/5913))
|
15
|
+
|
16
|
+
## [32.0.1]
|
17
|
+
|
10
18
|
### Fixed
|
11
19
|
|
12
20
|
- Remove `error_message` property from QuotesRequested event payload ([#5900](https://github.com/MetaMask/core/pull/5900))
|
21
|
+
- Fail gracefully when fee calculations return invalid value or throw errors
|
22
|
+
- Filter out single quote if `TransactionController.getLayer1GasFee` returns `undefined` ([#5910](https://github.com/MetaMask/core/pull/5910))
|
23
|
+
- Filter out single quote if an error is thrown by `getLayer1GasFee` ([#5910](https://github.com/MetaMask/core/pull/5910))
|
24
|
+
- Filter out single quote if an error is thrown by Solana snap's `getFeeForTransaction` method ([#5910](https://github.com/MetaMask/core/pull/5910))
|
13
25
|
|
14
26
|
## [32.0.0]
|
15
27
|
|
@@ -323,7 +335,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
323
335
|
|
324
336
|
- Initial release ([#5317](https://github.com/MetaMask/core/pull/5317))
|
325
337
|
|
326
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@32.
|
338
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@32.1.0...HEAD
|
339
|
+
[32.1.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@32.0.1...@metamask/bridge-controller@32.1.0
|
340
|
+
[32.0.1]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@32.0.0...@metamask/bridge-controller@32.0.1
|
327
341
|
[32.0.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@31.0.0...@metamask/bridge-controller@32.0.0
|
328
342
|
[31.0.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@30.0.0...@metamask/bridge-controller@31.0.0
|
329
343
|
[30.0.0]: https://github.com/MetaMask/core/compare/@metamask/bridge-controller@29.0.0...@metamask/bridge-controller@30.0.0
|
@@ -328,34 +328,47 @@ class BridgeController extends (0, polling_controller_1.StaticIntervalPollingCon
|
|
328
328
|
.includes(chainId);
|
329
329
|
});
|
330
330
|
// Only append L1 gas fees if all quotes are for either optimism or base
|
331
|
-
if (
|
332
|
-
return
|
333
|
-
const { quote, trade, approval } = quoteResponse;
|
334
|
-
const chainId = (0, utils_1.numberToHex)(quote.srcChainId);
|
335
|
-
const getTxParams = (txData) => ({
|
336
|
-
from: txData.from,
|
337
|
-
to: txData.to,
|
338
|
-
value: txData.value,
|
339
|
-
data: txData.data,
|
340
|
-
gasLimit: txData.gasLimit?.toString(),
|
341
|
-
});
|
342
|
-
const approvalL1GasFees = approval
|
343
|
-
? await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
344
|
-
transactionParams: getTxParams(approval),
|
345
|
-
chainId,
|
346
|
-
})
|
347
|
-
: '0';
|
348
|
-
const tradeL1GasFees = await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
349
|
-
transactionParams: getTxParams(trade),
|
350
|
-
chainId,
|
351
|
-
});
|
352
|
-
return {
|
353
|
-
...quoteResponse,
|
354
|
-
l1GasFeesInHexWei: (0, bridge_2.sumHexes)(approvalL1GasFees, tradeL1GasFees),
|
355
|
-
};
|
356
|
-
}));
|
331
|
+
if (hasInvalidQuotes) {
|
332
|
+
return undefined;
|
357
333
|
}
|
358
|
-
|
334
|
+
const l1GasFeePromises = Promise.allSettled(quotes.map(async (quoteResponse) => {
|
335
|
+
const { quote, trade, approval } = quoteResponse;
|
336
|
+
const chainId = (0, utils_1.numberToHex)(quote.srcChainId);
|
337
|
+
const getTxParams = (txData) => ({
|
338
|
+
from: txData.from,
|
339
|
+
to: txData.to,
|
340
|
+
value: txData.value,
|
341
|
+
data: txData.data,
|
342
|
+
gasLimit: txData.gasLimit?.toString(),
|
343
|
+
});
|
344
|
+
const approvalL1GasFees = approval
|
345
|
+
? await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
346
|
+
transactionParams: getTxParams(approval),
|
347
|
+
chainId,
|
348
|
+
})
|
349
|
+
: '0x0';
|
350
|
+
const tradeL1GasFees = await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
351
|
+
transactionParams: getTxParams(trade),
|
352
|
+
chainId,
|
353
|
+
});
|
354
|
+
if (approvalL1GasFees === undefined || tradeL1GasFees === undefined) {
|
355
|
+
return undefined;
|
356
|
+
}
|
357
|
+
return {
|
358
|
+
...quoteResponse,
|
359
|
+
l1GasFeesInHexWei: (0, bridge_2.sumHexes)(approvalL1GasFees, tradeL1GasFees),
|
360
|
+
};
|
361
|
+
}));
|
362
|
+
const quotesWithL1GasFees = (await l1GasFeePromises).reduce((acc, result) => {
|
363
|
+
if (result.status === 'fulfilled' && result.value) {
|
364
|
+
acc.push(result.value);
|
365
|
+
}
|
366
|
+
else if (result.status === 'rejected') {
|
367
|
+
console.error('Error calculating L1 gas fees for quote', result.reason);
|
368
|
+
}
|
369
|
+
return acc;
|
370
|
+
}, []);
|
371
|
+
return quotesWithL1GasFees;
|
359
372
|
});
|
360
373
|
_BridgeController_setMinimumBalanceForRentExemptionInLamports.set(this, async (srcChainId) => {
|
361
374
|
const selectedAccount = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this);
|
@@ -381,7 +394,7 @@ class BridgeController extends (0, polling_controller_1.StaticIntervalPollingCon
|
|
381
394
|
if (quotes.some(({ quote: { srcChainId } }) => !(0, bridge_2.isSolanaChainId)(srcChainId))) {
|
382
395
|
return undefined;
|
383
396
|
}
|
384
|
-
|
397
|
+
const solanaFeePromises = Promise.allSettled(quotes.map(async (quoteResponse) => {
|
385
398
|
const { trade } = quoteResponse;
|
386
399
|
const selectedAccount = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this);
|
387
400
|
if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {
|
@@ -393,6 +406,16 @@ class BridgeController extends (0, polling_controller_1.StaticIntervalPollingCon
|
|
393
406
|
}
|
394
407
|
return quoteResponse;
|
395
408
|
}));
|
409
|
+
const quotesWithSolanaFees = (await solanaFeePromises).reduce((acc, result) => {
|
410
|
+
if (result.status === 'fulfilled' && result.value) {
|
411
|
+
acc.push(result.value);
|
412
|
+
}
|
413
|
+
else if (result.status === 'rejected') {
|
414
|
+
console.error('Error calculating solana fees for quote', result.reason);
|
415
|
+
}
|
416
|
+
return acc;
|
417
|
+
}, []);
|
418
|
+
return quotesWithSolanaFees;
|
396
419
|
});
|
397
420
|
_BridgeController_getRequestParams.set(this, () => {
|
398
421
|
const srcChainIdCaip = (0, caip_formatters_1.formatChainIdToCaip)(this.state.quoteRequest.srcChainId ||
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"bridge-controller.cjs","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,wDAAoD;AACpD,wDAAwD;AAGxD,mEAAuD;AAEvD,qEAA+E;AAG/E,2CAAwD;AAExD,mDAO4B;AAC5B,mDAA+C;AAC/C,mDAA+C;AAC/C,+CAA+D;AAE/D,uCAUiB;AACjB,+CAAsE;AACtE,iDAAuD;AACvD,+CAKwB;AACxB,iEAIiC;AACjC,6DAA8D;AAC9D,6CAAoE;AACpE,6DAAuE;AACvE,+DASoC;AAQpC,6CAAoD;AACpD,6CAGuB;AAEvB,MAAM,QAAQ,GAAyC;IACrD,YAAY,EAAE;QACZ,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,MAAM,EAAE;QACN,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,qBAAqB,EAAE;QACrB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,iBAAiB,EAAE;QACjB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,mBAAmB,EAAE;QACnB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,eAAe,EAAE;QACf,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,kBAAkB,EAAE;QAClB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,wCAAwC,EAAE;QACxC,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEF,MAAM,yBAAyB,GAAG,wBAAwB,CAAC;AAuB3D,MAAa,gBAAiB,SAAQ,IAAA,oDAA+B,GAIpE;IA4BC,YAAY,EACV,SAAS,EACT,KAAK,EACL,QAAQ,EACR,eAAe,EACf,OAAO,EACP,MAAM,EACN,kBAAk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AAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,+BAAsB,0BAA0B,EACnD,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;QACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,+BAAsB,8BAA8B,EACvD,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC5C,CAAC;IACJ,CAAC;CAkoBF;AAzuBD,4CAyuBC;;IAjNG,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,iDAAiD,CAClD,CAAC;AACJ,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,OAAO,uBAAuB,CAAC;AACjC,CAAC;IAGC,MAAM,uBAAuB,GAAG,uBAAA,IAAI,iFAA4B,MAAhC,IAAI,CAA8B,CAAC;IACnE,MAAM,aAAa,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,wCAAwC,EACxC,uBAAuB,CACxB,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 { ChainId, TraceCallback } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport { numberToHex, type Hex } from '@metamask/utils';\n\nimport {\n type BridgeClientId,\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 { CHAIN_IDS } from './constants/chains';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type SolanaFees,\n type QuoteResponse,\n type TxData,\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 isSolanaChainId,\n sumHexes,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport { getBridgeFeatureFlags } from './utils/feature-flags';\nimport { fetchAssetPrices, fetchBridgeQuotes } from './utils/fetch';\nimport { UnifiedSwapBridgeEventName } from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getActionTypeFromQuoteRequest,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequestParams,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest } from './utils/quote';\nimport {\n getFeeForTransactionRequest,\n getMinimumBalanceForRentExemptionRequest,\n} from './utils/snaps';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n persist: false,\n anonymous: false,\n },\n quotes: {\n persist: false,\n anonymous: false,\n },\n quotesInitialLoadTime: {\n persist: false,\n anonymous: false,\n },\n quotesLastFetched: {\n persist: false,\n anonymous: false,\n },\n quotesLoadingStatus: {\n persist: false,\n anonymous: false,\n },\n quoteFetchError: {\n persist: false,\n anonymous: false,\n },\n quotesRefreshCount: {\n persist: false,\n anonymous: false,\n },\n assetExchangeRates: {\n persist: false,\n anonymous: false,\n },\n minimumBalanceForRentExemptionInLamports: {\n persist: false,\n anonymous: false,\n },\n};\n\nconst RESET_STATE_ABORT_MESSAGE = 'Reset controller state';\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param networkClientId - The network client ID of the selected network\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 networkClientId: NetworkClientId;\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuoteError\n >[UnifiedSwapBridgeEventName.QuoteError] &\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: string;\n\n readonly #getLayer1GasFee: (params: {\n transactionParams: TransactionParams;\n chainId: ChainId;\n }) => Promise<string>;\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 getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\n getLayer1GasFee: (params: {\n transactionParams: TransactionParams;\n chainId: ChainId;\n }) => Promise<string>;\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.#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.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`,\n this.getBridgeERC20Allowance.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.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 context: BridgePollingInput['context'],\n ) => {\n this.stopAllPolling();\n this.#abortController?.abort('Quote request updated');\n\n this.#trackInputChangedEvents(paramsToUpdate);\n\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n\n if (\n paramsToUpdate.srcChainId &&\n paramsToUpdate.srcChainId !== this.state.quoteRequest.srcChainId\n ) {\n await this.#setMinimumBalanceForRentExemptionInLamports(\n paramsToUpdate.srcChainId,\n );\n }\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\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.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\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 providerConfig = this.#getSelectedNetworkClient()?.configuration;\n\n let insufficientBal: boolean | undefined;\n if (isSolanaChainId(updatedQuoteRequest.srcChainId)) {\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 // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n !(await this.#hasSufficientBalance(updatedQuoteRequest));\n }\n\n const networkClientId = this.#getSelectedNetworkClientId();\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n networkClientId,\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n },\n context,\n });\n }\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messagingSystem.call('MultichainAssetsRatesController:getState'),\n ...this.messagingSystem.call('CurrencyRateController:getState'),\n ...this.messagingSystem.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.messagingSystem.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 fetchFn: this.#fetchFn,\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 walletAddress = this.#getMultichainSelectedAccount()?.address;\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider = this.#getSelectedNetworkClient()?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return (\n provider &&\n walletAddress &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n };\n\n resetState = () => {\n this.stopAllPolling();\n this.#abortController?.abort(RESET_STATE_ABORT_MESSAGE);\n\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.messagingSystem);\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 networkClientId: _networkClientId,\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 this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n });\n\n const fetchQuotes = async () => {\n const quotes = await fetchBridgeQuotes(\n updatedQuoteRequest,\n // AbortController is always defined by this line, because we assign it a few lines above,\n // not sure why Jest thinks it's not\n // Linters accurately say that it's defined\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#abortController!.signal as AbortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n );\n\n const quotesWithL1GasFees = await this.#appendL1GasFees(quotes);\n const quotesWithSolanaFees = await this.#appendSolanaFees(quotes);\n\n this.update((state) => {\n state.quotes = quotesWithL1GasFees ?? quotesWithSolanaFees ?? quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\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 fetchQuotes,\n );\n await this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n );\n } catch (error) {\n const isAbortError = (error as Error).name === 'AbortError';\n const isAbortedDueToReset = error === RESET_STATE_ABORT_MESSAGE;\n if (isAbortedDueToReset || isAbortError) {\n // Exit the function early to avoid other state updates\n return;\n }\n\n this.update((state) => {\n state.quoteFetchError =\n error instanceof Error ? error.message : 'Unknown error';\n state.quotesLoadingStatus = RequestStatus.ERROR;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuoteError,\n context,\n );\n console.log('Failed to fetch bridge quotes', error);\n }\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messagingSystem);\n const { maxRefreshCount } = bridgeFeatureFlags;\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 // Update quote fetching stats\n const quotesLastFetched = Date.now();\n this.update((state) => {\n state.quotesInitialLoadTime =\n state.quotesRefreshCount === 0 && this.#quotesFirstFetched\n ? quotesLastFetched - this.#quotesFirstFetched\n : this.state.quotesInitialLoadTime;\n state.quotesLastFetched = quotesLastFetched;\n state.quotesRefreshCount += 1;\n });\n };\n\n readonly #appendL1GasFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & L1GasFees)[] | undefined> => {\n // Indicates whether some of the quotes are not for optimism or base\n const hasInvalidQuotes = quotes.some(({ quote }) => {\n const chainId = formatChainIdToCaip(quote.srcChainId);\n return ![CHAIN_IDS.OPTIMISM, CHAIN_IDS.BASE]\n .map(formatChainIdToCaip)\n .includes(chainId);\n });\n\n // Only append L1 gas fees if all quotes are for either optimism or base\n if (!hasInvalidQuotes) {\n return await Promise.all(\n quotes.map(async (quoteResponse) => {\n const { quote, trade, approval } = quoteResponse;\n const chainId = numberToHex(quote.srcChainId) as ChainId;\n\n const getTxParams = (txData: TxData) => ({\n from: txData.from,\n to: txData.to,\n value: txData.value,\n data: txData.data,\n gasLimit: txData.gasLimit?.toString(),\n });\n const approvalL1GasFees = approval\n ? await this.#getLayer1GasFee({\n transactionParams: getTxParams(approval),\n chainId,\n })\n : '0';\n const tradeL1GasFees = await this.#getLayer1GasFee({\n transactionParams: getTxParams(trade),\n chainId,\n });\n return {\n ...quoteResponse,\n l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),\n };\n }),\n );\n }\n\n return undefined;\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n ) => {\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n try {\n if (isSolanaChainId(srcChainId) && selectedAccount?.metadata?.snap?.id) {\n const fees = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getMinimumBalanceForRentExemptionRequest(\n selectedAccount.metadata.snap?.id,\n ),\n )) as string;\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports = fees;\n });\n return;\n }\n } catch (error) {\n console.error('Error setting minimum balance for rent exemption', error);\n }\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n readonly #appendSolanaFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & SolanaFees)[] | undefined> => {\n // Return early if some of the quotes are not for solana\n if (\n quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId))\n ) {\n return undefined;\n }\n\n return await Promise.all(\n quotes.map(async (quoteResponse) => {\n const { trade } = quoteResponse;\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {\n const { value: fees } = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getFeeForTransactionRequest(\n selectedAccount.metadata.snap?.id,\n trade,\n ),\n )) as { value: string };\n\n return {\n ...quoteResponse,\n solanaFeesInLamports: fees,\n };\n }\n return quoteResponse;\n }),\n );\n };\n\n #getMultichainSelectedAccount() {\n return this.messagingSystem.call(\n 'AccountsController:getSelectedMultichainAccount',\n );\n }\n\n #getSelectedNetworkClientId() {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return selectedNetworkClientId;\n }\n\n #getSelectedNetworkClient() {\n const selectedNetworkClientId = this.#getSelectedNetworkClientId();\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestParams = (): Omit<\n RequestParams,\n 'token_symbol_source' | 'token_symbol_destination'\n > => {\n const srcChainIdCaip = formatChainIdToCaip(\n this.state.quoteRequest.srcChainId ||\n this.#getSelectedNetworkClient().configuration.chainId,\n );\n return getRequestParams(this.state.quoteRequest, srcChainIdCaip);\n };\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n 'stx_enabled' | 'usd_amount_source' | 'security_warnings'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact'\n > => {\n return {\n can_submit: Boolean(this.state.quoteRequest.insufficientBal), // TODO check if balance is sufficient for network fees\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 action_type: getActionTypeFromQuoteRequest(this.state.quoteRequest),\n ...propertiesFromClient,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...this.#getRequestParams(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuoteError:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\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 ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.SnapConfirmationViewed:\n return {\n ...baseProperties,\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n };\n // These are populated by BridgeStatusController\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n case UnifiedSwapBridgeEventName.Failed:\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 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',\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 provider = this.#getSelectedNetworkClient()?.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 { address: walletAddress } =\n this.#getMultichainSelectedAccount() ?? {};\n const allowance: BigNumber = await contract.allowance(\n walletAddress,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId],\n );\n return allowance.toString();\n };\n}\n"]}
|
1
|
+
{"version":3,"file":"bridge-controller.cjs","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,wDAAoD;AACpD,wDAAwD;AAGxD,mEA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4CAkwBC;;IAjNG,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,iDAAiD,CAClD,CAAC;AACJ,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,OAAO,uBAAuB,CAAC;AACjC,CAAC;IAGC,MAAM,uBAAuB,GAAG,uBAAA,IAAI,iFAA4B,MAAhC,IAAI,CAA8B,CAAC;IACnE,MAAM,aAAa,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,wCAAwC,EACxC,uBAAuB,CACxB,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 { ChainId, TraceCallback } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionController } from '@metamask/transaction-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport { numberToHex, type Hex } from '@metamask/utils';\n\nimport {\n type BridgeClientId,\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 { CHAIN_IDS } from './constants/chains';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type SolanaFees,\n type QuoteResponse,\n type TxData,\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 isSolanaChainId,\n sumHexes,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport { getBridgeFeatureFlags } from './utils/feature-flags';\nimport { fetchAssetPrices, fetchBridgeQuotes } from './utils/fetch';\nimport { UnifiedSwapBridgeEventName } from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getActionTypeFromQuoteRequest,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequestParams,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest } from './utils/quote';\nimport {\n getFeeForTransactionRequest,\n getMinimumBalanceForRentExemptionRequest,\n} from './utils/snaps';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n persist: false,\n anonymous: false,\n },\n quotes: {\n persist: false,\n anonymous: false,\n },\n quotesInitialLoadTime: {\n persist: false,\n anonymous: false,\n },\n quotesLastFetched: {\n persist: false,\n anonymous: false,\n },\n quotesLoadingStatus: {\n persist: false,\n anonymous: false,\n },\n quoteFetchError: {\n persist: false,\n anonymous: false,\n },\n quotesRefreshCount: {\n persist: false,\n anonymous: false,\n },\n assetExchangeRates: {\n persist: false,\n anonymous: false,\n },\n minimumBalanceForRentExemptionInLamports: {\n persist: false,\n anonymous: false,\n },\n};\n\nconst RESET_STATE_ABORT_MESSAGE = 'Reset controller state';\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param networkClientId - The network client ID of the selected network\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 networkClientId: NetworkClientId;\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuoteError\n >[UnifiedSwapBridgeEventName.QuoteError] &\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: 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 getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\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.#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.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`,\n this.getBridgeERC20Allowance.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.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 context: BridgePollingInput['context'],\n ) => {\n this.stopAllPolling();\n this.#abortController?.abort('Quote request updated');\n\n this.#trackInputChangedEvents(paramsToUpdate);\n\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n\n if (\n paramsToUpdate.srcChainId &&\n paramsToUpdate.srcChainId !== this.state.quoteRequest.srcChainId\n ) {\n await this.#setMinimumBalanceForRentExemptionInLamports(\n paramsToUpdate.srcChainId,\n );\n }\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\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.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\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 providerConfig = this.#getSelectedNetworkClient()?.configuration;\n\n let insufficientBal: boolean | undefined;\n if (isSolanaChainId(updatedQuoteRequest.srcChainId)) {\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 // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n !(await this.#hasSufficientBalance(updatedQuoteRequest));\n }\n\n const networkClientId = this.#getSelectedNetworkClientId();\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n networkClientId,\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n },\n context,\n });\n }\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messagingSystem.call('MultichainAssetsRatesController:getState'),\n ...this.messagingSystem.call('CurrencyRateController:getState'),\n ...this.messagingSystem.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.messagingSystem.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 fetchFn: this.#fetchFn,\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 walletAddress = this.#getMultichainSelectedAccount()?.address;\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider = this.#getSelectedNetworkClient()?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return (\n provider &&\n walletAddress &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n };\n\n resetState = () => {\n this.stopAllPolling();\n this.#abortController?.abort(RESET_STATE_ABORT_MESSAGE);\n\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.messagingSystem);\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 networkClientId: _networkClientId,\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 this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n });\n\n const fetchQuotes = async () => {\n const quotes = await fetchBridgeQuotes(\n updatedQuoteRequest,\n // AbortController is always defined by this line, because we assign it a few lines above,\n // not sure why Jest thinks it's not\n // Linters accurately say that it's defined\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#abortController!.signal as AbortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n );\n\n const quotesWithL1GasFees = await this.#appendL1GasFees(quotes);\n const quotesWithSolanaFees = await this.#appendSolanaFees(quotes);\n\n this.update((state) => {\n state.quotes = quotesWithL1GasFees ?? quotesWithSolanaFees ?? quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\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 fetchQuotes,\n );\n await this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n );\n } catch (error) {\n const isAbortError = (error as Error).name === 'AbortError';\n const isAbortedDueToReset = error === RESET_STATE_ABORT_MESSAGE;\n if (isAbortedDueToReset || isAbortError) {\n // Exit the function early to avoid other state updates\n return;\n }\n\n this.update((state) => {\n state.quoteFetchError =\n error instanceof Error ? error.message : 'Unknown error';\n state.quotesLoadingStatus = RequestStatus.ERROR;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuoteError,\n context,\n );\n console.log('Failed to fetch bridge quotes', error);\n }\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messagingSystem);\n const { maxRefreshCount } = bridgeFeatureFlags;\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 // Update quote fetching stats\n const quotesLastFetched = Date.now();\n this.update((state) => {\n state.quotesInitialLoadTime =\n state.quotesRefreshCount === 0 && this.#quotesFirstFetched\n ? quotesLastFetched - this.#quotesFirstFetched\n : this.state.quotesInitialLoadTime;\n state.quotesLastFetched = quotesLastFetched;\n state.quotesRefreshCount += 1;\n });\n };\n\n readonly #appendL1GasFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & L1GasFees)[] | undefined> => {\n // Indicates whether some of the quotes are not for optimism or base\n const hasInvalidQuotes = quotes.some(({ quote }) => {\n const chainId = formatChainIdToCaip(quote.srcChainId);\n return ![CHAIN_IDS.OPTIMISM, CHAIN_IDS.BASE]\n .map(formatChainIdToCaip)\n .includes(chainId);\n });\n\n // Only append L1 gas fees if all quotes are for either optimism or base\n if (hasInvalidQuotes) {\n return undefined;\n }\n\n const l1GasFeePromises = Promise.allSettled(\n quotes.map(async (quoteResponse) => {\n const { quote, trade, approval } = quoteResponse;\n const chainId = numberToHex(quote.srcChainId) as ChainId;\n\n const getTxParams = (txData: TxData) => ({\n from: txData.from,\n to: txData.to,\n value: txData.value,\n data: txData.data,\n gasLimit: txData.gasLimit?.toString(),\n });\n const approvalL1GasFees = approval\n ? await this.#getLayer1GasFee({\n transactionParams: getTxParams(approval),\n chainId,\n })\n : '0x0';\n const tradeL1GasFees = await this.#getLayer1GasFee({\n transactionParams: getTxParams(trade),\n chainId,\n });\n\n if (approvalL1GasFees === undefined || tradeL1GasFees === undefined) {\n return undefined;\n }\n\n return {\n ...quoteResponse,\n l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),\n };\n }),\n );\n\n const quotesWithL1GasFees = (await l1GasFeePromises).reduce<\n (QuoteResponse & L1GasFees)[]\n >((acc, result) => {\n if (result.status === 'fulfilled' && result.value) {\n acc.push(result.value);\n } else if (result.status === 'rejected') {\n console.error('Error calculating L1 gas fees for quote', result.reason);\n }\n return acc;\n }, []);\n\n return quotesWithL1GasFees;\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n ) => {\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n try {\n if (isSolanaChainId(srcChainId) && selectedAccount?.metadata?.snap?.id) {\n const fees = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getMinimumBalanceForRentExemptionRequest(\n selectedAccount.metadata.snap?.id,\n ),\n )) as string;\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports = fees;\n });\n return;\n }\n } catch (error) {\n console.error('Error setting minimum balance for rent exemption', error);\n }\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n readonly #appendSolanaFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & SolanaFees)[] | undefined> => {\n // Return early if some of the quotes are not for solana\n if (\n quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId))\n ) {\n return undefined;\n }\n\n const solanaFeePromises = Promise.allSettled(\n quotes.map(async (quoteResponse) => {\n const { trade } = quoteResponse;\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {\n const { value: fees } = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getFeeForTransactionRequest(\n selectedAccount.metadata.snap?.id,\n trade,\n ),\n )) as { value: string };\n\n return {\n ...quoteResponse,\n solanaFeesInLamports: fees,\n };\n }\n return quoteResponse;\n }),\n );\n\n const quotesWithSolanaFees = (await solanaFeePromises).reduce<\n (QuoteResponse & SolanaFees)[]\n >((acc, result) => {\n if (result.status === 'fulfilled' && result.value) {\n acc.push(result.value);\n } else if (result.status === 'rejected') {\n console.error('Error calculating solana fees for quote', result.reason);\n }\n return acc;\n }, []);\n\n return quotesWithSolanaFees;\n };\n\n #getMultichainSelectedAccount() {\n return this.messagingSystem.call(\n 'AccountsController:getSelectedMultichainAccount',\n );\n }\n\n #getSelectedNetworkClientId() {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return selectedNetworkClientId;\n }\n\n #getSelectedNetworkClient() {\n const selectedNetworkClientId = this.#getSelectedNetworkClientId();\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestParams = (): Omit<\n RequestParams,\n 'token_symbol_source' | 'token_symbol_destination'\n > => {\n const srcChainIdCaip = formatChainIdToCaip(\n this.state.quoteRequest.srcChainId ||\n this.#getSelectedNetworkClient().configuration.chainId,\n );\n return getRequestParams(this.state.quoteRequest, srcChainIdCaip);\n };\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n 'stx_enabled' | 'usd_amount_source' | 'security_warnings'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact'\n > => {\n return {\n can_submit: Boolean(this.state.quoteRequest.insufficientBal), // TODO check if balance is sufficient for network fees\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 action_type: getActionTypeFromQuoteRequest(this.state.quoteRequest),\n ...propertiesFromClient,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...this.#getRequestParams(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuoteError:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\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 ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.SnapConfirmationViewed:\n return {\n ...baseProperties,\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n };\n // These are populated by BridgeStatusController\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n case UnifiedSwapBridgeEventName.Failed:\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 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',\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 provider = this.#getSelectedNetworkClient()?.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 { address: walletAddress } =\n this.#getMultichainSelectedAccount() ?? {};\n const allowance: BigNumber = await contract.allowance(\n walletAddress,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId],\n );\n return allowance.toString();\n };\n}\n"]}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import type {
|
1
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
2
2
|
import type { NetworkClientId } from "@metamask/network-controller";
|
3
|
-
import type {
|
3
|
+
import type { TransactionController } from "@metamask/transaction-controller";
|
4
4
|
import { type Hex } from "@metamask/utils";
|
5
5
|
import { type BridgeClientId, BRIDGE_CONTROLLER_NAME } from "./constants/bridge.cjs";
|
6
6
|
import { type GenericQuoteRequest, type BridgeControllerState, type BridgeControllerMessenger, type FetchFunction } from "./types.cjs";
|
@@ -41,10 +41,7 @@ export declare class BridgeController extends BridgeController_base<typeof BRIDG
|
|
41
41
|
messenger: BridgeControllerMessenger;
|
42
42
|
state?: Partial<BridgeControllerState>;
|
43
43
|
clientId: BridgeClientId;
|
44
|
-
getLayer1GasFee:
|
45
|
-
transactionParams: TransactionParams;
|
46
|
-
chainId: ChainId;
|
47
|
-
}) => Promise<string>;
|
44
|
+
getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;
|
48
45
|
fetchFn: FetchFunction;
|
49
46
|
config?: {
|
50
47
|
customBridgeApiBaseUrl?: string;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"bridge-controller.d.cts","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"bridge-controller.d.cts","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAW,aAAa,EAAE,mCAAmC;AAEzE,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AAEpE,OAAO,KAAK,EAAE,qBAAqB,EAAE,yCAAyC;AAE9E,OAAO,EAAe,KAAK,GAAG,EAAE,wBAAwB;AAExD,OAAO,EACL,KAAK,cAAc,EACnB,sBAAsB,EAKvB,+BAA2B;AAK5B,OAAO,EAEL,KAAK,mBAAmB,EAIxB,KAAK,qBAAqB,EAC1B,KA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AC;KACzB;IA4CD,YAAY,iBAAwB,kBAAkB,mBAEpD;IAEF,8BAA8B,mBACZ,QAAQ,mBAAmB,CAAC,WACnC,kBAAkB,CAAC,SAAS,CAAC,mBAsEtC;IAyGF,UAAU,aAsBR;IAEF;;OAEG;IACH,sBAAsB,aAUpB;IA4YF;;;;;;;;;OASG;IACH,2BAA2B,iIAoBzB;IAEF;;;;;OAKG;IACH,uBAAuB,oBACJ,MAAM,WACd,GAAG,KACX,QAAQ,MAAM,CAAC,CAehB;CACH"}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import type {
|
1
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
2
2
|
import type { NetworkClientId } from "@metamask/network-controller";
|
3
|
-
import type {
|
3
|
+
import type { TransactionController } from "@metamask/transaction-controller";
|
4
4
|
import { type Hex } from "@metamask/utils";
|
5
5
|
import { type BridgeClientId, BRIDGE_CONTROLLER_NAME } from "./constants/bridge.mjs";
|
6
6
|
import { type GenericQuoteRequest, type BridgeControllerState, type BridgeControllerMessenger, type FetchFunction } from "./types.mjs";
|
@@ -41,10 +41,7 @@ export declare class BridgeController extends BridgeController_base<typeof BRIDG
|
|
41
41
|
messenger: BridgeControllerMessenger;
|
42
42
|
state?: Partial<BridgeControllerState>;
|
43
43
|
clientId: BridgeClientId;
|
44
|
-
getLayer1GasFee:
|
45
|
-
transactionParams: TransactionParams;
|
46
|
-
chainId: ChainId;
|
47
|
-
}) => Promise<string>;
|
44
|
+
getLayer1GasFee: typeof TransactionController.prototype.getLayer1GasFee;
|
48
45
|
fetchFn: FetchFunction;
|
49
46
|
config?: {
|
50
47
|
customBridgeApiBaseUrl?: string;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"bridge-controller.d.mts","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"bridge-controller.d.mts","sourceRoot":"","sources":["../src/bridge-controller.ts"],"names":[],"mappings":"AAIA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E,aAAa,CAAC;QACvB,MAAM,CAAC,EAAE;YACP,sBAAsB,CAAC,EAAE,MAAM,CAAC;SACjC,CAAC;QACF,kBAAkB,EAAE,CAClB,CAAC,SACC,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,OAAO,0BAA0B,CAAC,EAE9E,SAAS,EAAE,CAAC,EACZ,UAAU,EAAE,8BAA8B,CAAC,CAAC,CAAC,KAC1C,IAAI,CAAC;QACV,OAAO,CAAC,EAAE,aAAa,CAAC;KACzB;IA4CD,YAAY,iBAAwB,kBAAkB,mBAEpD;IAEF,8BAA8B,mBACZ,QAAQ,mBAAmB,CAAC,WACnC,kBAAkB,CAAC,SAAS,CAAC,mBAsEtC;IAyGF,UAAU,aAsBR;IAEF;;OAEG;IACH,sBAAsB,aAUpB;IA4YF;;;;;;;;;OASG;IACH,2BAA2B,iIAoBzB;IAEF;;;;;OAKG;IACH,uBAAuB,oBACJ,MAAM,WACd,GAAG,KACX,QAAQ,MAAM,CAAC,CAehB;CACH"}
|
@@ -325,34 +325,47 @@ export class BridgeController extends StaticIntervalPollingController() {
|
|
325
325
|
.includes(chainId);
|
326
326
|
});
|
327
327
|
// Only append L1 gas fees if all quotes are for either optimism or base
|
328
|
-
if (
|
329
|
-
return
|
330
|
-
const { quote, trade, approval } = quoteResponse;
|
331
|
-
const chainId = numberToHex(quote.srcChainId);
|
332
|
-
const getTxParams = (txData) => ({
|
333
|
-
from: txData.from,
|
334
|
-
to: txData.to,
|
335
|
-
value: txData.value,
|
336
|
-
data: txData.data,
|
337
|
-
gasLimit: txData.gasLimit?.toString(),
|
338
|
-
});
|
339
|
-
const approvalL1GasFees = approval
|
340
|
-
? await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
341
|
-
transactionParams: getTxParams(approval),
|
342
|
-
chainId,
|
343
|
-
})
|
344
|
-
: '0';
|
345
|
-
const tradeL1GasFees = await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
346
|
-
transactionParams: getTxParams(trade),
|
347
|
-
chainId,
|
348
|
-
});
|
349
|
-
return {
|
350
|
-
...quoteResponse,
|
351
|
-
l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),
|
352
|
-
};
|
353
|
-
}));
|
328
|
+
if (hasInvalidQuotes) {
|
329
|
+
return undefined;
|
354
330
|
}
|
355
|
-
|
331
|
+
const l1GasFeePromises = Promise.allSettled(quotes.map(async (quoteResponse) => {
|
332
|
+
const { quote, trade, approval } = quoteResponse;
|
333
|
+
const chainId = numberToHex(quote.srcChainId);
|
334
|
+
const getTxParams = (txData) => ({
|
335
|
+
from: txData.from,
|
336
|
+
to: txData.to,
|
337
|
+
value: txData.value,
|
338
|
+
data: txData.data,
|
339
|
+
gasLimit: txData.gasLimit?.toString(),
|
340
|
+
});
|
341
|
+
const approvalL1GasFees = approval
|
342
|
+
? await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
343
|
+
transactionParams: getTxParams(approval),
|
344
|
+
chainId,
|
345
|
+
})
|
346
|
+
: '0x0';
|
347
|
+
const tradeL1GasFees = await __classPrivateFieldGet(this, _BridgeController_getLayer1GasFee, "f").call(this, {
|
348
|
+
transactionParams: getTxParams(trade),
|
349
|
+
chainId,
|
350
|
+
});
|
351
|
+
if (approvalL1GasFees === undefined || tradeL1GasFees === undefined) {
|
352
|
+
return undefined;
|
353
|
+
}
|
354
|
+
return {
|
355
|
+
...quoteResponse,
|
356
|
+
l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),
|
357
|
+
};
|
358
|
+
}));
|
359
|
+
const quotesWithL1GasFees = (await l1GasFeePromises).reduce((acc, result) => {
|
360
|
+
if (result.status === 'fulfilled' && result.value) {
|
361
|
+
acc.push(result.value);
|
362
|
+
}
|
363
|
+
else if (result.status === 'rejected') {
|
364
|
+
console.error('Error calculating L1 gas fees for quote', result.reason);
|
365
|
+
}
|
366
|
+
return acc;
|
367
|
+
}, []);
|
368
|
+
return quotesWithL1GasFees;
|
356
369
|
});
|
357
370
|
_BridgeController_setMinimumBalanceForRentExemptionInLamports.set(this, async (srcChainId) => {
|
358
371
|
const selectedAccount = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this);
|
@@ -378,7 +391,7 @@ export class BridgeController extends StaticIntervalPollingController() {
|
|
378
391
|
if (quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId))) {
|
379
392
|
return undefined;
|
380
393
|
}
|
381
|
-
|
394
|
+
const solanaFeePromises = Promise.allSettled(quotes.map(async (quoteResponse) => {
|
382
395
|
const { trade } = quoteResponse;
|
383
396
|
const selectedAccount = __classPrivateFieldGet(this, _BridgeController_instances, "m", _BridgeController_getMultichainSelectedAccount).call(this);
|
384
397
|
if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {
|
@@ -390,6 +403,16 @@ export class BridgeController extends StaticIntervalPollingController() {
|
|
390
403
|
}
|
391
404
|
return quoteResponse;
|
392
405
|
}));
|
406
|
+
const quotesWithSolanaFees = (await solanaFeePromises).reduce((acc, result) => {
|
407
|
+
if (result.status === 'fulfilled' && result.value) {
|
408
|
+
acc.push(result.value);
|
409
|
+
}
|
410
|
+
else if (result.status === 'rejected') {
|
411
|
+
console.error('Error calculating solana fees for quote', result.reason);
|
412
|
+
}
|
413
|
+
return acc;
|
414
|
+
}, []);
|
415
|
+
return quotesWithSolanaFees;
|
393
416
|
});
|
394
417
|
_BridgeController_getRequestParams.set(this, () => {
|
395
418
|
const srcChainIdCaip = formatChainIdToCaip(this.state.quoteRequest.srcChainId ||
|
@@ -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;AAGxD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;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CACxC,GAAG,sBAAsB,yBAAyB,EAClD,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,CACvC,CAAC;QACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAsB,iCAAiC,EAC1D,IAAI,CAAC,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC/C,CAAC;QACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAsB,aAAa,EACtC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3B,CAAC;QACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAsB,0BAA0B,EACnD,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;QACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAsB,8BAA8B,EACvD,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC5C,CAAC;IACJ,CAAC;CAkoBF;;IAjNG,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,iDAAiD,CAClD,CAAC;AACJ,CAAC;IAGC,MAAM,EAAE,uBAAuB,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3D,4BAA4B,CAC7B,CAAC;IACF,OAAO,uBAAuB,CAAC;AACjC,CAAC;IAGC,MAAM,uBAAuB,GAAG,uBAAA,IAAI,iFAA4B,MAAhC,IAAI,CAA8B,CAAC;IACnE,MAAM,aAAa,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,wCAAwC,EACxC,uBAAuB,CACxB,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 { ChainId, TraceCallback } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport { numberToHex, type Hex } from '@metamask/utils';\n\nimport {\n type BridgeClientId,\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 { CHAIN_IDS } from './constants/chains';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type SolanaFees,\n type QuoteResponse,\n type TxData,\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 isSolanaChainId,\n sumHexes,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport { getBridgeFeatureFlags } from './utils/feature-flags';\nimport { fetchAssetPrices, fetchBridgeQuotes } from './utils/fetch';\nimport { UnifiedSwapBridgeEventName } from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getActionTypeFromQuoteRequest,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequestParams,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest } from './utils/quote';\nimport {\n getFeeForTransactionRequest,\n getMinimumBalanceForRentExemptionRequest,\n} from './utils/snaps';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n persist: false,\n anonymous: false,\n },\n quotes: {\n persist: false,\n anonymous: false,\n },\n quotesInitialLoadTime: {\n persist: false,\n anonymous: false,\n },\n quotesLastFetched: {\n persist: false,\n anonymous: false,\n },\n quotesLoadingStatus: {\n persist: false,\n anonymous: false,\n },\n quoteFetchError: {\n persist: false,\n anonymous: false,\n },\n quotesRefreshCount: {\n persist: false,\n anonymous: false,\n },\n assetExchangeRates: {\n persist: false,\n anonymous: false,\n },\n minimumBalanceForRentExemptionInLamports: {\n persist: false,\n anonymous: false,\n },\n};\n\nconst RESET_STATE_ABORT_MESSAGE = 'Reset controller state';\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param networkClientId - The network client ID of the selected network\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 networkClientId: NetworkClientId;\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuoteError\n >[UnifiedSwapBridgeEventName.QuoteError] &\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: string;\n\n readonly #getLayer1GasFee: (params: {\n transactionParams: TransactionParams;\n chainId: ChainId;\n }) => Promise<string>;\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 getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\n getLayer1GasFee: (params: {\n transactionParams: TransactionParams;\n chainId: ChainId;\n }) => Promise<string>;\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.#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.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`,\n this.getBridgeERC20Allowance.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.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 context: BridgePollingInput['context'],\n ) => {\n this.stopAllPolling();\n this.#abortController?.abort('Quote request updated');\n\n this.#trackInputChangedEvents(paramsToUpdate);\n\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n\n if (\n paramsToUpdate.srcChainId &&\n paramsToUpdate.srcChainId !== this.state.quoteRequest.srcChainId\n ) {\n await this.#setMinimumBalanceForRentExemptionInLamports(\n paramsToUpdate.srcChainId,\n );\n }\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\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.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\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 providerConfig = this.#getSelectedNetworkClient()?.configuration;\n\n let insufficientBal: boolean | undefined;\n if (isSolanaChainId(updatedQuoteRequest.srcChainId)) {\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 // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n !(await this.#hasSufficientBalance(updatedQuoteRequest));\n }\n\n const networkClientId = this.#getSelectedNetworkClientId();\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n networkClientId,\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n },\n context,\n });\n }\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messagingSystem.call('MultichainAssetsRatesController:getState'),\n ...this.messagingSystem.call('CurrencyRateController:getState'),\n ...this.messagingSystem.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.messagingSystem.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 fetchFn: this.#fetchFn,\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 walletAddress = this.#getMultichainSelectedAccount()?.address;\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider = this.#getSelectedNetworkClient()?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return (\n provider &&\n walletAddress &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n };\n\n resetState = () => {\n this.stopAllPolling();\n this.#abortController?.abort(RESET_STATE_ABORT_MESSAGE);\n\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.messagingSystem);\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 networkClientId: _networkClientId,\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 this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n });\n\n const fetchQuotes = async () => {\n const quotes = await fetchBridgeQuotes(\n updatedQuoteRequest,\n // AbortController is always defined by this line, because we assign it a few lines above,\n // not sure why Jest thinks it's not\n // Linters accurately say that it's defined\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#abortController!.signal as AbortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n );\n\n const quotesWithL1GasFees = await this.#appendL1GasFees(quotes);\n const quotesWithSolanaFees = await this.#appendSolanaFees(quotes);\n\n this.update((state) => {\n state.quotes = quotesWithL1GasFees ?? quotesWithSolanaFees ?? quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\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 fetchQuotes,\n );\n await this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n );\n } catch (error) {\n const isAbortError = (error as Error).name === 'AbortError';\n const isAbortedDueToReset = error === RESET_STATE_ABORT_MESSAGE;\n if (isAbortedDueToReset || isAbortError) {\n // Exit the function early to avoid other state updates\n return;\n }\n\n this.update((state) => {\n state.quoteFetchError =\n error instanceof Error ? error.message : 'Unknown error';\n state.quotesLoadingStatus = RequestStatus.ERROR;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuoteError,\n context,\n );\n console.log('Failed to fetch bridge quotes', error);\n }\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messagingSystem);\n const { maxRefreshCount } = bridgeFeatureFlags;\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 // Update quote fetching stats\n const quotesLastFetched = Date.now();\n this.update((state) => {\n state.quotesInitialLoadTime =\n state.quotesRefreshCount === 0 && this.#quotesFirstFetched\n ? quotesLastFetched - this.#quotesFirstFetched\n : this.state.quotesInitialLoadTime;\n state.quotesLastFetched = quotesLastFetched;\n state.quotesRefreshCount += 1;\n });\n };\n\n readonly #appendL1GasFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & L1GasFees)[] | undefined> => {\n // Indicates whether some of the quotes are not for optimism or base\n const hasInvalidQuotes = quotes.some(({ quote }) => {\n const chainId = formatChainIdToCaip(quote.srcChainId);\n return ![CHAIN_IDS.OPTIMISM, CHAIN_IDS.BASE]\n .map(formatChainIdToCaip)\n .includes(chainId);\n });\n\n // Only append L1 gas fees if all quotes are for either optimism or base\n if (!hasInvalidQuotes) {\n return await Promise.all(\n quotes.map(async (quoteResponse) => {\n const { quote, trade, approval } = quoteResponse;\n const chainId = numberToHex(quote.srcChainId) as ChainId;\n\n const getTxParams = (txData: TxData) => ({\n from: txData.from,\n to: txData.to,\n value: txData.value,\n data: txData.data,\n gasLimit: txData.gasLimit?.toString(),\n });\n const approvalL1GasFees = approval\n ? await this.#getLayer1GasFee({\n transactionParams: getTxParams(approval),\n chainId,\n })\n : '0';\n const tradeL1GasFees = await this.#getLayer1GasFee({\n transactionParams: getTxParams(trade),\n chainId,\n });\n return {\n ...quoteResponse,\n l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),\n };\n }),\n );\n }\n\n return undefined;\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n ) => {\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n try {\n if (isSolanaChainId(srcChainId) && selectedAccount?.metadata?.snap?.id) {\n const fees = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getMinimumBalanceForRentExemptionRequest(\n selectedAccount.metadata.snap?.id,\n ),\n )) as string;\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports = fees;\n });\n return;\n }\n } catch (error) {\n console.error('Error setting minimum balance for rent exemption', error);\n }\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n readonly #appendSolanaFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & SolanaFees)[] | undefined> => {\n // Return early if some of the quotes are not for solana\n if (\n quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId))\n ) {\n return undefined;\n }\n\n return await Promise.all(\n quotes.map(async (quoteResponse) => {\n const { trade } = quoteResponse;\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {\n const { value: fees } = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getFeeForTransactionRequest(\n selectedAccount.metadata.snap?.id,\n trade,\n ),\n )) as { value: string };\n\n return {\n ...quoteResponse,\n solanaFeesInLamports: fees,\n };\n }\n return quoteResponse;\n }),\n );\n };\n\n #getMultichainSelectedAccount() {\n return this.messagingSystem.call(\n 'AccountsController:getSelectedMultichainAccount',\n );\n }\n\n #getSelectedNetworkClientId() {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return selectedNetworkClientId;\n }\n\n #getSelectedNetworkClient() {\n const selectedNetworkClientId = this.#getSelectedNetworkClientId();\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestParams = (): Omit<\n RequestParams,\n 'token_symbol_source' | 'token_symbol_destination'\n > => {\n const srcChainIdCaip = formatChainIdToCaip(\n this.state.quoteRequest.srcChainId ||\n this.#getSelectedNetworkClient().configuration.chainId,\n );\n return getRequestParams(this.state.quoteRequest, srcChainIdCaip);\n };\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n 'stx_enabled' | 'usd_amount_source' | 'security_warnings'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact'\n > => {\n return {\n can_submit: Boolean(this.state.quoteRequest.insufficientBal), // TODO check if balance is sufficient for network fees\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 action_type: getActionTypeFromQuoteRequest(this.state.quoteRequest),\n ...propertiesFromClient,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...this.#getRequestParams(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuoteError:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\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 ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.SnapConfirmationViewed:\n return {\n ...baseProperties,\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n };\n // These are populated by BridgeStatusController\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n case UnifiedSwapBridgeEventName.Failed:\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 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',\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 provider = this.#getSelectedNetworkClient()?.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 { address: walletAddress } =\n this.#getMultichainSelectedAccount() ?? {};\n const allowance: BigNumber = await contract.allowance(\n walletAddress,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId],\n );\n return allowance.toString();\n };\n}\n"]}
|
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;AAGxD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAEvD,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAG/E,OAAO,EAAE,WAAW,EAAY,wBAAwB;AAExD,OAAO,EAEL,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,SAAS,EAAE,+BAA2B;AAC/C,OAAO,EAAE,gCAAgC,EAAE,wBAAoB;AAE/D,OAAO,EASL,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,QAAQ,EACT,2BAAuB;AACxB,OAAO,EACL,4BAA4B,EAC5B,mBAAmB,EACnB,kBAAkB,EACnB,oCAAgC;AACjC,OAAO,EAAE,qBAAqB,EAAE,kCAA8B;AAC9D,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,0BAAsB;AACpE,OAAO,EAAE,0BAA0B,EAAE,sCAAkC;AACvE,OAAO,EACL,mBAAmB,EACnB,6BAA6B,EAC7B,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB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,eAAe,CAAC,IAAI,CAC7C,wCAAwC,EACxC,uBAAuB,CACxB,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 { ChainId, TraceCallback } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionController } from '@metamask/transaction-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport { numberToHex, type Hex } from '@metamask/utils';\n\nimport {\n type BridgeClientId,\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 { CHAIN_IDS } from './constants/chains';\nimport { TraceName } from './constants/traces';\nimport { selectIsAssetExchangeRateInState } from './selectors';\nimport type { QuoteRequest } from './types';\nimport {\n type L1GasFees,\n type GenericQuoteRequest,\n type SolanaFees,\n type QuoteResponse,\n type TxData,\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 isSolanaChainId,\n sumHexes,\n} from './utils/bridge';\nimport {\n formatAddressToCaipReference,\n formatChainIdToCaip,\n formatChainIdToHex,\n} from './utils/caip-formatters';\nimport { getBridgeFeatureFlags } from './utils/feature-flags';\nimport { fetchAssetPrices, fetchBridgeQuotes } from './utils/fetch';\nimport { UnifiedSwapBridgeEventName } from './utils/metrics/constants';\nimport {\n formatProviderLabel,\n getActionTypeFromQuoteRequest,\n getRequestParams,\n getSwapTypeFromQuote,\n isCustomSlippage,\n isHardwareWallet,\n toInputChangedPropertyKey,\n toInputChangedPropertyValue,\n} from './utils/metrics/properties';\nimport type {\n QuoteFetchData,\n RequestMetadata,\n RequestParams,\n RequiredEventContextFromClient,\n} from './utils/metrics/types';\nimport { type CrossChainSwapsEventProperties } from './utils/metrics/types';\nimport { isValidQuoteRequest } from './utils/quote';\nimport {\n getFeeForTransactionRequest,\n getMinimumBalanceForRentExemptionRequest,\n} from './utils/snaps';\n\nconst metadata: StateMetadata<BridgeControllerState> = {\n quoteRequest: {\n persist: false,\n anonymous: false,\n },\n quotes: {\n persist: false,\n anonymous: false,\n },\n quotesInitialLoadTime: {\n persist: false,\n anonymous: false,\n },\n quotesLastFetched: {\n persist: false,\n anonymous: false,\n },\n quotesLoadingStatus: {\n persist: false,\n anonymous: false,\n },\n quoteFetchError: {\n persist: false,\n anonymous: false,\n },\n quotesRefreshCount: {\n persist: false,\n anonymous: false,\n },\n assetExchangeRates: {\n persist: false,\n anonymous: false,\n },\n minimumBalanceForRentExemptionInLamports: {\n persist: false,\n anonymous: false,\n },\n};\n\nconst RESET_STATE_ABORT_MESSAGE = 'Reset controller state';\n\n/**\n * The input to start polling for the {@link BridgeController}\n *\n * @param networkClientId - The network client ID of the selected network\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 networkClientId: NetworkClientId;\n updatedQuoteRequest: GenericQuoteRequest;\n context: Pick<\n RequiredEventContextFromClient,\n UnifiedSwapBridgeEventName.QuoteError\n >[UnifiedSwapBridgeEventName.QuoteError] &\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: 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 getLayer1GasFee,\n fetchFn,\n config,\n trackMetaMetricsFn,\n traceFn,\n }: {\n messenger: BridgeControllerMessenger;\n state?: Partial<BridgeControllerState>;\n clientId: BridgeClientId;\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.#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.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:setChainIntervalLength`,\n this.setChainIntervalLength.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,\n this.updateBridgeQuoteRequestParams.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:resetState`,\n this.resetState.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:getBridgeERC20Allowance`,\n this.getBridgeERC20Allowance.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${BRIDGE_CONTROLLER_NAME}:trackUnifiedSwapBridgeEvent`,\n this.trackUnifiedSwapBridgeEvent.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 context: BridgePollingInput['context'],\n ) => {\n this.stopAllPolling();\n this.#abortController?.abort('Quote request updated');\n\n this.#trackInputChangedEvents(paramsToUpdate);\n\n const updatedQuoteRequest = {\n ...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,\n ...paramsToUpdate,\n };\n\n if (\n paramsToUpdate.srcChainId &&\n paramsToUpdate.srcChainId !== this.state.quoteRequest.srcChainId\n ) {\n await this.#setMinimumBalanceForRentExemptionInLamports(\n paramsToUpdate.srcChainId,\n );\n }\n\n this.update((state) => {\n state.quoteRequest = updatedQuoteRequest;\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.quotesInitialLoadTime =\n DEFAULT_BRIDGE_CONTROLLER_STATE.quotesInitialLoadTime;\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 providerConfig = this.#getSelectedNetworkClient()?.configuration;\n\n let insufficientBal: boolean | undefined;\n if (isSolanaChainId(updatedQuoteRequest.srcChainId)) {\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 // Otherwise query the src token balance from the RPC provider\n insufficientBal =\n paramsToUpdate.insufficientBal ??\n !(await this.#hasSufficientBalance(updatedQuoteRequest));\n }\n\n const networkClientId = this.#getSelectedNetworkClientId();\n // Set refresh rate based on the source chain before starting polling\n this.setChainIntervalLength();\n this.startPolling({\n networkClientId,\n updatedQuoteRequest: {\n ...updatedQuoteRequest,\n insufficientBal,\n },\n context,\n });\n }\n };\n\n readonly #getExchangeRateSources = () => {\n return {\n ...this.messagingSystem.call('MultichainAssetsRatesController:getState'),\n ...this.messagingSystem.call('CurrencyRateController:getState'),\n ...this.messagingSystem.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.messagingSystem.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 fetchFn: this.#fetchFn,\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 walletAddress = this.#getMultichainSelectedAccount()?.address;\n const srcChainIdInHex = formatChainIdToHex(quoteRequest.srcChainId);\n const provider = this.#getSelectedNetworkClient()?.provider;\n const normalizedSrcTokenAddress = formatAddressToCaipReference(\n quoteRequest.srcTokenAddress,\n );\n\n return (\n provider &&\n walletAddress &&\n normalizedSrcTokenAddress &&\n quoteRequest.srcTokenAmount &&\n srcChainIdInHex &&\n (await hasSufficientBalance(\n provider,\n walletAddress,\n normalizedSrcTokenAddress,\n quoteRequest.srcTokenAmount,\n srcChainIdInHex,\n ))\n );\n };\n\n resetState = () => {\n this.stopAllPolling();\n this.#abortController?.abort(RESET_STATE_ABORT_MESSAGE);\n\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.messagingSystem);\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 networkClientId: _networkClientId,\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 this.update((state) => {\n state.quotesLoadingStatus = RequestStatus.LOADING;\n state.quoteRequest = updatedQuoteRequest;\n state.quoteFetchError = DEFAULT_BRIDGE_CONTROLLER_STATE.quoteFetchError;\n });\n\n const fetchQuotes = async () => {\n const quotes = await fetchBridgeQuotes(\n updatedQuoteRequest,\n // AbortController is always defined by this line, because we assign it a few lines above,\n // not sure why Jest thinks it's not\n // Linters accurately say that it's defined\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.#abortController!.signal as AbortSignal,\n this.#clientId,\n this.#fetchFn,\n this.#config.customBridgeApiBaseUrl ?? BRIDGE_PROD_API_BASE_URL,\n );\n\n const quotesWithL1GasFees = await this.#appendL1GasFees(quotes);\n const quotesWithSolanaFees = await this.#appendSolanaFees(quotes);\n\n this.update((state) => {\n state.quotes = quotesWithL1GasFees ?? quotesWithSolanaFees ?? quotes;\n state.quotesLoadingStatus = RequestStatus.FETCHED;\n });\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 fetchQuotes,\n );\n await this.#setMinimumBalanceForRentExemptionInLamports(\n updatedQuoteRequest.srcChainId,\n );\n } catch (error) {\n const isAbortError = (error as Error).name === 'AbortError';\n const isAbortedDueToReset = error === RESET_STATE_ABORT_MESSAGE;\n if (isAbortedDueToReset || isAbortError) {\n // Exit the function early to avoid other state updates\n return;\n }\n\n this.update((state) => {\n state.quoteFetchError =\n error instanceof Error ? error.message : 'Unknown error';\n state.quotesLoadingStatus = RequestStatus.ERROR;\n state.quotes = DEFAULT_BRIDGE_CONTROLLER_STATE.quotes;\n });\n this.trackUnifiedSwapBridgeEvent(\n UnifiedSwapBridgeEventName.QuoteError,\n context,\n );\n console.log('Failed to fetch bridge quotes', error);\n }\n const bridgeFeatureFlags = getBridgeFeatureFlags(this.messagingSystem);\n const { maxRefreshCount } = bridgeFeatureFlags;\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 // Update quote fetching stats\n const quotesLastFetched = Date.now();\n this.update((state) => {\n state.quotesInitialLoadTime =\n state.quotesRefreshCount === 0 && this.#quotesFirstFetched\n ? quotesLastFetched - this.#quotesFirstFetched\n : this.state.quotesInitialLoadTime;\n state.quotesLastFetched = quotesLastFetched;\n state.quotesRefreshCount += 1;\n });\n };\n\n readonly #appendL1GasFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & L1GasFees)[] | undefined> => {\n // Indicates whether some of the quotes are not for optimism or base\n const hasInvalidQuotes = quotes.some(({ quote }) => {\n const chainId = formatChainIdToCaip(quote.srcChainId);\n return ![CHAIN_IDS.OPTIMISM, CHAIN_IDS.BASE]\n .map(formatChainIdToCaip)\n .includes(chainId);\n });\n\n // Only append L1 gas fees if all quotes are for either optimism or base\n if (hasInvalidQuotes) {\n return undefined;\n }\n\n const l1GasFeePromises = Promise.allSettled(\n quotes.map(async (quoteResponse) => {\n const { quote, trade, approval } = quoteResponse;\n const chainId = numberToHex(quote.srcChainId) as ChainId;\n\n const getTxParams = (txData: TxData) => ({\n from: txData.from,\n to: txData.to,\n value: txData.value,\n data: txData.data,\n gasLimit: txData.gasLimit?.toString(),\n });\n const approvalL1GasFees = approval\n ? await this.#getLayer1GasFee({\n transactionParams: getTxParams(approval),\n chainId,\n })\n : '0x0';\n const tradeL1GasFees = await this.#getLayer1GasFee({\n transactionParams: getTxParams(trade),\n chainId,\n });\n\n if (approvalL1GasFees === undefined || tradeL1GasFees === undefined) {\n return undefined;\n }\n\n return {\n ...quoteResponse,\n l1GasFeesInHexWei: sumHexes(approvalL1GasFees, tradeL1GasFees),\n };\n }),\n );\n\n const quotesWithL1GasFees = (await l1GasFeePromises).reduce<\n (QuoteResponse & L1GasFees)[]\n >((acc, result) => {\n if (result.status === 'fulfilled' && result.value) {\n acc.push(result.value);\n } else if (result.status === 'rejected') {\n console.error('Error calculating L1 gas fees for quote', result.reason);\n }\n return acc;\n }, []);\n\n return quotesWithL1GasFees;\n };\n\n readonly #setMinimumBalanceForRentExemptionInLamports = async (\n srcChainId: GenericQuoteRequest['srcChainId'],\n ) => {\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n try {\n if (isSolanaChainId(srcChainId) && selectedAccount?.metadata?.snap?.id) {\n const fees = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getMinimumBalanceForRentExemptionRequest(\n selectedAccount.metadata.snap?.id,\n ),\n )) as string;\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports = fees;\n });\n return;\n }\n } catch (error) {\n console.error('Error setting minimum balance for rent exemption', error);\n }\n this.update((state) => {\n state.minimumBalanceForRentExemptionInLamports =\n DEFAULT_BRIDGE_CONTROLLER_STATE.minimumBalanceForRentExemptionInLamports;\n });\n };\n\n readonly #appendSolanaFees = async (\n quotes: QuoteResponse[],\n ): Promise<(QuoteResponse & SolanaFees)[] | undefined> => {\n // Return early if some of the quotes are not for solana\n if (\n quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId))\n ) {\n return undefined;\n }\n\n const solanaFeePromises = Promise.allSettled(\n quotes.map(async (quoteResponse) => {\n const { trade } = quoteResponse;\n const selectedAccount = this.#getMultichainSelectedAccount();\n\n if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') {\n const { value: fees } = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n getFeeForTransactionRequest(\n selectedAccount.metadata.snap?.id,\n trade,\n ),\n )) as { value: string };\n\n return {\n ...quoteResponse,\n solanaFeesInLamports: fees,\n };\n }\n return quoteResponse;\n }),\n );\n\n const quotesWithSolanaFees = (await solanaFeePromises).reduce<\n (QuoteResponse & SolanaFees)[]\n >((acc, result) => {\n if (result.status === 'fulfilled' && result.value) {\n acc.push(result.value);\n } else if (result.status === 'rejected') {\n console.error('Error calculating solana fees for quote', result.reason);\n }\n return acc;\n }, []);\n\n return quotesWithSolanaFees;\n };\n\n #getMultichainSelectedAccount() {\n return this.messagingSystem.call(\n 'AccountsController:getSelectedMultichainAccount',\n );\n }\n\n #getSelectedNetworkClientId() {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n return selectedNetworkClientId;\n }\n\n #getSelectedNetworkClient() {\n const selectedNetworkClientId = this.#getSelectedNetworkClientId();\n const networkClient = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return networkClient;\n }\n\n readonly #getRequestParams = (): Omit<\n RequestParams,\n 'token_symbol_source' | 'token_symbol_destination'\n > => {\n const srcChainIdCaip = formatChainIdToCaip(\n this.state.quoteRequest.srcChainId ||\n this.#getSelectedNetworkClient().configuration.chainId,\n );\n return getRequestParams(this.state.quoteRequest, srcChainIdCaip);\n };\n\n readonly #getRequestMetadata = (): Omit<\n RequestMetadata,\n 'stx_enabled' | 'usd_amount_source' | 'security_warnings'\n > => {\n return {\n slippage_limit: this.state.quoteRequest.slippage,\n swap_type: getSwapTypeFromQuote(this.state.quoteRequest),\n is_hardware_wallet: isHardwareWallet(\n this.#getMultichainSelectedAccount(),\n ),\n custom_slippage: isCustomSlippage(this.state.quoteRequest.slippage),\n };\n };\n\n readonly #getQuoteFetchData = (): Omit<\n QuoteFetchData,\n 'best_quote_provider' | 'price_impact'\n > => {\n return {\n can_submit: Boolean(this.state.quoteRequest.insufficientBal), // TODO check if balance is sufficient for network fees\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 action_type: getActionTypeFromQuoteRequest(this.state.quoteRequest),\n ...propertiesFromClient,\n };\n switch (eventName) {\n case UnifiedSwapBridgeEventName.ButtonClicked:\n case UnifiedSwapBridgeEventName.PageViewed:\n return {\n ...this.#getRequestParams(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesReceived:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n refresh_count: this.state.quotesRefreshCount,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuotesRequested:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n has_sufficient_funds: !this.state.quoteRequest.insufficientBal,\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.QuoteError:\n return {\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\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 ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n ...this.#getQuoteFetchData(),\n ...baseProperties,\n };\n case UnifiedSwapBridgeEventName.SnapConfirmationViewed:\n return {\n ...baseProperties,\n ...this.#getRequestParams(),\n ...this.#getRequestMetadata(),\n };\n // These are populated by BridgeStatusController\n case UnifiedSwapBridgeEventName.Submitted:\n case UnifiedSwapBridgeEventName.Completed:\n case UnifiedSwapBridgeEventName.Failed:\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 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',\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 provider = this.#getSelectedNetworkClient()?.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 { address: walletAddress } =\n this.#getMultichainSelectedAccount() ?? {};\n const allowance: BigNumber = await contract.allowance(\n walletAddress,\n METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId],\n );\n return allowance.toString();\n };\n}\n"]}
|
package/dist/utils/fetch.cjs
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.fetchAssetPrices = exports.fetchBridgeQuotes = exports.fetchBridgeTokens = exports.getClientIdHeader = void 0;
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
4
5
|
const utils_1 = require("@metamask/utils");
|
5
6
|
const caip_formatters_1 = require("./caip-formatters.cjs");
|
6
7
|
const validators_1 = require("./validators.cjs");
|
@@ -77,15 +78,28 @@ async function fetchBridgeQuotes(request, signal, clientId, fetchFn, bridgeApiBa
|
|
77
78
|
cacheOptions: { cacheRefreshTime: 0 },
|
78
79
|
functionName: 'fetchBridgeQuotes',
|
79
80
|
});
|
81
|
+
const validationFailuresByAggregator = {};
|
80
82
|
const filteredQuotes = quotes.filter((quoteResponse) => {
|
81
83
|
try {
|
82
84
|
return (0, validators_1.validateQuoteResponse)(quoteResponse);
|
83
85
|
}
|
84
86
|
catch (error) {
|
85
|
-
|
87
|
+
if (error instanceof superstruct_1.StructError) {
|
88
|
+
error.failures().forEach(({ branch, path }) => {
|
89
|
+
const aggregatorId = branch?.[0]?.quote?.bridgeId;
|
90
|
+
if (!validationFailuresByAggregator[aggregatorId]) {
|
91
|
+
validationFailuresByAggregator[aggregatorId] = new Set([]);
|
92
|
+
}
|
93
|
+
const pathString = path?.join('.') || 'unknown';
|
94
|
+
validationFailuresByAggregator[aggregatorId].add(pathString);
|
95
|
+
});
|
96
|
+
}
|
86
97
|
return false;
|
87
98
|
}
|
88
99
|
});
|
100
|
+
if (Object.keys(validationFailuresByAggregator).length > 0) {
|
101
|
+
console.error('Quote validation failed', validationFailuresByAggregator);
|
102
|
+
}
|
89
103
|
return filteredQuotes;
|
90
104
|
}
|
91
105
|
exports.fetchBridgeQuotes = fetchBridgeQuotes;
|
package/dist/utils/fetch.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":";;;AACA,2CAA2C;AAE3C,2DAG2B;AAC3B,iDAA+E;AAS/E,MAAM,yBAAyB,GAAG,EAAE,GAAG,gBAAQ,CAAC,MAAM,CAAC;AAEhD,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,CAAC;IACtD,aAAa,EAAE,QAAQ;CACxB,CAAC,CAAC;AAFU,QAAA,iBAAiB,qBAE3B;AAEH;;;;;;;;GA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C;aACJ;YACD,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAA2D,CAC5D,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAnCW,QAAA,gBAAgB,oBAmC3B","sourcesContent":["import type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\nimport { Duration } from '@metamask/utils';\n\nimport {\n formatAddressToCaipReference,\n formatChainIdToDec,\n} from './caip-formatters';\nimport { validateQuoteResponse, validateSwapsTokenObject } from './validators';\nimport type {\n QuoteResponse,\n FetchFunction,\n GenericQuoteRequest,\n QuoteRequest,\n BridgeAsset,\n} from '../types';\n\nconst CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;\n\nexport const getClientIdHeader = (clientId: string) => ({\n 'X-Client-Id': clientId,\n});\n\n/**\n * Returns a list of enabled (unblocked) tokens\n *\n * @param chainId - The chain ID to fetch tokens for\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of enabled (unblocked) tokens\n */\nexport async function fetchBridgeTokens(\n chainId: Hex | CaipChainId,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<Record<string, BridgeAsset>> {\n // TODO make token api v2 call\n const url = `${bridgeApiBaseUrl}/getTokens?chainId=${formatChainIdToDec(chainId)}`;\n\n // TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:\n // If we allow selecting dest networks which the user has not imported,\n // note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this\n const tokens = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },\n functionName: 'fetchBridgeTokens',\n });\n\n const transformedTokens: Record<string, BridgeAsset> = {};\n tokens.forEach((token: unknown) => {\n if (validateSwapsTokenObject(token)) {\n transformedTokens[token.address] = token;\n }\n });\n return transformedTokens;\n}\n\n/**\n * Converts the generic quote request to the type that the bridge-api expects\n * then fetches quotes from the bridge-api\n *\n * @param request - The quote request\n * @param signal - The abort signal\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of bridge tx quotes\n */\nexport async function fetchBridgeQuotes(\n request: GenericQuoteRequest,\n signal: AbortSignal,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<QuoteResponse[]> {\n const destWalletAddress = request.destWalletAddress ?? request.walletAddress;\n // Transform the generic quote request into QuoteRequest\n const normalizedRequest: QuoteRequest = {\n walletAddress: formatAddressToCaipReference(request.walletAddress),\n destWalletAddress: formatAddressToCaipReference(destWalletAddress),\n srcChainId: formatChainIdToDec(request.srcChainId),\n destChainId: formatChainIdToDec(request.destChainId),\n srcTokenAddress: formatAddressToCaipReference(request.srcTokenAddress),\n destTokenAddress: formatAddressToCaipReference(request.destTokenAddress),\n srcTokenAmount: request.srcTokenAmount,\n insufficientBal: Boolean(request.insufficientBal),\n resetApproval: Boolean(request.resetApproval),\n };\n if (request.slippage !== undefined) {\n normalizedRequest.slippage = request.slippage;\n }\n\n const queryParams = new URLSearchParams();\n Object.entries(normalizedRequest).forEach(([key, value]) => {\n queryParams.append(key, value.toString());\n });\n const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;\n const quotes: unknown[] = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n signal,\n cacheOptions: { cacheRefreshTime: 0 },\n functionName: 'fetchBridgeQuotes',\n });\n\n const filteredQuotes = quotes.filter((quoteResponse: unknown) => {\n try {\n return validateQuoteResponse(quoteResponse);\n } catch (error) {\n console.error(error);\n return false;\n }\n });\n return filteredQuotes as QuoteResponse[];\n}\n\nconst fetchAssetPricesForCurrency = async (request: {\n currency: string;\n assetIds: Set<CaipAssetType>;\n clientId: string;\n fetchFn: FetchFunction;\n}): Promise<Record<CaipAssetType, { [currency: string]: string }>> => {\n const { currency, assetIds, clientId, fetchFn } = request;\n const validAssetIds = Array.from(assetIds).filter(Boolean);\n if (validAssetIds.length === 0) {\n return {};\n }\n\n const queryParams = new URLSearchParams({\n assetIds: validAssetIds.filter(Boolean).join(','),\n vsCurrency: currency,\n });\n const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;\n const priceApiResponse = (await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: Number(Duration.Second * 30) },\n functionName: 'fetchAssetExchangeRates',\n })) as Record<CaipAssetType, { [currency: string]: number }>;\n\n if (!priceApiResponse || typeof priceApiResponse !== 'object') {\n return {};\n }\n\n return Object.entries(priceApiResponse).reduce(\n (acc, [assetId, currencyToPrice]) => {\n if (!currencyToPrice) {\n return acc;\n }\n if (!acc[assetId as CaipAssetType]) {\n acc[assetId as CaipAssetType] = {};\n }\n if (currencyToPrice[currency]) {\n acc[assetId as CaipAssetType][currency] =\n currencyToPrice[currency].toString();\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n};\n\n/**\n * Fetches the asset prices from the price API for multiple currencies\n *\n * @param request - The request object\n * @returns The asset prices by assetId\n */\nexport const fetchAssetPrices = async (\n request: {\n currencies: Set<string>;\n } & Omit<Parameters<typeof fetchAssetPricesForCurrency>[0], 'currency'>,\n): Promise<\n Record<CaipAssetType, { [currency: string]: string } | undefined>\n> => {\n const { currencies, ...args } = request;\n\n const combinedPrices = await Promise.allSettled(\n Array.from(currencies).map(\n async (currency) =>\n await fetchAssetPricesForCurrency({ ...args, currency }),\n ),\n ).then((priceApiResponse) => {\n return priceApiResponse.reduce(\n (acc, result) => {\n if (result.status === 'fulfilled') {\n Object.entries(result.value).forEach(([assetId, currencyToPrice]) => {\n const existingPrices = acc[assetId as CaipAssetType];\n if (!existingPrices) {\n acc[assetId as CaipAssetType] = {};\n }\n Object.entries(currencyToPrice).forEach(([currency, price]) => {\n acc[assetId as CaipAssetType][currency] = price;\n });\n });\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n });\n\n return combinedPrices;\n};\n"]}
|
1
|
+
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AAEpD,2CAA2C;AAE3C,2DAG2B;AAC3B,iDAA+E;AAS/E,MAAM,yBAAyB,GAAG,EAAE,GAAG,gBAAQ,CAAC,MAAM,CAAC;AAEhD,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,CAAC;IACtD,aAAa,EAAE,QAAQ;CACxB,CAAC,CAAC;AAFU,QAAA,iBAAiB,qBAE3B;AAEH;;;;;;;;GAQG;AACI,KAAK,UAAU,iBAAiB,CACrC,OAA0B,EAC1B,QAAgB,EAChB,OAAsB,EACtB,gBAAwB;IAExB,8BAA8B;IAC9B,MAAM,GAAG,GAAG,GAAG,gBAAgB,sBAAsB,IAAA,oCAAkB,EAAC,OAAO,CAAC,EAAE,CAAC;IAEnF,uGAAuG;IACvG,uEAAuE;IACvE,6IAA6I;IAC7I,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE;QAChC,OAAO,EAAE,IAAA,yBAAiB,EAAC,QAAQ,CAAC;QACpC,YAAY,EAAE,EAAE,gBAAgB,EAAE,yBAAyB,EAAE;QAC7D,YAAY,EAAE,mBAAmB;KAClC,CAAC,CAAC;IAEH,MAAM,iBAAiB,GAAgC,EAAE,CAAC;IAC1D,MAAM,CAAC,OAAO,CAAC,CAAC,KAAc,EAAE,EAAE;QAChC,IAAI,IAAA,qCAAwB,EAAC,KAAK,CAAC,EAAE;YACnC,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;SAC1C;IACH,CAAC,CAAC,CAAC;IACH,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAzBD,8CAyBC;AAED;;;;;;;;;;GAUG;AACI,KAAK,UAAU,iBAAiB,CACrC,OAA4B,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AAC;AACxB,CAAC,CAAC;AAnCW,QAAA,gBAAgB,oBAmC3B","sourcesContent":["import { StructError } from '@metamask/superstruct';\nimport type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\nimport { Duration } from '@metamask/utils';\n\nimport {\n formatAddressToCaipReference,\n formatChainIdToDec,\n} from './caip-formatters';\nimport { validateQuoteResponse, validateSwapsTokenObject } from './validators';\nimport type {\n QuoteResponse,\n FetchFunction,\n GenericQuoteRequest,\n QuoteRequest,\n BridgeAsset,\n} from '../types';\n\nconst CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;\n\nexport const getClientIdHeader = (clientId: string) => ({\n 'X-Client-Id': clientId,\n});\n\n/**\n * Returns a list of enabled (unblocked) tokens\n *\n * @param chainId - The chain ID to fetch tokens for\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of enabled (unblocked) tokens\n */\nexport async function fetchBridgeTokens(\n chainId: Hex | CaipChainId,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<Record<string, BridgeAsset>> {\n // TODO make token api v2 call\n const url = `${bridgeApiBaseUrl}/getTokens?chainId=${formatChainIdToDec(chainId)}`;\n\n // TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:\n // If we allow selecting dest networks which the user has not imported,\n // note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this\n const tokens = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },\n functionName: 'fetchBridgeTokens',\n });\n\n const transformedTokens: Record<string, BridgeAsset> = {};\n tokens.forEach((token: unknown) => {\n if (validateSwapsTokenObject(token)) {\n transformedTokens[token.address] = token;\n }\n });\n return transformedTokens;\n}\n\n/**\n * Converts the generic quote request to the type that the bridge-api expects\n * then fetches quotes from the bridge-api\n *\n * @param request - The quote request\n * @param signal - The abort signal\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of bridge tx quotes\n */\nexport async function fetchBridgeQuotes(\n request: GenericQuoteRequest,\n signal: AbortSignal,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<QuoteResponse[]> {\n const destWalletAddress = request.destWalletAddress ?? request.walletAddress;\n // Transform the generic quote request into QuoteRequest\n const normalizedRequest: QuoteRequest = {\n walletAddress: formatAddressToCaipReference(request.walletAddress),\n destWalletAddress: formatAddressToCaipReference(destWalletAddress),\n srcChainId: formatChainIdToDec(request.srcChainId),\n destChainId: formatChainIdToDec(request.destChainId),\n srcTokenAddress: formatAddressToCaipReference(request.srcTokenAddress),\n destTokenAddress: formatAddressToCaipReference(request.destTokenAddress),\n srcTokenAmount: request.srcTokenAmount,\n insufficientBal: Boolean(request.insufficientBal),\n resetApproval: Boolean(request.resetApproval),\n };\n if (request.slippage !== undefined) {\n normalizedRequest.slippage = request.slippage;\n }\n\n const queryParams = new URLSearchParams();\n Object.entries(normalizedRequest).forEach(([key, value]) => {\n queryParams.append(key, value.toString());\n });\n const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;\n const quotes: unknown[] = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n signal,\n cacheOptions: { cacheRefreshTime: 0 },\n functionName: 'fetchBridgeQuotes',\n });\n\n const validationFailuresByAggregator: {\n [aggregator: string]: Set<string>;\n } = {};\n const filteredQuotes = quotes.filter((quoteResponse: unknown) => {\n try {\n return validateQuoteResponse(quoteResponse);\n } catch (error) {\n if (error instanceof StructError) {\n error.failures().forEach(({ branch, path }) => {\n const aggregatorId = branch?.[0]?.quote?.bridgeId;\n if (!validationFailuresByAggregator[aggregatorId]) {\n validationFailuresByAggregator[aggregatorId] = new Set([]);\n }\n const pathString = path?.join('.') || 'unknown';\n validationFailuresByAggregator[aggregatorId].add(pathString);\n });\n }\n return false;\n }\n });\n\n if (Object.keys(validationFailuresByAggregator).length > 0) {\n console.error('Quote validation failed', validationFailuresByAggregator);\n }\n return filteredQuotes as QuoteResponse[];\n}\n\nconst fetchAssetPricesForCurrency = async (request: {\n currency: string;\n assetIds: Set<CaipAssetType>;\n clientId: string;\n fetchFn: FetchFunction;\n}): Promise<Record<CaipAssetType, { [currency: string]: string }>> => {\n const { currency, assetIds, clientId, fetchFn } = request;\n const validAssetIds = Array.from(assetIds).filter(Boolean);\n if (validAssetIds.length === 0) {\n return {};\n }\n\n const queryParams = new URLSearchParams({\n assetIds: validAssetIds.filter(Boolean).join(','),\n vsCurrency: currency,\n });\n const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;\n const priceApiResponse = (await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: Number(Duration.Second * 30) },\n functionName: 'fetchAssetExchangeRates',\n })) as Record<CaipAssetType, { [currency: string]: number }>;\n\n if (!priceApiResponse || typeof priceApiResponse !== 'object') {\n return {};\n }\n\n return Object.entries(priceApiResponse).reduce(\n (acc, [assetId, currencyToPrice]) => {\n if (!currencyToPrice) {\n return acc;\n }\n if (!acc[assetId as CaipAssetType]) {\n acc[assetId as CaipAssetType] = {};\n }\n if (currencyToPrice[currency]) {\n acc[assetId as CaipAssetType][currency] =\n currencyToPrice[currency].toString();\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n};\n\n/**\n * Fetches the asset prices from the price API for multiple currencies\n *\n * @param request - The request object\n * @returns The asset prices by assetId\n */\nexport const fetchAssetPrices = async (\n request: {\n currencies: Set<string>;\n } & Omit<Parameters<typeof fetchAssetPricesForCurrency>[0], 'currency'>,\n): Promise<\n Record<CaipAssetType, { [currency: string]: string } | undefined>\n> => {\n const { currencies, ...args } = request;\n\n const combinedPrices = await Promise.allSettled(\n Array.from(currencies).map(\n async (currency) =>\n await fetchAssetPricesForCurrency({ ...args, currency }),\n ),\n ).then((priceApiResponse) => {\n return priceApiResponse.reduce(\n (acc, result) => {\n if (result.status === 'fulfilled') {\n Object.entries(result.value).forEach(([assetId, currencyToPrice]) => {\n const existingPrices = acc[assetId as CaipAssetType];\n if (!existingPrices) {\n acc[assetId as CaipAssetType] = {};\n }\n Object.entries(currencyToPrice).forEach(([currency, price]) => {\n acc[assetId as CaipAssetType][currency] = price;\n });\n });\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n });\n\n return combinedPrices;\n};\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AAQvE,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,mBAAmB,EAEnB,WAAW,EACZ,qBAAiB;AAIlB,eAAO,MAAM,iBAAiB,aAAc,MAAM;;CAEhD,CAAC;AAEH;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,GAAG,GAAG,WAAW,EAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAoBtC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,aAAa,EAAE,CAAC,CAuD1B;AAED,QAAA,MAAM,2BAA2B,YAAmB;IAClD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,IAAI,aAAa,CAAC,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,aAAa,CAAC;CACxB;;GAsCA,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,YAClB;IACP,UAAU,EAAE,IAAI,MAAM,CAAC,CAAC;CACzB,GAAG,KAAK,WAAW,kCAAkC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC;;eAgCxE,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AAQvE,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,mBAAmB,EAEnB,WAAW,EACZ,qBAAiB;AAIlB,eAAO,MAAM,iBAAiB,aAAc,MAAM;;CAEhD,CAAC;AAEH;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,GAAG,GAAG,WAAW,EAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAoBtC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,aAAa,EAAE,CAAC,CAuD1B;AAED,QAAA,MAAM,2BAA2B,YAAmB;IAClD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,IAAI,aAAa,CAAC,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,aAAa,CAAC;CACxB;;GAsCA,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,YAClB;IACP,UAAU,EAAE,IAAI,MAAM,CAAC,CAAC;CACzB,GAAG,KAAK,WAAW,kCAAkC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC;;eAgCxE,CAAC"}
|
package/dist/utils/fetch.mjs
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { StructError } from "@metamask/superstruct";
|
1
2
|
import { Duration } from "@metamask/utils";
|
2
3
|
import { formatAddressToCaipReference, formatChainIdToDec } from "./caip-formatters.mjs";
|
3
4
|
import { validateQuoteResponse, validateSwapsTokenObject } from "./validators.mjs";
|
@@ -72,15 +73,28 @@ export async function fetchBridgeQuotes(request, signal, clientId, fetchFn, brid
|
|
72
73
|
cacheOptions: { cacheRefreshTime: 0 },
|
73
74
|
functionName: 'fetchBridgeQuotes',
|
74
75
|
});
|
76
|
+
const validationFailuresByAggregator = {};
|
75
77
|
const filteredQuotes = quotes.filter((quoteResponse) => {
|
76
78
|
try {
|
77
79
|
return validateQuoteResponse(quoteResponse);
|
78
80
|
}
|
79
81
|
catch (error) {
|
80
|
-
|
82
|
+
if (error instanceof StructError) {
|
83
|
+
error.failures().forEach(({ branch, path }) => {
|
84
|
+
const aggregatorId = branch?.[0]?.quote?.bridgeId;
|
85
|
+
if (!validationFailuresByAggregator[aggregatorId]) {
|
86
|
+
validationFailuresByAggregator[aggregatorId] = new Set([]);
|
87
|
+
}
|
88
|
+
const pathString = path?.join('.') || 'unknown';
|
89
|
+
validationFailuresByAggregator[aggregatorId].add(pathString);
|
90
|
+
});
|
91
|
+
}
|
81
92
|
return false;
|
82
93
|
}
|
83
94
|
});
|
95
|
+
if (Object.keys(validationFailuresByAggregator).length > 0) {
|
96
|
+
console.error('Quote validation failed', validationFailuresByAggregator);
|
97
|
+
}
|
84
98
|
return filteredQuotes;
|
85
99
|
}
|
86
100
|
const fetchAssetPricesForCurrency = async (request) => {
|
package/dist/utils/fetch.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fetch.mjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EACL,4BAA4B,EAC5B,kBAAkB,EACnB,8BAA0B;AAC3B,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,yBAAqB;AAS/E,MAAM,yBAAyB,GAAG,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC;AAEvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,CAAC;IACtD,aAAa,EAAE,QAAQ;CACxB,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAA0B,EAC1B,QAAgB,EAChB,OAAsB,EACtB,gBAAwB;IAExB,8BAA8B;IAC9B,MAAM,GAAG,GAAG,GAAG,gBAAgB,sBAAsB,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC;IAEnF,uGAAuG;IACvG,uEAAuE;IACvE,6IAA6I;IAC7I,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE;QAChC,OAAO,EAAE,iBAAiB,CAAC,QAAQ,CAAC;QACpC,YAAY,EAAE,EAAE,gBAAgB,EAAE,yBAAyB,EAAE;QAC7D,YAAY,EAAE,mBAAmB;KAClC,CAAC,CAAC;IAEH,MAAM,iBAAiB,GAAgC,EAAE,CAAC;IAC1D,MAAM,CAAC,OAAO,CAAC,CAAC,KAAc,EAAE,EAAE;QAChC,IAAI,wBAAwB,CAAC,KAAK,CAAC,EAAE;YACnC,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;SAC1C;IACH,CAAC,CAAC,CAAC;IACH,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAED;;;;;;;;;;GAU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AAO,CAAC,CAAC,CAAC,OAAO,EAAE,eAAe,CAAC,EAAE,EAAE;oBAClE,MAAM,cAAc,GAAG,GAAG,CAAC,OAAwB,CAAC,CAAC;oBACrD,IAAI,CAAC,cAAc,EAAE;wBACnB,GAAG,CAAC,OAAwB,CAAC,GAAG,EAAE,CAAC;qBACpC;oBACD,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,EAAE;wBAC5D,GAAG,CAAC,OAAwB,CAAC,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;oBAClD,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;aACJ;YACD,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAA2D,CAC5D,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC","sourcesContent":["import type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\nimport { Duration } from '@metamask/utils';\n\nimport {\n formatAddressToCaipReference,\n formatChainIdToDec,\n} from './caip-formatters';\nimport { validateQuoteResponse, validateSwapsTokenObject } from './validators';\nimport type {\n QuoteResponse,\n FetchFunction,\n GenericQuoteRequest,\n QuoteRequest,\n BridgeAsset,\n} from '../types';\n\nconst CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;\n\nexport const getClientIdHeader = (clientId: string) => ({\n 'X-Client-Id': clientId,\n});\n\n/**\n * Returns a list of enabled (unblocked) tokens\n *\n * @param chainId - The chain ID to fetch tokens for\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of enabled (unblocked) tokens\n */\nexport async function fetchBridgeTokens(\n chainId: Hex | CaipChainId,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<Record<string, BridgeAsset>> {\n // TODO make token api v2 call\n const url = `${bridgeApiBaseUrl}/getTokens?chainId=${formatChainIdToDec(chainId)}`;\n\n // TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:\n // If we allow selecting dest networks which the user has not imported,\n // note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this\n const tokens = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },\n functionName: 'fetchBridgeTokens',\n });\n\n const transformedTokens: Record<string, BridgeAsset> = {};\n tokens.forEach((token: unknown) => {\n if (validateSwapsTokenObject(token)) {\n transformedTokens[token.address] = token;\n }\n });\n return transformedTokens;\n}\n\n/**\n * Converts the generic quote request to the type that the bridge-api expects\n * then fetches quotes from the bridge-api\n *\n * @param request - The quote request\n * @param signal - The abort signal\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of bridge tx quotes\n */\nexport async function fetchBridgeQuotes(\n request: GenericQuoteRequest,\n signal: AbortSignal,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<QuoteResponse[]> {\n const destWalletAddress = request.destWalletAddress ?? request.walletAddress;\n // Transform the generic quote request into QuoteRequest\n const normalizedRequest: QuoteRequest = {\n walletAddress: formatAddressToCaipReference(request.walletAddress),\n destWalletAddress: formatAddressToCaipReference(destWalletAddress),\n srcChainId: formatChainIdToDec(request.srcChainId),\n destChainId: formatChainIdToDec(request.destChainId),\n srcTokenAddress: formatAddressToCaipReference(request.srcTokenAddress),\n destTokenAddress: formatAddressToCaipReference(request.destTokenAddress),\n srcTokenAmount: request.srcTokenAmount,\n insufficientBal: Boolean(request.insufficientBal),\n resetApproval: Boolean(request.resetApproval),\n };\n if (request.slippage !== undefined) {\n normalizedRequest.slippage = request.slippage;\n }\n\n const queryParams = new URLSearchParams();\n Object.entries(normalizedRequest).forEach(([key, value]) => {\n queryParams.append(key, value.toString());\n });\n const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;\n const quotes: unknown[] = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n signal,\n cacheOptions: { cacheRefreshTime: 0 },\n functionName: 'fetchBridgeQuotes',\n });\n\n const filteredQuotes = quotes.filter((quoteResponse: unknown) => {\n try {\n return validateQuoteResponse(quoteResponse);\n } catch (error) {\n console.error(error);\n return false;\n }\n });\n return filteredQuotes as QuoteResponse[];\n}\n\nconst fetchAssetPricesForCurrency = async (request: {\n currency: string;\n assetIds: Set<CaipAssetType>;\n clientId: string;\n fetchFn: FetchFunction;\n}): Promise<Record<CaipAssetType, { [currency: string]: string }>> => {\n const { currency, assetIds, clientId, fetchFn } = request;\n const validAssetIds = Array.from(assetIds).filter(Boolean);\n if (validAssetIds.length === 0) {\n return {};\n }\n\n const queryParams = new URLSearchParams({\n assetIds: validAssetIds.filter(Boolean).join(','),\n vsCurrency: currency,\n });\n const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;\n const priceApiResponse = (await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: Number(Duration.Second * 30) },\n functionName: 'fetchAssetExchangeRates',\n })) as Record<CaipAssetType, { [currency: string]: number }>;\n\n if (!priceApiResponse || typeof priceApiResponse !== 'object') {\n return {};\n }\n\n return Object.entries(priceApiResponse).reduce(\n (acc, [assetId, currencyToPrice]) => {\n if (!currencyToPrice) {\n return acc;\n }\n if (!acc[assetId as CaipAssetType]) {\n acc[assetId as CaipAssetType] = {};\n }\n if (currencyToPrice[currency]) {\n acc[assetId as CaipAssetType][currency] =\n currencyToPrice[currency].toString();\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n};\n\n/**\n * Fetches the asset prices from the price API for multiple currencies\n *\n * @param request - The request object\n * @returns The asset prices by assetId\n */\nexport const fetchAssetPrices = async (\n request: {\n currencies: Set<string>;\n } & Omit<Parameters<typeof fetchAssetPricesForCurrency>[0], 'currency'>,\n): Promise<\n Record<CaipAssetType, { [currency: string]: string } | undefined>\n> => {\n const { currencies, ...args } = request;\n\n const combinedPrices = await Promise.allSettled(\n Array.from(currencies).map(\n async (currency) =>\n await fetchAssetPricesForCurrency({ ...args, currency }),\n ),\n ).then((priceApiResponse) => {\n return priceApiResponse.reduce(\n (acc, result) => {\n if (result.status === 'fulfilled') {\n Object.entries(result.value).forEach(([assetId, currencyToPrice]) => {\n const existingPrices = acc[assetId as CaipAssetType];\n if (!existingPrices) {\n acc[assetId as CaipAssetType] = {};\n }\n Object.entries(currencyToPrice).forEach(([currency, price]) => {\n acc[assetId as CaipAssetType][currency] = price;\n });\n });\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n });\n\n return combinedPrices;\n};\n"]}
|
1
|
+
{"version":3,"file":"fetch.mjs","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAEpD,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EACL,4BAA4B,EAC5B,kBAAkB,EACnB,8BAA0B;AAC3B,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,yBAAqB;AAS/E,MAAM,yBAAyB,GAAG,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC;AAEvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,CAAC;IACtD,aAAa,EAAE,QAAQ;CACxB,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAA0B,EAC1B,QAAgB,EAChB,OAAsB,EACtB,gBAAwB;IAExB,8BAA8B;IAC9B,MAAM,GAAG,GAAG,GAAG,gBAAgB,sBAAsB,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC;IAEnF,uGAAuG;IACvG,uEAAuE;IACvE,6IAA6I;IAC7I,MAAM,MAAM,GAAG,MAAM,OAAO,CAA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B,CAAC,CAAC;oBACrD,IAAI,CAAC,cAAc,EAAE;wBACnB,GAAG,CAAC,OAAwB,CAAC,GAAG,EAAE,CAAC;qBACpC;oBACD,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,EAAE;wBAC5D,GAAG,CAAC,OAAwB,CAAC,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;oBAClD,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;aACJ;YACD,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAA2D,CAC5D,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC","sourcesContent":["import { StructError } from '@metamask/superstruct';\nimport type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\nimport { Duration } from '@metamask/utils';\n\nimport {\n formatAddressToCaipReference,\n formatChainIdToDec,\n} from './caip-formatters';\nimport { validateQuoteResponse, validateSwapsTokenObject } from './validators';\nimport type {\n QuoteResponse,\n FetchFunction,\n GenericQuoteRequest,\n QuoteRequest,\n BridgeAsset,\n} from '../types';\n\nconst CACHE_REFRESH_TEN_MINUTES = 10 * Duration.Minute;\n\nexport const getClientIdHeader = (clientId: string) => ({\n 'X-Client-Id': clientId,\n});\n\n/**\n * Returns a list of enabled (unblocked) tokens\n *\n * @param chainId - The chain ID to fetch tokens for\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of enabled (unblocked) tokens\n */\nexport async function fetchBridgeTokens(\n chainId: Hex | CaipChainId,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<Record<string, BridgeAsset>> {\n // TODO make token api v2 call\n const url = `${bridgeApiBaseUrl}/getTokens?chainId=${formatChainIdToDec(chainId)}`;\n\n // TODO we will need to cache these. In Extension fetchWithCache is used. This is due to the following:\n // If we allow selecting dest networks which the user has not imported,\n // note that the Assets controller won't be able to provide tokens. In extension we fetch+cache the token list from bridge-api to handle this\n const tokens = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: CACHE_REFRESH_TEN_MINUTES },\n functionName: 'fetchBridgeTokens',\n });\n\n const transformedTokens: Record<string, BridgeAsset> = {};\n tokens.forEach((token: unknown) => {\n if (validateSwapsTokenObject(token)) {\n transformedTokens[token.address] = token;\n }\n });\n return transformedTokens;\n}\n\n/**\n * Converts the generic quote request to the type that the bridge-api expects\n * then fetches quotes from the bridge-api\n *\n * @param request - The quote request\n * @param signal - The abort signal\n * @param clientId - The client ID for metrics\n * @param fetchFn - The fetch function to use\n * @param bridgeApiBaseUrl - The base URL for the bridge API\n * @returns A list of bridge tx quotes\n */\nexport async function fetchBridgeQuotes(\n request: GenericQuoteRequest,\n signal: AbortSignal,\n clientId: string,\n fetchFn: FetchFunction,\n bridgeApiBaseUrl: string,\n): Promise<QuoteResponse[]> {\n const destWalletAddress = request.destWalletAddress ?? request.walletAddress;\n // Transform the generic quote request into QuoteRequest\n const normalizedRequest: QuoteRequest = {\n walletAddress: formatAddressToCaipReference(request.walletAddress),\n destWalletAddress: formatAddressToCaipReference(destWalletAddress),\n srcChainId: formatChainIdToDec(request.srcChainId),\n destChainId: formatChainIdToDec(request.destChainId),\n srcTokenAddress: formatAddressToCaipReference(request.srcTokenAddress),\n destTokenAddress: formatAddressToCaipReference(request.destTokenAddress),\n srcTokenAmount: request.srcTokenAmount,\n insufficientBal: Boolean(request.insufficientBal),\n resetApproval: Boolean(request.resetApproval),\n };\n if (request.slippage !== undefined) {\n normalizedRequest.slippage = request.slippage;\n }\n\n const queryParams = new URLSearchParams();\n Object.entries(normalizedRequest).forEach(([key, value]) => {\n queryParams.append(key, value.toString());\n });\n const url = `${bridgeApiBaseUrl}/getQuote?${queryParams}`;\n const quotes: unknown[] = await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n signal,\n cacheOptions: { cacheRefreshTime: 0 },\n functionName: 'fetchBridgeQuotes',\n });\n\n const validationFailuresByAggregator: {\n [aggregator: string]: Set<string>;\n } = {};\n const filteredQuotes = quotes.filter((quoteResponse: unknown) => {\n try {\n return validateQuoteResponse(quoteResponse);\n } catch (error) {\n if (error instanceof StructError) {\n error.failures().forEach(({ branch, path }) => {\n const aggregatorId = branch?.[0]?.quote?.bridgeId;\n if (!validationFailuresByAggregator[aggregatorId]) {\n validationFailuresByAggregator[aggregatorId] = new Set([]);\n }\n const pathString = path?.join('.') || 'unknown';\n validationFailuresByAggregator[aggregatorId].add(pathString);\n });\n }\n return false;\n }\n });\n\n if (Object.keys(validationFailuresByAggregator).length > 0) {\n console.error('Quote validation failed', validationFailuresByAggregator);\n }\n return filteredQuotes as QuoteResponse[];\n}\n\nconst fetchAssetPricesForCurrency = async (request: {\n currency: string;\n assetIds: Set<CaipAssetType>;\n clientId: string;\n fetchFn: FetchFunction;\n}): Promise<Record<CaipAssetType, { [currency: string]: string }>> => {\n const { currency, assetIds, clientId, fetchFn } = request;\n const validAssetIds = Array.from(assetIds).filter(Boolean);\n if (validAssetIds.length === 0) {\n return {};\n }\n\n const queryParams = new URLSearchParams({\n assetIds: validAssetIds.filter(Boolean).join(','),\n vsCurrency: currency,\n });\n const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;\n const priceApiResponse = (await fetchFn(url, {\n headers: getClientIdHeader(clientId),\n cacheOptions: { cacheRefreshTime: Number(Duration.Second * 30) },\n functionName: 'fetchAssetExchangeRates',\n })) as Record<CaipAssetType, { [currency: string]: number }>;\n\n if (!priceApiResponse || typeof priceApiResponse !== 'object') {\n return {};\n }\n\n return Object.entries(priceApiResponse).reduce(\n (acc, [assetId, currencyToPrice]) => {\n if (!currencyToPrice) {\n return acc;\n }\n if (!acc[assetId as CaipAssetType]) {\n acc[assetId as CaipAssetType] = {};\n }\n if (currencyToPrice[currency]) {\n acc[assetId as CaipAssetType][currency] =\n currencyToPrice[currency].toString();\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n};\n\n/**\n * Fetches the asset prices from the price API for multiple currencies\n *\n * @param request - The request object\n * @returns The asset prices by assetId\n */\nexport const fetchAssetPrices = async (\n request: {\n currencies: Set<string>;\n } & Omit<Parameters<typeof fetchAssetPricesForCurrency>[0], 'currency'>,\n): Promise<\n Record<CaipAssetType, { [currency: string]: string } | undefined>\n> => {\n const { currencies, ...args } = request;\n\n const combinedPrices = await Promise.allSettled(\n Array.from(currencies).map(\n async (currency) =>\n await fetchAssetPricesForCurrency({ ...args, currency }),\n ),\n ).then((priceApiResponse) => {\n return priceApiResponse.reduce(\n (acc, result) => {\n if (result.status === 'fulfilled') {\n Object.entries(result.value).forEach(([assetId, currencyToPrice]) => {\n const existingPrices = acc[assetId as CaipAssetType];\n if (!existingPrices) {\n acc[assetId as CaipAssetType] = {};\n }\n Object.entries(currencyToPrice).forEach(([currency, price]) => {\n acc[assetId as CaipAssetType][currency] = price;\n });\n });\n }\n return acc;\n },\n {} as Record<CaipAssetType, { [currency: string]: string }>,\n );\n });\n\n return combinedPrices;\n};\n"]}
|
package/package.json
CHANGED