import {z} from 'zod' export type GetApiV1TraderAuthorityCollateralHistoryArgs = {authority: string, pdaIndex?: number | undefined, nextCursor?: string | undefined, prevCursor?: string | undefined, cursor?: string | undefined, limit: number}; export type GetApiV1TraderAuthorityCollateralHistoryOptions = {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 trader collateral history * * GET /v1/trader/{authority}/collateral-history */ export const getApiV1TraderAuthorityCollateralHistory = async (args: GetApiV1TraderAuthorityCollateralHistoryArgs, options?: GetApiV1TraderAuthorityCollateralHistoryOptions) => { 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.pdaIndex != null) query.append("pdaIndex", String(args.pdaIndex)) 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 (args.limit != null) query.append("limit", String(args.limit)) if (PhoenixBearerAuth !== undefined) headers.set('authorization', `Bearer ${PhoenixBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/trader/${encodeURIComponent(String(args.authority))}/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()) };