import {z} from 'zod' export type GetApiV1AccountsAddressArgs = {address: string, only_confirmed?: boolean | undefined, only_unconfirmed?: boolean | undefined}; export type GetApiV1AccountsAddressOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1AccountsAddressResponse = z.object({data: z.array(z.object({address: z.string().optional(), balance: z.number().int().optional(), account_name: z.string().optional(), create_time: z.number().int().optional(), is_witness: z.boolean().optional(), allowance: z.number().int().optional(), latest_withdraw_time: z.number().int().optional(), latest_opration_time: z.number().int().optional(), latest_consume_time: z.number().int().optional(), latest_consume_free_time: z.number().int().optional(), net_window_size: z.number().int().optional(), net_window_optimized: z.boolean().optional(), account_resource: z.object({latest_consume_time_for_energy: z.number().int().optional(), acquired_delegated_frozen_balance_for_energy: z.number().int().optional(), delegated_frozen_balance_for_energy: z.number().int().optional(), energy_window_size: z.number().int().optional(), acquired_delegated_frozenV2_balance_for_energy: z.number().int().optional(), delegated_frozenV2_balance_for_energy: z.number().int().optional(), energy_window_optimized: z.boolean().optional(), frozen_balance_for_energy: z.object({frozen_balance: z.number().int().optional(), expire_time: z.number().int().optional()}).optional(), energy_usage: z.number().int().optional()}).optional(), frozen: z.array(z.object({frozen_balance: z.number().int().optional(), expire_time: z.number().int().optional()})).optional(), owner_permission: z.object({permission_name: z.string().optional(), threshold: z.number().int().optional(), keys: z.array(z.object({address: z.string().optional(), weight: z.number().int().optional()})).optional()}).optional(), active_permission: z.array(z.object({type: z.string().optional(), id: z.number().int().optional(), permission_name: z.string().optional(), threshold: z.number().int().optional(), operations: z.string().optional(), keys: z.array(z.object({address: z.string().optional(), weight: z.number().int().optional()})).optional()})).optional(), witness_permission: z.object({type: z.string().optional(), id: z.number().int().optional(), permission_name: z.string().optional(), threshold: z.number().int().optional(), keys: z.array(z.object({address: z.string().optional(), weight: z.number().int().optional()})).optional()}).optional(), frozenV2: z.array(z.object({amount: z.number().int().optional(), type: z.string().optional()})).optional(), acquired_delegated_frozenV2_balance_for_bandwidth: z.number().int().optional(), delegated_frozenV2_balance_for_bandwidth: z.number().int().optional(), acquired_delegated_frozen_balance_for_bandwidth: z.number().int().optional(), delegated_frozen_balance_for_bandwidth: z.number().int().optional(), assetV2: z.array(z.object({key: z.string().optional(), value: z.number().int().optional()})).optional(), net_usage: z.number().int().optional(), free_net_usage: z.number().int().optional(), free_asset_net_usageV2: z.array(z.object({key: z.string().optional(), value: z.number().int().optional()})).optional(), trc20: z.array(z.object({})).optional(), votes: z.array(z.object({vote_address: z.string().optional(), vote_count: z.number().int().optional()})).optional(), unfrozenV2: z.array(z.object({unfreeze_amount: z.number().int().optional(), unfreeze_expire_time: z.number().int().optional()})).optional()})).optional(), success: z.boolean().optional(), meta: z.object({page_size: z.number().int().optional(), at: z.number().int().optional()}).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 }; /** * Get account info by address * * GET /v1/accounts/{address} */ export const getApiV1AccountsAddress = async (args: GetApiV1AccountsAddressArgs, options?: GetApiV1AccountsAddressOptions) => { const {baseUrl = "https://api.shasta.trongrid.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.only_confirmed != null) query.append("only_confirmed", String(args.only_confirmed)) if (args.only_unconfirmed != null) query.append("only_unconfirmed", String(args.only_unconfirmed)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.address))}${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(getApiV1AccountsAddressResponse, await res.json()) };