import {z} from 'zod' export type NamePrivileged = string; export type NameBasic = string; export type NameBid = string; export type NameCatchAll = string; export type Name = NamePrivileged | NameBasic | NameBid | NameCatchAll; export type CreateApiGetCodeArgs = {body: {account_name: Name, code_as_wasm: number}}; export type CreateApiGetCodeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const namePrivileged = z.string(); export const nameBasic = z.string(); export const nameBid = z.string(); export const nameCatchAll = z.string(); export const name = z.union([namePrivileged, nameBasic, nameBid, nameCatchAll]); export const sha256 = z.string(); export const cppSignature = z.string(); export const type = z.enum(["uint8", "uint16", "uint32", "uint64", "uint128", "float32", "float64", "float128", "bytes", "name", "string", "time_point", "time_point_sec", "symbol_code", "symbol", "asset", "checksum160", "checksum256", "checksum521", "public_key", "private_key", "signature", "account_name", "action_name"]); export const abiType = z.object({new_type_name: cppSignature, type: type}); export const abiStructFields = z.object({name: cppSignature, type: type}); export const abiStruct = z.object({name: cppSignature, base: z.string(), fields: z.array(abiStructFields)}); export const abiAction = z.object({name: name, type: name, ricardian_contract: z.string()}); export const indexType = z.literal("i64"); export const keyType = z.enum(["uint64", "double"]); export const abiTable = z.object({name: name, index_type: indexType, key_names: z.array(cppSignature).optional(), key_types: z.array(keyType).optional(), type: cppSignature.optional()}); export const extension = z.array(z.union([z.number().int(), z.string()])); export const abi = z.object({version: z.string(), types: z.array(abiType), structs: z.array(abiStruct), actions: z.array(abiAction), tables: z.array(abiTable), abi_extensions: z.array(extension), error_messages: z.array(z.string()), ricardian_clauses: z.array(z.string()), variants: z.array(z.string())}); export const createApiGetCodeResponse = z.object({name: name.optional(), code_hash: sha256.optional(), wast: z.string().optional(), wasm: z.string().optional(), abi: abi.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 }; /** * Returns an object containing the smart contract WASM code. * * POST /get_code */ export const createApiGetCode = async (args: CreateApiGetCodeArgs, options?: CreateApiGetCodeOptions) => { const {baseUrl = "https://wax.greymass.com/v1/chain", 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(/\/+$/, '')}/get_code${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(createApiGetCodeResponse, await res.json()) };