import {z} from 'zod' export type ApiParameterPeriod = '1d' | '7d' | '30d' | '90d' | '180d' | '1y' | 'total'; export type GetApiYieldGmPoolsArgs = {period?: ApiParameterPeriod | undefined, pools?: Array | undefined, includeComponents?: boolean | undefined}; export type GetApiYieldGmPoolsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const gmPoolYieldPnlComponents = z.object({unrealizedPnlShortUsd: z.number(), unrealizedPnlLongUsd: z.number(), realizedPnlShortUsd: z.number(), realizedPnlLongUsd: z.number()}); export const apiParameterPeriod = z.enum(["1d", "7d", "30d", "90d", "180d", "1y", "total"]); export const gmPoolYieldPnlWindow = z.object({endTimestamp: z.number(), startTimestamp: z.number(), period: apiParameterPeriod}); export const gmPoolYieldPnlEntry = z.object({components: gmPoolYieldPnlComponents.optional(), window: gmPoolYieldPnlWindow, timeWeightedPoolValue: z.number().nullable(), tradersPnlApr: z.number().nullable(), tradersPnlUsd: z.number().nullable(), feeApy: z.number().nullable(), marketToken: z.string()}); export const gmPoolsYieldPnlResponse = z.object({pools: z.array(gmPoolYieldPnlEntry)}); 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 }; /** * Fee-only APY plus trader PnL for GM pools, one row per GM market token. * * GET /yield/gm-pools */ export const getApiYieldGmPools = async (args: GetApiYieldGmPoolsArgs, options?: GetApiYieldGmPoolsOptions) => { const {baseUrl = "https://arbitrum.gmxapi.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) for (const item of args.pools ?? []) query.append("pools", String(item)) if (args.includeComponents != null) query.append("includeComponents", String(args.includeComponents)) const url = `${baseUrl.replace(/\/+$/, '')}/yield/gm-pools${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(gmPoolsYieldPnlResponse, await res.json()) };