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AC,OAAO,CAAC,CACvB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,aAA8B,EACoB,EAAE;IACpD,OAAO,CAAC,eAAe,CAAC,aAAa,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;AAC1D,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 { isCaipChainId, isStrictHexString } from '@metamask/utils';\nimport type { CaipAssetType, CaipChainId, Hex } from '@metamask/utils';\n\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 { SWAPS_CONTRACT_ADDRESSES } from '../constants/swaps';\nimport {\n  SWAPS_CHAINID_DEFAULT_TOKEN_MAP,\n  SYMBOL_TO_SLIP44_MAP,\n} from '../constants/tokens';\nimport type { SupportedSwapsNativeCurrencySymbols } from '../constants/tokens';\nimport type {\n  BridgeAsset,\n  BridgeControllerState,\n  GenericQuoteRequest,\n  QuoteResponseV1,\n  TxData,\n} from '../types';\nimport { ChainId } from '../types';\nimport {\n  formatChainIdToCaip,\n  formatChainIdToDec,\n  formatChainIdToHex,\n} from './caip-formatters';\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\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 * @param destChainId - The destination chain ID\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  destChainId: GenericQuoteRequest['destChainId'],\n) => {\n  const spenderAddress = isCrossChain(CHAIN_IDS.MAINNET, destChainId)\n    ? METABRIDGE_ETHEREUM_ADDRESS\n    : SWAPS_CONTRACT_ADDRESSES[CHAIN_IDS.MAINNET];\n  const UsdtContractInterface = new Contract(ETH_USDT_ADDRESS, abiERC20)\n    .interface;\n  const data = UsdtContractInterface.encodeFunctionData('approve', [\n    spenderAddress,\n    '0',\n  ]);\n\n  return data as Hex;\n};\n\nexport const isEthUsdt = (\n  chainId: GenericQuoteRequest['srcChainId'],\n  address: string,\n) =>\n  formatChainIdToDec(chainId) === ChainId.ETH &&\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: QuoteResponseV1,\n): quoteResponse is QuoteResponseV1<TxData, TxData> => {\n  return !isNonEvmChainId(quoteResponse.quote.srcChainId);\n};\n"]}