import {z} from 'zod' export type GetApiAddressesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenInfo = z.object({circulating_market_cap: z.string(), icon_url: z.string(), name: z.string(), decimals: z.string(), symbol: z.string(), address_hash: z.string().optional(), type: z.string(), holders_count: z.string().optional(), exchange_rate: z.string(), total_supply: z.string()}); 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 addressWithTxCount = z.object({creator_address_hash: z.string().optional(), creation_transaction_hash: z.string().optional(), token: tokenInfo.optional(), coin_balance: z.string().optional(), exchange_rate: z.string().optional(), implementation_address: z.string().optional(), block_number_balance_updated_at: z.number().int().optional(), hash: z.string(), implementation_name: z.string().optional(), name: z.string().optional(), is_contract: z.boolean().optional(), private_tags: z.array(addressTag).optional(), watchlist_names: z.array(watchlistName).optional(), public_tags: z.array(addressTag).optional(), is_verified: z.boolean().optional(), has_beacon_chain_withdrawals: z.boolean().optional(), has_logs: z.boolean().optional(), has_token_transfers: z.boolean().optional(), has_tokens: z.boolean().optional(), has_validated_blocks: z.boolean().optional(), creation_status: z.string().optional(), transactions_count: z.string()}); export const getApiAddressesResponse = z.object({exchange_rate: z.string(), total_supply: z.string(), items: z.array(addressWithTxCount), 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 native coin holders list * * GET /addresses */ export const getApiAddresses = async (options?: GetApiAddressesOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/addresses${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(getApiAddressesResponse, await res.json()) };