import {z} from 'zod' export type GetApiBlockchainV2AssetTransactionsArgs = {asset: string, txids?: Array | undefined, block_hashes?: Array | undefined, start_time?: string | undefined, end_time?: string | undefined, start_height?: number | undefined, end_height?: number | undefined, chain?: string | undefined, start_inclusive?: boolean | undefined, end_inclusive?: boolean | undefined, timezone?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, pretty?: boolean | undefined, next_page_token?: string | undefined}; export type GetApiBlockchainV2AssetTransactionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const blockchainTransactionId = z.string(); export const time = z.string(); export const blockchainTransactionPosition = z.string(); export const blockchainChainSequenceNumber = z.string(); export const blockchainNumberOfBalanceUpdates = z.string(); export const blockchainTransactionAmount = z.string(); export const blockchainStaleBlock = z.string(); export const blockchainBlockHash = z.string(); export const blockchainBlockHeight = z.string(); export const blockchainTransactionVersion = z.string(); export const blockchainTransactionPhysicalSize = z.string(); export const blockchainTransactionConsensusSize = z.string(); export const blockchainTransactionFee = z.string(); export const blockchainTransactionInfoV2 = z.object({txid: blockchainTransactionId, consensus_time: time, miner_time: time.optional(), tx_position: blockchainTransactionPosition, min_chain_sequence_number: blockchainChainSequenceNumber.optional(), max_chain_sequence_number: blockchainChainSequenceNumber.optional(), n_balance_updates: blockchainNumberOfBalanceUpdates, amount: blockchainTransactionAmount, stale: blockchainStaleBlock.optional(), block_hash: blockchainBlockHash, height: blockchainBlockHeight, version: blockchainTransactionVersion.optional(), physical_size: blockchainTransactionPhysicalSize.optional(), consensus_size: blockchainTransactionConsensusSize.optional(), fee: blockchainTransactionFee.optional()}); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const warningObject = z.object({type: z.string(), message: z.string()}); export const blockchainTransactionsResponseV2 = z.object({data: z.array(blockchainTransactionInfoV2), next_page_token: nextPageToken.optional(), next_page_url: nextPageUrl.optional(), warning: warningObject.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 of transactions * * GET /blockchain-v2/{asset}/transactions */ export const getApiBlockchainV2AssetTransactions = async (args: GetApiBlockchainV2AssetTransactionsArgs, options?: GetApiBlockchainV2AssetTransactionsOptions) => { const {baseUrl = "https://api.coinmetrics.io/v4", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.txids != null) query.append("txids", args.txids.join(',')) if (args.block_hashes != null) query.append("block_hashes", args.block_hashes.join(',')) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.start_height != null) query.append("start_height", String(args.start_height)) if (args.end_height != null) query.append("end_height", String(args.end_height)) if (args.chain != null) query.append("chain", String(args.chain)) if (args.start_inclusive != null) query.append("start_inclusive", String(args.start_inclusive)) if (args.end_inclusive != null) query.append("end_inclusive", String(args.end_inclusive)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.paging_from != null) query.append("paging_from", String(args.paging_from)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/blockchain-v2/${encodeURIComponent(String(args.asset))}/transactions${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(blockchainTransactionsResponseV2, await res.json()) };