import {z} from 'zod' export type TokenScanSupportedChains = '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 type TokenAddressFilter = {filter_type?: 'token_address' | undefined, token_addresses: Array}; export type TokenTypeFilter = {filter_type?: 'token_type' | undefined, token_type: 'Fungible' | 'NonFungible'}; export type HookRequest = {url: string, shared_secret_key?: (string | null) | undefined, filter?: (TokenAddressFilter | TokenTypeFilter | null) | undefined}; export type CreateApiV0TokenHooksChainArgs = {chain: TokenScanSupportedChains, body: HookRequest}; export type CreateApiV0TokenHooksChainOptions = {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 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 }; /** * Create a new webhook * * POST /v0/token/hooks/{chain} * * Also accepts auth: ClientID */ export const createApiV0TokenHooksChain = async (args: CreateApiV0TokenHooksChainArgs, options?: CreateApiV0TokenHooksChainOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/token/hooks/${encodeURIComponent(String(args.chain))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(hook, await res.json()) };