import {z} from 'zod' export type GetApiV0TokenHooksOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenAddressFilter = z.object({filter_type: z.literal("token_address").optional(), token_addresses: z.array(z.string())}); export const tokenTypeFilter = z.object({filter_type: z.literal("token_type").optional(), token_type: z.enum(["Fungible", "NonFungible"])}); export const tokenScanSupportedChains = z.enum(["arbitrum", "avalanche", "base", "bsc", "ethereum", "optimism", "polygon", "zora", "solana", "starknet", "starknet-sepolia", "stellar", "linea", "degen", "zksync", "scroll", "blast", "soneium-minato", "base-sepolia", "bitcoin", "abstract", "soneium", "ink", "berachain", "unichain", "ronin", "sui", "hedera", "hyperevm", "xlayer", "monad", "megaeth", "tempo", "sei", "kaia", "tron", "robinhood", "flare", "arc"]); export const hook = z.object({url: z.string().min(1), shared_secret_key: z.string().nullable().optional(), filter: z.union([tokenAddressFilter, tokenTypeFilter]).nullable().optional(), chain: tokenScanSupportedChains, active: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const getApiV0TokenHooksResponse = z.array(hook); 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 }; /** * List all webhooks * * GET /v0/token/hooks/ * * Also accepts auth: ClientID */ export const getApiV0TokenHooks = async (options?: GetApiV0TokenHooksOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/token/hooks/${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(getApiV0TokenHooksResponse, await res.json()) };