@metamask/bridge-controller 59.0.0 → 60.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/bridge-controller.cjs.map +1 -1
  3. package/dist/bridge-controller.d.cts +3 -3
  4. package/dist/bridge-controller.d.mts +3 -3
  5. package/dist/bridge-controller.mjs.map +1 -1
  6. package/dist/constants/bridge.cjs +1 -0
  7. package/dist/constants/bridge.cjs.map +1 -1
  8. package/dist/constants/bridge.d.cts +2 -2
  9. package/dist/constants/bridge.d.cts.map +1 -1
  10. package/dist/constants/bridge.d.mts +2 -2
  11. package/dist/constants/bridge.d.mts.map +1 -1
  12. package/dist/constants/bridge.mjs +2 -1
  13. package/dist/constants/bridge.mjs.map +1 -1
  14. package/dist/constants/tokens.cjs +10 -0
  15. package/dist/constants/tokens.cjs.map +1 -1
  16. package/dist/constants/tokens.d.cts +7 -0
  17. package/dist/constants/tokens.d.cts.map +1 -1
  18. package/dist/constants/tokens.d.mts +7 -0
  19. package/dist/constants/tokens.d.mts.map +1 -1
  20. package/dist/constants/tokens.mjs +11 -1
  21. package/dist/constants/tokens.mjs.map +1 -1
  22. package/dist/index.cjs +5 -1
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +2 -1
  25. package/dist/index.d.cts.map +1 -1
  26. package/dist/index.d.mts +2 -1
  27. package/dist/index.d.mts.map +1 -1
  28. package/dist/index.mjs +1 -0
  29. package/dist/index.mjs.map +1 -1
  30. package/dist/selectors.cjs.map +1 -1
  31. package/dist/selectors.d.cts +2208 -0
  32. package/dist/selectors.d.cts.map +1 -1
  33. package/dist/selectors.d.mts +2208 -0
  34. package/dist/selectors.d.mts.map +1 -1
  35. package/dist/selectors.mjs.map +1 -1
  36. package/dist/types.cjs +1 -0
  37. package/dist/types.cjs.map +1 -1
  38. package/dist/types.d.cts +7 -4
  39. package/dist/types.d.cts.map +1 -1
  40. package/dist/types.d.mts +7 -4
  41. package/dist/types.d.mts.map +1 -1
  42. package/dist/types.mjs +1 -0
  43. package/dist/types.mjs.map +1 -1
  44. package/dist/utils/assets.cjs.map +1 -1
  45. package/dist/utils/assets.mjs.map +1 -1
  46. package/dist/utils/balance.cjs.map +1 -1
  47. package/dist/utils/balance.mjs.map +1 -1
  48. package/dist/utils/bridge.cjs +12 -3
  49. package/dist/utils/bridge.cjs.map +1 -1
  50. package/dist/utils/bridge.d.cts +10 -1
  51. package/dist/utils/bridge.d.cts.map +1 -1
  52. package/dist/utils/bridge.d.mts +10 -1
  53. package/dist/utils/bridge.d.mts.map +1 -1
  54. package/dist/utils/bridge.mjs +11 -3
  55. package/dist/utils/bridge.mjs.map +1 -1
  56. package/dist/utils/caip-formatters.cjs +6 -0
  57. package/dist/utils/caip-formatters.cjs.map +1 -1
  58. package/dist/utils/caip-formatters.d.cts.map +1 -1
  59. package/dist/utils/caip-formatters.d.mts.map +1 -1
  60. package/dist/utils/caip-formatters.mjs +8 -2
  61. package/dist/utils/caip-formatters.mjs.map +1 -1
  62. package/dist/utils/feature-flags.cjs.map +1 -1
  63. package/dist/utils/feature-flags.mjs.map +1 -1
  64. package/dist/utils/fetch-server-events.cjs.map +1 -1
  65. package/dist/utils/fetch-server-events.mjs.map +1 -1
  66. package/dist/utils/fetch.cjs.map +1 -1
  67. package/dist/utils/fetch.mjs.map +1 -1
  68. package/dist/utils/metrics/properties.cjs.map +1 -1
  69. package/dist/utils/metrics/properties.mjs.map +1 -1
  70. package/dist/utils/quote-fees.cjs +11 -7
  71. package/dist/utils/quote-fees.cjs.map +1 -1
  72. package/dist/utils/quote-fees.d.cts.map +1 -1
  73. package/dist/utils/quote-fees.d.mts.map +1 -1
  74. package/dist/utils/quote-fees.mjs +11 -7
  75. package/dist/utils/quote-fees.mjs.map +1 -1
  76. package/dist/utils/quote.cjs.map +1 -1
  77. package/dist/utils/quote.d.cts +2 -2
  78. package/dist/utils/quote.d.cts.map +1 -1
  79. package/dist/utils/quote.d.mts +2 -2
  80. package/dist/utils/quote.d.mts.map +1 -1
  81. package/dist/utils/quote.mjs.map +1 -1
  82. package/dist/utils/slippage.cjs.map +1 -1
  83. package/dist/utils/slippage.mjs.map +1 -1
  84. package/dist/utils/snaps.cjs +3 -1
  85. package/dist/utils/snaps.cjs.map +1 -1
  86. package/dist/utils/snaps.d.cts +3 -1
  87. package/dist/utils/snaps.d.cts.map +1 -1
  88. package/dist/utils/snaps.d.mts +3 -1
  89. package/dist/utils/snaps.d.mts.map +1 -1
  90. package/dist/utils/snaps.mjs +3 -1
  91. package/dist/utils/snaps.mjs.map +1 -1
  92. package/dist/utils/trade-utils.cjs +65 -0
  93. package/dist/utils/trade-utils.cjs.map +1 -0
  94. package/dist/utils/trade-utils.d.cts +50 -0
  95. package/dist/utils/trade-utils.d.cts.map +1 -0
  96. package/dist/utils/trade-utils.d.mts +50 -0
  97. package/dist/utils/trade-utils.d.mts.map +1 -0
  98. package/dist/utils/trade-utils.mjs +58 -0
  99. package/dist/utils/trade-utils.mjs.map +1 -0
  100. package/dist/utils/validators.cjs +17 -3
  101. package/dist/utils/validators.cjs.map +1 -1
  102. package/dist/utils/validators.d.cts +74 -9
  103. package/dist/utils/validators.d.cts.map +1 -1
  104. package/dist/utils/validators.d.mts +74 -9
  105. package/dist/utils/validators.d.mts.map +1 -1
  106. package/dist/utils/validators.mjs +16 -2
  107. package/dist/utils/validators.mjs.map +1 -1
  108. package/package.json +5 -5
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isCrossChain = exports.isEvmQuoteResponse = exports.isNonEvmChainId = exports.isBitcoinChainId = exports.isSolanaChainId = exports.isNativeAddress = exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getNativeAssetForChainId = exports.getDefaultBridgeControllerState = void 0;
3
+ exports.isCrossChain = exports.isEvmQuoteResponse = exports.isNonEvmChainId = exports.isTronChainId = exports.isBitcoinChainId = exports.isSolanaChainId = exports.isNativeAddress = exports.isSwapsDefaultTokenSymbol = exports.isSwapsDefaultTokenAddress = exports.sumHexes = exports.isEthUsdt = exports.getEthUsdtResetData = exports.getNativeAssetForChainId = exports.getDefaultBridgeControllerState = void 0;
4
4
  const constants_1 = require("@ethersproject/constants");
