import {z} from 'zod' export type AssetId = string; export type BlockchainJobAssets = Array; export type BlockchainJobAccount = string; export type BlockchainJobAccounts = Array; export type BlockchainJobStartTime = string; export type BlockchainJobEndTime = string; export type BlockchainJobStartHeight = number; export type BlockchainJobEndHeight = number; export type BlockchainJobStartSlot = number; export type BlockchainJobEndSlot = number; export type BlockchainJobBalanceUpdatesRequest = {assets: BlockchainJobAssets, accounts: BlockchainJobAccounts, start_time?: BlockchainJobStartTime | undefined, end_time?: BlockchainJobEndTime | undefined, start_height?: BlockchainJobStartHeight | undefined, end_height?: BlockchainJobEndHeight | undefined, start_slot?: BlockchainJobStartSlot | undefined, end_slot?: BlockchainJobEndSlot | undefined}; export type UpdateApiBlockchainJobBalanceUpdatesArgs = {body: BlockchainJobBalanceUpdatesRequest}; export type UpdateApiBlockchainJobBalanceUpdatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createdJobDetailsResponse = z.object({job_id: z.string(), job_url: z.string()}); 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 }; /** * Balance updates * * PUT /blockchain-job/balance-updates */ export const updateApiBlockchainJobBalanceUpdates = async (args: UpdateApiBlockchainJobBalanceUpdatesArgs, options?: UpdateApiBlockchainJobBalanceUpdatesOptions) => { const {baseUrl = "https://api.coinmetrics.io/v4", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/blockchain-job/balance-updates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createdJobDetailsResponse, await res.json()) };