import {z} from 'zod' export type Frequency = {hertz: number}; export type Voltage = {volt: number}; export type HashboardPerformanceSettings = {frequency?: (Frequency | null) | undefined, id: string, voltage?: (Voltage | null) | undefined}; export type ManualPerformanceMode = {global_frequency?: (Frequency | null) | undefined, global_voltage?: (Voltage | null) | undefined, hashboards: Array}; export type Power = {watt: number}; export type PowerTargetMode = {power_target?: (Power | null) | undefined}; export type TeraHashrate = {terahash_per_second: number}; export type HashrateTargetMode = {hashrate_target?: (TeraHashrate | null) | undefined}; export type TunerPerformanceModeTarget = {powertarget: PowerTargetMode} | {hashratetarget: HashrateTargetMode}; export type TunerPerformanceMode = {target?: (TunerPerformanceModeTarget | null) | undefined}; export type PerformanceModeMode = {manualmode: ManualPerformanceMode} | {tunermode: TunerPerformanceMode}; export type UpdateApiApiV1PerformanceModeArgs = {body: PerformanceModeMode}; export type UpdateApiApiV1PerformanceModeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const frequency = z.object({hertz: z.number()}); export const voltage = z.object({volt: z.number()}); export const hashboardPerformanceSettings = z.object({frequency: frequency.nullable().optional(), id: z.string(), voltage: voltage.nullable().optional()}); export const manualPerformanceMode = z.object({global_frequency: frequency.nullable().optional(), global_voltage: voltage.nullable().optional(), hashboards: z.array(hashboardPerformanceSettings)}); export const power = z.object({watt: z.number().int().gte(0)}); export const powerTargetMode = z.object({power_target: power.nullable().optional()}); export const teraHashrate = z.object({terahash_per_second: z.number()}); export const hashrateTargetMode = z.object({hashrate_target: teraHashrate.nullable().optional()}); export const tunerPerformanceModeTarget = z.union([z.object({powertarget: powerTargetMode}), z.object({hashratetarget: hashrateTargetMode})]); export const tunerPerformanceMode = z.object({target: tunerPerformanceModeTarget.nullable().optional()}); export const performanceModeMode = z.union([z.object({manualmode: manualPerformanceMode}), z.object({tunermode: tunerPerformanceMode})]); 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 }; /** * Set performance mode * * PUT /api/v1/performance/mode */ export const updateApiApiV1PerformanceMode = async (args: UpdateApiApiV1PerformanceModeArgs, options?: UpdateApiApiV1PerformanceModeOptions) => { const {baseUrl = "http://miner", 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(/\/+$/, '')}/api/v1/performance/mode${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(performanceModeMode, await res.json()) };