@metamask/bridge-controller 52.0.0 → 53.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -1
- package/dist/bridge-controller.cjs +5 -4
- package/dist/bridge-controller.cjs.map +1 -1
- package/dist/bridge-controller.d.cts +1 -1
- package/dist/bridge-controller.d.cts.map +1 -1
- package/dist/bridge-controller.d.mts +1 -1
- package/dist/bridge-controller.d.mts.map +1 -1
- package/dist/bridge-controller.mjs +6 -5
- package/dist/bridge-controller.mjs.map +1 -1
- package/dist/constants/bridge.cjs +1 -0
- package/dist/constants/bridge.cjs.map +1 -1
- package/dist/constants/bridge.d.cts +1 -1
- package/dist/constants/bridge.d.cts.map +1 -1
- package/dist/constants/bridge.d.mts +1 -1
- package/dist/constants/bridge.d.mts.map +1 -1
- package/dist/constants/bridge.mjs +1 -0
- package/dist/constants/bridge.mjs.map +1 -1
- package/dist/constants/chains.cjs +4 -1
- package/dist/constants/chains.cjs.map +1 -1
- package/dist/constants/chains.d.cts +3 -0
- package/dist/constants/chains.d.cts.map +1 -1
- package/dist/constants/chains.d.mts +3 -0
- package/dist/constants/chains.d.mts.map +1 -1
- package/dist/constants/chains.mjs +3 -0
- package/dist/constants/chains.mjs.map +1 -1
- package/dist/constants/tokens.cjs +10 -0
- package/dist/constants/tokens.cjs.map +1 -1
- package/dist/constants/tokens.d.cts +7 -0
- package/dist/constants/tokens.d.cts.map +1 -1
- package/dist/constants/tokens.d.mts +7 -0
- package/dist/constants/tokens.d.mts.map +1 -1
- package/dist/constants/tokens.mjs +10 -0
- package/dist/constants/tokens.mjs.map +1 -1
- package/dist/selectors.cjs +1 -1
- package/dist/selectors.cjs.map +1 -1
- package/dist/selectors.d.cts +346 -244
- package/dist/selectors.d.cts.map +1 -1
- package/dist/selectors.d.mts +346 -244
- package/dist/selectors.d.mts.map +1 -1
- package/dist/selectors.mjs +2 -2
- package/dist/selectors.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +3 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +3 -2
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/bridge.cjs +5 -1
- package/dist/utils/bridge.cjs.map +1 -1
- package/dist/utils/bridge.d.cts +10 -1
- package/dist/utils/bridge.d.cts.map +1 -1
- package/dist/utils/bridge.d.mts +10 -1
- package/dist/utils/bridge.d.mts.map +1 -1
- package/dist/utils/bridge.mjs +3 -0
- package/dist/utils/bridge.mjs.map +1 -1
- package/dist/utils/feature-flags.cjs +24 -1
- package/dist/utils/feature-flags.cjs.map +1 -1
- package/dist/utils/feature-flags.d.cts +12 -1
- package/dist/utils/feature-flags.d.cts.map +1 -1
- package/dist/utils/feature-flags.d.mts +12 -1
- package/dist/utils/feature-flags.d.mts.map +1 -1
- package/dist/utils/feature-flags.mjs +22 -0
- package/dist/utils/feature-flags.mjs.map +1 -1
- package/dist/utils/fetch-server-events.cjs +61 -0
- package/dist/utils/fetch-server-events.cjs.map +1 -0
- package/dist/utils/fetch-server-events.d.cts +17 -0
- package/dist/utils/fetch-server-events.d.cts.map +1 -0
- package/dist/utils/fetch-server-events.d.mts +17 -0
- package/dist/utils/fetch-server-events.d.mts.map +1 -0
- package/dist/utils/fetch-server-events.mjs +57 -0
- package/dist/utils/fetch-server-events.mjs.map +1 -0
- package/dist/utils/fetch.cjs +14 -24
- 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 -25
- package/dist/utils/fetch.mjs.map +1 -1
- package/dist/utils/quote-fees.cjs.map +1 -1
- package/dist/utils/quote-fees.mjs.map +1 -1
- package/dist/utils/quote.cjs +2 -1
- package/dist/utils/quote.cjs.map +1 -1
- package/dist/utils/quote.d.cts +3 -3
- package/dist/utils/quote.d.cts.map +1 -1
- package/dist/utils/quote.d.mts +3 -3
- package/dist/utils/quote.d.mts.map +1 -1
- package/dist/utils/quote.mjs +2 -1
- package/dist/utils/quote.mjs.map +1 -1
- package/dist/utils/validators.cjs +11 -13
- package/dist/utils/validators.cjs.map +1 -1
- package/dist/utils/validators.d.cts +20 -977
- package/dist/utils/validators.d.cts.map +1 -1
- package/dist/utils/validators.d.mts +20 -977
- package/dist/utils/validators.d.mts.map +1 -1
- package/dist/utils/validators.mjs +10 -11
- package/dist/utils/validators.mjs.map +1 -1
- package/package.json +5 -6
package/dist/utils/bridge.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CaipChainId } from "@metamask/utils";
|
|
2
2
|
import { type Hex } from "@metamask/utils";
|
|
3
|
-
import type { BridgeAsset, BridgeControllerState, GenericQuoteRequest } from "../types.mjs";
|
|
3
|
+
import type { BridgeAsset, BridgeControllerState, GenericQuoteRequest, QuoteResponse } from "../types.mjs";
|
|
4
4
|
export declare const getDefaultBridgeControllerState: () => BridgeControllerState;
|
|
5
5
|
/**
|
|
6
6
|
* Returns the native swaps or bridge asset for a given chainId
|
|
@@ -59,6 +59,15 @@ export declare const isBitcoinChainId: (chainId: Hex | number | CaipChainId | st
|
|
|
59
59
|
* @returns True if the chain is a supported non-EVM chain, false otherwise
|
|
60
60
|
*/
|
|
61
61
|
export declare const isNonEvmChainId: (chainId: GenericQuoteRequest['srcChainId']) => boolean;
|
|
62
|
+
export declare const isEvmQuoteResponse: (quoteResponse: QuoteResponse) => quoteResponse is QuoteResponse<{
|
|
63
|
+
data: string;
|
|
64
|
+
value: string;
|
|
65
|
+
to: string;
|
|
66
|
+
from: string;
|
|
67
|
+
gasLimit: number | null;
|
|
68
|
+
chainId: number;
|
|
69
|
+
effectiveGas?: number | undefined;
|
|
70
|
+
}>;
|
|
62
71
|
/**
|
|
63
72
|
* Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
|
|
64
73
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.d.mts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAiB,WAAW,EAAE,wBAAwB;AAClE,OAAO,EAAoC,KAAK,GAAG,EAAE,wBAAwB;AAkB7E,OAAO,KAAK,EACV,WAAW,EACX,qBAAqB,EACrB,mBAAmB,
|
|
1
|
+
{"version":3,"file":"bridge.d.mts","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAiB,WAAW,EAAE,wBAAwB;AAClE,OAAO,EAAoC,KAAK,GAAG,EAAE,wBAAwB;AAkB7E,OAAO,KAAK,EACV,WAAW,EACX,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EAEd,qBAAiB;AAGlB,eAAO,MAAM,+BAA+B,QAAO,qBAElD,CAAC;AAiBF;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,YAC1B,MAAM,GAAG,MAAM,GAAG,GAAG,GAAG,WAAW,KAC3C,WAyBF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,cAS/B,CAAC;AAEF,eAAO,MAAM,SAAS,YAAa,GAAG,WAAW,MAAM,YAEG,CAAC;AAE3D,eAAO,MAAM,QAAQ,kBAAmB,MAAM,EAAE,KAAG,GAOlD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,0BAA0B,YAC5B,MAAM,WACN,GAAG,GAAG,WAAW,YAO3B,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,WAC5B,MAAM,WACL,GAAG,GAAG,WAAW,YAO3B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,aAAc,MAAM,GAAG,IAAI,YAQjD,CAAC;AAEP;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YACjB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF,eAAO,MAAM,gBAAgB,YAClB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAM7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,YACjB,mBAAmB,CAAC,YAAY,CAAC,KACzC,OAEF,CAAC;AAEF,eAAO,MAAM,kBAAkB,kBACd,aAAa;;;;;;;;EAG7B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,eACX,mBAAmB,CAAC,YAAY,CAAC,gBAC/B,mBAAmB,CAAC,aAAa,CAAC,YAUjD,CAAC"}
|
package/dist/utils/bridge.mjs
CHANGED
|
@@ -134,6 +134,9 @@ export const isBitcoinChainId = (chainId) => {
|
|
|
134
134
|
export const isNonEvmChainId = (chainId) => {
|
|
135
135
|
return isSolanaChainId(chainId) || isBitcoinChainId(chainId);
|
|
136
136
|
};
|
|
137
|
+
export const isEvmQuoteResponse = (quoteResponse) => {
|
|
138
|
+
return !isNonEvmChainId(quoteResponse.quote.srcChainId);
|
|
139
|
+
};
|
|
137
140
|
/**
|
|
138
141
|
* Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
|
|
139
142
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bridge.mjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AAC3D,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAEvD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAY,wBAAwB;AAE7E,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EACnB,8BAA0B;AAC3B,OAAO,EACL,+BAA+B,EAC/B,gBAAgB,EAChB,2BAA2B,EAC5B,gCAA4B;AAC7B,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAChD,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EAErB,gCAA4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;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { CaipAssetType, CaipChainId } from '@metamask/utils';\nimport { isCaipChainId, isStrictHexString, type Hex } from '@metamask/utils';\n\nimport {\n formatChainIdToCaip,\n formatChainIdToDec,\n formatChainIdToHex,\n} from './caip-formatters';\nimport {\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n ETH_USDT_ADDRESS,\n METABRIDGE_ETHEREUM_ADDRESS,\n} from '../constants/bridge';\nimport { CHAIN_IDS } from '../constants/chains';\nimport {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n type SupportedSwapsNativeCurrencySymbols,\n} from '../constants/tokens';\nimport type {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n} from '../types';\nimport { ChainId } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * Returns the native assetType for a given chainId and native currency symbol\n * Note that the return value is used as the assetId although it is a CaipAssetType\n *\n * @param chainId - The chainId to get the native assetType for\n * @param nativeCurrencySymbol - The native currency symbol for the given chainId\n * @returns The native assetType for the given chainId\n */\nconst getNativeAssetCaipAssetType = (\n chainId: CaipChainId,\n nativeCurrencySymbol: SupportedSwapsNativeCurrencySymbols,\n): CaipAssetType => {\n return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;\n};\n\n/**\n * Returns the native swaps or bridge asset for a given chainId\n *\n * @param chainId - The chainId to get the default token for\n * @returns The native asset for the given chainId\n * @throws If no native asset is defined for the given chainId\n */\nexport const getNativeAssetForChainId = (\n chainId: string | number | Hex | CaipChainId,\n): BridgeAsset => {\n const chainIdInCaip = formatChainIdToCaip(chainId);\n const nativeToken =\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToCaip(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ] ??\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToHex(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ];\n\n if (!nativeToken) {\n throw new Error(\n `No XChain Swaps native asset found for chainId: ${chainId}`,\n );\n }\n\n return {\n ...nativeToken,\n chainId: formatChainIdToDec(chainId),\n assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),\n };\n};\n\n/**\n * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum\n *\n * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API\n */\nexport const getEthUsdtResetData = () => {\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n METABRIDGE_ETHEREUM_ADDRESS,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (chainId: Hex, address: string) =>\n chainId === CHAIN_IDS.MAINNET &&\n address.toLowerCase() === ETH_USDT_ADDRESS.toLowerCase();\n\nexport const sumHexes = (...hexStrings: string[]): Hex => {\n if (hexStrings.length === 0) {\n return '0x0';\n }\n\n const sum = hexStrings.reduce((acc, hex) => acc + BigInt(hex), BigInt(0));\n return `0x${sum.toString(16)}`;\n};\n\n/**\n * Checks whether the provided address is strictly equal to the address for\n * the default swaps token of the provided chain.\n *\n * @param address - The string to compare to the default token address\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the address is the provided chain's default token address\n */\nexport const isSwapsDefaultTokenAddress = (\n address: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!address || !chainId) {\n return false;\n }\n\n return address === getNativeAssetForChainId(chainId)?.address;\n};\n\n/**\n * Checks whether the provided symbol is strictly equal to the symbol for\n * the default swaps token of the provided chain.\n *\n * @param symbol - The string to compare to the default token symbol\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the symbol is the provided chain's default token symbol\n */\nexport const isSwapsDefaultTokenSymbol = (\n symbol: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return symbol === getNativeAssetForChainId(chainId)?.symbol;\n};\n\n/**\n * Checks whether the address is a native asset in any supported xchain swaps network\n *\n * @param address - The address to check\n * @returns Whether the address is a native asset\n */\nexport const isNativeAddress = (address?: string | null) =>\n address === AddressZero || // bridge and swap apis set the native asset address to zero\n address === '' || // assets controllers set the native asset address to an empty string\n !address ||\n (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\n\n/**\n * Checks whether the chainId matches Solana in CaipChainId or number format\n *\n * @param chainId - The chainId to check\n * @returns Whether the chainId is Solana\n */\nexport const isSolanaChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === SolScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.SOLANA.toString();\n};\n\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana and Bitcoin\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return isSolanaChainId(chainId) || isBitcoinChainId(chainId);\n};\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\n }\n};\n"]}
|
|
1
|
+
{"version":3,"file":"bridge.mjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,QAAQ,EAAE,iCAAiC;AACpD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AAC3D,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAEvD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAY,wBAAwB;AAE7E,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EACnB,8BAA0B;AAC3B,OAAO,EACL,+BAA+B,EAC/B,gBAAgB,EAChB,2BAA2B,EAC5B,gCAA4B;AAC7B,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAChD,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EAErB,gCAA4B;AAQ7B,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,+BAA+B,CAAC;AACzC,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,2BAA2B,GAAG,CAClC,OAAoB,EACpB,oBAAyD,EAC1C,EAAE;IACjB,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,IAAI,oBAAoB,CAAC,oBAAoB,CAAC,EAAE,CAAC;AACzF,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,OAA4C,EAC/B,EAAE;IACf,MAAM,aAAa,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACnD,MAAM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M,CAAC,QAAQ,EAAE,CAAC;AAC1D,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,OAA4C,EAC5C,EAAE;IACF,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE;QAC1B,OAAO,OAAO,KAAK,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;KAChD;IACD,OAAO,OAAO,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;AACvD,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAA0C,EACjC,EAAE;IACX,OAAO,eAAe,CAAC,OAAO,CAAC,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAC/D,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,aAA4B,EACY,EAAE;IAC1C,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,UAA6C,EAC7C,WAAgD,EAChD,EAAE;IACF,IAAI;QACF,IAAI,CAAC,WAAW,EAAE;YAChB,OAAO,KAAK,CAAC;SACd;QACD,OAAO,mBAAmB,CAAC,UAAU,CAAC,KAAK,mBAAmB,CAAC,WAAW,CAAC,CAAC;KAC7E;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope } from '@metamask/keyring-api';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { CaipAssetType, CaipChainId } from '@metamask/utils';\nimport { isCaipChainId, isStrictHexString, type Hex } from '@metamask/utils';\n\nimport {\n formatChainIdToCaip,\n formatChainIdToDec,\n formatChainIdToHex,\n} from './caip-formatters';\nimport {\n DEFAULT_BRIDGE_CONTROLLER_STATE,\n ETH_USDT_ADDRESS,\n METABRIDGE_ETHEREUM_ADDRESS,\n} from '../constants/bridge';\nimport { CHAIN_IDS } from '../constants/chains';\nimport {\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n SYMBOL_TO_SLIP44_MAP,\n type SupportedSwapsNativeCurrencySymbols,\n} from '../constants/tokens';\nimport type {\n BridgeAsset,\n BridgeControllerState,\n GenericQuoteRequest,\n QuoteResponse,\n TxData,\n} from '../types';\nimport { ChainId } from '../types';\n\nexport const getDefaultBridgeControllerState = (): BridgeControllerState => {\n return DEFAULT_BRIDGE_CONTROLLER_STATE;\n};\n\n/**\n * Returns the native assetType for a given chainId and native currency symbol\n * Note that the return value is used as the assetId although it is a CaipAssetType\n *\n * @param chainId - The chainId to get the native assetType for\n * @param nativeCurrencySymbol - The native currency symbol for the given chainId\n * @returns The native assetType for the given chainId\n */\nconst getNativeAssetCaipAssetType = (\n chainId: CaipChainId,\n nativeCurrencySymbol: SupportedSwapsNativeCurrencySymbols,\n): CaipAssetType => {\n return `${formatChainIdToCaip(chainId)}/${SYMBOL_TO_SLIP44_MAP[nativeCurrencySymbol]}`;\n};\n\n/**\n * Returns the native swaps or bridge asset for a given chainId\n *\n * @param chainId - The chainId to get the default token for\n * @returns The native asset for the given chainId\n * @throws If no native asset is defined for the given chainId\n */\nexport const getNativeAssetForChainId = (\n chainId: string | number | Hex | CaipChainId,\n): BridgeAsset => {\n const chainIdInCaip = formatChainIdToCaip(chainId);\n const nativeToken =\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToCaip(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ] ??\n SWAPS_CHAINID_DEFAULT_TOKEN_MAP[\n formatChainIdToHex(\n chainId,\n ) as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP\n ];\n\n if (!nativeToken) {\n throw new Error(\n `No XChain Swaps native asset found for chainId: ${chainId}`,\n );\n }\n\n return {\n ...nativeToken,\n chainId: formatChainIdToDec(chainId),\n assetId: getNativeAssetCaipAssetType(chainIdInCaip, nativeToken.symbol),\n };\n};\n\n/**\n * A function to return the txParam data for setting allowance to 0 for USDT on Ethereum\n *\n * @returns The txParam data that will reset allowance to 0, combine it with the approval tx params received from Bridge API\n */\nexport const getEthUsdtResetData = () => {\n const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n .interface;\n const data = UsdtContractInterface.encodeFunctionData('approve', [\n METABRIDGE_ETHEREUM_ADDRESS,\n '0',\n ]);\n\n return data;\n};\n\nexport const isEthUsdt = (chainId: Hex, address: string) =>\n chainId === CHAIN_IDS.MAINNET &&\n address.toLowerCase() === ETH_USDT_ADDRESS.toLowerCase();\n\nexport const sumHexes = (...hexStrings: string[]): Hex => {\n if (hexStrings.length === 0) {\n return '0x0';\n }\n\n const sum = hexStrings.reduce((acc, hex) => acc + BigInt(hex), BigInt(0));\n return `0x${sum.toString(16)}`;\n};\n\n/**\n * Checks whether the provided address is strictly equal to the address for\n * the default swaps token of the provided chain.\n *\n * @param address - The string to compare to the default token address\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the address is the provided chain's default token address\n */\nexport const isSwapsDefaultTokenAddress = (\n address: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!address || !chainId) {\n return false;\n }\n\n return address === getNativeAssetForChainId(chainId)?.address;\n};\n\n/**\n * Checks whether the provided symbol is strictly equal to the symbol for\n * the default swaps token of the provided chain.\n *\n * @param symbol - The string to compare to the default token symbol\n * @param chainId - The hex encoded chain ID of the default swaps token to check\n * @returns Whether the symbol is the provided chain's default token symbol\n */\nexport const isSwapsDefaultTokenSymbol = (\n symbol: string,\n chainId: Hex | CaipChainId,\n) => {\n if (!symbol || !chainId) {\n return false;\n }\n\n return symbol === getNativeAssetForChainId(chainId)?.symbol;\n};\n\n/**\n * Checks whether the address is a native asset in any supported xchain swaps network\n *\n * @param address - The address to check\n * @returns Whether the address is a native asset\n */\nexport const isNativeAddress = (address?: string | null) =>\n address === AddressZero || // bridge and swap apis set the native asset address to zero\n address === '' || // assets controllers set the native asset address to an empty string\n !address ||\n (!isStrictHexString(address) &&\n Object.values(SYMBOL_TO_SLIP44_MAP).some(\n // check if it matches any supported SLIP44 references\n (reference) => address.includes(reference) || reference.endsWith(address),\n ));\n\n/**\n * Checks whether the chainId matches Solana in CaipChainId or number format\n *\n * @param chainId - The chainId to check\n * @returns Whether the chainId is Solana\n */\nexport const isSolanaChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === SolScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.SOLANA.toString();\n};\n\nexport const isBitcoinChainId = (\n chainId: Hex | number | CaipChainId | string,\n) => {\n if (isCaipChainId(chainId)) {\n return chainId === BtcScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.BTC.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana and Bitcoin\n *\n * @param chainId - The chain ID to check\n * @returns True if the chain is a supported non-EVM chain, false otherwise\n */\nexport const isNonEvmChainId = (\n chainId: GenericQuoteRequest['srcChainId'],\n): boolean => {\n return isSolanaChainId(chainId) || isBitcoinChainId(chainId);\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData> => {\n return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n\n/**\n * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds\n *\n * @param srcChainId - The source chainId\n * @param destChainId - The destination chainId\n * @returns Whether the transaction is a cross-chain transaction\n */\nexport const isCrossChain = (\n srcChainId: GenericQuoteRequest['srcChainId'],\n destChainId?: GenericQuoteRequest['destChainId'],\n) => {\n try {\n if (!destChainId) {\n return false;\n }\n return formatChainIdToCaip(srcChainId) !== formatChainIdToCaip(destChainId);\n } catch {\n return false;\n }\n};\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getBridgeFeatureFlags = exports.processFeatureFlags = exports.formatFeatureFlags = void 0;
|
|
3
|
+
exports.hasMinimumRequiredVersion = exports.getBridgeFeatureFlags = exports.processFeatureFlags = exports.formatFeatureFlags = void 0;
|
|
4
4
|
const caip_formatters_1 = require("./caip-formatters.cjs");
|
|
5
5
|
const validators_1 = require("./validators.cjs");
|
|
6
6
|
const bridge_1 = require("../constants/bridge.cjs");
|
|
@@ -42,4 +42,27 @@ function getBridgeFeatureFlags(messenger) {
|
|
|
42
42
|
return (0, exports.processFeatureFlags)(rawMobileFlags || rawBridgeConfig);
|
|
43
43
|
}
|
|
44
44
|
exports.getBridgeFeatureFlags = getBridgeFeatureFlags;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if the client version is greater than or equal to the minimum required version
|
|
47
|
+
*
|
|
48
|
+
* @param clientVersion - The client version
|
|
49
|
+
* @param minRequiredVersion - The minimum required version
|
|
50
|
+
* @returns True if the client version is greater than or equal to the minimum required version, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
const hasMinimumRequiredVersion = (clientVersion, minRequiredVersion) => {
|
|
53
|
+
const [clientMajor, clientMinor, clientPatch] = clientVersion
|
|
54
|
+
.split('.')
|
|
55
|
+
.map(Number);
|
|
56
|
+
const [minRequiredMajor, minRequiredMinor, minRequiredPatch] = minRequiredVersion.split('.').map(Number);
|
|
57
|
+
if (clientMajor > minRequiredMajor) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (clientMajor === minRequiredMajor && clientMinor > minRequiredMinor) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return (clientMajor === minRequiredMajor &&
|
|
64
|
+
clientMinor === minRequiredMinor &&
|
|
65
|
+
clientPatch >= minRequiredPatch);
|
|
66
|
+
};
|
|
67
|
+
exports.hasMinimumRequiredVersion = hasMinimumRequiredVersion;
|
|
45
68
|
//# sourceMappingURL=feature-flags.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAEA,2DAAwD;AACxD,iDAA4D;AAC5D,oDAAkE;AAG3D,MAAM,kBAAkB,GAAG,CAChC,kBAA8C,EAC9C,EAAE;IACF,MAAM,YAAY,GAAG,CAAC,MAA0C,EAAE,EAAE,CAClE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAC3B,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,GAAG,GAAG;QACN,CAAC,IAAA,qCAAmB,EAAC,OAAO,CAAC,CAAC,EAAE,KAAK;KACtC,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO;QACL,GAAG,kBAAkB;QACrB,MAAM,EAAE,YAAY,CAAC,kBAAkB,CAAC,MAAM,CAAC;KAChD,CAAC;AACJ,CAAC,CAAC;AAhBW,QAAA,kBAAkB,sBAgB7B;AAEK,MAAM,mBAAmB,GAAG,CACjC,kBAA2B,EACC,EAAE;IAC9B,IAAI,IAAA,yCAA4B,EAAC,kBAAkB,CAAC,EAAE;QACpD,OAAO,IAAA,0BAAkB,EAAC,kBAAkB,CAAC,CAAC;KAC/C;IACD,OAAO,oCAA2B,CAAC;AACrC,CAAC,CAAC;AAPW,QAAA,mBAAmB,uBAO9B;AAEF;;;;;GAKG;AACH,SAAgB,qBAAqB,CAMnC,SAAY;IACZ,kGAAkG;IAClG,MAAM,gCAAgC,GAAG,SAAS,CAAC,IAAI,CACrD,sCAAsC,CACvC,CAAC;IAEF,wDAAwD;IACxD,2GAA2G;IAC3G,0FAA0F;IAC1F,2HAA2H;IAC3H,uHAAuH;IACvH,MAAM,cAAc,GAClB,gCAAgC,EAAE,kBAAkB,EAAE,cAAc,CAAC;IAEvE,uGAAuG;IACvG,MAAM,eAAe,GACnB,gCAAgC,EAAE,kBAAkB,EAAE,YAAY,CAAC;IAErE,OAAO,IAAA,2BAAmB,EAAC,cAAc,IAAI,eAAe,CAAC,CAAC;AAChE,CAAC;AAzBD,sDAyBC","sourcesContent":["import type { RemoteFeatureFlagControllerState } from '@metamask/remote-feature-flag-controller';\n\nimport { formatChainIdToCaip } from './caip-formatters';\nimport { validateFeatureFlagsResponse } from './validators';\nimport { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';\nimport type { FeatureFlagsPlatformConfig, ChainConfiguration } from '../types';\n\nexport const formatFeatureFlags = (\n bridgeFeatureFlags: FeatureFlagsPlatformConfig,\n) => {\n const getChainsObj = (chains: Record<string, ChainConfiguration>) =>\n Object.entries(chains).reduce(\n (acc, [chainId, value]) => ({\n ...acc,\n [formatChainIdToCaip(chainId)]: value,\n }),\n {},\n );\n\n return {\n ...bridgeFeatureFlags,\n chains: getChainsObj(bridgeFeatureFlags.chains),\n };\n};\n\nexport const processFeatureFlags = (\n bridgeFeatureFlags: unknown,\n): FeatureFlagsPlatformConfig => {\n if (validateFeatureFlagsResponse(bridgeFeatureFlags)) {\n return formatFeatureFlags(bridgeFeatureFlags);\n }\n return DEFAULT_FEATURE_FLAG_CONFIG;\n};\n\n/**\n * Gets the bridge feature flags from the remote feature flag controller\n *\n * @param messenger - Any messenger with access to RemoteFeatureFlagController:getState\n * @returns The bridge feature flags\n */\nexport function getBridgeFeatureFlags<\n T extends {\n call(\n action: 'RemoteFeatureFlagController:getState',\n ): RemoteFeatureFlagControllerState;\n },\n>(messenger: T): FeatureFlagsPlatformConfig {\n // This will return the bridgeConfig for the current platform even without specifying the platform\n const remoteFeatureFlagControllerState = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n\n // bridgeConfigV2 is the feature flag for the mobile app\n // bridgeConfig for Mobile has been deprecated since release of bridge and Solana in 7.46.0 was pushed back\n // and there's no way to turn on bridgeConfig for 7.47.0 without affecting 7.46.0 as well.\n // You will still get bridgeConfig returned from remoteFeatureFlagControllerState but you should use bridgeConfigV2 instead\n // Mobile's bridgeConfig will be permanently serving the disabled variation, so falling back to it in Mobile will be ok\n const rawMobileFlags =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfigV2;\n\n // Extension LaunchDarkly will not have the bridgeConfigV2 field, so we'll continue to use bridgeConfig\n const rawBridgeConfig =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfig;\n\n return processFeatureFlags(rawMobileFlags || rawBridgeConfig);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAEA,2DAAwD;AACxD,iDAA4D;AAC5D,oDAAkE;AAG3D,MAAM,kBAAkB,GAAG,CAChC,kBAA8C,EAC9C,EAAE;IACF,MAAM,YAAY,GAAG,CAAC,MAA0C,EAAE,EAAE,CAClE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAC3B,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,GAAG,GAAG;QACN,CAAC,IAAA,qCAAmB,EAAC,OAAO,CAAC,CAAC,EAAE,KAAK;KACtC,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO;QACL,GAAG,kBAAkB;QACrB,MAAM,EAAE,YAAY,CAAC,kBAAkB,CAAC,MAAM,CAAC;KAChD,CAAC;AACJ,CAAC,CAAC;AAhBW,QAAA,kBAAkB,sBAgB7B;AAEK,MAAM,mBAAmB,GAAG,CACjC,kBAA2B,EACC,EAAE;IAC9B,IAAI,IAAA,yCAA4B,EAAC,kBAAkB,CAAC,EAAE;QACpD,OAAO,IAAA,0BAAkB,EAAC,kBAAkB,CAAC,CAAC;KAC/C;IACD,OAAO,oCAA2B,CAAC;AACrC,CAAC,CAAC;AAPW,QAAA,mBAAmB,uBAO9B;AAEF;;;;;GAKG;AACH,SAAgB,qBAAqB,CAMnC,SAAY;IACZ,kGAAkG;IAClG,MAAM,gCAAgC,GAAG,SAAS,CAAC,IAAI,CACrD,sCAAsC,CACvC,CAAC;IAEF,wDAAwD;IACxD,2GAA2G;IAC3G,0FAA0F;IAC1F,2HAA2H;IAC3H,uHAAuH;IACvH,MAAM,cAAc,GAClB,gCAAgC,EAAE,kBAAkB,EAAE,cAAc,CAAC;IAEvE,uGAAuG;IACvG,MAAM,eAAe,GACnB,gCAAgC,EAAE,kBAAkB,EAAE,YAAY,CAAC;IAErE,OAAO,IAAA,2BAAmB,EAAC,cAAc,IAAI,eAAe,CAAC,CAAC;AAChE,CAAC;AAzBD,sDAyBC;AAED;;;;;;GAMG;AACI,MAAM,yBAAyB,GAAG,CACvC,aAAqB,EACrB,kBAA0B,EAC1B,EAAE;IACF,MAAM,CAAC,WAAW,EAAE,WAAW,EAAE,WAAW,CAAC,GAAG,aAAa;SAC1D,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,MAAM,CAAC,CAAC;IACf,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,EAAE,gBAAgB,CAAC,GAC1D,kBAAkB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAE5C,IAAI,WAAW,GAAG,gBAAgB,EAAE;QAClC,OAAO,IAAI,CAAC;KACb;IACD,IAAI,WAAW,KAAK,gBAAgB,IAAI,WAAW,GAAG,gBAAgB,EAAE;QACtE,OAAO,IAAI,CAAC;KACb;IACD,OAAO,CACL,WAAW,KAAK,gBAAgB;QAChC,WAAW,KAAK,gBAAgB;QAChC,WAAW,IAAI,gBAAgB,CAChC,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,yBAAyB,6BAqBpC","sourcesContent":["import type { RemoteFeatureFlagControllerState } from '@metamask/remote-feature-flag-controller';\n\nimport { formatChainIdToCaip } from './caip-formatters';\nimport { validateFeatureFlagsResponse } from './validators';\nimport { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';\nimport type { FeatureFlagsPlatformConfig, ChainConfiguration } from '../types';\n\nexport const formatFeatureFlags = (\n bridgeFeatureFlags: FeatureFlagsPlatformConfig,\n) => {\n const getChainsObj = (chains: Record<string, ChainConfiguration>) =>\n Object.entries(chains).reduce(\n (acc, [chainId, value]) => ({\n ...acc,\n [formatChainIdToCaip(chainId)]: value,\n }),\n {},\n );\n\n return {\n ...bridgeFeatureFlags,\n chains: getChainsObj(bridgeFeatureFlags.chains),\n };\n};\n\nexport const processFeatureFlags = (\n bridgeFeatureFlags: unknown,\n): FeatureFlagsPlatformConfig => {\n if (validateFeatureFlagsResponse(bridgeFeatureFlags)) {\n return formatFeatureFlags(bridgeFeatureFlags);\n }\n return DEFAULT_FEATURE_FLAG_CONFIG;\n};\n\n/**\n * Gets the bridge feature flags from the remote feature flag controller\n *\n * @param messenger - Any messenger with access to RemoteFeatureFlagController:getState\n * @returns The bridge feature flags\n */\nexport function getBridgeFeatureFlags<\n T extends {\n call(\n action: 'RemoteFeatureFlagController:getState',\n ): RemoteFeatureFlagControllerState;\n },\n>(messenger: T): FeatureFlagsPlatformConfig {\n // This will return the bridgeConfig for the current platform even without specifying the platform\n const remoteFeatureFlagControllerState = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n\n // bridgeConfigV2 is the feature flag for the mobile app\n // bridgeConfig for Mobile has been deprecated since release of bridge and Solana in 7.46.0 was pushed back\n // and there's no way to turn on bridgeConfig for 7.47.0 without affecting 7.46.0 as well.\n // You will still get bridgeConfig returned from remoteFeatureFlagControllerState but you should use bridgeConfigV2 instead\n // Mobile's bridgeConfig will be permanently serving the disabled variation, so falling back to it in Mobile will be ok\n const rawMobileFlags =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfigV2;\n\n // Extension LaunchDarkly will not have the bridgeConfigV2 field, so we'll continue to use bridgeConfig\n const rawBridgeConfig =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfig;\n\n return processFeatureFlags(rawMobileFlags || rawBridgeConfig);\n}\n\n/**\n * Checks if the client version is greater than or equal to the minimum required version\n *\n * @param clientVersion - The client version\n * @param minRequiredVersion - The minimum required version\n * @returns True if the client version is greater than or equal to the minimum required version, false otherwise\n */\nexport const hasMinimumRequiredVersion = (\n clientVersion: string,\n minRequiredVersion: string,\n) => {\n const [clientMajor, clientMinor, clientPatch] = clientVersion\n .split('.')\n .map(Number);\n const [minRequiredMajor, minRequiredMinor, minRequiredPatch] =\n minRequiredVersion.split('.').map(Number);\n\n if (clientMajor > minRequiredMajor) {\n return true;\n }\n if (clientMajor === minRequiredMajor && clientMinor > minRequiredMinor) {\n return true;\n }\n return (\n clientMajor === minRequiredMajor &&\n clientMinor === minRequiredMinor &&\n clientPatch >= minRequiredPatch\n );\n};\n"]}
|
|
@@ -19,7 +19,10 @@ export declare const formatFeatureFlags: (bridgeFeatureFlags: FeatureFlagsPlatfo
|
|
|
19
19
|
standard: Record<string, string>;
|
|
20
20
|
other: Record<string, string>;
|
|
21
21
|
} | undefined> | undefined;
|
|
22
|
-
|
|
22
|
+
sse?: {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
minimumVersion: string;
|
|
25
|
+
} | undefined;
|
|
23
26
|
};
|
|
24
27
|
export declare const processFeatureFlags: (bridgeFeatureFlags: unknown) => FeatureFlagsPlatformConfig;
|
|
25
28
|
/**
|
|
@@ -31,4 +34,12 @@ export declare const processFeatureFlags: (bridgeFeatureFlags: unknown) => Featu
|
|
|
31
34
|
export declare function getBridgeFeatureFlags<T extends {
|
|
32
35
|
call(action: 'RemoteFeatureFlagController:getState'): RemoteFeatureFlagControllerState;
|
|
33
36
|
}>(messenger: T): FeatureFlagsPlatformConfig;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if the client version is greater than or equal to the minimum required version
|
|
39
|
+
*
|
|
40
|
+
* @param clientVersion - The client version
|
|
41
|
+
* @param minRequiredVersion - The minimum required version
|
|
42
|
+
* @returns True if the client version is greater than or equal to the minimum required version, false otherwise
|
|
43
|
+
*/
|
|
44
|
+
export declare const hasMinimumRequiredVersion: (clientVersion: string, minRequiredVersion: string) => boolean;
|
|
34
45
|
//# sourceMappingURL=feature-flags.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.d.cts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAKjG,OAAO,KAAK,EAAE,0BAA0B,EAAsB,qBAAiB;AAE/E,eAAO,MAAM,kBAAkB,uBACT,0BAA0B
|
|
1
|
+
{"version":3,"file":"feature-flags.d.cts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAKjG,OAAO,KAAK,EAAE,0BAA0B,EAAsB,qBAAiB;AAE/E,eAAO,MAAM,kBAAkB,uBACT,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;CAe/C,CAAC;AAEF,eAAO,MAAM,mBAAmB,uBACV,OAAO,KAC1B,0BAKF,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,CAAC,SAAS;IACR,IAAI,CACF,MAAM,EAAE,sCAAsC,GAC7C,gCAAgC,CAAC;CACrC,EACD,SAAS,EAAE,CAAC,GAAG,0BAA0B,CAmB1C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,kBACrB,MAAM,sBACD,MAAM,YAmB3B,CAAC"}
|
|
@@ -19,7 +19,10 @@ export declare const formatFeatureFlags: (bridgeFeatureFlags: FeatureFlagsPlatfo
|
|
|
19
19
|
standard: Record<string, string>;
|
|
20
20
|
other: Record<string, string>;
|
|
21
21
|
} | undefined> | undefined;
|
|
22
|
-
|
|
22
|
+
sse?: {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
minimumVersion: string;
|
|
25
|
+
} | undefined;
|
|
23
26
|
};
|
|
24
27
|
export declare const processFeatureFlags: (bridgeFeatureFlags: unknown) => FeatureFlagsPlatformConfig;
|
|
25
28
|
/**
|
|
@@ -31,4 +34,12 @@ export declare const processFeatureFlags: (bridgeFeatureFlags: unknown) => Featu
|
|
|
31
34
|
export declare function getBridgeFeatureFlags<T extends {
|
|
32
35
|
call(action: 'RemoteFeatureFlagController:getState'): RemoteFeatureFlagControllerState;
|
|
33
36
|
}>(messenger: T): FeatureFlagsPlatformConfig;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if the client version is greater than or equal to the minimum required version
|
|
39
|
+
*
|
|
40
|
+
* @param clientVersion - The client version
|
|
41
|
+
* @param minRequiredVersion - The minimum required version
|
|
42
|
+
* @returns True if the client version is greater than or equal to the minimum required version, false otherwise
|
|
43
|
+
*/
|
|
44
|
+
export declare const hasMinimumRequiredVersion: (clientVersion: string, minRequiredVersion: string) => boolean;
|
|
34
45
|
//# sourceMappingURL=feature-flags.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.d.mts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAKjG,OAAO,KAAK,EAAE,0BAA0B,EAAsB,qBAAiB;AAE/E,eAAO,MAAM,kBAAkB,uBACT,0BAA0B
|
|
1
|
+
{"version":3,"file":"feature-flags.d.mts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gCAAgC,EAAE,iDAAiD;AAKjG,OAAO,KAAK,EAAE,0BAA0B,EAAsB,qBAAiB;AAE/E,eAAO,MAAM,kBAAkB,uBACT,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;CAe/C,CAAC;AAEF,eAAO,MAAM,mBAAmB,uBACV,OAAO,KAC1B,0BAKF,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,CAAC,SAAS;IACR,IAAI,CACF,MAAM,EAAE,sCAAsC,GAC7C,gCAAgC,CAAC;CACrC,EACD,SAAS,EAAE,CAAC,GAAG,0BAA0B,CAmB1C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,kBACrB,MAAM,sBACD,MAAM,YAmB3B,CAAC"}
|
|
@@ -36,4 +36,26 @@ export function getBridgeFeatureFlags(messenger) {
|
|
|
36
36
|
const rawBridgeConfig = remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfig;
|
|
37
37
|
return processFeatureFlags(rawMobileFlags || rawBridgeConfig);
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Checks if the client version is greater than or equal to the minimum required version
|
|
41
|
+
*
|
|
42
|
+
* @param clientVersion - The client version
|
|
43
|
+
* @param minRequiredVersion - The minimum required version
|
|
44
|
+
* @returns True if the client version is greater than or equal to the minimum required version, false otherwise
|
|
45
|
+
*/
|
|
46
|
+
export const hasMinimumRequiredVersion = (clientVersion, minRequiredVersion) => {
|
|
47
|
+
const [clientMajor, clientMinor, clientPatch] = clientVersion
|
|
48
|
+
.split('.')
|
|
49
|
+
.map(Number);
|
|
50
|
+
const [minRequiredMajor, minRequiredMinor, minRequiredPatch] = minRequiredVersion.split('.').map(Number);
|
|
51
|
+
if (clientMajor > minRequiredMajor) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (clientMajor === minRequiredMajor && clientMinor > minRequiredMinor) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return (clientMajor === minRequiredMajor &&
|
|
58
|
+
clientMinor === minRequiredMinor &&
|
|
59
|
+
clientPatch >= minRequiredPatch);
|
|
60
|
+
};
|
|
39
61
|
//# sourceMappingURL=feature-flags.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EA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atureFlagControllerState } from '@metamask/remote-feature-flag-controller';\n\nimport { formatChainIdToCaip } from './caip-formatters';\nimport { validateFeatureFlagsResponse } from './validators';\nimport { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';\nimport type { FeatureFlagsPlatformConfig, ChainConfiguration } from '../types';\n\nexport const formatFeatureFlags = (\n bridgeFeatureFlags: FeatureFlagsPlatformConfig,\n) => {\n const getChainsObj = (chains: Record<string, ChainConfiguration>) =>\n Object.entries(chains).reduce(\n (acc, [chainId, value]) => ({\n ...acc,\n [formatChainIdToCaip(chainId)]: value,\n }),\n {},\n );\n\n return {\n ...bridgeFeatureFlags,\n chains: getChainsObj(bridgeFeatureFlags.chains),\n };\n};\n\nexport const processFeatureFlags = (\n bridgeFeatureFlags: unknown,\n): FeatureFlagsPlatformConfig => {\n if (validateFeatureFlagsResponse(bridgeFeatureFlags)) {\n return formatFeatureFlags(bridgeFeatureFlags);\n }\n return DEFAULT_FEATURE_FLAG_CONFIG;\n};\n\n/**\n * Gets the bridge feature flags from the remote feature flag controller\n *\n * @param messenger - Any messenger with access to RemoteFeatureFlagController:getState\n * @returns The bridge feature flags\n */\nexport function getBridgeFeatureFlags<\n T extends {\n call(\n action: 'RemoteFeatureFlagController:getState',\n ): RemoteFeatureFlagControllerState;\n },\n>(messenger: T): FeatureFlagsPlatformConfig {\n // This will return the bridgeConfig for the current platform even without specifying the platform\n const remoteFeatureFlagControllerState = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n\n // bridgeConfigV2 is the feature flag for the mobile app\n // bridgeConfig for Mobile has been deprecated since release of bridge and Solana in 7.46.0 was pushed back\n // and there's no way to turn on bridgeConfig for 7.47.0 without affecting 7.46.0 as well.\n // You will still get bridgeConfig returned from remoteFeatureFlagControllerState but you should use bridgeConfigV2 instead\n // Mobile's bridgeConfig will be permanently serving the disabled variation, so falling back to it in Mobile will be ok\n const rawMobileFlags =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfigV2;\n\n // Extension LaunchDarkly will not have the bridgeConfigV2 field, so we'll continue to use bridgeConfig\n const rawBridgeConfig =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfig;\n\n return processFeatureFlags(rawMobileFlags || rawBridgeConfig);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"feature-flags.mjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,8BAA0B;AACxD,OAAO,EAAE,4BAA4B,EAAE,yBAAqB;AAC5D,OAAO,EAAE,2BAA2B,EAAE,gCAA4B;AAGlE,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,kBAA8C,EAC9C,EAAE;IACF,MAAM,YAAY,GAAG,CAAC,MAA0C,EAAE,EAAE,CAClE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAC3B,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,GAAG,GAAG;QACN,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK;KACtC,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO;QACL,GAAG,kBAAkB;QACrB,MAAM,EAAE,YAAY,CAAC,kBAAkB,CAAC,MAAM,CAAC;KAChD,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,kBAA2B,EACC,EAAE;IAC9B,IAAI,4BAA4B,CAAC,kBAAkB,CAAC,EAAE;QACpD,OAAO,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;KAC/C;IACD,OAAO,2BAA2B,CAAC;AACrC,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAMnC,SAAY;IACZ,kGAAkG;IAClG,MAAM,gCAAgC,GAAG,SAAS,CAAC,IAAI,CACrD,sCAAsC,CACvC,CAAC;IAEF,wDAAwD;IACxD,2GAA2G;IAC3G,0FAA0F;IAC1F,2HAA2H;IAC3H,uHAAuH;IACvH,MAAM,cAAc,GAClB,gCAAgC,EAAE,kBAAkB,EAAE,cAAc,CAAC;IAEvE,uGAAuG;IACvG,MAAM,eAAe,GACnB,gCAAgC,EAAE,kBAAkB,EAAE,YAAY,CAAC;IAErE,OAAO,mBAAmB,CAAC,cAAc,IAAI,eAAe,CAAC,CAAC;AAChE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,aAAqB,EACrB,kBAA0B,EAC1B,EAAE;IACF,MAAM,CAAC,WAAW,EAAE,WAAW,EAAE,WAAW,CAAC,GAAG,aAAa;SAC1D,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,MAAM,CAAC,CAAC;IACf,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,EAAE,gBAAgB,CAAC,GAC1D,kBAAkB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAE5C,IAAI,WAAW,GAAG,gBAAgB,EAAE;QAClC,OAAO,IAAI,CAAC;KACb;IACD,IAAI,WAAW,KAAK,gBAAgB,IAAI,WAAW,GAAG,gBAAgB,EAAE;QACtE,OAAO,IAAI,CAAC;KACb;IACD,OAAO,CACL,WAAW,KAAK,gBAAgB;QAChC,WAAW,KAAK,gBAAgB;QAChC,WAAW,IAAI,gBAAgB,CAChC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { RemoteFeatureFlagControllerState } from '@metamask/remote-feature-flag-controller';\n\nimport { formatChainIdToCaip } from './caip-formatters';\nimport { validateFeatureFlagsResponse } from './validators';\nimport { DEFAULT_FEATURE_FLAG_CONFIG } from '../constants/bridge';\nimport type { FeatureFlagsPlatformConfig, ChainConfiguration } from '../types';\n\nexport const formatFeatureFlags = (\n bridgeFeatureFlags: FeatureFlagsPlatformConfig,\n) => {\n const getChainsObj = (chains: Record<string, ChainConfiguration>) =>\n Object.entries(chains).reduce(\n (acc, [chainId, value]) => ({\n ...acc,\n [formatChainIdToCaip(chainId)]: value,\n }),\n {},\n );\n\n return {\n ...bridgeFeatureFlags,\n chains: getChainsObj(bridgeFeatureFlags.chains),\n };\n};\n\nexport const processFeatureFlags = (\n bridgeFeatureFlags: unknown,\n): FeatureFlagsPlatformConfig => {\n if (validateFeatureFlagsResponse(bridgeFeatureFlags)) {\n return formatFeatureFlags(bridgeFeatureFlags);\n }\n return DEFAULT_FEATURE_FLAG_CONFIG;\n};\n\n/**\n * Gets the bridge feature flags from the remote feature flag controller\n *\n * @param messenger - Any messenger with access to RemoteFeatureFlagController:getState\n * @returns The bridge feature flags\n */\nexport function getBridgeFeatureFlags<\n T extends {\n call(\n action: 'RemoteFeatureFlagController:getState',\n ): RemoteFeatureFlagControllerState;\n },\n>(messenger: T): FeatureFlagsPlatformConfig {\n // This will return the bridgeConfig for the current platform even without specifying the platform\n const remoteFeatureFlagControllerState = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n\n // bridgeConfigV2 is the feature flag for the mobile app\n // bridgeConfig for Mobile has been deprecated since release of bridge and Solana in 7.46.0 was pushed back\n // and there's no way to turn on bridgeConfig for 7.47.0 without affecting 7.46.0 as well.\n // You will still get bridgeConfig returned from remoteFeatureFlagControllerState but you should use bridgeConfigV2 instead\n // Mobile's bridgeConfig will be permanently serving the disabled variation, so falling back to it in Mobile will be ok\n const rawMobileFlags =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfigV2;\n\n // Extension LaunchDarkly will not have the bridgeConfigV2 field, so we'll continue to use bridgeConfig\n const rawBridgeConfig =\n remoteFeatureFlagControllerState?.remoteFeatureFlags?.bridgeConfig;\n\n return processFeatureFlags(rawMobileFlags || rawBridgeConfig);\n}\n\n/**\n * Checks if the client version is greater than or equal to the minimum required version\n *\n * @param clientVersion - The client version\n * @param minRequiredVersion - The minimum required version\n * @returns True if the client version is greater than or equal to the minimum required version, false otherwise\n */\nexport const hasMinimumRequiredVersion = (\n clientVersion: string,\n minRequiredVersion: string,\n) => {\n const [clientMajor, clientMinor, clientPatch] = clientVersion\n .split('.')\n .map(Number);\n const [minRequiredMajor, minRequiredMinor, minRequiredPatch] =\n minRequiredVersion.split('.').map(Number);\n\n if (clientMajor > minRequiredMajor) {\n return true;\n }\n if (clientMajor === minRequiredMajor && clientMinor > minRequiredMinor) {\n return true;\n }\n return (\n clientMajor === minRequiredMajor &&\n clientMinor === minRequiredMinor &&\n clientPatch >= minRequiredPatch\n );\n};\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchServerEvents = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Streams server-sent events from the given URL
|
|
6
|
+
*
|
|
7
|
+
* @param url - The URL to stream events from
|
|
8
|
+
* @param options - The options for the SSE stream
|
|
9
|
+
* @param options.onMessage - The function to call when a message is received
|
|
10
|
+
* @param options.onError - The function to call when an error occurs
|
|
11
|
+
* @param options.onClose - The function to call when the stream finishes successfully
|
|
12
|
+
* @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.
|
|
13
|
+
*/
|
|
14
|
+
const fetchServerEvents = async (url, { onMessage, onError, onClose, fetchFn, ...requestOptions }) => {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetchFn(url, requestOptions);
|
|
17
|
+
if (!response.ok || !response.body) {
|
|
18
|
+
throw new Error(`${response.status}`);
|
|
19
|
+
}
|
|
20
|
+
const reader = response.body.getReader();
|
|
21
|
+
const decoder = new TextDecoder('utf-8');
|
|
22
|
+
let buffer = '';
|
|
23
|
+
while (true) {
|
|
24
|
+
const { done, value } = await reader.read();
|
|
25
|
+
if (done) {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
buffer += decoder.decode(value, { stream: true });
|
|
29
|
+
// Split SSE messages at double newlines
|
|
30
|
+
const parts = buffer.split('\n\n');
|
|
31
|
+
buffer = parts.pop() || '';
|
|
32
|
+
// Split chunks into lines and parse the data
|
|
33
|
+
for (const chunk of parts) {
|
|
34
|
+
const lines = chunk.split('\n');
|
|
35
|
+
let eventName;
|
|
36
|
+
const dataLines = [];
|
|
37
|
+
for (const line of lines) {
|
|
38
|
+
if (line.startsWith('event:')) {
|
|
39
|
+
eventName = line.slice(6).trim();
|
|
40
|
+
}
|
|
41
|
+
else if (line.startsWith('data:')) {
|
|
42
|
+
dataLines.push(line.slice(5).trim());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (eventName === 'error') {
|
|
46
|
+
throw new Error(`Bridge-api error: ${dataLines.join('\n')}`);
|
|
47
|
+
}
|
|
48
|
+
if (dataLines.length > 0) {
|
|
49
|
+
const parsedJSONData = JSON.parse(dataLines.join('\n'));
|
|
50
|
+
onMessage(parsedJSONData, eventName);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
onClose?.();
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
onError?.(error);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.fetchServerEvents = fetchServerEvents;
|
|
61
|
+
//# sourceMappingURL=fetch-server-events.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-server-events.cjs","sourceRoot":"","sources":["../../src/utils/fetch-server-events.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;GASG;AACI,MAAM,iBAAiB,GAAG,KAAK,EACpC,GAAW,EACX,EACE,SAAS,EACT,OAAO,EACP,OAAO,EACP,OAAO,EACP,GAAG,cAAc,EAMlB,EACD,EAAE;IACF,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;QACpD,IAAI,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;YAClC,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,CAA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CAAC,EAAE,CAAC,CAAC;iBAC9D;gBACD,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE;oBACxB,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;oBACxD,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;iBACtC;aACF;SACF;QACD,OAAO,EAAE,EAAE,CAAC;KACb;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;KAClB;AACH,CAAC,CAAC;AAjEW,QAAA,iBAAiB,qBAiE5B","sourcesContent":["/**\n * Streams server-sent events from the given URL\n *\n * @param url - The URL to stream events from\n * @param options - The options for the SSE stream\n * @param options.onMessage - The function to call when a message is received\n * @param options.onError - The function to call when an error occurs\n * @param options.onClose - The function to call when the stream finishes successfully\n * @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.\n */\nexport const fetchServerEvents = async (\n url: string,\n {\n onMessage,\n onError,\n onClose,\n fetchFn,\n ...requestOptions\n }: RequestInit & {\n onMessage: (data: Record<string, unknown>, eventName?: string) => void;\n onError?: (err: unknown) => void;\n onClose?: () => void;\n fetchFn: typeof fetch;\n },\n) => {\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder('utf-8');\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n // Split SSE messages at double newlines\n const parts = buffer.split('\\n\\n');\n buffer = parts.pop() || '';\n\n // Split chunks into lines and parse the data\n for (const chunk of parts) {\n const lines = chunk.split('\\n');\n let eventName: string | undefined;\n const dataLines: string[] = [];\n\n for (const line of lines) {\n if (line.startsWith('event:')) {\n eventName = line.slice(6).trim();\n } else if (line.startsWith('data:')) {\n dataLines.push(line.slice(5).trim());\n }\n }\n\n if (eventName === 'error') {\n throw new Error(`Bridge-api error: ${dataLines.join('\\n')}`);\n }\n if (dataLines.length > 0) {\n const parsedJSONData = JSON.parse(dataLines.join('\\n'));\n onMessage(parsedJSONData, eventName);\n }\n }\n }\n onClose?.();\n } catch (error) {\n onError?.(error);\n }\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streams server-sent events from the given URL
|
|
3
|
+
*
|
|
4
|
+
* @param url - The URL to stream events from
|
|
5
|
+
* @param options - The options for the SSE stream
|
|
6
|
+
* @param options.onMessage - The function to call when a message is received
|
|
7
|
+
* @param options.onError - The function to call when an error occurs
|
|
8
|
+
* @param options.onClose - The function to call when the stream finishes successfully
|
|
9
|
+
* @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchServerEvents: (url: string, { onMessage, onError, onClose, fetchFn, ...requestOptions }: RequestInit & {
|
|
12
|
+
onMessage: (data: Record<string, unknown>, eventName?: string) => void;
|
|
13
|
+
onError?: ((err: unknown) => void) | undefined;
|
|
14
|
+
onClose?: (() => void) | undefined;
|
|
15
|
+
fetchFn: typeof fetch;
|
|
16
|
+
}) => Promise<void>;
|
|
17
|
+
//# sourceMappingURL=fetch-server-events.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-server-events.d.cts","sourceRoot":"","sources":["../../src/utils/fetch-server-events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,QACvB,MAAM;sBAQS,OAAO,MAAM,EAAE,OAAO,CAAC,cAAc,MAAM,KAAK,IAAI;qBACtD,OAAO,KAAK,IAAI;qBAChB,IAAI;aACX,YAAY;mBAqDxB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streams server-sent events from the given URL
|
|
3
|
+
*
|
|
4
|
+
* @param url - The URL to stream events from
|
|
5
|
+
* @param options - The options for the SSE stream
|
|
6
|
+
* @param options.onMessage - The function to call when a message is received
|
|
7
|
+
* @param options.onError - The function to call when an error occurs
|
|
8
|
+
* @param options.onClose - The function to call when the stream finishes successfully
|
|
9
|
+
* @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchServerEvents: (url: string, { onMessage, onError, onClose, fetchFn, ...requestOptions }: RequestInit & {
|
|
12
|
+
onMessage: (data: Record<string, unknown>, eventName?: string) => void;
|
|
13
|
+
onError?: ((err: unknown) => void) | undefined;
|
|
14
|
+
onClose?: (() => void) | undefined;
|
|
15
|
+
fetchFn: typeof fetch;
|
|
16
|
+
}) => Promise<void>;
|
|
17
|
+
//# sourceMappingURL=fetch-server-events.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-server-events.d.mts","sourceRoot":"","sources":["../../src/utils/fetch-server-events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,QACvB,MAAM;sBAQS,OAAO,MAAM,EAAE,OAAO,CAAC,cAAc,MAAM,KAAK,IAAI;qBACtD,OAAO,KAAK,IAAI;qBAChB,IAAI;aACX,YAAY;mBAqDxB,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streams server-sent events from the given URL
|
|
3
|
+
*
|
|
4
|
+
* @param url - The URL to stream events from
|
|
5
|
+
* @param options - The options for the SSE stream
|
|
6
|
+
* @param options.onMessage - The function to call when a message is received
|
|
7
|
+
* @param options.onError - The function to call when an error occurs
|
|
8
|
+
* @param options.onClose - The function to call when the stream finishes successfully
|
|
9
|
+
* @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.
|
|
10
|
+
*/
|
|
11
|
+
export const fetchServerEvents = async (url, { onMessage, onError, onClose, fetchFn, ...requestOptions }) => {
|
|
12
|
+
try {
|
|
13
|
+
const response = await fetchFn(url, requestOptions);
|
|
14
|
+
if (!response.ok || !response.body) {
|
|
15
|
+
throw new Error(`${response.status}`);
|
|
16
|
+
}
|
|
17
|
+
const reader = response.body.getReader();
|
|
18
|
+
const decoder = new TextDecoder('utf-8');
|
|
19
|
+
let buffer = '';
|
|
20
|
+
while (true) {
|
|
21
|
+
const { done, value } = await reader.read();
|
|
22
|
+
if (done) {
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
buffer += decoder.decode(value, { stream: true });
|
|
26
|
+
// Split SSE messages at double newlines
|
|
27
|
+
const parts = buffer.split('\n\n');
|
|
28
|
+
buffer = parts.pop() || '';
|
|
29
|
+
// Split chunks into lines and parse the data
|
|
30
|
+
for (const chunk of parts) {
|
|
31
|
+
const lines = chunk.split('\n');
|
|
32
|
+
let eventName;
|
|
33
|
+
const dataLines = [];
|
|
34
|
+
for (const line of lines) {
|
|
35
|
+
if (line.startsWith('event:')) {
|
|
36
|
+
eventName = line.slice(6).trim();
|
|
37
|
+
}
|
|
38
|
+
else if (line.startsWith('data:')) {
|
|
39
|
+
dataLines.push(line.slice(5).trim());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (eventName === 'error') {
|
|
43
|
+
throw new Error(`Bridge-api error: ${dataLines.join('\n')}`);
|
|
44
|
+
}
|
|
45
|
+
if (dataLines.length > 0) {
|
|
46
|
+
const parsedJSONData = JSON.parse(dataLines.join('\n'));
|
|
47
|
+
onMessage(parsedJSONData, eventName);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
onClose?.();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
onError?.(error);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=fetch-server-events.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-server-events.mjs","sourceRoot":"","sources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AAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;wBAC7B,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;qBAClC;yBAAM,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;wBACnC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;qBACtC;iBACF;gBAED,IAAI,SAAS,KAAK,OAAO,EAAE;oBACzB,MAAM,IAAI,KAAK,CAAC,qBAAqB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;iBAC9D;gBACD,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE;oBACxB,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;oBACxD,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;iBACtC;aACF;SACF;QACD,OAAO,EAAE,EAAE,CAAC;KACb;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;KAClB;AACH,CAAC,CAAC","sourcesContent":["/**\n * Streams server-sent events from the given URL\n *\n * @param url - The URL to stream events from\n * @param options - The options for the SSE stream\n * @param options.onMessage - The function to call when a message is received\n * @param options.onError - The function to call when an error occurs\n * @param options.onClose - The function to call when the stream finishes successfully\n * @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.\n */\nexport const fetchServerEvents = async (\n url: string,\n {\n onMessage,\n onError,\n onClose,\n fetchFn,\n ...requestOptions\n }: RequestInit & {\n onMessage: (data: Record<string, unknown>, eventName?: string) => void;\n onError?: (err: unknown) => void;\n onClose?: () => void;\n fetchFn: typeof fetch;\n },\n) => {\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder('utf-8');\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n // Split SSE messages at double newlines\n const parts = buffer.split('\\n\\n');\n buffer = parts.pop() || '';\n\n // Split chunks into lines and parse the data\n for (const chunk of parts) {\n const lines = chunk.split('\\n');\n let eventName: string | undefined;\n const dataLines: string[] = [];\n\n for (const line of lines) {\n if (line.startsWith('event:')) {\n eventName = line.slice(6).trim();\n } else if (line.startsWith('data:')) {\n dataLines.push(line.slice(5).trim());\n }\n }\n\n if (eventName === 'error') {\n throw new Error(`Bridge-api error: ${dataLines.join('\\n')}`);\n }\n if (dataLines.length > 0) {\n const parsedJSONData = JSON.parse(dataLines.join('\\n'));\n onMessage(parsedJSONData, eventName);\n }\n }\n }\n onClose?.();\n } catch (error) {\n onError?.(error);\n }\n};\n"]}
|
package/dist/utils/fetch.cjs
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchBridgeQuoteStream = exports.fetchAssetPrices = exports.fetchBridgeQuotes = exports.fetchBridgeTokens = exports.getClientHeaders = void 0;
|
|
4
4
|
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
-
const fetch_event_source_1 = require("@microsoft/fetch-event-source");
|
|
6
|
-
const bridge_1 = require("./bridge.cjs");
|
|
7
5
|
const caip_formatters_1 = require("./caip-formatters.cjs");
|
|
6
|
+
const fetch_server_events_1 = require("./fetch-server-events.cjs");
|
|
8
7
|
const validators_1 = require("./validators.cjs");
|
|
9
8
|
const getClientHeaders = (clientId, clientVersion) => ({
|
|
10
9
|
'X-Client-Id': clientId,
|
|
@@ -102,10 +101,6 @@ async function fetchBridgeQuotes(request, signal, clientId, fetchFn, bridgeApiBa
|
|
|
102
101
|
const filteredQuotes = quotes
|
|
103
102
|
.filter((quoteResponse) => {
|
|
104
103
|
try {
|
|
105
|
-
const isBitcoinQuote = (0, bridge_1.isBitcoinChainId)(request.srcChainId);
|
|
106
|
-
if (isBitcoinQuote) {
|
|
107
|
-
return (0, validators_1.validateBitcoinQuoteResponse)(quoteResponse);
|
|
108
|
-
}
|
|
109
104
|
return (0, validators_1.validateQuoteResponse)(quoteResponse);
|
|
110
105
|
}
|
|
111
106
|
catch (error) {
|
|
@@ -214,18 +209,15 @@ exports.fetchAssetPrices = fetchAssetPrices;
|
|
|
214
209
|
*/
|
|
215
210
|
async function fetchBridgeQuoteStream(fetchFn, request, signal, clientId, bridgeApiBaseUrl, serverEventHandlers, clientVersion) {
|
|
216
211
|
const queryParams = formatQueryParams(request);
|
|
217
|
-
const onMessage = (
|
|
212
|
+
const onMessage = (quoteResponse) => {
|
|
218
213
|
const uniqueValidationFailures = new Set([]);
|
|
219
|
-
if (event.data === '') {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const quoteResponse = JSON.parse(event.data);
|
|
223
214
|
try {
|
|
224
|
-
(0, validators_1.validateQuoteResponse)(quoteResponse)
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
215
|
+
if ((0, validators_1.validateQuoteResponse)(quoteResponse)) {
|
|
216
|
+
// eslint-disable-next-line promise/catch-or-return, @typescript-eslint/no-floating-promises
|
|
217
|
+
serverEventHandlers.onValidQuoteReceived(quoteResponse).then((v) => {
|
|
218
|
+
return v;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
229
221
|
}
|
|
230
222
|
catch (error) {
|
|
231
223
|
if (error instanceof superstruct_1.StructError) {
|
|
@@ -251,23 +243,21 @@ async function fetchBridgeQuoteStream(fetchFn, request, signal, clientId, bridge
|
|
|
251
243
|
}
|
|
252
244
|
};
|
|
253
245
|
const urlStream = `${bridgeApiBaseUrl}/getQuoteStream?${queryParams}`;
|
|
254
|
-
await (0,
|
|
246
|
+
await (0, fetch_server_events_1.fetchServerEvents)(urlStream, {
|
|
255
247
|
headers: {
|
|
256
248
|
...(0, exports.getClientHeaders)(clientId, clientVersion),
|
|
257
249
|
'Content-Type': 'text/event-stream',
|
|
258
250
|
},
|
|
259
251
|
signal,
|
|
260
|
-
|
|
261
|
-
|
|
252
|
+
onMessage,
|
|
253
|
+
onError: (e) => {
|
|
262
254
|
// Rethrow error to prevent silent fetch failures
|
|
263
|
-
throw
|
|
255
|
+
throw e;
|
|
264
256
|
},
|
|
265
|
-
|
|
257
|
+
onClose: () => {
|
|
266
258
|
serverEventHandlers.onClose();
|
|
267
259
|
},
|
|
268
|
-
|
|
269
|
-
openWhenHidden: false,
|
|
270
|
-
fetch: fetchFn,
|
|
260
|
+
fetchFn,
|
|
271
261
|
});
|
|
272
262
|
}
|
|
273
263
|
exports.fetchBridgeQuoteStream = fetchBridgeQuoteStream;
|