import {z} from 'zod' export type GetApiV1ContestsArgs = {active?: boolean | undefined}; export type GetApiV1ContestsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiContentReference = z.object({ref_name: z.string(), ref_url: z.string()}); export const apiDexSwapConditionType = z.enum(["unique", "listed", "partner_address", "tokens", "tokens-pair", "tokens-exact", "users-whitelist", "swap-tx-min-amount", "blacklist-pair", "blacklist-tokens", "dexes"]); export const apiDexSwapCondition = z.object({type: apiDexSwapConditionType, args: z.array(z.string()).optional()}); export const apiContestReward = z.object({reward: z.string(), place: z.number().int().optional(), place_from: z.number().int().optional(), place_to: z.number().int().optional()}); export const apiContestInfo = z.object({title: z.string(), description: z.string(), references: z.array(apiContentReference), conditions: z.array(apiDexSwapCondition), rewards: z.array(apiContestReward), start_time_unix: z.number().int(), end_time_unix: z.number().int(), image_url: z.string(), total_reward: z.string(), hidden: z.boolean()}); export const apiContestInfoWithID = z.object({id: z.number().int(), info: apiContestInfo}); export const getApiV1ContestsResponse = z.array(apiContestInfoWithID); 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 contests * * GET /v1/contests */ export const getApiV1Contests = async (args: GetApiV1ContestsArgs, options?: GetApiV1ContestsOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active != null) query.append("active", String(args.active)) if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contests${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(getApiV1ContestsResponse, await res.json()) };