import {z} from 'zod' export type CreateApiV2BlockchainAccountsBulkArgs = {body: {account_ids: Array}}; export type CreateApiV2BlockchainAccountsBulkOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ecPreview = z.object({decimals: z.number().int(), id: z.number().int(), image: z.string(), symbol: z.string()}); export const extraCurrency = z.object({amount: z.string(), preview: ecPreview}); export const accountStatus = z.enum(["nonexist", "uninit", "active", "frozen"]); export const accountStorageInfo = z.object({due_payment: z.number().int(), last_paid: z.number().int(), used_bits: z.number().int(), used_cells: z.number().int(), used_public_cells: z.number().int()}); export const blockchainRawAccount = z.object({address: z.string(), balance: z.number().int(), code: z.string().optional(), data: z.string().optional(), extra_balance: z.array(extraCurrency).optional(), frozen_hash: z.string().optional(), last_transaction_hash: z.string().optional(), last_transaction_lt: z.number().int(), libraries: z.array(z.object({public: z.boolean(), root: z.string()})).optional(), status: accountStatus, storage: accountStorageInfo}); export const blockchainRawAccounts = z.object({accounts: z.array(blockchainRawAccount)}); 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 low-level information about several accounts taken directly from the blockchain. * * POST /v2/blockchain/accounts/_bulk */ export const createApiV2BlockchainAccountsBulk = async (args: CreateApiV2BlockchainAccountsBulkArgs, options?: CreateApiV2BlockchainAccountsBulkOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/blockchain/accounts/_bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(blockchainRawAccounts, await res.json()) };