import {z} from 'zod' export type GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanArgs = {cryptoCode: string, derivationScheme: string}; export type GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanResponse = z.object({error: z.string().nullable().optional(), queuedAt: z.number().int().optional(), status: z.enum(["Queued", "Pending", "Complete", "Error"]).optional(), progress: z.object({startedAt: z.number().int().optional(), completedAt: z.number().int().nullable().optional(), found: z.number().int().optional(), batchNumber: z.number().int().optional(), remainingBatches: z.number().int().optional(), currentBatchProgress: z.number().int().optional(), remainingSeconds: z.number().int().optional(), overallProgress: z.number().int().optional(), from: z.number().int().optional(), count: z.number().int().optional(), totalSearched: z.number().int().optional(), totalSizeOfUTXOSet: z.number().int().nullable().optional(), highestKeyIndexFound: z.object({change: z.number().int().nullable().optional(), deposit: z.number().int().nullable().optional(), direct: z.number().int().nullable().optional()}).optional()}).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 UTXO scan status * * GET /v1/cryptos/{cryptoCode}/derivations/{derivationScheme}/utxos/scan */ export const getApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScan = async (args: GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanArgs, options: GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cryptos/${encodeURIComponent(String(args.cryptoCode))}/derivations/${encodeURIComponent(String(args.derivationScheme))}/utxos/scan${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(getApiV1CryptosCryptoCodeDerivationsDerivationSchemeUtxosScanResponse, await res.json()) };