# Token Data Retrieval

Fetch token data using Jupiter API and DexScreener

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.

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

Copy

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

// Returns JupiterTokenData
console.log(tokenData);
```

Copy

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

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

Copy

```typescript
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;
  };
}
```

Copy

```
"Get token data for USDC"

"Look up token info by address"

"Find token details for SOL"

"Get metadata for BONK token"
```

Copy

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

// Direct ticker input
"SOL"
```

Copy

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

Copy

```typescript
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;
}
```

Copy

```typescript
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
  }
}
```

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

4. **Token Lookup**
   * Unknown tokens
   * Invalid addresses
   * Missing data
   * API timeouts

5. **Data Quality**
   * Outdated information
   * Missing metadata
   * Incorrect symbols
   * Logo URL issues

6. **API Issues**
   * Rate limiting
   * Network errors
   * Service outages
   * Invalid responses

Copy

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

Copy

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

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

Copy

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

* [Jupiter API Docs](https://docs.jup.ag/apis/token-list-api)
* [DexScreener API](https://docs.dexscreener.com/api/reference)
* [Solana Token List](https://github.com/solana-labs/token-list)
* [Token Standards](https://spl.solana.com/token)