5
5
  const contracts_1 = require("@ethersproject/contracts");
6
6
  const keyring_api_1 = require("@metamask/keyring-api");
@@ -137,15 +137,24 @@ const isBitcoinChainId = (chainId) => {
137
137
  return chainId.toString() === types_1.ChainId.BTC.toString();
138
138
  };
139
139
  exports.isBitcoinChainId = isBitcoinChainId;
140
+ const isTronChainId = (chainId) => {
141
+ if ((0, utils_1.isCaipChainId)(chainId)) {
142
+ return chainId === keyring_api_1.TrxScope.Mainnet.toString();
143
+ }
144
+ return chainId.toString() === types_1.ChainId.TRON.toString();
145
+ };
146
+ exports.isTronChainId = isTronChainId;
140
147
  /**
141
148
  * Checks if a chain ID represents a non-EVM blockchain supported by swaps
142
- * Currently supports Solana and Bitcoin
149
+ * Currently supports Solana, Bitcoin and Tron
143
150
  *
144
151
  * @param chainId - The chain ID to check
145
152
  * @returns True if the chain is a supported non-EVM chain, false otherwise
146
153
  */
147
154
  const isNonEvmChainId = (chainId) => {
148
- return (0, exports.isSolanaChainId)(chainId) || (0, exports.isBitcoinChainId)(chainId);
155
+ return ((0, exports.isSolanaChainId)(chainId) ||
156
+ (0, exports.isBitcoinChainId)(chainId) ||
157
+ (0, exports.isTronChainId)(chainId));
149
158
  };
