import {z} from 'zod' export type ApprovalsSupportedChains = 'ethereum' | 'polygon' | 'bsc' | 'avalanche' | 'arbitrum' | 'base' | 'linea' | 'kaia' | 'optimism'; export type RoutersEvmModelsAccount = {account_id: string, created?: string | undefined, age_in_years?: number | undefined, user_country_code: string}; export type RoutersEvmModelsConnection = {user_agent: string, ip_address: unknown}; export type NewMetadata = {account: RoutersEvmModelsAccount, connection: RoutersEvmModelsConnection}; export type ApprovalsRequest = {address: string, chain: ApprovalsSupportedChains | string, metadata: NewMetadata}; export type CreateApiV0EvmAddressApprovalsArgs = {body: ApprovalsRequest}; export type CreateApiV0EvmAddressApprovalsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const allowance = z.object({usd_price: z.string().optional(), value: z.string().optional()}); export const asset = z.object({address: z.string(), decimals: z.number().int(), logo_url: z.string().optional(), name: z.string(), symbol: z.string(), type: z.string()}); export const exposure = z.object({raw_value: z.string(), usd_price: z.string().optional(), value: z.string()}); export const featureID = z.enum(["VERIFIED_CONTRACT", "UNVERIFIED_CONTRACT", "HIGH_TRADE_VOLUME", "MARKET_PLACE_SALES_HISTORY", "HIGH_REPUTATION_TOKEN", "ONCHAIN_ACTIVITY_VALIDATOR", "STATIC_CODE_SIGNATURE", "KNOWN_MALICIOUS", "IS_EOA", "IS_CONTRACT", "ERC20_CONTRACT", "TRUSTED_CONTRACT", "BENIGN_CREATOR", "METADATA", "AIRDROP_PATTERN", "IMPERSONATOR", "INORGANIC_VOLUME", "DYNAMIC_ANALYSIS", "CONCENTRATED_SUPPLY_DISTRIBUTION", "HONEYPOT", "INSUFFICIENT_LOCKED_LIQUIDITY", "UNSTABLE_TOKEN_PRICE", "RUGPULL", "KNOWN_RUGPULL_CODE_PATTERN", "WASH_TRADING", "CONSUMER_OVERRIDE", "INAPPROPRIATE_CONTENT", "HIGH_TRANSFER_FEE", "HIGH_BUY_FEE", "HIGH_SELL_FEE", "UNSELLABLE_TOKEN", "IS_MINTABLE", "MODIFIABLE_TAXES", "CAN_BLACKLIST", "CAN_WHITELIST", "HAS_TRADING_COOLDOWN", "EXTERNAL_FUNCTIONS", "HIDDEN_OWNER", "TRANSFER_PAUSEABLE", "OWNERSHIP_RENOUNCED", "OWNER_CAN_CHANGE_BALANCE", "PROXY_CONTRACT", "SIMILAR_MALICIOUS_CONTRACT", "IMPERSONATOR_SENSITIVE_ASSET", "IMPERSONATOR_HIGH_CONFIDENCE", "IMPERSONATOR_MEDIUM_CONFIDENCE", "IMPERSONATOR_LOW_CONFIDENCE", "IMPERSONATION_PROTECTED", "FAKE_VOLUME", "HIDDEN_SUPPLY_BY_KEY_HOLDER", "FAKE_TRADE_MAKER_COUNT", "TRANSFER_FROM_REVERTS", "OFFENSIVE_TOKEN_METADATA", "LISTED_ON_CENTRALIZED_EXCHANGE", "SANCTIONED_CREATOR", "SPAM_TEXT", "BONDING_CURVE_TOKEN", "HEAVILY_SNIPED", "SOLANA_TOKEN_2022", "POST_DUMP", "DEX_PAID", "LOW_REPUTATION_CREATOR", "SNIPE_AT_MINT", "TRANSFER_HOOK_ENABLED", "CONFIDENTIAL_TRANSFERS_ENABLED", "NON_TRANSERABLE", "TOKEN_BACKDOOR", "CREATED_VIA_LAUNCHPAD", "COMPROMISED_TOKEN", "LONG_FUND_TRAIL", "METADATA_PROMPT_INJECTION"]); export const featureType = z.enum(["Benign", "Info", "Warning", "Malicious"]); export const feature = z.object({feature_id: featureID, type: featureType, description: z.string()}); export const spender = z.object({address: z.string(), features: z.array(feature).optional(), label: z.string()}); export const routersEvmModelsValidationResultType = z.enum(["Malicious", "Warning", "Benign", "Error"]); export const approval = z.object({allowance: allowance, asset: asset, exposure: exposure, spender: spender, verdict: routersEvmModelsValidationResultType}); export const approvalsResponse = z.object({approvals: z.array(approval).optional()}); 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 approvals with enrichments * * POST /v0/evm/address/approvals * * Also accepts auth: ClientID */ export const createApiV0EvmAddressApprovals = async (args: CreateApiV0EvmAddressApprovalsArgs, options?: CreateApiV0EvmAddressApprovalsOptions) => { 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/evm/address/approvals${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(approvalsResponse, await res.json()) };