import {z} from 'zod' export type GetApiV1DextradesActivityArgs = {active_within: number, windows: Array}; export type GetApiV1DextradesActivityOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiDexActivityWindow = z.object({window: z.number().int(), change: z.number().nullable().optional(), volume_usd_total: z.number(), volume_usd_buy: z.number(), volume_usd_sell: z.number(), trades_total: z.number().int(), trades_buy: z.number().int(), trades_sell: z.number().int(), uniq_traders: z.number().int(), uniq_buyers: z.number().int(), uniq_sellers: z.number().int()}); export const apiDexActivityToken = z.object({token: z.string(), price_usd: z.number(), tvl_usd: z.number().nullable().optional(), pools_count: z.number().int().nullable().optional(), windows: z.array(apiDexActivityWindow)}); export const getApiV1DextradesActivityResponse = z.array(apiDexActivityToken); 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 }; /** * Tokens with trades within the last active_within seconds, with metrics per window. * * GET /v1/dextrades/activity */ export const getApiV1DextradesActivity = async (args: GetApiV1DextradesActivityArgs, options?: GetApiV1DextradesActivityOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active_within != null) query.append("active_within", String(args.active_within)) for (const item of args.windows ?? []) query.append("windows", String(item)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dextrades/activity${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(getApiV1DextradesActivityResponse, await res.json()) };