150
159
  exports.isNonEvmChainId = isNonEvmChainId;
151
160
  const isEvmQuoteResponse = (quoteResponse) => {
@@ -1 +1 @@
1
- {"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,wDAAoD;AACpD,uDAA2D;AAC3D,mEAAuD;AAEvD,2CAA6E;AAE7E,2DAI2B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C;AAJW,QAAA,eAAe,mBAI1B;AAEK,MAAM,kBAAkB,GAAG,CAChC,aAA4B,EACY,EAAE;IAC1C,OAAO,CAAC,IAAA,uBAAe,EAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC;AAJW,QAAA,kBAAkB,sBAI7B;AAEF;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAC1B,UAA6C,EAC7C,WAAgD,EAChD,EAAE;IACF,IAAI;QACF,IAAI,CAAC,WAAW,EAAE;YAChB,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAA,qCAAmB,EAAC,UAAU,CAAC,KAAK,IAAA,qCAAmB,EAAC,WAAW,CAAC,CAAC;KAC7E;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC,CAAC;AAZW,QAAA,YAAY,gBAYvB","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
+ {"version":3,"file":"bridge.cjs","sourceRoot":"","sources":["../../src/utils/bridge.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,wDAAoD;AACpD,uDAAqE;AACrE,mEAAuD;AAEvD,2CAA6E;AAE7E,2DAI2B;AAC3B,oDAI6B;AAC7B,oDAAgD;AAChD,oDAI6B;AAQ7B,wCAAmC;AAE5B,MAAM,+BAA+B,GAAG,GAA0B,EAAE;IACzE,OAAO,wCAA+B,CAAC;AACzC,CAAC,CAAC;AAFW,QAAA,+BAA+B,mCAE1C;AAEF;;;;;;;GAOG;AACH,MAAM,2BAA2B,GAAG,CAClC,OAAoB,EACpB,oBAAyD,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AAe,GAAG,CAC7B,OAA0C,EACjC,EAAE;IACX,OAAO,CACL,IAAA,uBAAe,EAAC,OAAO,CAAC;QACxB,IAAA,wBAAgB,EAAC,OAAO,CAAC;QACzB,IAAA,qBAAa,EAAC,OAAO,CAAC,CACvB,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,eAAe,mBAQ1B;AAEK,MAAM,kBAAkB,GAAG,CAChC,aAA4B,EACoB,EAAE;IAClD,OAAO,CAAC,IAAA,uBAAe,EAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,CAAC,CAAC;AAJW,QAAA,kBAAkB,sBAI7B;AAEF;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAC1B,UAA6C,EAC7C,WAAgD,EAChD,EAAE;IACF,IAAI,CAAC;QACH,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,IAAA,qCAAmB,EAAC,UAAU,CAAC,KAAK,IAAA,qCAAmB,EAAC,WAAW,CAAC,CAAC;IAC9E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAZW,QAAA,YAAY,gBAYvB","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } 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\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\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 (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, 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"]}
@@ -51,9 +51,10 @@ export declare const isNativeAddress: (address?: string | null) => boolean;
51
51
  */
52
52
  export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
53
53
  export declare const isBitcoinChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
54
+ export declare const isTronChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
54
55
  /**
55
56
  * Checks if a chain ID represents a non-EVM blockchain supported by swaps
56
- * Currently supports Solana and Bitcoin
57
+ * Currently supports Solana, Bitcoin and Tron
57
58
  *
58
59
  * @param chainId - The chain ID to check
59
60
  * @returns True if the chain is a supported non-EVM chain, false otherwise
@@ -67,6 +68,14 @@ export declare const isEvmQuoteResponse: (quoteResponse: QuoteResponse) => quote
67
68
  data: string;
68
69
  gasLimit: number | null;
69
70
  effectiveGas?: number | undefined;
71
+ }, {
72
+ chainId: number;
73
+ to: string;
74
+ from: string;
75
+ value: string;
76
+ data: string;
77
+ gasLimit: number | null;
78
+ effectiveGas?: number | undefined;
70
79
  }>;
71
80
  /**
72
81
  * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
@@ -1 +1 @@
1
- {"version":3,"file":"bridge.d.cts","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,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C;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,eAAO,MAAM,aAAa,YAAa,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAKzE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,YACjB,mBAAmB,CAAC,YAAY,CAAC,KACzC,OAMF,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"}
@@ -51,9 +51,10 @@ export declare const isNativeAddress: (address?: string | null) => boolean;
51
51
  */
52
52
  export declare const isSolanaChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
53
53
  export declare const isBitcoinChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
54
+ export declare const isTronChainId: (chainId: Hex | number | CaipChainId | string) => boolean;
54
55
  /**
55
56
  * Checks if a chain ID represents a non-EVM blockchain supported by swaps
56
- * Currently supports Solana and Bitcoin
57
+ * Currently supports Solana, Bitcoin and Tron
57
58
  *
58
59
  * @param chainId - The chain ID to check
59
60
  * @returns True if the chain is a supported non-EVM chain, false otherwise
@@ -67,6 +68,14 @@ export declare const isEvmQuoteResponse: (quoteResponse: QuoteResponse) => quote
67
68
  data: string;
68
69
  gasLimit: number | null;
69
70
  effectiveGas?: number | undefined;
71
+ }, {
72
+ chainId: number;
73
+ to: string;
74
+ from: string;
75
+ value: string;
76
+ data: string;
77
+ gasLimit: number | null;
78
+ effectiveGas?: number | undefined;
70
79
  }>;
71
80
  /**
72
81
  * Checks whether the transaction is a cross-chain transaction by comparing the source and destination chainIds
@@ -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,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,e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AyB,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,eAAO,MAAM,aAAa,YAAa,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,YAKzE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,YACjB,mBAAmB,CAAC,YAAY,CAAC,KACzC,OAMF,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"}
@@ -1,6 +1,6 @@
1
1
  import { AddressZero } from "@ethersproject/constants";
2
2
  import { Contract } from "@ethersproject/contracts";
3
- import { BtcScope, SolScope } from "@metamask/keyring-api";
3
+ import { BtcScope, SolScope, TrxScope } from "@metamask/keyring-api";
4
4
  import { abiERC20 } from "@metamask/metamask-eth-abis";
5
5
  import { isCaipChainId, isStrictHexString } from "@metamask/utils";
6
6
  import { formatChainIdToCaip, formatChainIdToDec, formatChainIdToHex } from "./caip-formatters.mjs";
@@ -124,15 +124,23 @@ export const isBitcoinChainId = (chainId) => {
124
124
  }
125
125
  return chainId.toString() === ChainId.BTC.toString();
126
126
  };
127
+ export const isTronChainId = (chainId) => {
128
+ if (isCaipChainId(chainId)) {
129
+ return chainId === TrxScope.Mainnet.toString();
130
+ }
131
+ return chainId.toString() === ChainId.TRON.toString();
132
+ };
127
133
  /**
128
134
  * Checks if a chain ID represents a non-EVM blockchain supported by swaps
129
- * Currently supports Solana and Bitcoin
135
+ * Currently supports Solana, Bitcoin and Tron
130
136
  *
131
137
  * @param chainId - The chain ID to check
132
138
  * @returns True if the chain is a supported non-EVM chain, false otherwise
133
139
  */
134
140
  export const isNonEvmChainId = (chainId) => {
135
- return isSolanaChainId(chainId) || isBitcoinChainId(chainId);
141
+ return (isSolanaChainId(chainId) ||
142
+ isBitcoinChainId(chainId) ||
143
+ isTronChainId(chainId));
136
144
  };
137
145
  export const isEvmQuoteResponse = (quoteResponse) => {
138
146
  return !isNonEvmChainId(quoteResponse.quote.srcChainId);
@@ -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,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,oB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C,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,MAAM,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
+ {"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,QAAQ,EAAE,8BAA8B;AACrE,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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,CAAC;QACf,CAAC;QACD,OAAO,mBAAmB,CAAC,UAAU,CAAC,KAAK,mBAAmB,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { AddressZero } from '@ethersproject/constants';\nimport { Contract } from '@ethersproject/contracts';\nimport { BtcScope, SolScope, TrxScope } 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\nexport const isTronChainId = (chainId: Hex | number | CaipChainId | string) => {\n if (isCaipChainId(chainId)) {\n return chainId === TrxScope.Mainnet.toString();\n }\n return chainId.toString() === ChainId.TRON.toString();\n};\n\n/**\n * Checks if a chain ID represents a non-EVM blockchain supported by swaps\n * Currently supports Solana, Bitcoin and Tron\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 (\n isSolanaChainId(chainId) ||\n isBitcoinChainId(chainId) ||\n isTronChainId(chainId)\n );\n};\n\nexport const isEvmQuoteResponse = (\n quoteResponse: QuoteResponse,\n): quoteResponse is QuoteResponse<TxData, 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"]}
@@ -28,6 +28,9 @@ const formatChainIdToCaip = (chainId) => {
28
28
  if ((0, bridge_1.isBitcoinChainId)(chainId)) {
29
29
  return keyring_api_1.BtcScope.Mainnet;
30
30
  }
31
+ if ((0, bridge_1.isTronChainId)(chainId)) {
32
+ return keyring_api_1.TrxScope.Mainnet;
33
+ }
31
34
  return (0, multichain_network_controller_1.toEvmCaipChainId)((0, utils_1.numberToHex)(Number(chainId)));
32
35
  };
33
36
  exports.formatChainIdToCaip = formatChainIdToCaip;
@@ -47,6 +50,9 @@ const formatChainIdToDec = (chainId) => {
47
50
  if (chainId === keyring_api_1.BtcScope.Mainnet) {
48
51
  return types_1.ChainId.BTC;
49
52
  }
53
+ if (chainId === keyring_api_1.TrxScope.Mainnet) {
54
+ return types_1.ChainId.TRON;
55
+ }
50
56
  if ((0, utils_1.isCaipChainId)(chainId)) {
51
57
  return Number(chainId.split(':').at(-1));
52
58
  }
@@ -1 +1 @@
1
- {"version":3,"file":"caip-form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CAAC,EAAE;QAC5B,OAAO,uBAAW,CAAC;KACpB;IACD,MAAM,oBAAoB,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,2EAA2E;IAC3E,oDAAoD;IACpD,IAAI,CAAC,oBAAoB,EAAE;QACzB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;KACpC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC;AAhBW,QAAA,4BAA4B,gCAgBvC;AAEF;;;;;;GAMG;AACI,MAAM,sBAAsB,GAAG,CACpC,gBAA8C,EAC9C,OAA0C,EACf,EAAE;IAC7B,IAAI,IAAA,uBAAe,EAAC,gBAAgB,CAAC,EAAE;QACrC,OAAO,gBAAgB,CAAC;KACzB;IACD,IAAI,IAAA,wBAAe,EAAC,gBAAgB,CAAC,EAAE;QACrC,OAAO,IAAA,iCAAwB,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC;KAClD;IACD,IAAI,OAAO,KAAK,sBAAQ,CAAC,OAAO,EAAE;QAChC,OAAO,2BAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;KAC3E;IAED,aAAa;IACb,IAAI,CAAC,IAAA,yBAAiB,EAAC,gBAAgB,CAAC,EAAE;QACxC,OAAO,SAAS,CAAC;KAClB;IACD,OAAO,2BAAmB,CAAC,MAAM,CAC/B,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,sBAAsB,0BAqBjC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n\n/**\n * Converts an address or assetId to a CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
1
+ {"version":3,"file":"caip-formatters.cjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":";;;AAAA,oDAAoD;AACpD,wDAAuD;AACvD,iEAAiE;AACjE,uDAAqE;AACrE,2FAA2E;AAE3E,2CAUyB;AAEzB,yCAMkB;AAElB,wCA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BAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,sBAAsB,0BAqBjC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n isTronChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n if (isTronChainId(chainId)) {\n return TrxScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (chainId === TrxScope.Mainnet) {\n return ChainId.TRON;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n\n/**\n * Converts an address or assetId to a CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AACrD,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAQjB,wBAAwB;AAQzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAcF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAkB7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YACpB,GAAG,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,KAC3C,GAgBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,YAAa,MAAM,WAgB3D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,qBACf,GAAG,GAAG,aAAa,GAAG,MAAM,WACrC,mBAAmB,CAAC,YAAY,CAAC,KACzC,aAAa,GAAG,SAkBlB,CAAC"}
1
+ {"version":3,"file":"caip-formatters.d.cts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AACrD,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAQjB,wBAAwB;AASzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;A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AAa,GAAG,MAAM,WACrC,mBAAmB,CAAC,YAAY,CAAC,KACzC,aAAa,GAAG,SAkBlB,CAAC"}
1
+ {"version":3,"file":"caip-formatters.d.mts","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AACrD,OAAO,EACL,KAAK,GAAG,EACR,KAAK,WAAW,EAQjB,wBAAwB;AASzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAiB;AAGpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACrB,GAAG,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,KAC3C,WAiBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YACpB,MAAM,GAAG,GAAG,GAAG,WAAW,GAAG,MAAM,WAqB7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YACpB,GAAG,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,KAC3C,GAgBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,YAAa,MAAM,WAgB3D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,qBACf,GAAG,GAAG,aAAa,GAAG,MAAM,WACrC,mBAAmB,CAAC,YAAY,CAAC,KACzC,aAAa,GAAG,SAkBlB,CAAC"}
@@ -1,10 +1,10 @@
1
1
  import { getAddress } from "@ethersproject/address";
2
2
  import { AddressZero } from "@ethersproject/constants";
3
3
  import { convertHexToDecimal } from "@metamask/controller-utils";
4
- import { BtcScope, SolScope } from "@metamask/keyring-api";
4
+ import { BtcScope, SolScope, TrxScope } from "@metamask/keyring-api";
5
5
  import { toEvmCaipChainId } from "@metamask/multichain-network-controller";
6
6
  import { isCaipChainId, isStrictHexString, parseCaipChainId, isCaipReference, numberToHex, isCaipAssetType, CaipAssetTypeStruct } from "@metamask/utils";
7
- import { getNativeAssetForChainId, isBitcoinChainId, isNativeAddress, isSolanaChainId } from "./bridge.mjs";
7
+ import { getNativeAssetForChainId, isBitcoinChainId, isNativeAddress, isSolanaChainId, isTronChainId } from "./bridge.mjs";
8
8
  import { ChainId } from "../types.mjs";
9
9
  /**
10
10
  * Converts a chainId to a CaipChainId
@@ -25,6 +25,9 @@ export const formatChainIdToCaip = (chainId) => {
25
25
  if (isBitcoinChainId(chainId)) {
26
26
  return BtcScope.Mainnet;
27
27
  }
28
+ if (isTronChainId(chainId)) {
29
+ return TrxScope.Mainnet;
30
+ }
28
31
  return toEvmCaipChainId(numberToHex(Number(chainId)));
29
32
  };
30
33
  /**
@@ -43,6 +46,9 @@ export const formatChainIdToDec = (chainId) => {
43
46
  if (chainId === BtcScope.Mainnet) {
44
47
  return ChainId.BTC;
45
48
  }
49
+ if (chainId === TrxScope.Mainnet) {
50
+ return ChainId.TRON;
51
+ }
46
52
  if (isCaipChainId(chainId)) {
47
53
  return Number(chainId.split(':').at(-1));
48
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"caip-formatters.mjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AACpD,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AACjE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AAC3D,OAAO,EAAE,gBAAgB,EAAE,gDAAgD;AAE3E,OAAO,EAGL,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,eAAe,EACf,mBAAmB,EACpB,wBAAwB;AAEzB,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,eAAe,EACf,eAAe,EAChB,qBAAiB;AAElB,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAA4C,EAC/B,EAAE;IACf,IAAI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E;QACrC,OAAO,gBAAgB,CAAC;KACzB;IACD,IAAI,eAAe,CAAC,gBAAgB,CAAC,EAAE;QACrC,OAAO,wBAAwB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;KAClD;IACD,IAAI,OAAO,KAAK,QAAQ,CAAC,OAAO,EAAE;QAChC,OAAO,mBAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;KAC3E;IAED,aAAa;IACb,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE;QACxC,OAAO,SAAS,CAAC;KAClB;IACD,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n\n/**\n * Converts an address or assetId to a CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
1
+ {"version":3,"file":"caip-formatters.mjs","sourceRoot":"","sources":["../../src/utils/caip-formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,+BAA+B;AACpD,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AACjE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AACrE,OAAO,EAAE,gBAAgB,EAAE,gDAAgD;AAE3E,OAAO,EAGL,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,eAAe,EACf,mBAAmB,EACpB,wBAAwB;AAEzB,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,EACd,qBAAiB;AAElB,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAA4C,EAC/B,EAAE;IACf,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,IAAI,iBAAi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2E;IAC3E,oDAAoD;IACpD,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,gBAA8C,EAC9C,OAA0C,EACf,EAAE;IAC7B,IAAI,eAAe,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IACD,IAAI,eAAe,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACtC,OAAO,wBAAwB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;IACnD,CAAC;IACD,IAAI,OAAO,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;QACjC,OAAO,mBAAmB,CAAC,MAAM,CAAC,GAAG,OAAO,UAAU,gBAAgB,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,aAAa;IACb,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,mBAAmB,CAAC,MAAM,CAC/B,GAAG,mBAAmB,CAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAC5D,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getAddress } from '@ethersproject/address';\nimport { AddressZero } from '@ethersproject/constants';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipAssetType } from '@metamask/utils';\nimport {\n type Hex,\n type CaipChainId,\n isCaipChainId,\n isStrictHexString,\n parseCaipChainId,\n isCaipReference,\n numberToHex,\n isCaipAssetType,\n CaipAssetTypeStruct,\n} from '@metamask/utils';\n\nimport {\n getNativeAssetForChainId,\n isBitcoinChainId,\n isNativeAddress,\n isSolanaChainId,\n isTronChainId,\n} from './bridge';\nimport type { GenericQuoteRequest } from '../types';\nimport { ChainId } from '../types';\n\n/**\n * Converts a chainId to a CaipChainId\n *\n * @param chainId - The chainId to convert\n * @returns The CaipChainId\n */\nexport const formatChainIdToCaip = (\n chainId: Hex | number | CaipChainId | string,\n): CaipChainId => {\n if (isCaipChainId(chainId)) {\n return chainId;\n }\n if (isStrictHexString(chainId)) {\n return toEvmCaipChainId(chainId);\n }\n if (isSolanaChainId(chainId)) {\n return SolScope.Mainnet;\n }\n if (isBitcoinChainId(chainId)) {\n return BtcScope.Mainnet;\n }\n if (isTronChainId(chainId)) {\n return TrxScope.Mainnet;\n }\n return toEvmCaipChainId(numberToHex(Number(chainId)));\n};\n\n/**\n * Converts a chainId to a decimal number that can be used for bridge-api requests\n *\n * @param chainId - The chainId to convert\n * @returns The decimal number\n */\nexport const formatChainIdToDec = (\n chainId: number | Hex | CaipChainId | string,\n) => {\n if (isStrictHexString(chainId)) {\n return convertHexToDecimal(chainId);\n }\n if (chainId === SolScope.Mainnet) {\n return ChainId.SOLANA;\n }\n if (chainId === BtcScope.Mainnet) {\n return ChainId.BTC;\n }\n if (chainId === TrxScope.Mainnet) {\n return ChainId.TRON;\n }\n if (isCaipChainId(chainId)) {\n return Number(chainId.split(':').at(-1));\n }\n if (typeof chainId === 'string') {\n return parseInt(chainId, 10);\n }\n return chainId;\n};\n\n/**\n * Converts a chainId to a hex string used to read controller data within the app\n * Hex chainIds are also used for fetching exchange rates\n *\n * @param chainId - The chainId to convert\n * @returns The hex string\n */\nexport const formatChainIdToHex = (\n chainId: Hex | CaipChainId | string | number,\n): Hex => {\n if (isStrictHexString(chainId)) {\n return chainId;\n }\n if (typeof chainId === 'number' || parseInt(chainId, 10)) {\n return numberToHex(Number(chainId));\n }\n if (isCaipChainId(chainId)) {\n const { reference } = parseCaipChainId(chainId);\n if (isCaipReference(reference) && !isNaN(Number(reference))) {\n return numberToHex(Number(reference));\n }\n }\n // Throw an error if a non-evm chainId is passed to this function\n // This should never happen, but it's a sanity check\n throw new Error(`Invalid cross-chain swaps chainId: ${chainId}`);\n};\n\n/**\n * Converts an asset or account address to a string that can be used for bridge-api requests\n *\n * @param address - The address to convert\n * @returns The converted address\n */\nexport const formatAddressToCaipReference = (address: string) => {\n if (isStrictHexString(address)) {\n return getAddress(address);\n }\n // If the address looks like a native token, return the zero address because it's\n // what bridge-api uses to represent a native asset\n if (isNativeAddress(address)) {\n return AddressZero;\n }\n const addressWithoutPrefix = address.split(':').at(-1);\n // If the address is not a valid hex string or CAIP address, throw an error\n // This should never happen, but it's a sanity check\n if (!addressWithoutPrefix) {\n throw new Error('Invalid address');\n }\n return addressWithoutPrefix;\n};\n\n/**\n * Converts an address or assetId to a CaipAssetType\n *\n * @param addressOrAssetId - The address or assetId to convert\n * @param chainId - The chainId of the asset\n * @returns The CaipAssetType\n */\nexport const formatAddressToAssetId = (\n addressOrAssetId: Hex | CaipAssetType | string,\n chainId: GenericQuoteRequest['srcChainId'],\n): CaipAssetType | undefined => {\n if (isCaipAssetType(addressOrAssetId)) {\n return addressOrAssetId;\n }\n if (isNativeAddress(addressOrAssetId)) {\n return getNativeAssetForChainId(chainId).assetId;\n }\n if (chainId === SolScope.Mainnet) {\n return CaipAssetTypeStruct.create(`${chainId}/token:${addressOrAssetId}`);\n }\n\n // EVM assets\n if (!isStrictHexString(addressOrAssetId)) {\n return undefined;\n }\n return CaipAssetTypeStruct.create(\n `${formatChainIdToCaip(chainId)}/erc20:${addressOrAssetId}`,\n );\n};\n"]}
@@ -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,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;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"]}
1
+ {"version":3,"file":"feature-flags.cjs","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":";;;AAEA,2DAAwD;AACxD,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;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,CAAC;QACnC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,WAAW,KAAK,gBAAgB,IAAI,WAAW,GAAG,gBAAgB,EAAE,CAAC;QACvE,OAAO,IAAI,CAAC;IACd,CAAC;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"]}
@@ -1 +1 @@
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"]}
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,CAAC;QACrD,OAAO,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;IAChD,CAAC;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,CAAC;QACnC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,WAAW,KAAK,gBAAgB,IAAI,WAAW,GAAG,gBAAgB,EAAE,CAAC;QACvE,OAAO,IAAI,CAAC;IACd,CAAC;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"]}
@@ -1 +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,MAA2D,CAAC;IAChE,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,CAAC,MAAM,EAAE,CAAC,CAAC;SACvC;QAED,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;QACzC,IAAI,MAAM,GAAG,EAAE,CAAC;QAEhB,OAAO,IAAI,EAAE;YACX,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAE5C,IAAI,IAAI,EAAE;gBAC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AAC,CAAC;KAClB;YAAS;QACR,IAAI;YACF,MAAM,MAAM,EAAE,MAAM,EAAE,CAAC;SACxB;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;SACzD;KACF;AACH,CAAC,CAAC;AAxEW,QAAA,iBAAiB,qBAwE5B","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 let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n 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 } finally {\n try {\n await reader?.cancel();\n } catch (error) {\n console.error('Error cleaning up stream reader', error);\n }\n }\n};\n"]}
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,MAA2D,CAAC;IAChE,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;QACpD,IAAI,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;QACxC,CAAC;QAED,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;QACzC,IAAI,MAAM,GAAG,EAAE,CAAC;QAEhB,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAE5C,IAAI,IAAI,EAAE,CAAC;gBACT,MAAM;YACR,CAAC;YAED,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YAElD,wCAAwC;YACxC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YACnC,MAAM,GAAG,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;YAE3B,6CAA6C;YAC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E,KAAK,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAxEW,QAAA,iBAAiB,qBAwE5B","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 let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n 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 } finally {\n try {\n await reader?.cancel();\n } catch (error) {\n console.error('Error cleaning up stream reader', error);\n }\n }\n};\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SAAS,GAAa,EAAE,CAAC;gBAE/B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;oBACxB,IAAI,IAAI,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;YAAS;QACR,IAAI;YACF,MAAM,MAAM,EAAE,MAAM,EAAE,CAAC;SACxB;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;SACzD;KACF;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 let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n 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 } finally {\n try {\n await reader?.cancel();\n } catch (error) {\n console.error('Error cleaning up stream reader', error);\n }\n }\n};\n"]}
1
+ {"version":3,"file":"fetch-server-events.mjs","sourceRoot":"","sources":["../../src/utils/fetch-server-events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EACpC,GAAW,EACX,EACE,SAAS,EACT,OAAO,EACP,OAAO,EACP,OAAO,EACP,GAAG,cAAc,EAMlB,EACD,EAAE;IACF,IAAI,MAA2D,CAAC;IAChE,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,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AE,CAAC;wBACpC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;oBACvC,CAAC;gBACH,CAAC;gBAED,IAAI,SAAS,KAAK,OAAO,EAAE,CAAC;oBAC1B,MAAM,IAAI,KAAK,CAAC,qBAAqB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC/D,CAAC;gBACD,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACzB,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;oBACxD,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;gBACvC,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,EAAE,EAAE,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,MAAM,MAAM,EAAE,MAAM,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;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 let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const response = await fetchFn(url, requestOptions);\n if (!response.ok || !response.body) {\n throw new Error(`${response.status}`);\n }\n\n 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 } finally {\n try {\n await reader?.cancel();\n } catch (error) {\n console.error('Error cleaning up stream reader', error);\n }\n }\n};\n"]}