import {z} from 'zod' export type GetApiApiV1MinerStatsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const megaHashrate = z.object({megahash_per_second: z.number()}); export const gigaHashrate = z.object({gigahash_per_second: z.number()}); export const realHashrate = z.object({last_15m: gigaHashrate.nullable().optional(), last_15s: gigaHashrate.nullable().optional(), last_1h: gigaHashrate.nullable().optional(), last_1m: gigaHashrate.nullable().optional(), last_24h: gigaHashrate.nullable().optional(), last_30m: gigaHashrate.nullable().optional(), last_30s: gigaHashrate.nullable().optional(), last_5m: gigaHashrate.nullable().optional(), last_5s: gigaHashrate.nullable().optional(), since_restart: gigaHashrate.nullable().optional()}); export const workSolverStats = z.object({best_share: z.number().int().gte(0), best_share_str: z.string(), error_hashrate: megaHashrate.nullable().optional(), found_blocks: z.number().int().gte(0), nominal_hashrate: gigaHashrate.nullable().optional(), real_hashrate: realHashrate.nullable().optional()}); export const timestamp = z.object({nanos: z.number().int(), seconds: z.number().int()}); export const poolStats = z.object({accepted_shares: z.number().int().gte(0), best_share: z.number().int().gte(0), best_share_str: z.string(), generated_work: z.number().int().gte(0), last_difficulty: z.number().int().gte(0), last_share_time: timestamp.nullable().optional(), rejected_shares: z.number().int().gte(0), stale_shares: z.number().int().gte(0)}); export const power = z.object({watt: z.number().int().gte(0)}); export const powerEfficiency = z.object({joule_per_terahash: z.number()}); export const minerPowerStats = z.object({approximated_consumption: power.nullable().optional(), efficiency: powerEfficiency.nullable().optional()}); export const getMinerStatsResponse = z.object({miner_stats: workSolverStats.nullable().optional(), pool_stats: poolStats.nullable().optional(), power_stats: minerPowerStats.nullable().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 miner statistics * * GET /api/v1/miner/stats */ export const getApiApiV1MinerStats = async (options?: GetApiApiV1MinerStatsOptions) => { const {baseUrl = "http://miner", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/miner/stats${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(getMinerStatsResponse, await res.json()) };