import {z} from 'zod' export type MD5 = string; export type Address = string; export type GetApiGetBenchmarksArgs = {block_id: MD5, player_id: Address}; export type GetApiGetBenchmarksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mD5 = z.string(); export const preciseNumber = z.string(); export const precommitDetails = z.object({block_started: z.number().int().optional(), num_bundles: z.number().int().optional(), num_nonces: z.number().int().optional(), rand_hash: mD5.optional(), fee_paid: preciseNumber.optional(), fuel_budget: z.number().int().optional(), hyperparameters: z.object({}).optional(), compute_type: z.enum(["aws_t3", "aws_t3a", "aws_t4g", "aws_c7i", "aws_c7a", "aws_c7g", "aws_m7i", "aws_m7a", "aws_m7g", "aws_g4dn"]).optional()}); export const address = z.string(); export const challengeId = z.string(); export const algorithmId = z.string(); export const trackId = z.string(); export const benchmarkSettings = z.object({player_id: address.optional(), block_id: mD5.optional(), challenge_id: challengeId.optional(), algorithm_id: algorithmId.optional(), track_id: trackId.optional()}); export const precommitState = z.object({block_confirmed: z.number().int().optional()}); export const precommit = z.object({benchmark_id: mD5.optional(), details: precommitDetails.optional(), settings: benchmarkSettings.optional(), state: precommitState.optional(), hyperparameters: z.object({}).nullable().optional()}); export const merkleHash = z.string(); export const benchmarkDetails = z.object({num_solutions: z.number().int().optional(), num_discarded_solutions: z.number().int().optional(), num_non_solutions: z.number().int().optional(), merkle_root: merkleHash.optional(), sampled_nonces: z.array(z.number().int()).optional()}); export const benchmarkState = z.object({block_confirmed: z.number().int().optional()}); export const benchmark = z.object({id: z.string().optional(), details: benchmarkDetails.optional(), state: benchmarkState.optional(), solution_nonces: z.array(z.number().int()).nullable().optional(), discarded_solution_nonces: z.array(z.number().int()).nullable().optional(), non_solution_nonces: z.array(z.number().int()).nullable().optional()}); export const proofDetails = z.object({submission_delay: z.number().int().optional()}); export const proofState = z.object({block_confirmed: z.number().int().optional(), block_active: z.number().int().optional()}); export const outputData = z.object({nonce: z.number().int().optional(), runtime_signature: z.array(z.array(z.number().int())).optional(), fuel_consumed: z.number().int().optional(), solution: z.record(z.string(), z.string()).optional(), cpu_arch: z.enum(["amd64", "arm64"]).optional()}); export const merkleBranch = z.string(); export const merkleProof = z.object({leaf: outputData.optional(), branch: merkleBranch.optional()}); export const proof = z.object({benchmark_id: mD5.optional(), details: proofDetails.optional(), state: proofState.optional(), merkle_proofs: z.array(merkleProof).nullable().optional()}); export const fraudState = z.object({block_confirmed: z.number().int().optional()}); export const fraud = z.object({benchmark_id: mD5.optional(), state: fraudState.optional(), allegation: z.string().nullable().optional()}); export const getBenchmarksResponse = z.object({precommits: z.array(precommit).optional(), benchmarks: z.array(benchmark).optional(), proofs: z.array(proof).optional(), frauds: z.array(fraud).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 latest confirmed benchmarks for a player * * GET /get-benchmarks */ export const getApiGetBenchmarks = async (args: GetApiGetBenchmarksArgs, options?: GetApiGetBenchmarksOptions) => { const {baseUrl = "https://mainnet-api.tig.foundation", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.block_id != null) query.append("block_id", String(args.block_id)) if (args.player_id != null) query.append("player_id", String(args.player_id)) const url = `${baseUrl.replace(/\/+$/, '')}/get-benchmarks${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(getBenchmarksResponse, await res.json()) };