import {z} from 'zod' export type GetApiV1RequestsUnshieldedHistoryArgs = {address?: string | undefined, txId?: (string | Array) | undefined, offset?: number | undefined}; export type GetApiV1RequestsUnshieldedHistoryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1RequestsUnshieldedHistoryResponse = z.object({networkId: z.string(), contractAddress: z.string(), history: z.array(z.object({txId: z.string().min(1), txHash: z.string(), blockHeight: z.number().int(), timestamp: z.string(), requestType: z.enum(["swap", "liquidity"]), liquidityAction: z.enum(["deposit", "withdraw", "liquidityPoolCreation"]).nullable(), poolId: z.string(), poolType: z.enum(["unshielded", "mixed", "stableUnshielded"]), tokenA: z.string(), tokenB: z.string(), lpToken: z.string(), sellToken: z.string().nullable(), buyToken: z.string().nullable(), amountIn: z.string(), amountInSecondary: z.string().nullable(), amountOut: z.string(), amountOutSecondary: z.string().nullable()})), pageSize: z.number().int(), offset: z.number().int(), hasMore: z.boolean()}); 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 indexed direct-unshielded request history, optionally filtered by address or txId. * * GET /v1/requests/unshielded/history */ export const getApiV1RequestsUnshieldedHistory = async (args: GetApiV1RequestsUnshieldedHistoryArgs, options: GetApiV1RequestsUnshieldedHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.txId != null) query.append("txId", String(args.txId)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/requests/unshielded/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(getApiV1RequestsUnshieldedHistoryResponse, await res.json()) };