import {z} from 'zod' export type EmptyString = string; export type AddressHashNullable = string | null; export type FloatString = string; export type NullString = string; export type IntegerString = string; export type GetApiChainIdApiV2TokensArgs = {type?: (EmptyString | string) | undefined, q?: (string | null) | undefined, limit?: (number | null) | undefined, sort?: ('fiat_value' | 'holders_count' | 'circulating_market_cap') | undefined, order?: ('asc' | 'desc') | undefined, items_count?: number | undefined, contract_address_hash?: AddressHashNullable | undefined, fiat_value?: (FloatString | EmptyString | NullString) | undefined, holders_count?: (IntegerString | EmptyString | NullString) | undefined, is_name_null?: boolean | undefined, market_cap?: (FloatString | EmptyString | NullString) | undefined, name?: string | undefined, chain_id: string}; export type GetApiChainIdApiV2TokensOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const addressHash = z.string(); export const floatStringNullable = z.string().nullable(); export const integerStringNullable = z.string().nullable(); export const uRLNullable = z.string().nullable(); export const tokenType = z.enum(["ERC-20", "ERC-721", "ERC-1155", "ERC-404", "ERC-7984"]); export const token = z.object({address_hash: addressHash, bridge_type: z.enum(["omni", "amb"]).nullable().optional(), circulating_market_cap: floatStringNullable, circulating_supply: floatStringNullable, decimals: integerStringNullable, exchange_rate: floatStringNullable, foreign_address: z.string().nullable().optional(), holders_count: integerStringNullable, icon_url: uRLNullable, name: z.string().nullable(), origin_chain_id: integerStringNullable.optional(), reputation: z.enum(["ok", "scam"]).nullable(), symbol: z.string().nullable(), total_supply: integerStringNullable, type: tokenType.nullable(), volume_24h: floatStringNullable}); export const getApiChainIdApiV2TokensResponse = z.object({items: z.array(token), next_page_params: z.record(z.string(), z.unknown()).nullable()}); 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 }; /** * List tokens with optional filtering by name, symbol, or type * * GET /{chain_id}/api/v2/tokens/ * * Also accepts auth: apiKeyAuth */ export const getApiChainIdApiV2Tokens = async (args: GetApiChainIdApiV2TokensArgs, options?: GetApiChainIdApiV2TokensOptions) => { const {baseUrl = "https://api.blockscout.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.items_count != null) query.append("items_count", String(args.items_count)) if (args.contract_address_hash != null) query.append("contract_address_hash", String(args.contract_address_hash)) if (args.fiat_value != null) query.append("fiat_value", String(args.fiat_value)) if (args.holders_count != null) query.append("holders_count", String(args.holders_count)) if (args.is_name_null != null) query.append("is_name_null", String(args.is_name_null)) if (args.market_cap != null) query.append("market_cap", String(args.market_cap)) if (args.name != null) query.append("name", String(args.name)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.chain_id))}/api/v2/tokens/${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(getApiChainIdApiV2TokensResponse, await res.json()) };