import {z} from 'zod' export type GetApiBlockchainAssetTransactionTrackerArgs = {asset: string, txids?: Array | undefined, replacements_for_txids?: Array | undefined, replacements_only?: boolean | undefined, addresses?: Array | undefined, unconfirmed_only?: boolean | undefined, start_time?: string | undefined, end_time?: 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, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiBlockchainAssetTransactionTrackerOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const txTrackerTxid = z.string(); export const time = z.string(); export const txTrackerTxStatus = z.string(); export const txTrackerMempoolApproximateQueuePosition = z.string(); export const txTrackerNextBlockApproxSettlementProbabilityPercentage = z.string(); export const blockchainBlockHash = z.string(); export const blockchainBlockHeight = z.string(); export const txTrackerNConfirmations = z.string(); export const txTrackerRemovalReason = z.string(); export const txTrackerReplacementTxid = z.string(); export const txTrackerTxStatusUpdate = z.object({time: time, status: txTrackerTxStatus, block_hash: blockchainBlockHash.optional(), height: blockchainBlockHeight.optional(), removal_reason: txTrackerRemovalReason.optional(), replacement_txid: txTrackerReplacementTxid.optional()}); export const txTrackerTxStatusUpdates = z.array(txTrackerTxStatusUpdate); export const blockchainTransactionVersion = z.string(); export const txTrackerTxAmount = z.string(); export const txTrackerReplaceByFeeSupported = z.boolean(); export const blockchainTransactionFee = z.string(); export const blockchainTransactionFeerate = z.string(); export const blockchainMempoolFeerate = z.string(); export const blockchainTransactionConsensusSize = z.string(); export const blockchainTransactionPhysicalSize = z.string(); export const txTrackerTxDetails = z.object({version: blockchainTransactionVersion.optional(), amount: txTrackerTxAmount, replace_by_fee_supported: txTrackerReplaceByFeeSupported.optional(), fee: blockchainTransactionFee.optional(), feerate: blockchainTransactionFeerate.optional(), mempool_feerate_mean_at_first_seen_time: blockchainMempoolFeerate.optional(), mempool_feerate_min_at_first_seen_time: blockchainMempoolFeerate.optional(), consensus_size: blockchainTransactionConsensusSize.optional(), physical_size: blockchainTransactionPhysicalSize.optional()}); export const nodeLocation = z.string(); export const blockchainNodeLocationSeenTime = z.object({location: nodeLocation, seen_time: time}); export const blockchainNodeLocationSeenTimes = z.array(blockchainNodeLocationSeenTime); export const txTrackerTxAddress = z.string(); export const txTrackerTxOutput = z.object({address: txTrackerTxAddress}); export const txTrackerTxOutputs = z.array(txTrackerTxOutput); export const txTrackerTransaction = z.object({txid: txTrackerTxid, time: time, first_seen_time: time, status: txTrackerTxStatus, status_update_time: time, mempool_approx_queue_position: txTrackerMempoolApproximateQueuePosition.optional(), next_block_approx_settlement_probability_pct: txTrackerNextBlockApproxSettlementProbabilityPercentage.optional(), block_hash: blockchainBlockHash.optional(), height: blockchainBlockHeight.optional(), n_confirmations: txTrackerNConfirmations.optional(), status_updates: txTrackerTxStatusUpdates, details: txTrackerTxDetails, geo: blockchainNodeLocationSeenTimes.optional(), replacement_for_txid: txTrackerReplacementTxid.optional(), inputs: txTrackerTxOutputs.optional(), outputs: txTrackerTxOutputs.optional()}); export const txTrackerTransactions = z.array(txTrackerTransaction); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const txTrackerResponse = z.object({data: txTrackerTransactions, next_page_token: nextPageToken.optional(), next_page_url: nextPageUrl.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 }; /** * Transaction tracker * * GET /blockchain/{asset}/transaction-tracker */ export const getApiBlockchainAssetTransactionTracker = async (args: GetApiBlockchainAssetTransactionTrackerArgs, options?: GetApiBlockchainAssetTransactionTrackerOptions) => { 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.replacements_for_txids != null) query.append("replacements_for_txids", args.replacements_for_txids.join(',')) if (args.replacements_only != null) query.append("replacements_only", String(args.replacements_only)) if (args.addresses != null) query.append("addresses", args.addresses.join(',')) if (args.unconfirmed_only != null) query.append("unconfirmed_only", String(args.unconfirmed_only)) 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_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.format != null) query.append("format", String(args.format)) 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/${encodeURIComponent(String(args.asset))}/transaction-tracker${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(txTrackerResponse, await res.json()) };