import {z} from 'zod' export type GetApiTimeseriesDefiBalanceSheetsArgs = {defi_protocols: Array, start_time?: string | undefined, end_time?: string | undefined, start_height?: number | undefined, end_height?: number | undefined, start_inclusive?: boolean | undefined, end_inclusive?: boolean | undefined, timezone?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, pretty?: boolean | undefined, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiTimeseriesDefiBalanceSheetsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const defiProtocol = z.string(); export const defiBalanceSheetBlockHeight = z.string(); export const defiBalanceSheetTime = z.string(); export const defiBalanceSheetAssetsTotalUsd = z.string(); export const defiBalanceSheetAssetsTotalCount = z.string(); export const defiBalanceSheetLiabilitiesTotalUsd = z.string(); export const defiBalanceSheetLiabilitiesTotalCount = z.string(); export const defiBalanceSheetLoansLentTotalUsd = z.string(); export const defiBalanceSheetTvlTotalUsd = z.string(); export const defiBalanceSheetNetWorkingCapitalUsd = z.string(); export const defiBalanceSheetProtocolUtilizationRatio = z.string(); export const defiBalanceSheetLiquidSupplyRatio = z.string(); export const defiBalanceSheetCurrentRatio = z.string(); export const defiBalanceSheetDebtToAssetRatio = z.string(); export const asset = z.string(); export const defiBalanceSheetItemBalanceUnits = z.string(); export const defiBalanceSheetItemBalanceUsd = z.string(); export const defiBalanceSheetItemShare = z.string(); export const defiBalanceSheetAssetItem = z.object({asset: asset, total_units: defiBalanceSheetItemBalanceUnits, loans_lent_units: defiBalanceSheetItemBalanceUnits.optional(), tvl_units: defiBalanceSheetItemBalanceUnits.optional(), total_usd: defiBalanceSheetItemBalanceUsd.optional(), loans_lent_usd: defiBalanceSheetItemBalanceUsd.optional(), tvl_usd: defiBalanceSheetItemBalanceUsd.optional(), total_share: defiBalanceSheetItemShare.optional(), loans_lent_share: defiBalanceSheetItemShare.optional(), tvl_share: defiBalanceSheetItemShare.optional()}); export const defiBalanceSheetAssets = z.array(defiBalanceSheetAssetItem); export const defiBalanceSheetLiabilityItem = z.object({asset: asset, total_units: defiBalanceSheetItemBalanceUnits, total_usd: defiBalanceSheetItemBalanceUsd.optional(), total_share: defiBalanceSheetItemShare.optional()}); export const defiBalanceSheetLiabilities = z.array(defiBalanceSheetLiabilityItem); export const defiBalanceSheet = z.object({defi_protocol: defiProtocol, block_height: defiBalanceSheetBlockHeight, time: defiBalanceSheetTime, assets_total_usd: defiBalanceSheetAssetsTotalUsd, assets_total_count: defiBalanceSheetAssetsTotalCount, liabilities_total_usd: defiBalanceSheetLiabilitiesTotalUsd, liabilities_total_count: defiBalanceSheetLiabilitiesTotalCount, loans_lent_total_usd: defiBalanceSheetLoansLentTotalUsd, tvl_total_usd: defiBalanceSheetTvlTotalUsd, net_working_capital_usd: defiBalanceSheetNetWorkingCapitalUsd, protocol_utilization_ratio: defiBalanceSheetProtocolUtilizationRatio.optional(), liquid_supply_ratio: defiBalanceSheetLiquidSupplyRatio.optional(), current_ratio: defiBalanceSheetCurrentRatio.optional(), debt_to_assets_ratio: defiBalanceSheetDebtToAssetRatio.optional(), assets: defiBalanceSheetAssets, liabilities: defiBalanceSheetLiabilities}); export const defiBalanceSheets = z.array(defiBalanceSheet); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const defiBalanceSheetsResponse = z.object({data: defiBalanceSheets, next_page_token: nextPageToken.optional(), next_page_url: nextPageUrl.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 }; /** * DeFi balance sheets (experimental) * * GET /timeseries/defi-balance-sheets */ export const getApiTimeseriesDefiBalanceSheets = async (args: GetApiTimeseriesDefiBalanceSheetsArgs, options?: GetApiTimeseriesDefiBalanceSheetsOptions) => { const {baseUrl = "https://api.coinmetrics.io/v4", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.defi_protocols != null) query.append("defi_protocols", args.defi_protocols.join(',')) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.start_height != null) query.append("start_height", String(args.start_height)) if (args.end_height != null) query.append("end_height", String(args.end_height)) if (args.start_inclusive != null) query.append("start_inclusive", String(args.start_inclusive)) if (args.end_inclusive != null) query.append("end_inclusive", String(args.end_inclusive)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.paging_from != null) query.append("paging_from", String(args.paging_from)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.format != null) query.append("format", String(args.format)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/timeseries/defi-balance-sheets${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(defiBalanceSheetsResponse, await res.json()) };