import {z} from 'zod' export type CreateApiV1PaymentsBatchPayArgs = {body: {wallet: string, rise_id: string | number, total_amount: string | number, payments: Array<{recipient: string | number, amount: string | number, salt: string | number, title?: string | undefined, description?: string | undefined, external_id?: string | undefined}>, request: {[key: string]: unknown}, signature: string}}; export type CreateApiV1PaymentsBatchPayOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1PaymentsBatchPayResponse = z.object({data: z.object({to: z.string().nullable(), from: z.string().nullable(), contractAddress: z.string().nullable(), transactionIndex: z.number(), gasUsed: z.object({type: z.string(), hex: z.string()}), logsBloom: z.string(), blockHash: z.string(), transactionHash: z.string(), logs: z.array(z.object({transactionIndex: z.number(), blockNumber: z.number(), transactionHash: z.string(), address: z.string(), topics: z.array(z.string()), data: z.string(), logIndex: z.number(), blockHash: z.string()})), blockNumber: z.number(), confirmations: z.number(), cumulativeGasUsed: z.object({type: z.string(), hex: z.string()}), effectiveGasPrice: z.object({type: z.string(), hex: z.string()}), status: z.number().nullable(), type: z.number(), byzantium: z.literal(true)})}); 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 }; /** * Execute batch payment (V1 Legacy) * * POST /v1/payments/batch-pay */ export const createApiV1PaymentsBatchPay = async (args: CreateApiV1PaymentsBatchPayArgs, options?: CreateApiV1PaymentsBatchPayOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/payments/batch-pay${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1PaymentsBatchPayResponse, await res.json()) };