import {z} from 'zod' export type CreateApiSendReadOnlyTransactionArgs = {body: {transaction?: ({compression?: boolean | undefined, packed_context_free_data?: string | undefined, packed_trx?: string | undefined}) | undefined}}; export type CreateApiSendReadOnlyTransactionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSendReadOnlyTransactionResponse = z.unknown(); 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 }; /** * Sends a read-only transaction in JSON format to the blockchain. This transaction is not intended for inclusion in the blockchain. When a user sends a transaction, which modifies the blockchain state, the connected node will fail the transaction. * * POST /send_read_only_transaction */ export const createApiSendReadOnlyTransaction = async (args: CreateApiSendReadOnlyTransactionArgs, options?: CreateApiSendReadOnlyTransactionOptions) => { const {baseUrl = "https://wax.greymass.com/v1/chain", 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(/\/+$/, '')}/send_read_only_transaction${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(createApiSendReadOnlyTransactionResponse, await res.json()) };