import {z} from 'zod' export type GetApiV1ChainNameEventsArgs = {chainName: string, 'starting-block'?: number | undefined, 'ending-block'?: string | undefined, address?: string | undefined, topics?: string | undefined, 'block-hash'?: string | undefined, 'skip-decode'?: boolean | undefined}; export type GetApiV1ChainNameEventsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameEventsResponse = z.object({updated_at: z.string().optional(), chain_id: z.number().int().optional(), chain_name: z.string().optional(), chain_tip_height: z.number().int().optional(), chain_tip_signed_at: z.string().optional(), items: z.array(z.object({block_signed_at: z.string().optional(), block_height: z.number().int().optional(), block_hash: z.string().optional(), tx_offset: z.number().int().optional(), log_offset: z.number().int().optional(), tx_hash: z.string().optional(), raw_log_topics: z.array(z.string()).optional(), sender_contract_decimals: z.number().int().optional(), sender_name: z.string().optional(), sender_contract_ticker_symbol: z.string().optional(), sender_address: z.string().optional(), supports_erc: z.array(z.string()).optional(), sender_logo_url: z.string().optional(), sender_factory_address: z.string().optional(), raw_log_data: z.string().optional(), decoded: z.object({name: z.string().optional(), signature: z.string().optional(), params: z.array(z.object({name: z.string().optional(), type: z.string().optional(), indexed: z.boolean().optional(), decoded: z.boolean().optional(), value: z.string().optional()})).optional()}).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 }; /** * Returns all the event logs of the latest block, or for a range of blocks. Includes sender contract metadata as well as decoded logs. * * GET /v1/{chainName}/events/ */ export const getApiV1ChainNameEvents = async (args: GetApiV1ChainNameEventsArgs, options?: GetApiV1ChainNameEventsOptions) => { 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['starting-block'] != null) query.append("starting-block", String(args['starting-block'])) if (args['ending-block'] != null) query.append("ending-block", String(args['ending-block'])) if (args.address != null) query.append("address", String(args.address)) if (args.topics != null) query.append("topics", String(args.topics)) if (args['block-hash'] != null) query.append("block-hash", String(args['block-hash'])) if (args['skip-decode'] != null) query.append("skip-decode", String(args['skip-decode'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/events/${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(getApiV1ChainNameEventsResponse, await res.json()) };