Skip to content

Asset Utilities

Convenience functions that operate on AssetInfo objects. Use these to detect which chain or token type an asset belongs to, extract identifiers, and branch logic by chain namespace.

Import

import {
  // Chain detection
  isBitcoin, isEvm, isSolana, isCosmos, isTron,
  // Token type detection
  isNativeTokenFromAsset, isEvmNative, isEvmErc20,
  isSolanaNative, isSolanaToken, isCosmosNative, isCosmosIbc,
  // CAIP-19 field extraction
  getChainNamespaceFromAsset, getChainIdFromAsset,
  getAssetNamespaceFromAsset, getAssetId, getCaip2, getTokenAddress,
  // Chain ID helpers
  getTokenChainId, getTokenChainIdString, extractChainIdFromAsset,
  extractChainIdsFromAssets,
  // Chain info
  getTokenChainInfo, getTokenChainName,
  // Pattern matching
  switchChainNamespace,
} from '@silentswap/sdk';

Chain Detection

Determine which blockchain an asset belongs to. Each function accepts an AssetInfo and returns a boolean.

import { isEvm, isSolana, isBitcoin, isTron } from '@silentswap/sdk';
 
if (isEvm(asset)) {
  // EVM chain (Ethereum, Polygon, Arbitrum, etc.)
} else if (isSolana(asset)) {
  // Solana
} else if (isBitcoin(asset)) {
  // Bitcoin
} else if (isTron(asset)) {
  // Tron
}
FunctionMatches
isBitcoin(asset)Bitcoin (bip122)
isEvm(asset)Ethereum, Polygon, Arbitrum, Base, Avalanche, BSC, Optimism, etc. (eip155)
isSolana(asset)Solana
isCosmos(asset)Cosmos ecosystem
isSui(asset)Sui
isTron(asset)Tron

Token Type Detection

Check whether an asset is a native gas token or a contract-based token.

import { isNativeTokenFromAsset, isEvmErc20, isSolanaToken } from '@silentswap/sdk';
 
if (isNativeTokenFromAsset(asset)) {
  console.log('This is a native gas token (ETH, SOL, BTC, etc.)');
}
 
if (isEvmErc20(asset)) {
  console.log('This is an ERC-20 token');
}
FunctionMatches
isNativeTokenFromAsset(asset)Any native token (ETH, SOL, BTC, BNB, etc.)
isEvmNative(asset)EVM native tokens (ETH, MATIC, AVAX, BNB)
isEvmErc20(asset)ERC-20 tokens
isSolanaNative(asset)SOL
isSolanaToken(asset)SPL tokens
isCosmosNative(asset)Cosmos native tokens
isCosmosIbc(asset)IBC tokens

CAIP-19 Field Extraction

Extract individual components from an asset's CAIP-19 identifier without manual string parsing.

import {
  getChainNamespaceFromAsset,
  getChainIdFromAsset,
  getAssetNamespaceFromAsset,
  getAssetId,
  getCaip2,
  getTokenAddress,
} from '@silentswap/sdk';
 
// For USDC on Ethereum (eip155:1/erc20:0xA0b8...)
getChainNamespaceFromAsset(usdc);    // "eip155"
getChainIdFromAsset(usdc);           // "1"
getAssetNamespaceFromAsset(usdc);    // "erc20"
getAssetId(usdc);                    // "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
getCaip2(usdc);                      // "eip155:1"
getTokenAddress(usdc);               // "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
 
// For native ETH (eip155:1/slip44:60)
getTokenAddress(eth);                // null (native tokens have no contract address)

Chain ID Helpers

getTokenChainId

Returns a numeric chain ID. EVM chains return their chain ID (e.g. 1, 137). Solana, Bitcoin, and Tron return their SDK constants.

function getTokenChainId(token: AssetInfo | null): number | null

getTokenChainIdString

Returns the chain ID portion of the CAIP-19 as a string. Works for all chain types.

function getTokenChainIdString(token: AssetInfo | null): string | null

extractChainIdFromAsset

Extract chain ID from a CAIP-19 string or AssetInfo. Returns number for EVM, or 'solana' / 'bitcoin' / 'tron' literals.

function extractChainIdFromAsset(
  asset: string | AssetInfo | null,
  solanaChainId?: number
): number | 'solana' | 'bitcoin' | 'tron' | null

extractChainIdsFromAssets

Deduplicate chain IDs from an array of CAIP-19 strings.

function extractChainIdsFromAssets(
  assets: string[],
  options?: { includeSolana?: boolean }
): (number | 'solana')[]
import { extractChainIdsFromAssets } from '@silentswap/sdk';
 
const caip19s = [
  'eip155:1/erc20:0xA0b8...',
  'eip155:1/slip44:60',
  'eip155:137/erc20:0xc213...',
  'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501',
];
 
const chainIds = extractChainIdsFromAssets(caip19s);
// [1, 137, 'solana']

Chain Info

getTokenChainInfo

Returns the full ChainInfo object for the chain an asset lives on.

function getTokenChainInfo(token: AssetInfo): ChainInfo | undefined

getTokenChainName

Returns the human-readable chain name.

function getTokenChainName(token: AssetInfo): string | null
import { getTokenChainName } from '@silentswap/sdk';
 
console.log(getTokenChainName(asset)); // "Ethereum"

Pattern Matching with switchChainNamespace

Branch logic by chain type without a chain of if/else checks. Provide a handler for each chain namespace you care about, plus an optional _else fallback.

import { switchChainNamespace } from '@silentswap/sdk';
 
const explorerUrl = switchChainNamespace(asset, {
  evm: () => `https://etherscan.io/token/${getTokenAddress(asset)}`,
  solana: () => `https://solscan.io/token/${getAssetId(asset)}`,
  bitcoin: () => 'https://mempool.space',
  tron: () => `https://tronscan.org/#/token20/${getAssetId(asset)}`,
  _else: () => null,
});
Type signature:
function switchChainNamespace<T>(
  token: AssetInfo,
  handlers: {
    bitcoin?: () => T;
    evm?: () => T;
    solana?: () => T;
    cosmos?: () => T;
    sui?: () => T;
    tron?: () => T;
    _else?: () => T;
  }
): T

If the matched handler is not provided, falls back to _else. If neither is provided, returns null.

Related