import {z} from 'zod' export type GoogleProtobufAny = {'@type'?: string | undefined} | {[key: string]: unknown}; export type CosmosTxV1beta1TxBody = {messages?: Array | undefined, memo?: string | undefined, timeoutHeight?: unknown | undefined, unordered?: boolean | undefined, timeoutTimestamp?: string | undefined, extensionOptions?: Array | undefined, nonCriticalExtensionOptions?: Array | undefined}; export type CosmosTxSigningV1beta1SignMode = 'SIGN_MODE_UNSPECIFIED' | 'SIGN_MODE_DIRECT' | 'SIGN_MODE_TEXTUAL' | 'SIGN_MODE_DIRECT_AUX' | 'SIGN_MODE_LEGACY_AMINO_JSON' | 'SIGN_MODE_EIP_191'; export type CosmosTxV1beta1ModeInfoSingle = {mode?: CosmosTxSigningV1beta1SignMode | undefined}; export type CosmosCryptoMultisigV1beta1CompactBitArray = {extraBitsStored?: number | undefined, elems?: string | undefined}; export type CosmosTxV1beta1ModeInfoMulti = {bitarray?: CosmosCryptoMultisigV1beta1CompactBitArray | undefined, modeInfos?: Array | undefined}; export type CosmosTxV1beta1ModeInfo = {single?: CosmosTxV1beta1ModeInfoSingle | undefined, multi?: CosmosTxV1beta1ModeInfoMulti | undefined}; export type CosmosTxV1beta1SignerInfo = {publicKey?: GoogleProtobufAny | undefined, modeInfo?: CosmosTxV1beta1ModeInfo | undefined, sequence?: unknown | undefined}; export type CosmosBaseV1beta1Coin = {denom?: string | undefined, amount?: string | undefined}; export type CosmosTxV1beta1Fee = {amount?: Array | undefined, gasLimit?: unknown | undefined, payer?: string | undefined, granter?: string | undefined}; export type CosmosTxV1beta1Tip = {amount?: Array | undefined, tipper?: string | undefined}; export type CosmosTxV1beta1AuthInfo = {signerInfos?: Array | undefined, fee?: CosmosTxV1beta1Fee | undefined, tip?: CosmosTxV1beta1Tip | undefined}; export type CosmosTxV1beta1Tx = {body?: CosmosTxV1beta1TxBody | undefined, authInfo?: CosmosTxV1beta1AuthInfo | undefined, signatures?: Array | undefined}; export type CosmosTxV1beta1TxEncodeRequest = {tx?: CosmosTxV1beta1Tx | undefined}; export type CreateApiCosmosTxV1beta1EncodeArgs = {body: CosmosTxV1beta1TxEncodeRequest}; export type CreateApiCosmosTxV1beta1EncodeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cosmosTxV1beta1TxEncodeResponse = z.object({txBytes: z.string().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 }; /** * TxEncode encodes the transaction. * * POST /cosmos/tx/v1beta1/encode */ export const createApiCosmosTxV1beta1Encode = async (args: CreateApiCosmosTxV1beta1EncodeArgs, options: CreateApiCosmosTxV1beta1EncodeOptions) => { const {baseUrl, 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(/\/+$/, '')}/cosmos/tx/v1beta1/encode${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(cosmosTxV1beta1TxEncodeResponse, await res.json()) };