import {z} from 'zod' export type GetApiV1ProtocolStateArgs = {granularity?: ('5m' | '1h' | '1d') | undefined, days?: number | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiV1ProtocolStateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const date = z.string(); export const decimal = z.string(); export const protocolState = z.object({since: date, until: date, granularity: z.enum(["5m", "1h", "1d"]), series: z.array(z.object({ts: z.string(), markets: z.array(z.object({market: z.string(), collateralRatio: decimal.optional(), levercoinLeverage: decimal.optional(), stablecoinNav: decimal.optional(), levercoinNav: decimal.optional(), virtualStablecoinSupply: decimal.optional(), levercoinSupply: decimal.optional(), totalCollateral: decimal.optional(), collateralPriceUsd: decimal.optional(), tvlUsd: decimal.optional()})), usdcPeg: z.object({vaultReserves: decimal.optional(), virtualStablecoinSupply: decimal.optional()}).optional(), earnPool: z.object({hyusdPoolBalance: decimal.optional(), ehyusdSupply: decimal.optional(), ehyusdNav: decimal.optional(), effectiveApy: decimal.optional(), projectedApy: decimal.optional()}).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 }; /** * State history * * GET /v1/protocol/state */ export const getApiV1ProtocolState = async (args: GetApiV1ProtocolStateArgs, options?: GetApiV1ProtocolStateOptions) => { const {baseUrl = "https://api.hylo.so", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.days != null) query.append("days", String(args.days)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/protocol/state${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(protocolState, await res.json()) };