import {z} from 'zod' export type GetApiV1StatsUserVolumeArgs = {address: string, window?: string | undefined, start_time?: unknown | undefined, end_time?: unknown | undefined}; export type GetApiV1StatsUserVolumeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiGetUserVolumeStatsResponse = z.object({address: z.string().optional(), traded_volume: z.string().optional(), liquidation_volume: z.string().optional(), total_volume: z.string().optional(), maker_fee: z.string().optional(), taker_fee: z.string().optional(), liquidation_fee: z.string().optional(), total_fee: z.string().optional(), trade_count: z.number().int().optional(), window: z.string().optional(), start_time: z.string().optional(), end_time: z.string().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 user volume stats * * GET /v1/stats/user-volume */ export const getApiV1StatsUserVolume = async (args: GetApiV1StatsUserVolumeArgs, options?: GetApiV1StatsUserVolumeOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.window != null) query.append("window", String(args.window)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stats/user-volume${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(apiGetUserVolumeStatsResponse, await res.json()) };