import {z} from 'zod' export type GetApiV1ProtocolDigestArgs = {days?: number | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiV1ProtocolDigestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const date = z.string(); export const decimal = z.string(); export const pair = z.object({amount: decimal, usd: decimal}); export const market = z.string(); export const tokenPair = z.object({token: z.string(), amount: decimal, usd: decimal}); export const daily = z.object({since: date, until: date, daily: z.array(z.object({date: date, eventCount: z.number().int(), hyusd: z.object({minted: pair, redeemed: pair}), ehyusd: z.object({minted: pair, redeemed: pair}), markets: z.array(z.object({market: market, levercoin: z.string(), levercoinMinted: pair, levercoinRedeemed: pair, yieldToPool: tokenPair.optional(), pnlSettled: tokenPair.optional(), collaterals: z.array(z.object({token: z.string(), mint: z.string(), vaultInflow: pair, vaultOutflow: pair, rebalanceSwapInUsd: z.string(), rebalanceSwapOutUsd: z.string(), lstSwapInUsd: z.string().nullable(), lstSwapOutUsd: z.string().nullable()}))})), usdcVault: z.object({inflow: pair, outflow: pair}).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 }; /** * Daily digest * * GET /v1/protocol/digest */ export const getApiV1ProtocolDigest = async (args: GetApiV1ProtocolDigestArgs, options?: GetApiV1ProtocolDigestOptions) => { 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.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/digest${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(daily, await res.json()) };