import {z} from 'zod' export type DateTimeSeconds = string; export type EmptyString = string; export type NamePrivileged = string; export type NameBasic = string; export type NameBid = string; export type NameCatchAll = string; export type Name = NamePrivileged | NameBasic | NameBid | NameCatchAll; export type Sha256 = string; export type ProducerAuthorityList = Array<{threshold?: number | undefined, keys?: (Array<{key?: string | undefined, weight?: number | undefined}>) | undefined}>; export type ProducerSchedule = {version?: number | undefined, producers?: (Array<{producer_name?: string | undefined, authority?: ProducerAuthorityList | undefined}>) | undefined}; export type Extension = Array; export type Signature = string; export type DateTime = string; export type WholeNumber = string | number; export type Authority = {actor: Name, permission: Name}; export type Action = {account: Name, name: Name, authorization: Array, data: {[key: string]: unknown}, hex_data: string}; export type Transaction = {expiration: DateTime, ref_block_num: number, ref_block_prefix: number, max_net_usage_words: WholeNumber, max_cpu_usage_ms: WholeNumber, delay_sec: number, context_free_actions: Array, actions: Array, transaction_extensions?: Array | undefined}; export type PackedTransaction = {id: Sha256, signatures: Array, compression: string, packed_context_free_data: string, context_free_data: Array, packed_trx: string, transaction: Transaction}; export type TransactionVariant = Sha256 | PackedTransaction; export type TransactionReceipt = {status: 'executed' | 'soft_fail' | 'hard_fail' | 'delayed' | 'expired', cpu_usage_us: number, net_usage_words: number, trx: TransactionVariant}; export type Block = {timestamp: DateTimeSeconds, producer: EmptyString | Name, confirmed: number, previous: Sha256, transaction_mroot: Sha256, action_mroot: Sha256, schedule_version: number, new_producers: ProducerSchedule, header_extensions?: Extension | undefined, new_protocol_features?: Array> | undefined, producer_signature: Signature, transactions?: Array | undefined, block_extensions?: Extension | undefined, id?: Sha256 | undefined, block_num?: number | undefined, ref_block_prefix?: number | undefined}; export type CreateApiPushBlockArgs = {body: Block}; export type CreateApiPushBlockOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPushBlockResponse = 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 block to the blockchain. * * POST /push_block */ export const createApiPushBlock = async (args: CreateApiPushBlockArgs, options?: CreateApiPushBlockOptions) => { 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(/\/+$/, '')}/push_block${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(createApiPushBlockResponse, await res.json()) };