import {z} from 'zod' export type GetApiSmartContractsArgs = {q?: string | undefined, filter?: string | undefined}; export type GetApiSmartContractsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressTag = z.object({address_hash: z.string(), display_name: z.string(), label: z.string()}); export const watchlistName = z.object({display_name: z.string(), label: z.string()}); export const addressParam = z.object({hash: z.string(), implementation_name: z.string(), name: z.string(), ens_domain_name: z.string().optional(), metadata: z.object({}).optional(), is_contract: z.boolean(), private_tags: z.array(addressTag), watchlist_names: z.array(watchlistName), public_tags: z.array(addressTag), is_verified: z.boolean()}); export const smartContractForList = z.object({address_hash: addressParam.optional(), coin_balance: z.string(), compiler_version: z.string(), language: z.string(), has_constructor_args: z.boolean(), optimization_enabled: z.boolean(), transactions_count: z.number().int().optional(), verified_at: z.string(), market_cap: z.number().optional()}); export const getApiSmartContractsResponse = z.object({items: z.array(smartContractForList), next_page_params: z.object({})}); 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 verified smart contracts * * GET /smart-contracts */ export const getApiSmartContracts = async (args: GetApiSmartContractsArgs, options?: GetApiSmartContractsOptions) => { const {baseUrl = "https://eth.blockscout.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.filter != null) query.append("filter", String(args.filter)) const url = `${baseUrl.replace(/\/+$/, '')}/smart-contracts${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(getApiSmartContractsResponse, await res.json()) };