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 TokenType = 'fungible' | 'non_fungible'; export type ExportFormat = 'jsonl_gzip' | 'parquet_snappy'; export type BulkExportJobRequest = {chain: TokenScanSupportedChains, token_type?: (TokenType | null) | undefined, format: ExportFormat, sync_start_time?: (number | null) | undefined}; export type CreateApiV0TokenBulkExportJobsArgs = {body: BulkExportJobRequest}; export type CreateApiV0TokenBulkExportJobsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jobStatus = z.enum(["queued", "running", "succeeded", "failed"]); export const bulkExportJobResponse = z.object({job_id: z.string(), status: jobStatus}); 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 Export Job * * POST /v0/token/bulk-export/jobs * * Also accepts auth: ClientID */ export const createApiV0TokenBulkExportJobs = async (args: CreateApiV0TokenBulkExportJobsArgs, options?: CreateApiV0TokenBulkExportJobsOptions) => { 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/bulk-export/jobs${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(bulkExportJobResponse, await res.json()) };