import {z} from 'zod' export type GetApiMessagesArgs = {hash?: string | undefined, src_workchain?: number | undefined, dst_workchain?: number | undefined, src_address?: Array | undefined, dst_address?: Array | undefined, operation_id?: string | undefined, src_contract?: Array | undefined, dst_contract?: Array | undefined, operation_name?: Array | undefined, order?: ('ASC' | 'DESC') | undefined, after?: number | undefined, limit?: number | undefined, count?: boolean | undefined}; export type GetApiMessagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bunbigInt = z.object({}); export const abiTLBType = z.enum(["addr", "bool", "bigInt", "string", "bytes", "cell", "slice", "content", "struct", "tag"]); export const abiStackType = z.enum(["int", "cell", "slice"]); export const abiTLBFieldsDesc = z.array(z.lazy(() => abiTLBFieldDesc)); export const abiTLBFieldDesc: z.ZodType = z.object({format: abiTLBType.optional(), name: z.string().optional(), optional: z.boolean().optional(), struct_fields: abiTLBFieldsDesc.optional(), tlb_type: z.string().optional()}); export const abiVmValueDesc = z.object({format: abiTLBType.optional(), name: z.string().optional(), stack_type: abiStackType.optional(), struct_fields: z.array(abiTLBFieldDesc).optional()}); export const abiGetMethodExecution = z.object({address: z.array(z.number().int()).optional(), arguments: z.array(abiVmValueDesc).optional(), error: z.string().optional(), name: z.string().optional(), receives: z.array(z.unknown()).optional(), return_values: z.array(abiVmValueDesc).optional(), returns: z.array(z.unknown()).optional()}); export const coreAddressLabel = z.object({address: z.array(z.number().int()).optional(), categories: z.array(z.string()).optional(), name: z.string().optional()}); export const coreAccountState = z.object({address: z.array(z.number().int()).optional(), balance: bunbigInt.optional(), block_seq_no: z.number().int().optional(), code: z.array(z.number().int()).optional(), code_hash: z.array(z.number().int()).optional(), content_description: z.string().optional(), content_image: z.string().optional(), content_image_data: z.array(z.number().int()).optional(), content_name: z.string().optional(), content_uri: z.string().optional(), data: z.array(z.number().int()).optional(), data_hash: z.array(z.number().int()).optional(), executed_get_methods: z.record(z.string(), z.array(abiGetMethodExecution)).optional(), fake: z.boolean().optional(), get_method_hashes: z.array(z.number().int()).optional(), is_active: z.boolean().optional(), jetton_balance: z.string().optional(), label: coreAddressLabel.optional(), last_tx_hash: z.array(z.number().int()).optional(), last_tx_lt: z.number().int().optional(), libraries: z.array(z.number().int()).optional(), minter_address: z.array(z.number().int()).optional(), owner_address: z.array(z.number().int()).optional(), shard: z.number().int().optional(), state_hash: z.array(z.number().int()).optional(), status: z.string().optional(), types: z.array(z.string()).optional(), updated_at: z.string().optional(), workchain: z.number().int().optional()}); export const coreMessage = z.object({amount: bunbigInt.optional(), body: z.array(z.number().int()).optional(), body_hash: z.array(z.number().int()).optional(), bounce: z.boolean().optional(), bounced: z.boolean().optional(), created_at: z.string().optional(), created_lt: z.number().int().optional(), data: z.array(z.number().int()).optional(), dst_address: z.array(z.number().int()).optional(), dst_block_seq_no: z.number().int().optional(), dst_contract: z.string().optional(), dst_shard: z.number().int().optional(), dst_state: coreAccountState.optional(), dst_tx_hash: z.array(z.number().int()).optional(), dst_tx_lt: z.number().int().optional(), dst_workchain: z.number().int().optional(), error: z.string().optional(), fwd_fee: bunbigInt.optional(), hash: z.array(z.number().int()).optional(), ihr_disabled: z.boolean().optional(), ihr_fee: bunbigInt.optional(), operation_id: z.number().int().optional(), operation_name: z.string().optional(), src_address: z.array(z.number().int()).optional(), src_block_seq_no: z.number().int().optional(), src_contract: z.string().optional(), src_shard: z.number().int().optional(), src_state: coreAccountState.optional(), src_tx_hash: z.array(z.number().int()).optional(), src_tx_lt: z.number().int().optional(), src_workchain: z.number().int().optional(), state_init_code: z.array(z.number().int()).optional(), state_init_data: z.array(z.number().int()).optional(), transfer_comment: z.string().optional(), type: z.string().optional()}); export const filterMessagesRes = z.object({results: z.array(coreMessage).optional(), total: z.number().int().optional()}); 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 }; /** * transaction messages * * GET /messages */ export const getApiMessages = async (args: GetApiMessagesArgs, options?: GetApiMessagesOptions) => { const {baseUrl = "https://anton.tools/api/v0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hash != null) query.append("hash", String(args.hash)) if (args.src_workchain != null) query.append("src_workchain", String(args.src_workchain)) if (args.dst_workchain != null) query.append("dst_workchain", String(args.dst_workchain)) for (const item of args.src_address ?? []) query.append("src_address", String(item)) for (const item of args.dst_address ?? []) query.append("dst_address", String(item)) if (args.operation_id != null) query.append("operation_id", String(args.operation_id)) for (const item of args.src_contract ?? []) query.append("src_contract", String(item)) for (const item of args.dst_contract ?? []) query.append("dst_contract", String(item)) for (const item of args.operation_name ?? []) query.append("operation_name", String(item)) if (args.order != null) query.append("order", String(args.order)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.count != null) query.append("count", String(args.count)) const url = `${baseUrl.replace(/\/+$/, '')}/messages${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(filterMessagesRes, await res.json()) };