import { FungibleTokenDetailed , getNetworkTypeFromChainId , isNativeTokenAddress , NetworkType , useAccount , useBlockNumber , useTokenConstants , } from '@masknet/web3-shared-evm' ; import { useAsyncRetry } from 'react-use' ; import { safeUnreachable } from '@dimensiondev/kit' ; import { ZRX_AFFILIATE_ADDRESS } from '../../constants' ; import { PluginTraderRPC } from '../../messages' ; import { TradeStrategy } from '../../types' ; import { useSlippageTolerance } from '../0x/useSlippageTolerance' ; import { useTradeProviderSettings } from '../useTradeSettings' ; import { currentNetworkSettings } from '../../../Wallet/settings' ; import { TargetChainIdContext } from '../useTargetChainIdContext' ; import { TradeProvider } from '@masknet/public-api' ; const NATIVE_TOKEN_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' ; export function getNativeTokenLabel ( networkType ) { switch ( networkType ) { case NetworkType . Ethereum : return 'ETH' ; case NetworkType . Binance : case NetworkType . Polygon : case NetworkType . Arbitrum : case NetworkType . xDai : case NetworkType . Celo : case NetworkType . Fantom : case NetworkType . Aurora : return NATIVE_TOKEN_ADDRESS ; default : safeUnreachable ( networkType ) ; return '' ; } } export function useTrade ( strategy , inputAmount , outputAmount , inputToken ? , outputToken ? ) { const account = useAccount ( ) ; const { targetChainId } = TargetChainIdContext . useContainer ( ) ; const { NATIVE_TOKEN_ADDRESS } = useTokenConstants ( targetChainId ) ; const blockNumber = useBlockNumber ( ) ; const slippage = useSlippageTolerance ( ) ; const { pools } = useTradeProviderSettings ( TradeProvider . ZRX ) ; return useAsyncRetry ( async ( ) => { if ( ! inputToken || ! outputToken ) return null ; const isExactIn = strategy === TradeStrategy . ExactIn ; if ( inputAmount === '0' && isExactIn ) return null ; if ( outputAmount === '0' && ! isExactIn ) return null ; const sellToken = isNativeTokenAddress ( inputToken ) ? getNativeTokenLabel ( getNetworkTypeFromChainId ( targetChainId ) ?? currentNetworkSettings . value ) : inputToken . address ; const buyToken = isNativeTokenAddress ( outputToken ) ? getNativeTokenLabel ( getNetworkTypeFromChainId ( targetChainId ) ?? currentNetworkSettings . value ) : outputToken . address ; return PluginTraderRPC . swapQuote ( { sellToken , buyToken , takerAddress : account , sellAmount : isExactIn ? inputAmount : void 0 , buyAmount : isExactIn ? void 0 : outputAmount , skipValidation : true , slippagePercentage : slippage , affiliateAddress : ZRX_AFFILIATE_ADDRESS , } , getNetworkTypeFromChainId ( targetChainId ) ?? currentNetworkSettings . value ) ; } , [ NATIVE_TOKEN_ADDRESS , account , strategy , inputAmount , outputAmount , inputToken ?. address , outputToken ?. address , slippage , pools . length , blockNumber , ] ) ; }