import {z} from 'zod' export type GetApiV2BlockchainAccountsAccountIdArgs = {account_id: unknown}; export type GetApiV2BlockchainAccountsAccountIdOptions = {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 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 an account taken directly from the blockchain. * * GET /v2/blockchain/accounts/{account_id} */ export const getApiV2BlockchainAccountsAccountId = async (args: GetApiV2BlockchainAccountsAccountIdArgs, options?: GetApiV2BlockchainAccountsAccountIdOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/blockchain/accounts/${encodeURIComponent(String(args.account_id))}${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(blockchainRawAccount, await res.json()) };