import {z} from 'zod' export type GetApiV1UsersUserPubkeyTradesV2Args = {market_symbol?: string | undefined, trader_pda_index?: number | undefined, limit?: number | undefined, cursor?: string | undefined, privy_id?: string | undefined, user_pubkey: string}; export type GetApiV1UsersUserPubkeyTradesV2Options = {baseUrl?: string | undefined, PhoenixBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const liquidityRole = z.enum(["maker", "taker"]); export const tradeType = z.enum(["limit", "market", "liquidation", "adl"]); export const paginatedResponseVecTradeHistoryV2Item = z.object({data: z.array(z.object({baseLotsAfter: z.string(), baseLotsBefore: z.string(), baseLotsDelta: z.string(), eventIndex: z.number().int(), fees: z.string(), fillId: z.string().nullable().optional(), instructionIndex: z.number().int(), instructionType: z.string(), liquidity: liquidityRole, marketSymbol: z.string(), orderSequenceNumber: z.number().int().nullable().optional(), price: z.string(), realizedPnl: z.string(), signature: z.string().nullable().optional(), slot: z.number().int(), slotIndex: z.number().int(), splineSequenceNumber: z.number().int().nullable().optional(), subaccountIndex: z.number().int(), timestamp: z.string(), tradeType: tradeType, traderId: z.number().int(), traderPdaIndex: z.number().int(), userId: z.number().int(), virtualQuoteLotsAfter: z.string(), virtualQuoteLotsBefore: z.string(), virtualQuoteLotsDelta: z.string()})), 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 trade history v2 * * GET /v1/users/{user_pubkey}/trades_v2 */ export const getApiV1UsersUserPubkeyTradesV2 = async (args: GetApiV1UsersUserPubkeyTradesV2Args, options?: GetApiV1UsersUserPubkeyTradesV2Options) => { 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.market_symbol != null) query.append("market_symbol", String(args.market_symbol)) if (args.trader_pda_index != null) query.append("trader_pda_index", String(args.trader_pda_index)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.privy_id != null) query.append("privy_id", String(args.privy_id)) if (PhoenixBearerAuth !== undefined) headers.set('authorization', `Bearer ${PhoenixBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.user_pubkey))}/trades_v2${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(paginatedResponseVecTradeHistoryV2Item, await res.json()) };