import {z} from 'zod' export type Int64Parameter = {eq?: (number | null) | undefined, ne?: (number | null) | undefined, gt?: (number | null) | undefined, ge?: (number | null) | undefined, lt?: (number | null) | undefined, le?: (number | null) | undefined, in?: (Array | null) | undefined, ni?: (Array | null) | undefined, empty?: boolean | undefined}; export type AccountParameter = {eq?: (string | null) | undefined, ne?: (string | null) | undefined, in?: (Array | null) | undefined, ni?: (Array | null) | undefined, eqx?: (string | null) | undefined, nex?: (string | null) | undefined, null?: (boolean | null) | undefined}; export type NatParameter = {eq?: (string | null) | undefined, ne?: (string | null) | undefined, gt?: (string | null) | undefined, ge?: (string | null) | undefined, lt?: (string | null) | undefined, le?: (string | null) | undefined, in?: (Array | null) | undefined, ni?: (Array | null) | undefined}; export type SortParameter = {asc?: (string | null) | undefined, desc?: (string | null) | undefined}; export type OffsetParameter = {el?: (number | null) | undefined, pg?: (number | null) | undefined, cr?: (number | null) | undefined}; export type SelectionParameter = {fields?: (Array | null) | undefined, values?: (Array | null) | undefined}; export type GetApiV1TicketsHistoricalBalancesLevelArgs = {level: number, id?: (Int64Parameter | null) | undefined, 'ticket.id'?: (Int64Parameter | null) | undefined, 'ticket.ticketer'?: (AccountParameter | null) | undefined, account?: (AccountParameter | null) | undefined, balance?: (NatParameter | null) | undefined, sort?: (SortParameter | null) | undefined, offset?: (OffsetParameter | null) | undefined, limit?: number | undefined, select?: (SelectionParameter | null) | undefined}; export type GetApiV1TicketsHistoricalBalancesLevelOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const alias = z.object({alias: z.string().nullable().optional(), address: z.string()}); export const michelineType = z.union([z.literal(0), z.literal(32), z.literal(64), z.literal(96), z.literal(128)]); export const iMicheline = z.object({type: michelineType.optional()}); export const ticketInfoShort = z.object({id: z.number().int().optional(), ticketer: alias, rawType: iMicheline, rawContent: iMicheline, content: z.unknown(), typeHash: z.number().int().optional(), contentHash: z.number().int().optional()}); export const ticketBalanceShort = z.object({id: z.number().int().optional(), ticket: ticketInfoShort, account: alias, balance: z.string().optional()}); export const getApiV1TicketsHistoricalBalancesLevelResponse = z.array(ticketBalanceShort); 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 historical ticket balances * * GET /v1/tickets/historical_balances/{level} */ export const getApiV1TicketsHistoricalBalancesLevel = async (args: GetApiV1TicketsHistoricalBalancesLevelArgs, options?: GetApiV1TicketsHistoricalBalancesLevelOptions) => { const {baseUrl = "https://api.tzkt.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", JSON.stringify(args.id)) if (args['ticket.id'] != null) query.append("ticket.id", JSON.stringify(args['ticket.id'])) if (args['ticket.ticketer'] != null) query.append("ticket.ticketer", JSON.stringify(args['ticket.ticketer'])) if (args.account != null) query.append("account", JSON.stringify(args.account)) if (args.balance != null) query.append("balance", JSON.stringify(args.balance)) if (args.sort != null) query.append("sort", JSON.stringify(args.sort)) if (args.offset != null) query.append("offset", JSON.stringify(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.select != null) query.append("select", JSON.stringify(args.select)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tickets/historical_balances/${encodeURIComponent(String(args.level))}${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(getApiV1TicketsHistoricalBalancesLevelResponse, await res.json()) };