import {z} from 'zod' export type GetApiV1UsersUserPubkeyCollateralHistoryArgs = {limit: number, nextCursor?: string | undefined, prevCursor?: string | undefined, cursor?: string | undefined, user_pubkey: string}; export type GetApiV1UsersUserPubkeyCollateralHistoryOptions = {baseUrl?: string | undefined, PhoenixBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const collateralEvent = z.object({amount: z.number().int(), collateralAfter: z.number().int(), eventIndex: z.number().int(), eventType: z.string(), slot: z.number().int(), slotIndex: z.number().int(), timestamp: z.string(), traderPdaIndex: z.number().int(), traderSubaccountIndex: z.number().int()}); export const collateralHistoryResponse = z.object({data: z.array(collateralEvent), hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), prevCursor: z.string().nullable().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 collateral history * * GET /v1/users/{user_pubkey}/collateral-history */ export const getApiV1UsersUserPubkeyCollateralHistory = async (args: GetApiV1UsersUserPubkeyCollateralHistoryArgs, options?: GetApiV1UsersUserPubkeyCollateralHistoryOptions) => { const {baseUrl = "https://perp-api.phoenix.trade", fetch: fetchFn = fetch, PhoenixBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.nextCursor != null) query.append("nextCursor", String(args.nextCursor)) if (args.prevCursor != null) query.append("prevCursor", String(args.prevCursor)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (PhoenixBearerAuth !== undefined) headers.set('authorization', `Bearer ${PhoenixBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.user_pubkey))}/collateral-history${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(collateralHistoryResponse, await res.json()) };