Skip to content

Commit 349ebd4

Browse files
chore: use more complete mapping for debank chain to chain id mapping (#11967)
* chore: use more complete mapping for debank chain to chain id mapping * refactor: debank helpers (#11970) --------- Co-authored-by: guanbinrui <52657989+guanbinrui@users.noreply.github.com>
1 parent d7adaf9 commit 349ebd4

File tree

10 files changed

+41
-75
lines changed

10 files changed

+41
-75
lines changed

packages/mask/popups/components/TokenPicker/TokenItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useEverSeen } from '@masknet/shared-base-ui'
66
import { makeStyles } from '@masknet/theme'
77
import type { Web3Helper } from '@masknet/web3-helpers'
88
import { useFungibleTokenBalance, useWeb3Utils } from '@masknet/web3-hooks-base'
9-
import { CHAIN_ID_TO_DEBANK_CHAIN_MAP } from '@masknet/web3-providers'
9+
import { debank } from '@masknet/web3-providers/helpers'
1010
import { type ReasonableNetwork } from '@masknet/web3-shared-base'
1111
import type { ChainId } from '@masknet/web3-shared-evm'
1212
import {
@@ -104,7 +104,7 @@ export const TokenItem = memo(function TokenItem({
104104
const providerURL = network?.isCustomized ? network.rpcUrl : undefined
105105
const [seen, ref] = useEverSeen<HTMLLIElement>()
106106
// Debank might not provide asset from current custom network
107-
const supportedByDebank = CHAIN_ID_TO_DEBANK_CHAIN_MAP[asset.chainId]
107+
const supportedByDebank = debank.getDebankChain(asset.chainId)
108108
const tryRpc = (!supportedByDebank || !('balance' in asset)) && seen
109109
const { data: rpcBalance, isPending } = useFungibleTokenBalance(
110110
NetworkPluginID.PLUGIN_EVM,

packages/mask/popups/pages/Wallet/components/AssetsList/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
import { Trans } from '@lingui/macro'
12
import { FormattedCurrency, NetworkIcon, ProgressiveText, TokenIcon } from '@masknet/shared'
23
import { NetworkPluginID, PopupRoutes } from '@masknet/shared-base'
34
import { useEverSeen } from '@masknet/shared-base-ui'
45
import { TextOverflowTooltip, makeStyles } from '@masknet/theme'
56
import { useFungibleTokenBalance, useNetworks, useWallet } from '@masknet/web3-hooks-base'
7+
import { debank } from '@masknet/web3-providers/helpers'
68
import { formatCurrency, isGte, isLessThan, type FungibleAsset } from '@masknet/web3-shared-base'
7-
import { isNativeTokenAddress, ChainId, type SchemaType } from '@masknet/web3-shared-evm'
9+
import { ChainId, isNativeTokenAddress, type SchemaType } from '@masknet/web3-shared-evm'
810
import { Box, List, ListItem, ListItemText, Skeleton, Typography, type ListItemProps } from '@mui/material'
911
import { range } from 'lodash-es'
1012
import { memo, useCallback, useMemo } from 'react'
1113
import { useNavigate } from 'react-router-dom'
1214
import urlcat from 'urlcat'
1315
import { formatTokenBalance } from '../../../../../shared/index.js'
16+
import { useHasNavigator } from '../../../../hooks/useHasNavigator.js'
1417
import { useAssetExpand, useWalletAssets } from '../../hooks/index.js'
1518
import { MoreBar } from './MoreBar.js'
16-
import { useHasNavigator } from '../../../../hooks/useHasNavigator.js'
17-
import { CHAIN_ID_TO_DEBANK_CHAIN_MAP } from '@masknet/web3-providers'
18-
import { Trans } from '@lingui/macro'
1919

2020
const useStyles = makeStyles<{ hasNav?: boolean }>()((theme, { hasNav }) => ({
2121
container: {
@@ -100,7 +100,7 @@ const AssetItem = memo(function AssetItem({ asset, onItemClick, ...rest }: Asset
100100
const providerURL = network?.isCustomized ? network.rpcUrl : undefined
101101
const [seen, ref] = useEverSeen<HTMLLIElement>()
102102
// Debank might not provide asset from current custom network
103-
const tryRpc = network?.chainId ? !CHAIN_ID_TO_DEBANK_CHAIN_MAP[network.chainId] && seen : false
103+
const tryRpc = network?.chainId ? !debank.getDebankChain(network.chainId) && seen : false
104104
const { data: rpcBalance, isPending } = useFungibleTokenBalance(
105105
NetworkPluginID.PLUGIN_EVM,
106106
asset.address,

packages/web3-constants/evm/debank.json

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/web3-providers/src/DeBank/apis/GasOptionAPI.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import urlcat from 'urlcat'
22
import { GasOptionType, toFixed } from '@masknet/web3-shared-base'
3-
import { type ChainId, getDeBankConstants, type GasOption } from '@masknet/web3-shared-evm'
3+
import { type ChainId, type GasOption } from '@masknet/web3-shared-evm'
44
import type { GasPriceResponse } from '../types.js'
55
import { DEBANK_OPEN_API } from '../constants.js'
66
import { fetchSquashedJSON } from '../../helpers/fetchJSON.js'
77
import type { BaseGasOptions } from '../../entry-types.js'
8+
import { getDebankChain } from '../helpers.js'
89

910
class DeBankGasOptionAPI implements BaseGasOptions.Provider<ChainId, GasOption> {
1011
async getGasOptions(chainId: ChainId): Promise<Record<GasOptionType, GasOption>> {
11-
const { CHAIN_ID } = getDeBankConstants(chainId)
12+
const CHAIN_ID = getDebankChain(chainId)
1213
if (!CHAIN_ID) throw new Error('Failed to get gas price.')
1314

1415
const result = await fetchSquashedJSON<GasPriceResponse>(

packages/web3-providers/src/DeBank/apis/HistoryAPI.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import urlcat from 'urlcat'
22
import { compact, last, uniq } from 'lodash-es'
33
import { createPageable, createIndicator, type Pageable, createNextIndicator, EMPTY_LIST } from '@masknet/shared-base'
44
import { type Transaction } from '@masknet/web3-shared-base'
5-
import { ChainId, getDeBankConstants, type SchemaType } from '@masknet/web3-shared-evm'
6-
import { formatTransactions, resolveDeBankAssetIdReversed } from '../helpers.js'
5+
import { ChainId, type SchemaType } from '@masknet/web3-shared-evm'
6+
import { formatTransactions, getDebankChain, resolveDeBankAssetIdReversed } from '../helpers.js'
77
import type { HistoryRecord } from '../types.js'
8-
import { CHAIN_ID_TO_DEBANK_CHAIN_MAP, DEBANK_OPEN_API } from '../constants.js'
8+
import { DEBANK_OPEN_API } from '../constants.js'
99
import { fetchSquashedJSON } from '../../helpers/fetchJSON.js'
1010
import type { HistoryAPI, BaseHubOptions } from '../../entry-types.js'
1111
import { evm } from '../../Manager/registry.js'
@@ -17,14 +17,14 @@ class DeBankHistoryAPI implements HistoryAPI.Provider<ChainId, SchemaType> {
1717
const networks = evm.state?.Network?.networks?.getCurrentValue()
1818
// Fallback to commonly used chains
1919
if (!networks) return PRESET_CHAIN_IDS
20-
const RUNTIME_CHAIN_IDS = networks.map((x) => CHAIN_ID_TO_DEBANK_CHAIN_MAP[x.chainId])
20+
const RUNTIME_CHAIN_IDS = networks.map((x) => getDebankChain(x.chainId))
2121
return compact(uniq([...RUNTIME_CHAIN_IDS, PRESET_CHAIN_IDS])).join(',')
2222
}
2323
async getTransactions(
2424
address: string,
2525
{ chainId = ChainId.Mainnet, indicator, size = 20 }: BaseHubOptions<ChainId> = {},
2626
): Promise<Pageable<Transaction<ChainId, SchemaType>>> {
27-
const { CHAIN_ID = '' } = getDeBankConstants(chainId)
27+
const CHAIN_ID = getDebankChain(chainId)
2828
if (!CHAIN_ID) return createPageable(EMPTY_LIST, createIndicator(indicator))
2929

3030
const result = await fetchSquashedJSON<HistoryRecord>(

packages/web3-providers/src/DeBank/helpers.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,28 @@ import {
2121
import { EVMChainResolver } from '../Web3/EVM/apis/ResolverAPI.js'
2222
import {
2323
DebankTransactionDirection,
24+
type DebankChains,
2425
type HistoryResponse,
2526
type TransferringAsset,
2627
type WalletTokenRecord,
2728
} from './types.js'
28-
import { DEBANK_CHAIN_TO_CHAIN_ID_MAP } from './constants.js'
29+
import { CHAIN_ID_TO_DEBANK_CHAIN_MAP, DEBANK_CHAIN_TO_CHAIN_ID_MAP } from './constants.js'
30+
31+
export function getChainIdByDebankChain(chain: DebankChains) {
32+
const chainId = DEBANK_CHAIN_TO_CHAIN_ID_MAP[chain]
33+
if (process.env.NODE_ENV === 'development' && !chainId) {
34+
console.warn('[Debank] no matching chainId for chain', chain)
35+
}
36+
return chainId
37+
}
38+
39+
export function getDebankChain(chainId: number) {
40+
const chain = CHAIN_ID_TO_DEBANK_CHAIN_MAP[chainId]
41+
if (process.env.NODE_ENV === 'development' && !chain) {
42+
console.warn('[Debank] no matching chainId', chainId)
43+
}
44+
return chain
45+
}
2946

3047
export function formatAssets(data: WalletTokenRecord[]): Array<FungibleAsset<ChainId, SchemaType>> {
3148
const resolveNativeAddress = memoize((chainId: ChainId) => {
@@ -38,9 +55,9 @@ export function formatAssets(data: WalletTokenRecord[]): Array<FungibleAsset<Cha
3855
})
3956

4057
return data
41-
.filter((x) => DEBANK_CHAIN_TO_CHAIN_ID_MAP[x.chain])
58+
.filter((x) => getChainIdByDebankChain(x.chain))
4259
.map((x) => {
43-
const chainId = DEBANK_CHAIN_TO_CHAIN_ID_MAP[x.chain]
60+
const chainId = getChainIdByDebankChain(x.chain)
4461
const address = x.id in DEBANK_CHAIN_TO_CHAIN_ID_MAP ? resolveNativeAddress(chainId) : x.id
4562

4663
return {
@@ -123,7 +140,7 @@ export function formatTransactions(
123140
txType = 'contract interaction'
124141
}
125142

126-
const chainId = DEBANK_CHAIN_TO_CHAIN_ID_MAP[transaction.chain]
143+
const chainId = getChainIdByDebankChain(transaction.chain)
127144
if (!chainId) return
128145

129146
if (isSameAddress(transaction.sends[0]?.to_addr, ZERO_ADDRESS)) {

packages/web3-providers/src/Rabby/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { NON_FUNGIBLE_TOKEN_API_URL, FUNGIBLE_TOKEN_API_URL } from './constants.
77
import type { NFTInfo, RawTokenInfo, TokenSpender } from './types.js'
88
import { fetchJSON } from '../helpers/fetchJSON.js'
99
import type { AuthorizationAPI } from '../entry-types.js'
10-
import { CHAIN_ID_TO_DEBANK_CHAIN_MAP } from '../DeBank/constants.js'
10+
import { getDebankChain } from '../DeBank/helpers.js'
1111

1212
class RabbyAPI implements AuthorizationAPI.Provider<ChainId> {
1313
async getNonFungibleTokenSpenders(chainId: ChainId, account: string) {
1414
const maskDappContractInfoList = getAllMaskDappContractInfo(chainId, 'nft')
15-
const debankChainId = CHAIN_ID_TO_DEBANK_CHAIN_MAP[chainId]
15+
const debankChainId = getDebankChain(chainId)
1616

1717
if (!debankChainId || !account || !isValidChainId(chainId)) return []
1818
const rawData = await fetchJSON<{ contracts: NFTInfo[] }>(
@@ -60,7 +60,7 @@ class RabbyAPI implements AuthorizationAPI.Provider<ChainId> {
6060

6161
async getFungibleTokenSpenders(chainId: ChainId, account: string) {
6262
const maskDappContractInfoList = getAllMaskDappContractInfo(chainId, 'token')
63-
const debankChainId = CHAIN_ID_TO_DEBANK_CHAIN_MAP[chainId]
63+
const debankChainId = getDebankChain(chainId)
6464

6565
if (!debankChainId || !account || !isValidChainId(chainId)) return []
6666

packages/web3-providers/src/entry-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './helpers/createWeb3ProviderFromURL.js'
2020

2121
export * as trending from './Trending/helpers.js'
2222
export * as chainbase from './Chainbase/helpers.js'
23+
export * as debank from './DeBank/helpers.js'

packages/web3-providers/src/entry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export { R2D2TokenList } from './R2D2/index.js'
7272
export { ENS } from './ENS/index.js'
7373

7474
// Debank
75-
export { DeBankHistory, CHAIN_ID_TO_DEBANK_CHAIN_MAP } from './DeBank/index.js'
75+
export { DeBankHistory } from './DeBank/index.js'
7676
export { OKX } from './OKX/index.js'
7777

7878
// NFTScan

packages/web3-shared/evm/src/constants/constants.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Arb from '@masknet/web3-constants/evm/arb.json' with { type: 'json' }
44
import ArtBlocks from '@masknet/web3-constants/evm/artblocks.json' with { type: 'json' }
55
import CoinGecko from '@masknet/web3-constants/evm/coingecko.json' with { type: 'json' }
66
import CryptoPunks from '@masknet/web3-constants/evm/cryptopunks.json' with { type: 'json' }
7-
import DeBank from '@masknet/web3-constants/evm/debank.json' with { type: 'json' }
87
import ENS from '@masknet/web3-constants/evm/ens.json' with { type: 'json' }
98
import Ethereum from '@masknet/web3-constants/evm/ethereum.json' with { type: 'json' }
109
import Etherscan from '@masknet/web3-constants/evm/etherscan.json' with { type: 'json' }
@@ -46,11 +45,6 @@ export const getEthereumConstants = transformAll(ChainId, Ethereum)
4645
export const useEthereumConstant = transformHook(getEthereumConstants)
4746
export const useEthereumConstants = transformAllHook(getEthereumConstants)
4847

49-
export const getDeBankConstant = transform(ChainId, DeBank)
50-
export const getDeBankConstants = transformAll(ChainId, DeBank)
51-
export const useDeBankConstant = transformHook(getDeBankConstants)
52-
export const useDeBankConstants = transformAllHook(getDeBankConstants)
53-
5448
export const getCoinGeckoConstant = transform(ChainId, CoinGecko)
5549
export const getCoinGeckoConstants = transformAll(ChainId, CoinGecko)
5650
export const useCoinGeckoConstant = transformHook(getCoinGeckoConstants)

0 commit comments

Comments
 (0)