Skip to content

Commit

Permalink
chore: implement getAssetNamespaceFromChainId helper (#8677)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis authored Jan 28, 2025
1 parent ea74117 commit c03e3b5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 83 deletions.
33 changes: 33 additions & 0 deletions packages/utils/src/getAssetNamespaceFromChainId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { AssetNamespace } from '@shapeshiftoss/caip'
import { ASSET_NAMESPACE } from '@shapeshiftoss/caip'
import { KnownChainIds } from '@shapeshiftoss/types'

import { assertUnreachable } from './assertUnreachable'

export const getAssetNamespaceFromChainId = (chainId: KnownChainIds): AssetNamespace => {
switch (chainId) {
case KnownChainIds.BnbSmartChainMainnet:
return ASSET_NAMESPACE.bep20
case KnownChainIds.SolanaMainnet:
return ASSET_NAMESPACE.splToken
case KnownChainIds.EthereumMainnet:
case KnownChainIds.AvalancheMainnet:
case KnownChainIds.OptimismMainnet:
case KnownChainIds.PolygonMainnet:
case KnownChainIds.GnosisMainnet:
case KnownChainIds.ArbitrumMainnet:
case KnownChainIds.ArbitrumNovaMainnet:
case KnownChainIds.BaseMainnet:
return ASSET_NAMESPACE.erc20
case KnownChainIds.CosmosMainnet:
case KnownChainIds.ThorchainMainnet:
return ASSET_NAMESPACE.ibc
case KnownChainIds.BitcoinMainnet:
case KnownChainIds.BitcoinCashMainnet:
case KnownChainIds.DogecoinMainnet:
case KnownChainIds.LitecoinMainnet:
throw Error(`Unhandled case '${chainId}'`)
default:
return assertUnreachable(chainId)
}
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './historyTimeframe'
export * from './getNativeFeeAssetReference'
export * from './assetData'
export * from './unfreeze'
export * from './getAssetNamespaceFromChainId'

export const isSome = <T>(option: T | null | undefined): option is T =>
!isUndefined(option) && !isNull(option)
31 changes: 5 additions & 26 deletions scripts/generateAssetData/generateRelatedAssetIndex/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import type { AssetId } from '@shapeshiftoss/caip'
import {
adapters,
ASSET_NAMESPACE,
bscChainId,
solanaChainId,
toAssetId,
} from '@shapeshiftoss/caip'
import type { ZerionChainId } from '@shapeshiftoss/types'
import { adapters, toAssetId } from '@shapeshiftoss/caip'
import type { KnownChainIds, ZerionChainId } from '@shapeshiftoss/types'
import { zerionChainIdToChainId } from '@shapeshiftoss/types'
import { getAssetNamespaceFromChainId } from '@shapeshiftoss/utils'

import type { ZerionImplementation } from './validators/fungible'

Expand All @@ -17,14 +12,7 @@ export const zerionImplementationToMaybeAssetId = (
const { chain_id, address: assetReference } = implementation
const chainId = zerionChainIdToChainId(chain_id as ZerionChainId)
if (!chainId || !assetReference) return undefined
const assetNamespace = (() => {
switch (true) {
case chainId === bscChainId:
return ASSET_NAMESPACE.bep20
default:
return ASSET_NAMESPACE.erc20
}
})()
const assetNamespace = getAssetNamespaceFromChainId(chainId as KnownChainIds)
return toAssetId({ chainId, assetNamespace, assetReference })
}

Expand All @@ -36,15 +24,6 @@ export const coingeckoPlatformDetailsToMaybeAssetId = (
platform as adapters.CoingeckoAssetPlatform,
)
if (!chainId || !contractAddress) return undefined
const assetNamespace = (() => {
switch (true) {
case chainId === bscChainId:
return ASSET_NAMESPACE.bep20
case chainId === solanaChainId:
return ASSET_NAMESPACE.splToken
default:
return ASSET_NAMESPACE.erc20
}
})()
const assetNamespace = getAssetNamespaceFromChainId(chainId as KnownChainIds)
return toAssetId({ chainId, assetNamespace, assetReference: contractAddress })
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
import { isNft, solanaChainId, toAssetId } from '@shapeshiftoss/caip'
import type { Asset } from '@shapeshiftoss/types'
import type { Asset, KnownChainIds } from '@shapeshiftoss/types'
import type { MinimalAsset } from '@shapeshiftoss/utils'
import { bnOrZero, makeAsset } from '@shapeshiftoss/utils'
import { bnOrZero, getAssetNamespaceFromChainId, makeAsset } from '@shapeshiftoss/utils'
import { orderBy } from 'lodash'
import { useMemo } from 'react'
import { ALCHEMY_SDK_SUPPORTED_CHAIN_IDS } from 'lib/alchemySdkInstance'
Expand All @@ -15,7 +15,6 @@ import {
import { selectAssets } from 'state/slices/selectors'
import { useAppSelector } from 'state/store'

import { getAssetNamespaceFromChainId } from '../helpers/customAssetSearch'
import { filterAssetsBySearchTerm } from '../helpers/filterAssetsBySearchTerm/filterAssetsBySearchTerm'
import { useGetCustomTokensQuery } from '../hooks/useGetCustomTokensQuery'
import { GroupedAssetList } from './GroupedAssetList/GroupedAssetList'
Expand Down Expand Up @@ -95,7 +94,7 @@ export const SearchTermAssetList = ({
if (!name || !symbol || !decimals) return null
const assetId = toAssetId({
chainId: metaData.chainId,
assetNamespace: getAssetNamespaceFromChainId(metaData.chainId),
assetNamespace: getAssetNamespaceFromChainId(metaData.chainId as KnownChainIds),
assetReference: metaData.contractAddress,
})
const minimalAsset: MinimalAsset = {
Expand Down
13 changes: 0 additions & 13 deletions src/components/TradeAssetSearch/helpers/customAssetSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { AssetNamespace, ChainId } from '@shapeshiftoss/caip'
import { ASSET_NAMESPACE, bscChainId, solanaChainId } from '@shapeshiftoss/caip'
import { PublicKey } from '@solana/web3.js'

export const isSolanaAddress = (contractAddress: string) => {
Expand All @@ -11,14 +9,3 @@ export const isSolanaAddress = (contractAddress: string) => {
return false
}
}

export const getAssetNamespaceFromChainId = (chainId: ChainId): AssetNamespace => {
switch (chainId) {
case bscChainId:
return ASSET_NAMESPACE.bep20
case solanaChainId:
return ASSET_NAMESPACE.splToken
default:
return ASSET_NAMESPACE.erc20
}
}
34 changes: 3 additions & 31 deletions src/lib/coingecko/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
adapters,
arbitrumChainId,
arbitrumNovaChainId,
ASSET_NAMESPACE,
avalancheChainId,
baseChainId,
bchChainId,
Expand All @@ -18,13 +17,13 @@ import {
thorchainChainId,
toAssetId,
} from '@shapeshiftoss/caip'
import { KnownChainIds } from '@shapeshiftoss/types'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { getAssetNamespaceFromChainId } from '@shapeshiftoss/utils'
import axios from 'axios'
import { getConfig } from 'config'
import { queryClient } from 'context/QueryClientProvider/queryClient'
import type { CoinGeckoSortKey } from 'lib/market-service/coingecko/coingecko'
import type { CoinGeckoMarketCap } from 'lib/market-service/coingecko/coingecko-types'
import { assertUnreachable } from 'lib/utils'

import { COINGECKO_NATIVE_ASSET_ID_TO_ASSET_ID } from './constants'
import type {
Expand Down Expand Up @@ -70,34 +69,7 @@ const getCoinDetails = async (
)
if (!chainId) return

const assetNamespace = (() => {
const knownChainId = chainId as KnownChainIds
switch (knownChainId) {
case KnownChainIds.BnbSmartChainMainnet:
return ASSET_NAMESPACE.bep20
case KnownChainIds.SolanaMainnet:
return ASSET_NAMESPACE.splToken
case KnownChainIds.EthereumMainnet:
case KnownChainIds.AvalancheMainnet:
case KnownChainIds.OptimismMainnet:
case KnownChainIds.PolygonMainnet:
case KnownChainIds.GnosisMainnet:
case KnownChainIds.ArbitrumMainnet:
case KnownChainIds.ArbitrumNovaMainnet:
case KnownChainIds.BaseMainnet:
return ASSET_NAMESPACE.erc20
case KnownChainIds.CosmosMainnet:
case KnownChainIds.ThorchainMainnet:
return ASSET_NAMESPACE.ibc
case KnownChainIds.BitcoinMainnet:
case KnownChainIds.BitcoinCashMainnet:
case KnownChainIds.DogecoinMainnet:
case KnownChainIds.LitecoinMainnet:
throw Error(`Unhandled case '${chainId}'`)
default:
return assertUnreachable(knownChainId)
}
})()
const assetNamespace = getAssetNamespaceFromChainId(chainId as KnownChainIds)

const assetId = toAssetId({
chainId,
Expand Down
12 changes: 3 additions & 9 deletions src/state/apis/zapper/validators.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
import {
arbitrumChainId,
ASSET_NAMESPACE,
avalancheChainId,
baseChainId,
bscChainId,
Expand All @@ -11,6 +10,8 @@ import {
polygonChainId,
toAssetId,
} from '@shapeshiftoss/caip'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { getAssetNamespaceFromChainId } from '@shapeshiftoss/utils'
import { invert } from 'lodash'
import type { Infer, Type } from 'myzod'
import z from 'myzod'
Expand Down Expand Up @@ -213,14 +214,7 @@ export const zapperAssetToMaybeAssetId = (
): AssetId | undefined => {
const chainId = zapperNetworkToChainId(asset.network as SupportedZapperNetwork)
if (!chainId) return undefined
const assetNamespace = (() => {
switch (true) {
case chainId === bscChainId:
return ASSET_NAMESPACE.bep20
default:
return ASSET_NAMESPACE.erc20
}
})()
const assetNamespace = getAssetNamespaceFromChainId(chainId as KnownChainIds)

const assetId = toAssetId({
chainId,
Expand Down

0 comments on commit c03e3b5

Please sign in to comment.