import {z} from 'zod' export type GetApiExecutionsArgs = {profile_id?: string | undefined, account_id?: string | undefined, order_id?: string | undefined, since_execution_id?: string | undefined, 'range.begin'?: string | undefined, 'range.end'?: string | undefined, page_cursor?: string | undefined, limit?: number | undefined}; export type GetApiExecutionsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const market = z.enum(["ETHEUR", "ETHSGD", "ETHUSD", "BTCEUR", "BTCSGD", "BTCUSD", "PAXGUSD", "BCHUSD", "LTCUSD", "USDPUSD", "LINKUSD", "AAVEUSD", "UNIUSD", "PEPEUSD", "TRUMPUSD", "SHIBUSD", "ARBUSD", "BONKUSD", "ENAUSD", "MNTUSD", "ONDOUSD", "PENGUUSD", "QNTUSD", "RENDERUSD", "SKYUSD", "WIFUSD", "WLDUSD", "DOGEUSD", "AVAXUSD", "SUIUSD", "POLUSD", "XLMUSD", "BNBUSD"]); export const orderSide = z.enum(["BUY", "SELL"]); export const execution = z.object({execution_id: z.string().optional(), order_id: z.string().optional(), executed_at: z.string().optional(), market: market.optional(), side: orderSide.optional(), amount: z.string().optional(), price: z.string().optional(), commission: z.string().optional(), commission_asset: z.string().optional(), rebate: z.string().optional(), rebate_asset: z.string().optional(), account_id: z.string().optional(), gross_trade_amount: z.string().optional()}); export const listExecutionsResponse = z.object({items: z.array(execution).optional(), next_page_cursor: 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 }; /** * List Executions * * GET /executions */ export const getApiExecutions = async (args: GetApiExecutionsArgs, options?: GetApiExecutionsOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.profile_id != null) query.append("profile_id", String(args.profile_id)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.order_id != null) query.append("order_id", String(args.order_id)) if (args.since_execution_id != null) query.append("since_execution_id", String(args.since_execution_id)) if (args['range.begin'] != null) query.append("range.begin", String(args['range.begin'])) if (args['range.end'] != null) query.append("range.end", String(args['range.end'])) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/executions${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(listExecutionsResponse, await res.json()) };