Skip to content

Commit

Permalink
refactor: replace the trading configuration in the market module with…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
huhuanming authored Dec 16, 2024
1 parent ffe3f12 commit 3ad28f4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 139 deletions.
2 changes: 2 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 5 additions & 10 deletions packages/kit/src/views/Market/components/MarketTradeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -33,6 +32,9 @@ export function MarketTradeButton({
buy: true,
sell: true,
});

const { tokenAddress: realContractAddress = '' } = network || {};

const sections = useMemo(
() => [
{
Expand All @@ -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,
Expand All @@ -76,7 +71,7 @@ export function MarketTradeButton({
sell: !sellResult,
});
}
}, [network, networkId, token.symbol]);
}, [networkId, realContractAddress]);

useEffect(() => {
void checkDisabled();
Expand Down
62 changes: 29 additions & 33 deletions packages/kit/src/views/Market/components/tradeHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,39 @@ 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';
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(() => {
Expand All @@ -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 });
Expand Down Expand Up @@ -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,
});

Expand All @@ -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,
});
Expand All @@ -144,10 +145,9 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => {
},
[
activeAccount.account,
contractAddress,
networkId,
realContractAddress,
remindUnsupportedToken,
symbol,
],
);

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -212,9 +206,11 @@ export const useMarketTradeActions = (token: IMarketTokenDetail | null) => {
},
[
contractAddress,
isNative,
name,
navigation,
networkId,
realContractAddress,
remindUnsupportedToken,
symbol,
],
Expand Down
17 changes: 11 additions & 6 deletions packages/shared/types/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -128,6 +132,7 @@ export interface IMarketTokenDetail {
links: IMarketDetailLinks;
stats: IMarketDetailStats;
detailPlatforms: IMarketDetailPlatform;
platforms: Record<string, string>;
tickers?: IMarketDetailTicker[];
}

Expand Down
89 changes: 0 additions & 89 deletions packages/shared/types/market/marketProvider.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

0 comments on commit 3ad28f4

Please sign in to comment.