import {z} from 'zod' export type Address = unknown; export type U64 = unknown; export type EntryFunctionId = string; export type MoveType = string; export type HexEncodedBytes = unknown; export type IdentifierWrapper = string; export type MoveFunctionVisibility = 'private' | 'public' | 'friend'; export type MoveAbility = string; export type MoveFunctionGenericTypeParam = {constraints: Array}; export type MoveFunction = {name: IdentifierWrapper, visibility: MoveFunctionVisibility, is_entry: boolean, is_view: boolean, generic_type_params: Array, params: Array, return: Array}; export type MoveScriptBytecode = {bytecode: HexEncodedBytes, abi?: MoveFunction | undefined}; export type MultisigTransactionPayload = {type: 'entry_function_payload', function: EntryFunctionId, type_arguments: Array, arguments: Array}; export type TransactionPayload = {type: 'entry_function_payload', function: EntryFunctionId, type_arguments: Array, arguments: Array} | {type: 'script_payload', code: MoveScriptBytecode, type_arguments: Array, arguments: Array} | {type: 'module_bundle_payload'} | {type: 'multisig_payload', multisig_address: Address, transaction_payload?: MultisigTransactionPayload | undefined}; export type EncodeSubmissionRequest = {sender: Address, sequence_number: U64, max_gas_amount: U64, gas_unit_price: U64, expiration_timestamp_secs: U64, payload: TransactionPayload, replay_protection_nonce?: U64 | undefined, secondary_signers?: Array
| undefined}; export type CreateApiTransactionsEncodeSubmissionArgs = {body: EncodeSubmissionRequest}; export type CreateApiTransactionsEncodeSubmissionOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const hexEncodedBytes = 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 }; /** * Encode submission * * POST /transactions/encode_submission */ export const createApiTransactionsEncodeSubmission = async (args: CreateApiTransactionsEncodeSubmissionArgs, options: CreateApiTransactionsEncodeSubmissionOptions) => { 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(/\/+$/, '')}/transactions/encode_submission${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(hexEncodedBytes, await res.json()) };