import {z} from 'zod' export type ChainEnumForTrendingTokens = 'ethereum-mainnet' | 'base-mainnet'; export type GetApiV4DataMarketplaceCryptoslamTokenPriceArgs = {chain: ChainEnumForTrendingTokens, symbols?: string | undefined, addresses?: string | undefined, quoteSymbol?: ('USD' | 'ETH' | 'BTC') | undefined, estimateCost?: boolean | undefined}; export type GetApiV4DataMarketplaceCryptoslamTokenPriceOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV4DataMarketplaceCryptoslamTokenPriceResponse = z.object({slam: z.number().optional(), chain: z.string().optional(), block: z.number().optional(), utc: z.string().optional(), quote: z.string().optional(), tokens: z.array(z.object({name: z.string().optional(), symbol: z.string().optional(), address: z.string().optional(), price: z.number().optional(), prev: z.array(z.object({minutes: z.number().optional(), change: z.string().optional()})).optional(), fdv: z.number().optional(), liquidity: z.number().optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get token prices from Cryptoslam * * GET /v4/data/marketplace/cryptoslam/token/price */ export const getApiV4DataMarketplaceCryptoslamTokenPrice = async (args: GetApiV4DataMarketplaceCryptoslamTokenPriceArgs, options?: GetApiV4DataMarketplaceCryptoslamTokenPriceOptions) => { const {baseUrl = "https://api.tatum.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.chain != null) query.append("chain", String(args.chain)) if (args.symbols != null) query.append("symbols", String(args.symbols)) if (args.addresses != null) query.append("addresses", String(args.addresses)) if (args.quoteSymbol != null) query.append("quoteSymbol", String(args.quoteSymbol)) if (args.estimateCost != null) query.append("estimateCost", String(args.estimateCost)) if (XAPIKey !== undefined) headers.set("x-api-key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v4/data/marketplace/cryptoslam/token/price${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV4DataMarketplaceCryptoslamTokenPriceResponse, await res.json()) };