import {z} from 'zod' export type GetApiV1UserFeesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiNextTierProgress = z.object({tier: z.number().int().optional(), threshold_usd: z.string().optional(), remaining_usd: z.string().optional(), progress_pct: z.number().optional(), taker_bps: z.number().optional(), maker_bps: z.number().optional()}); export const apiFeeScheduleEntry = z.object({tier: z.number().int().optional(), threshold_usd: z.string().optional(), taker_bps: z.number().optional(), maker_bps: z.number().optional()}); export const apiGetUserFeesResponse = z.object({tier: z.number().int().optional(), taker_bps: z.number().optional(), maker_bps: z.number().optional(), weighted_14d_volume_usd: z.string().optional(), applied_at: z.string().optional(), next_tier: apiNextTierProgress.optional(), schedule: z.array(apiFeeScheduleEntry).optional(), trial_tier: z.number().int().optional(), trial_ends_at: z.string().optional(), earned_tier: z.number().int().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 my fees * * GET /v1/user/fees */ export const getApiV1UserFees = async (options?: GetApiV1UserFeesOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/user/fees${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(apiGetUserFeesResponse, await res.json()) };