Fetch token data from Jupiter and DexScreener APIs. This implementation enables token lookup by both address and ticker symbol, providing comprehensive token information for Solana tokens.

Core Features

  1. Token Data Retrieval

    • Address-based lookup
    • Ticker symbol lookup
    • Multiple data sources
    • Comprehensive token info
  2. API Integration

    • Jupiter API integration
    • DexScreener API support
    • Error handling
    • Data validation

Usage

Get Token by Address

// Using mint address
const tokenData = await getTokenDataByAddress(
  new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
);

// Returns JupiterTokenData
console.log(tokenData);

Get Token by Ticker

// Using ticker symbol
const tokenData = await getTokenDataByTicker("USDC");

// Returns JupiterTokenData or undefined
console.log(tokenData);

Data Structures

Jupiter Token Data

interface JupiterTokenData {
  address: string;        // Token mint address
  chainId: number;        // Solana chain ID
  decimals: number;       // Token decimals
  name: string;          // Token name
  symbol: string;        // Token symbol
  logoURI?: string;      // Optional logo URL
  tags?: string[];       // Optional token tags
  extensions?: {         // Optional extensions
    [key: string]: any;
  };
}

Example Prompts

Natural Language Prompts

"Get token data for USDC"

"Look up token info by address"

"Find token details for SOL"

"Get metadata for BONK token"

LangChain Tool Prompts

// Get by ticker
{
  "ticker": "USDC"
}

// Direct ticker input
"SOL"

Implementation Details

Address-based Lookup

async function getTokenDataByAddress(
  mint: PublicKey
): Promise<JupiterTokenData | undefined> {
  const response = await fetch(
    `https://tokens.jup.ag/token/${mint}`
  );
  return response.json();
}

Ticker-based Lookup

async function getTokenAddressFromTicker(
  ticker: string
): Promise<string | null> {
  // Use DexScreener for address lookup
  const response = await fetch(
    `https://api.dexscreener.com/latest/dex/search?q=${ticker}`
  );
  
  // Filter and sort by FDV
  const pairs = data.pairs
    .filter(pair => pair.chainId === "solana")
    .sort((a, b) => (b.fdv || 0) - (a.fdv || 0));
    
  return pairs[0]?.baseToken.address;
}

Error Handling

try {
  const tokenData = await getTokenDataByTicker(ticker);
} catch (error) {
  if (error.message.includes("not found")) {
    // Handle unknown token
  } else if (error.message.includes("API")) {
    // Handle API issues
  }
}

Best Practices

  1. Data Validation

    • Validate addresses
    • Check ticker format
    • Handle missing data
    • Verify responses
  2. API Management

    • Handle rate limits
    • Cache responses
    • Monitor errors
    • Implement retries
  3. Response Processing

    • Filter results
    • Sort by relevance
    • Handle duplicates
    • Format data

Common Issues

  1. Token Lookup

    • Unknown tokens
    • Invalid addresses
    • Missing data
    • API timeouts
  2. Data Quality

    • Outdated information
    • Missing metadata
    • Incorrect symbols
    • Logo URL issues
  3. API Issues

    • Rate limiting
    • Network errors
    • Service outages
    • Invalid responses

Response Format

Success Response

{
  status: "success",
  tokenData: {
    address: "token-address",
    symbol: "TOKEN",
    name: "Token Name",
    decimals: 6
    // ... other fields
  }
}

Error Response

{
  status: "error",
  message: "Error message",
  code: "ERROR_CODE"
}

Tips for Token Resolution

  1. Address Resolution

    • Validate format
    • Check checksum
    • Handle case sensitivity
    • Verify network
  2. Ticker Resolution

    • Handle case sensitivity
    • Check aliases
    • Filter by chain
    • Sort by relevance
  3. Data Management

    • Cache common tokens
    • Update periodically
    • Log resolutions
    • Monitor changes

Common Token Addresses

const COMMON_TOKENS = {
  USDC: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
  SOL: "So11111111111111111111111111111111111111112",
  BONK: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263"
};

Resources