import {z} from 'zod' export type GetApiV1PricingSpotPricesChainNamePoolsContractAddressArgs = {chainName: string, contractAddress: string, 'quote-currency'?: string | undefined}; export type GetApiV1PricingSpotPricesChainNamePoolsContractAddressOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1PricingSpotPricesChainNamePoolsContractAddressResponse = z.object({updated_at: z.string().optional(), pool_address: z.string().optional(), token_0_address: z.string().optional(), token_0_name: z.string().optional(), token_0_ticker: z.string().optional(), token_0_price: z.string().optional(), token_0_price_24h: z.string().optional(), token_0_price_quote: z.string().optional(), token_0_price_24h_quote: z.string().optional(), token_1_address: z.string().optional(), token_1_name: z.string().optional(), token_1_ticker: z.string().optional(), token_1_price: z.string().optional(), token_1_price_24h: z.string().optional(), token_1_price_quote: z.string().optional(), token_1_price_24h_quote: z.string().optional(), quote_currency: z.string().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 }; /** * Get the spot token pair prices for a specified pool contract address. Supports pools on Uniswap V2, V3 and their forks. * * GET /v1/pricing/spot_prices/{chainName}/pools/{contractAddress}/ */ export const getApiV1PricingSpotPricesChainNamePoolsContractAddress = async (args: GetApiV1PricingSpotPricesChainNamePoolsContractAddressArgs, options?: GetApiV1PricingSpotPricesChainNamePoolsContractAddressOptions) => { const {baseUrl = "https://api.covalenthq.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['quote-currency'] != null) query.append("quote-currency", String(args['quote-currency'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/pricing/spot_prices/${encodeURIComponent(String(args.chainName))}/pools/${encodeURIComponent(String(args.contractAddress))}/${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(getApiV1PricingSpotPricesChainNamePoolsContractAddressResponse, await res.json()) };