import {z} from 'zod' export type GetApiBlockchainV2AssetBalanceUpdatesArgs = {asset: string, accounts?: Array | undefined, limit_per_account?: number | undefined, sub_accounts?: Array | undefined, txids?: Array | undefined, block_hashes?: Array | undefined, start_time?: string | undefined, end_time?: string | undefined, start_height?: number | undefined, end_height?: number | undefined, start_chain_sequence_number?: number | undefined, end_chain_sequence_number?: number | undefined, include_sub_accounts?: boolean | 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 GetApiBlockchainV2AssetBalanceUpdatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const blockchainChainSequenceNumber = z.string(); export const blockchainAccount = z.string(); export const blockchainBlockHeight = z.string(); export const blockchainBalanceChange = z.string(); export const blockchainAccountBalance = z.string(); export const blockchainTransactionSequenceNumber = z.string(); export const blockchainNumberOfDebits = z.string(); export const blockchainNumberOfCredits = z.string(); export const blockchainSubAccount = z.string(); export const blockchainTotalReceived = z.string(); export const blockchainTotalSent = z.string(); export const blockchainBalanceUpdateSubAccount = z.object({sub_account: blockchainSubAccount.optional(), previous_balance: blockchainAccountBalance.optional(), new_balance: blockchainAccountBalance.optional(), n_debits: blockchainNumberOfDebits.optional(), n_credits: blockchainNumberOfCredits.optional(), previous_credit_height: blockchainBlockHeight.optional(), previous_debit_height: blockchainBlockHeight.optional(), previous_chain_sequence_number: blockchainTransactionSequenceNumber.optional(), total_received: blockchainTotalReceived.optional(), total_sent: blockchainTotalSent.optional(), creation_height: blockchainBlockHeight.optional()}); export const blockchainStaleBlock = z.string(); export const blockchainBlockHash = z.string(); export const time = z.string(); export const blockchainTransactionId = z.string(); export const blockchainBalanceUpdateCredit = z.boolean(); export const blockchainBalanceUpdateV2 = z.object({chain_sequence_number: blockchainChainSequenceNumber, account: blockchainAccount, account_creation_height: blockchainBlockHeight, change: blockchainBalanceChange, previous_balance: blockchainAccountBalance, new_balance: blockchainAccountBalance, transaction_sequence_number: blockchainTransactionSequenceNumber, n_debits: blockchainNumberOfDebits, n_credits: blockchainNumberOfCredits, previous_debit_height: blockchainBlockHeight.optional(), previous_credit_height: blockchainBlockHeight.optional(), previous_chain_sequence_number: blockchainTransactionSequenceNumber.optional(), sub_account: blockchainBalanceUpdateSubAccount.optional(), stale: blockchainStaleBlock.optional(), block_hash: blockchainBlockHash, height: blockchainBlockHeight, consensus_time: time, txid: blockchainTransactionId.optional(), credit: blockchainBalanceUpdateCredit, total_received: blockchainTotalReceived, total_sent: blockchainTotalSent}); export const blockchainBalanceUpdatesV2 = z.array(blockchainBalanceUpdateV2); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const warningObject = z.object({type: z.string(), message: z.string()}); export const blockchainBalanceUpdatesResponseV2 = z.object({data: blockchainBalanceUpdatesV2, 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 balance updates * * GET /blockchain-v2/{asset}/balance-updates */ export const getApiBlockchainV2AssetBalanceUpdates = async (args: GetApiBlockchainV2AssetBalanceUpdatesArgs, options?: GetApiBlockchainV2AssetBalanceUpdatesOptions) => { 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.accounts != null) query.append("accounts", args.accounts.join(',')) if (args.limit_per_account != null) query.append("limit_per_account", String(args.limit_per_account)) if (args.sub_accounts != null) query.append("sub_accounts", args.sub_accounts.join(',')) 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.start_chain_sequence_number != null) query.append("start_chain_sequence_number", String(args.start_chain_sequence_number)) if (args.end_chain_sequence_number != null) query.append("end_chain_sequence_number", String(args.end_chain_sequence_number)) if (args.include_sub_accounts != null) query.append("include_sub_accounts", String(args.include_sub_accounts)) 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))}/balance-updates${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(blockchainBalanceUpdatesResponseV2, await res.json()) };