|
| 1 | +import { Command } from "commander" |
| 2 | +import type { OpenSeaClient } from "../client.js" |
| 3 | +import { formatOutput } from "../output.js" |
| 4 | +import type { Chain, Token, TokenDetails } from "../types/index.js" |
| 5 | + |
| 6 | +export function tokensCommand( |
| 7 | + getClient: () => OpenSeaClient, |
| 8 | + getFormat: () => "json" | "table", |
| 9 | +): Command { |
| 10 | + const cmd = new Command("tokens").description( |
| 11 | + "Query trending tokens, top tokens, and token details", |
| 12 | + ) |
| 13 | + |
| 14 | + cmd |
| 15 | + .command("trending") |
| 16 | + .description("Get trending tokens based on OpenSea's trending score") |
| 17 | + .option("--chains <chains>", "Comma-separated list of chains to filter by") |
| 18 | + .option("--limit <limit>", "Number of results (max 100)", "20") |
| 19 | + .option("--cursor <cursor>", "Pagination cursor") |
| 20 | + .action( |
| 21 | + async (options: { chains?: string; limit: string; cursor?: string }) => { |
| 22 | + const client = getClient() |
| 23 | + const result = await client.get<{ tokens: Token[]; next?: string }>( |
| 24 | + "/api/v2/tokens/trending", |
| 25 | + { |
| 26 | + chains: options.chains, |
| 27 | + limit: Number.parseInt(options.limit, 10), |
| 28 | + cursor: options.cursor, |
| 29 | + }, |
| 30 | + ) |
| 31 | + console.log(formatOutput(result, getFormat())) |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + cmd |
| 36 | + .command("top") |
| 37 | + .description("Get top tokens ranked by 24-hour trading volume") |
| 38 | + .option("--chains <chains>", "Comma-separated list of chains to filter by") |
| 39 | + .option("--limit <limit>", "Number of results (max 100)", "20") |
| 40 | + .option("--cursor <cursor>", "Pagination cursor") |
| 41 | + .action( |
| 42 | + async (options: { chains?: string; limit: string; cursor?: string }) => { |
| 43 | + const client = getClient() |
| 44 | + const result = await client.get<{ tokens: Token[]; next?: string }>( |
| 45 | + "/api/v2/tokens/top", |
| 46 | + { |
| 47 | + chains: options.chains, |
| 48 | + limit: Number.parseInt(options.limit, 10), |
| 49 | + cursor: options.cursor, |
| 50 | + }, |
| 51 | + ) |
| 52 | + console.log(formatOutput(result, getFormat())) |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + cmd |
| 57 | + .command("get") |
| 58 | + .description("Get detailed information about a specific token") |
| 59 | + .argument("<chain>", "Blockchain chain") |
| 60 | + .argument("<address>", "Token contract address") |
| 61 | + .action(async (chain: string, address: string) => { |
| 62 | + const client = getClient() |
| 63 | + const result = await client.get<TokenDetails>( |
| 64 | + `/api/v2/chain/${chain as Chain}/token/${address}`, |
| 65 | + ) |
| 66 | + console.log(formatOutput(result, getFormat())) |
| 67 | + }) |
| 68 | + |
| 69 | + return cmd |
| 70 | +} |
0 commit comments