import {z} from 'zod' export type GetApiApiV1BlockchainTokenAddressTokenTransfersArgs = {limit?: number | undefined, fromTs?: number | undefined, toTs?: number | undefined, blockchain: 'ethereum' | 'arbitrum' | 'avalanche' | 'polygon' | 'plasma' | 'sepolia' | 'tron' | 'ton' | 'bitcoin' | 'spark', token: 'usdt' | 'xaut' | 'usat' | 'btc', address: string}; export type GetApiApiV1BlockchainTokenAddressTokenTransfersOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1BlockchainTokenAddressTokenTransfersResponse = z.object({transfers: z.array(z.object({blockchain: z.string().optional(), blockNumber: z.number().int().optional(), transactionHash: z.string().optional(), transferIndex: z.number().int().optional(), token: z.string().optional(), amount: z.string().optional(), timestamp: z.number().int().optional(), transactionIndex: z.number().int().optional(), logIndex: z.number().int().optional(), from: z.string().optional(), to: z.string().optional(), label: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional()}).and(z.record(z.string(), z.unknown()))).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 }; /** * Get token transfers for an address * * GET /api/v1/{blockchain}/{token}/{address}/token-transfers */ export const getApiApiV1BlockchainTokenAddressTokenTransfers = async (args: GetApiApiV1BlockchainTokenAddressTokenTransfersArgs, options: GetApiApiV1BlockchainTokenAddressTokenTransfersOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.fromTs != null) query.append("fromTs", String(args.fromTs)) if (args.toTs != null) query.append("toTs", String(args.toTs)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.blockchain))}/${encodeURIComponent(String(args.token))}/${encodeURIComponent(String(args.address))}/token-transfers${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(getApiApiV1BlockchainTokenAddressTokenTransfersResponse, await res.json()) };