import {z} from 'zod' export type Address = unknown; export type IdentifierWrapper = string; export type U64 = unknown; export type GetApiAccountsAddressModuleModuleNameArgs = {address: Address, module_name: IdentifierWrapper, ledger_version?: U64 | undefined}; export type GetApiAccountsAddressModuleModuleNameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const hexEncodedBytes = z.string(); export const address = z.string(); export const identifierWrapper = z.string(); export const moveModuleId = z.string(); export const moveFunctionVisibility = z.enum(["private", "public", "friend"]); export const moveAbility = z.string(); export const moveFunctionGenericTypeParam = z.object({constraints: z.array(moveAbility)}); export const moveType = z.string(); export const moveFunction = z.object({name: identifierWrapper, visibility: moveFunctionVisibility, is_entry: z.boolean(), is_view: z.boolean(), generic_type_params: z.array(moveFunctionGenericTypeParam), params: z.array(moveType), return: z.array(moveType)}); export const moveStructGenericTypeParam = z.object({constraints: z.array(moveAbility)}); export const moveStructField = z.object({name: identifierWrapper, type: moveType}); export const moveStruct = z.object({name: identifierWrapper, is_native: z.boolean(), is_event: z.boolean(), abilities: z.array(moveAbility), generic_type_params: z.array(moveStructGenericTypeParam), fields: z.array(moveStructField)}); export const moveModule = z.object({address: address, name: identifierWrapper, friends: z.array(moveModuleId), exposed_functions: z.array(moveFunction), structs: z.array(moveStruct)}); export const moveModuleBytecode = z.object({bytecode: hexEncodedBytes, abi: moveModule.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 }; /** * Get account module * * GET /accounts/{address}/module/{module_name} */ export const getApiAccountsAddressModuleModuleName = async (args: GetApiAccountsAddressModuleModuleNameArgs, options: GetApiAccountsAddressModuleModuleNameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ledger_version != null) query.append("ledger_version", String(args.ledger_version)) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.address))}/module/${encodeURIComponent(String(args.module_name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(moveModuleBytecode, await res.json()) };