import {z} from 'zod' export type GetApiV2AccountsAccountIdJettonsHistoryArgs = {account_id: unknown, before_lt?: number | undefined, limit: number}; export type GetApiV2AccountsAccountIdJettonsHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const accountAddress = z.object({address: z.string(), icon: z.string().optional(), is_scam: z.boolean(), is_wallet: z.boolean(), name: z.string().optional()}); export const scaledUI = z.object({denominator: z.string(), numerator: z.string()}); export const jettonVerificationType = z.enum(["whitelist", "graylist", "blacklist", "none"]); export const jettonPreview = z.object({address: z.string(), custom_payload_api_uri: z.string().optional(), decimals: z.number().int(), description: z.string().optional(), image: z.string(), name: z.string(), scaled_ui: scaledUI.optional(), score: z.number().int(), symbol: z.string(), verification: jettonVerificationType}); export const jettonOperation = z.object({amount: z.string(), destination: accountAddress.optional(), jetton: jettonPreview, lt: z.number().int(), operation: z.enum(["transfer", "mint", "burn"]), payload: z.unknown(), query_id: z.string(), source: accountAddress.optional(), trace_id: z.string(), transaction_hash: z.string(), utime: z.number().int()}); export const jettonOperations = z.object({next_from: z.number().int().optional(), operations: z.array(jettonOperation)}); 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 the transfer jettons history for account * * GET /v2/accounts/{account_id}/jettons/history */ export const getApiV2AccountsAccountIdJettonsHistory = async (args: GetApiV2AccountsAccountIdJettonsHistoryArgs, options?: GetApiV2AccountsAccountIdJettonsHistoryOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.before_lt != null) query.append("before_lt", String(args.before_lt)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.account_id))}/jettons/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(jettonOperations, await res.json()) };