import {z} from 'zod' export type StakingAddress = string; export type GetApiConsensusAccountsAddressArgs = {address: StakingAddress}; export type GetApiConsensusAccountsAddressOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const textBigInt = z.string(); export const allowance = z.object({address: z.string(), amount: textBigInt}); export const accountStats = z.object({total_sent: textBigInt.optional(), total_received: textBigInt.optional(), num_txns: z.number().int()}); export const account = z.object({address: z.string(), nonce: z.number().int(), available: textBigInt, escrow: textBigInt, debonding: textBigInt, delegations_balance: textBigInt, debonding_delegations_balance: textBigInt, first_activity: z.string().optional(), allowances: z.array(allowance), stats: accountStats}); 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 a consensus layer account. * * GET /consensus/accounts/{address} */ export const getApiConsensusAccountsAddress = async (args: GetApiConsensusAccountsAddressArgs, options?: GetApiConsensusAccountsAddressOptions) => { const {baseUrl = "https://nexus.oasis.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/consensus/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(account, await res.json()) };