import {z} from 'zod' export type Address = unknown; export type U64 = unknown; export type StateKeyWrapper = string; export type GetApiAccountsAddressModulesArgs = {address: Address, ledger_version?: U64 | undefined, start?: StateKeyWrapper | undefined, limit?: number | undefined}; export type GetApiAccountsAddressModulesOptions = {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 getApiAccountsAddressModulesResponse = z.array(moveModuleBytecode); 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 modules * * GET /accounts/{address}/modules */ export const getApiAccountsAddressModules = async (args: GetApiAccountsAddressModulesArgs, options: GetApiAccountsAddressModulesOptions) => { 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)) if (args.start != null) query.append("start", String(args.start)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.address))}/modules${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(getApiAccountsAddressModulesResponse, await res.json()) };