import {z} from 'zod' export type BlockIdVariant0 = number; export type CryptoHash = string; export type BlockId = BlockIdVariant0 | CryptoHash; export type RpcGasPriceRequest = {block_id?: (BlockId | unknown) | undefined}; export type JsonRpcRequestForGasPrice = {id: string, jsonrpc: string, method: 'gas_price', params: RpcGasPriceRequest}; export type CreateApiGasPriceArgs = {body: JsonRpcRequestForGasPrice}; export type CreateApiGasPriceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nearToken = z.string(); export const rpcGasPriceResponse = z.object({gas_price: nearToken}); export const rpcRequestValidationErrorKindVariant0 = z.object({info: z.object({method_name: z.string()}), name: z.literal("METHOD_NOT_FOUND")}); export const rpcRequestValidationErrorKindVariant1 = z.object({info: z.object({error_message: z.string()}), name: z.literal("PARSE_ERROR")}); export const rpcRequestValidationErrorKind = z.union([rpcRequestValidationErrorKindVariant0, rpcRequestValidationErrorKindVariant1]); export const errorWrapperForRpcGasPriceErrorVariant0 = z.object({cause: rpcRequestValidationErrorKind, name: z.literal("REQUEST_VALIDATION_ERROR")}); export const rpcGasPriceErrorVariant0 = z.object({info: z.object({error_message: z.string()}), name: z.literal("INTERNAL_ERROR")}); export const rpcGasPriceErrorVariant1 = z.object({info: z.object({}), name: z.literal("UNKNOWN_BLOCK")}); export const rpcGasPriceError = z.union([rpcGasPriceErrorVariant0, rpcGasPriceErrorVariant1]); export const errorWrapperForRpcGasPriceErrorVariant1 = z.object({cause: rpcGasPriceError, name: z.literal("HANDLER_ERROR")}); export const internalError = z.object({info: z.object({error_message: z.string()}), name: z.literal("INTERNAL_ERROR")}); export const errorWrapperForRpcGasPriceErrorVariant2 = z.object({cause: internalError, name: z.literal("INTERNAL_ERROR")}); export const errorWrapperForRpcGasPriceError = z.union([errorWrapperForRpcGasPriceErrorVariant0, errorWrapperForRpcGasPriceErrorVariant1, errorWrapperForRpcGasPriceErrorVariant2]); export const jsonRpcResponseForRpcGasPriceResponseAndRpcGasPriceError = z.union([z.object({result: rpcGasPriceResponse, id: z.string(), jsonrpc: z.string()}), z.object({error: errorWrapperForRpcGasPriceError, id: z.string(), jsonrpc: 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 }; /** * Gas Price * * POST /gas_price */ export const createApiGasPrice = async (args: CreateApiGasPriceArgs, options?: CreateApiGasPriceOptions) => { const {baseUrl = "https://rpc.mainnet.near.org", 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(/\/+$/, '')}/gas_price${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(jsonRpcResponseForRpcGasPriceResponseAndRpcGasPriceError, await res.json()) };