import {z} from 'zod' export type MD5 = string; export type Address = string; export type ChallengeId = string; export type AlgorithmId = string; export type TrackId = string; export type BenchmarkSettings = {player_id?: Address | undefined, block_id?: MD5 | undefined, challenge_id?: ChallengeId | undefined, algorithm_id?: AlgorithmId | undefined, track_id?: TrackId | undefined}; export type TrackSettings = {hyperparameters?: ({[key: string]: unknown} | null) | undefined, fuel_budget: number, num_bundles: number}; export type SubmitPrecommitRequest = {settings: BenchmarkSettings, track_settings: {[key: string]: TrackSettings}, compute_type: 'aws_t3' | 'aws_t3a' | 'aws_t4g' | 'aws_c7i' | 'aws_c7a' | 'aws_c7g' | 'aws_m7i' | 'aws_m7a' | 'aws_m7g' | 'aws_g4dn'}; export type CreateApiSubmitPrecommitArgs = {'X-Api-Key'?: MD5 | undefined, body: SubmitPrecommitRequest}; export type CreateApiSubmitPrecommitOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mD5 = z.string(); export const submitPrecommitResponse = z.object({benchmark_id: mD5.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 }; /** * Submit a precommit * * POST /submit-precommit */ export const createApiSubmitPrecommit = async (args: CreateApiSubmitPrecommitArgs, options?: CreateApiSubmitPrecommitOptions) => { const {baseUrl = "https://mainnet-api.tig.foundation", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Api-Key'] != null) headers.set("X-Api-Key", String(args['X-Api-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/submit-precommit${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(submitPrecommitResponse, await res.json()) };