import {z} from 'zod' export type NamePrivileged = string; export type NameBasic = string; export type NameBid = string; export type NameCatchAll = string; export type Name = NamePrivileged | NameBasic | NameBid | NameCatchAll; export type CreateApiGetAccountArgs = {body: {account_name: Name}}; export type CreateApiGetAccountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const namePrivileged = z.string(); export const nameBasic = z.string(); export const nameBid = z.string(); export const nameCatchAll = z.string(); export const name = z.union([namePrivileged, nameBasic, nameBid, nameCatchAll]); export const dateTimeSeconds = z.string(); export const symbol = z.string(); export const refundRequest = z.object({owner: name, request_time: z.string(), net_amount: symbol, cpu_amount: symbol}).nullable(); export const wholeNumber = z.union([z.string(), z.number().int()]); export const resourceLimits = z.object({max: wholeNumber, available: wholeNumber, used: wholeNumber}); export const resourceOverview = z.object({owner: name, ram_bytes: wholeNumber, net_weight: symbol, cpu_weight: symbol}); export const resourceDelegation = z.object({from: name, to: name, net_weight: symbol, cpu_weight: symbol}).nullable(); export const wait = z.object({wait_sec: z.number().int(), weight: z.number().int()}); export const publicKey = z.string(); export const keyAuthority = z.object({key: publicKey, weight: z.number().int()}); export const authority = z.object({actor: name, permission: name}); export const accountAuthority = z.object({weight: z.number().int(), permission: authority}); export const requiredAuthority = z.object({waits: z.array(wait), keys: z.array(keyAuthority), threshold: z.number().int(), accounts: z.array(accountAuthority)}); export const permission = z.object({parent: z.string(), perm_name: z.string(), required_auth: requiredAuthority}); export const emptyString = z.string(); export const voterInfo = z.object({owner: name, proxy: z.union([name, emptyString]), producers: z.array(name), staked: wholeNumber, last_vote_weight: z.string(), proxied_vote_weight: z.string(), is_proxy: z.number().int(), flags1: z.number().int(), reserved2: z.number().int(), reserved3: symbol}).nullable(); export const account = z.object({account_name: name, head_block_num: z.number().int(), head_block_time: dateTimeSeconds, last_code_update: dateTimeSeconds, created: dateTimeSeconds, refund_request: refundRequest, ram_quota: wholeNumber, net_limit: resourceLimits, cpu_limit: resourceLimits, total_resources: resourceOverview, core_liquid_balance: symbol, self_delegated_bandwidth: resourceDelegation, net_weight: wholeNumber, cpu_weight: wholeNumber, ram_usage: wholeNumber, privileged: z.boolean(), permissions: z.array(permission), voter_info: voterInfo}); 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 }; /** * Returns an object containing various details about a specific account on the blockchain. * * POST /get_account */ export const createApiGetAccount = async (args: CreateApiGetAccountArgs, options?: CreateApiGetAccountOptions) => { const {baseUrl = "https://wax.greymass.com/v1/chain", 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(/\/+$/, '')}/get_account${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(account, await res.json()) };