import {z} from 'zod' export type GetApiV2PurchasesAccountIdHistoryArgs = {account_id: unknown, before_lt?: number | undefined, limit?: number | undefined}; export type GetApiV2PurchasesAccountIdHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyType = z.enum(["native", "extra_currency", "jetton", "fiat"]); export const trustType = z.enum(["whitelist", "graylist", "blacklist", "none"]); export const price = z.object({currency_type: currencyType, decimals: z.number().int(), image: z.string(), jetton: z.string().optional(), token_name: z.string(), value: z.string(), verification: trustType}); 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 metadata = z.object({decryption_key: z.string().optional(), encrypted_binary: z.string()}); export const purchase = z.object({amount: price, destination: accountAddress, event_id: z.string(), invoice_id: z.string(), lt: z.number().int(), metadata: metadata, source: accountAddress, utime: z.number().int()}); export const accountPurchases = z.object({next_from: z.number().int(), purchases: z.array(purchase)}); 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 history of purchases * * GET /v2/purchases/{account_id}/history */ export const getApiV2PurchasesAccountIdHistory = async (args: GetApiV2PurchasesAccountIdHistoryArgs, options?: GetApiV2PurchasesAccountIdHistoryOptions) => { 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/purchases/${encodeURIComponent(String(args.account_id))}/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(accountPurchases, await res.json()) };