From 3ad28f495922432981730c597566f3da9f8de60f Mon Sep 17 00:00:00 2001 From: huhuanming Date: Mon, 16 Dec 2024 20:49:52 +0800 Subject: [PATCH] refactor: replace the trading configuration in the market module with remote configuration. (#6353) * fix: fix issues * Update marketProvider.constants.ts * Update tradeHook.tsx * Update market.ts * Update tradeHook.tsx * Update MarketTradeButton.tsx * Update jest-setup.js * Update tradeHook.tsx * Update useTradingViewProps.ts --- jest-setup.js | 2 + .../TradingView/useTradingViewProps.ts | 2 +- .../Market/components/MarketTradeButton.tsx | 15 ++-- .../src/views/Market/components/tradeHook.tsx | 62 ++++++------- packages/shared/types/market.ts | 17 ++-- .../types/market/marketProvider.constants.ts | 89 ------------------- 6 files changed, 48 insertions(+), 139 deletions(-) diff --git a/jest-setup.js b/jest-setup.js index 865fde7f02d..346df6bdaa4 100644 --- a/jest-setup.js +++ b/jest-setup.js @@ -75,6 +75,8 @@ jest.mock('@sentry/react-native', () => ({ init: () => jest.fn(), reactNavigationIntegration: () => jest.fn(), reactNativeTracingIntegration: () => jest.fn(), + TimeToInitialDisplay: () => jest.fn(), + TimeToFullDisplay: () => jest.fn(), })); jest.mock('expo-localization', () => ({ diff --git a/packages/kit/src/components/TradingView/useTradingViewProps.ts b/packages/kit/src/components/TradingView/useTradingViewProps.ts index b459bcfcb58..3e81b67723a 100644 --- a/packages/kit/src/components/TradingView/useTradingViewProps.ts +++ b/packages/kit/src/components/TradingView/useTradingViewProps.ts @@ -74,12 +74,12 @@ export const useTradingViewProps = ({ 'show_popup_button': 'false', 'autosize': 'true', 'symbol': `${identifier.toUpperCase()}:${baseToken.toUpperCase()}${targetToken.toUpperCase()}`, - 'range': '3M', 'timezone': timezone, 'theme': theme, 'style': '1', 'gridColor': 'rgba(255, 255, 255, 0)', 'locale': locale, + 'interval': '15', 'hide_legend': 'true', 'allow_symbol_change': 'false', 'save_image': 'false', diff --git a/packages/kit/src/views/Market/components/MarketTradeButton.tsx b/packages/kit/src/views/Market/components/MarketTradeButton.tsx index fc7c435a7ab..cb847f16408 100644 --- a/packages/kit/src/views/Market/components/MarketTradeButton.tsx +++ b/packages/kit/src/views/Market/components/MarketTradeButton.tsx @@ -6,7 +6,6 @@ import type { IActionListItemProps } from '@onekeyhq/components'; import { ActionList, Button, IconButton, XStack } from '@onekeyhq/components'; import { ETranslations } from '@onekeyhq/shared/src/locale'; import type { IMarketTokenDetail } from '@onekeyhq/shared/types/market'; -import { getImportFromToken } from '@onekeyhq/shared/types/market/marketProvider.constants'; import backgroundApiProxy from '../../../background/instance/backgroundApiProxy'; import { ReviewControl } from '../../../components/ReviewControl'; @@ -33,6 +32,9 @@ export function MarketTradeButton({ buy: true, sell: true, }); + + const { tokenAddress: realContractAddress = '' } = network || {}; + const sections = useMemo( () => [ { @@ -51,14 +53,7 @@ export function MarketTradeButton({ const checkDisabled = useCallback(async () => { if (networkId) { - const { isNative, realContractAddress = '' } = - getImportFromToken({ - networkId, - tokenSymbol: token.symbol, - contractAddress: network?.contract_address || '', - }) || {}; - const contractAddress = isNative ? '' : network?.contract_address || ''; - + const contractAddress = realContractAddress; const [buyResult, sellResult] = await Promise.all([ backgroundApiProxy.serviceFiatCrypto.isTokenSupported({ networkId, @@ -76,7 +71,7 @@ export function MarketTradeButton({ sell: !sellResult, }); } - }, [network, networkId, token.symbol]); + }, [networkId, realContractAddress]); useEffect(() => { void checkDisabled(); diff --git a/packages/kit/src/views/Market/components/tradeHook.tsx b/packages/kit/src/views/Market/components/tradeHook.tsx index 5fae0158f74..5f5816bd704 100644 --- a/packages/kit/src/views/Market/components/tradeHook.tsx +++ b/packages/kit/src/views/Market/components/tradeHook.tsx @@ -16,11 +16,11 @@ import { openUrlExternal } from '@onekeyhq/shared/src/utils/openUrlUtils'; import timerUtils from '@onekeyhq/shared/src/utils/timerUtils'; import { isSupportStaking } from '@onekeyhq/shared/types/earn/earnProvider.constants'; import type { IFiatCryptoType } from '@onekeyhq/shared/types/fiatCrypto'; -import type { IMarketTokenDetail } from '@onekeyhq/shared/types/market'; -import { - getImportFromToken, - getNetworkIdBySymbol, -} from '@onekeyhq/shared/types/market/marketProvider.constants'; +import type { + IMarketDetailPlatformNetwork, + IMarketTokenDetail, +} from '@onekeyhq/shared/types/market'; +import { getNetworkIdBySymbol } from '@onekeyhq/shared/types/market/marketProvider.constants'; import { ESwapTabSwitchType } from '@onekeyhq/shared/types/swap/types'; import backgroundApiProxy from '../../../background/instance/backgroundApiProxy'; @@ -28,23 +28,27 @@ import useAppNavigation from '../../../hooks/useAppNavigation'; import { useActiveAccount } from '../../../states/jotai/contexts/accountSelector'; export const useMarketTradeNetwork = (token: IMarketTokenDetail | null) => { - const { detailPlatforms, name } = token || {}; + const { detailPlatforms, platforms = {} } = token || {}; const network = useMemo(() => { - if (detailPlatforms && name === 'Toncoin') { - return detailPlatforms['the-open-network']; + if (detailPlatforms) { + const values = Object.values(detailPlatforms); + const nativePlatform = values.find((i) => i.isNative); + if (nativePlatform) { + return nativePlatform; + } + + const tokenAddress = Object.values(platforms)[0]; + const tokenAddressPlatform = values.find( + (i) => i.tokenAddress === tokenAddress, + ); + return tokenAddressPlatform ?? values[0]; } - return detailPlatforms ? Object.values(detailPlatforms)[0] : null; - }, [detailPlatforms, name]); + }, [detailPlatforms, platforms]); return network; }; export const useMarketTradeNetworkId = ( - network: { - contract_address: string; - onekeyNetworkId?: string; - hideContractAddress?: boolean; - coingeckoNetworkId?: string; - } | null, + network: IMarketDetailPlatformNetwork | null | undefined, symbol: string, ) => useMemo(() => { @@ -68,6 +72,9 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { [network], ); + const { isNative = false, tokenAddress: realContractAddress = '' } = + network || {}; + const remindUnsupportedToken = useCallback( (action: 'buy' | 'sell' | 'trade', showDialog = true) => { defaultLogger.market.token.unsupportedToken({ name: symbol, action }); @@ -100,16 +107,10 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { return; } - const { isNative } = - getImportFromToken({ - networkId, - tokenSymbol: symbol, - contractAddress, - }) || {}; const isSupported = await backgroundApiProxy.serviceFiatCrypto.isTokenSupported({ networkId, - tokenAddress: isNative ? '' : contractAddress, + tokenAddress: realContractAddress, type, }); @@ -132,7 +133,7 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { const { url, build } = await backgroundApiProxy.serviceFiatCrypto.generateWidgetUrl({ networkId, - tokenAddress: '', + tokenAddress: realContractAddress, accountId: dbAccount.id, type, }); @@ -144,10 +145,9 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { }, [ activeAccount.account, - contractAddress, networkId, + realContractAddress, remindUnsupportedToken, - symbol, ], ); @@ -172,12 +172,6 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { }); return; } - const { isNative, realContractAddress = '' } = - getImportFromToken({ - networkId, - tokenSymbol: symbol, - contractAddress, - }) || {}; const { isSupportSwap, isSupportCrossChain } = await backgroundApiProxy.serviceSwap.checkSupportSwap({ networkId, @@ -198,7 +192,7 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { importFromToken: { ...onekeyNetwork, logoURI: isNative ? onekeyNetwork.logoURI : undefined, - contractAddress: isNative ? '' : contractAddress, + contractAddress: realContractAddress, networkId, isNative, networkLogoURI: onekeyNetwork.logoURI, @@ -212,9 +206,11 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => { }, [ contractAddress, + isNative, name, navigation, networkId, + realContractAddress, remindUnsupportedToken, symbol, ], diff --git a/packages/shared/types/market.ts b/packages/shared/types/market.ts index 54b5413ef40..4150ceb2c58 100644 --- a/packages/shared/types/market.ts +++ b/packages/shared/types/market.ts @@ -52,13 +52,17 @@ export interface IMarketPerformance { priceChangePercentage1y: number; } +export interface IMarketDetailPlatformNetwork { + contract_address: string; + onekeyNetworkId?: string; + hideContractAddress?: boolean; + coingeckoNetworkId?: string; + isNative?: true; + tokenAddress?: string; +} + export interface IMarketDetailPlatform { - [key: string]: { - contract_address: string; - onekeyNetworkId?: string; - hideContractAddress?: boolean; - coingeckoNetworkId?: string; - }; + [key: string]: IMarketDetailPlatformNetwork; } export interface IMarketResponsePool { @@ -128,6 +132,7 @@ export interface IMarketTokenDetail { links: IMarketDetailLinks; stats: IMarketDetailStats; detailPlatforms: IMarketDetailPlatform; + platforms: Record; tickers?: IMarketDetailTicker[]; } diff --git a/packages/shared/types/market/marketProvider.constants.ts b/packages/shared/types/market/marketProvider.constants.ts index bb44604ea7d..fc39bac021f 100644 --- a/packages/shared/types/market/marketProvider.constants.ts +++ b/packages/shared/types/market/marketProvider.constants.ts @@ -2,72 +2,6 @@ import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils'; import { getNetworkIdsMap } from '../../src/config/networkIds'; -// TODO: This token configuration list should be moved to backend service -const getSwapTokenMap = memoizee( - (): Record< - string, - { - contractAddress: string; - symbol: string; - realContractAddress: string; - } - > => { - const networkIdsMap = getNetworkIdsMap(); - return { - [networkIdsMap.btc]: { - contractAddress: '', - symbol: 'BTC', - realContractAddress: '', - }, - [networkIdsMap.eth]: { - contractAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', - symbol: 'ETH', - realContractAddress: '', - }, - [networkIdsMap.sol]: { - contractAddress: 'So11111111111111111111111111111111111111112', - symbol: 'SOL', - realContractAddress: '', - }, - [networkIdsMap.bsc]: { - contractAddress: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c', - symbol: 'BNB', - realContractAddress: '', - }, - [networkIdsMap.polygon]: { - contractAddress: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270', - symbol: 'POL', - realContractAddress: '', - }, - [networkIdsMap.avalanche]: { - contractAddress: '0x0000000000000000000000000000000000000000', - symbol: 'AVAX', - realContractAddress: '', - }, - [networkIdsMap.apt]: { - contractAddress: '0x1::aptos_coin::AptosCoin', - symbol: 'APT', - realContractAddress: '', - }, - [networkIdsMap.kaspa]: { - contractAddress: '', - symbol: 'KAS', - realContractAddress: '', - }, - [networkIdsMap.ton]: { - contractAddress: 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c', - symbol: 'TON', - realContractAddress: '', - }, - [networkIdsMap.sui]: { - contractAddress: '0x2::sui::SUI', - symbol: 'SUI', - realContractAddress: '0x2::sui::SUI', - }, - }; - }, -); - export const getNetworkIdBySymbol = memoizee((symbol: string) => { const networkIdsMap = getNetworkIdsMap(); switch (symbol) { @@ -77,26 +11,3 @@ export const getNetworkIdBySymbol = memoizee((symbol: string) => { return undefined; } }); - -export function getImportFromToken({ - networkId, - tokenSymbol, - contractAddress, -}: { - networkId: string; - tokenSymbol: string; - contractAddress: string; -}) { - const map = getSwapTokenMap(); - const item = map[networkId]; - if (item) { - const isNative = - tokenSymbol.toUpperCase() === item.symbol && - item.contractAddress === contractAddress; - return { - isNative, - realContractAddress: item.realContractAddress, - }; - } - return undefined; -}