UNPKG

@veive-io/mod-hooks-as

Version:

Veive smart account hooks module

130 lines (119 loc) 5.44 kB
import { System, Protobuf, StringBytes } from "@koinos/sdk-as"; import { modhooks } from "./proto/modhooks"; export class ModHooks { _contractId: Uint8Array; /** * Create an instance of a ModHooks contract * @example * ```ts * const contract = new ModHooks(Base58.decode("1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe")); * ``` */ constructor(contractId: Uint8Array) { this._contractId = contractId; } /** * Performs a pre-check before the main operation is executed. * * This method is called to execute any logic that needs to run before the main operation. * It logs a message indicating that the pre-check has been called. * @external * @readonly */ pre_check(args: modhooks.pre_check_args): modhooks.pre_check_result { const argsBuffer = Protobuf.encode(args, modhooks.pre_check_args.encode); const callRes = System.call(this._contractId, 0x608cce22, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.pre_check': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } if (!callRes.res.object) return new modhooks.pre_check_result(); return Protobuf.decode<modhooks.pre_check_result>(callRes.res.object, modhooks.pre_check_result.decode); } /** * Performs a post-check after the main operation is executed. * * This method is called to execute any logic that needs to run after the main operation. * It logs a message indicating that the post-check has been called. * @external * @readonly */ post_check(args: modhooks.post_check_args): void { const argsBuffer = Protobuf.encode(args, modhooks.post_check_args.encode); const callRes = System.call(this._contractId, 0xe9101696, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.post_check': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } return; } /** * Handles the installation of the module. * * This method is called when the module is installed. It can include logic * for setting up the module, initializing storage, or other setup tasks. * @external */ on_install(args: modhooks.on_install_args): void { const argsBuffer = Protobuf.encode(args, modhooks.on_install_args.encode); const callRes = System.call(this._contractId, 0xd3813539, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.on_install': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } return; } /** * Handles the uninstallation of the module. * * This method is called when the module is uninstalled. It can include logic * for cleanup tasks, such as removing storage or other resources used by the module. * @external */ on_uninstall(args: modhooks.on_uninstall_args): void { const argsBuffer = Protobuf.encode(args, modhooks.on_uninstall_args.encode); const callRes = System.call(this._contractId, 0x3278f284, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.on_uninstall': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } return; } /** * Checks if the module matches a specific type. * * This method is called to verify if the module is of a certain type. It returns * a boolean indicating whether the module type matches the provided type ID. * @external * @readonly */ is_type(args: modhooks.is_type_args): modhooks.is_type_result { const argsBuffer = Protobuf.encode(args, modhooks.is_type_args.encode); const callRes = System.call(this._contractId, 0xb4fc81c5, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.is_type': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } if (!callRes.res.object) return new modhooks.is_type_result(); return Protobuf.decode<modhooks.is_type_result>(callRes.res.object, modhooks.is_type_result.decode); } /** * Returns the manifest of the module. * * This method provides the module's manifest, which includes metadata such as * the module's name, description, and type ID. It is used to describe the module * and its capabilities. * @external * @readonly */ manifest(): modhooks.manifest { const argsBuffer = new Uint8Array(0); const callRes = System.call(this._contractId, 0x05b3abf2, argsBuffer); if (callRes.code != 0) { const errorMessage = `failed to call 'ModHooks.manifest': ${callRes.res.error && callRes.res.error!.message ? callRes.res.error!.message : "unknown error"}`; System.exit(callRes.code, StringBytes.stringToBytes(errorMessage)); } if (!callRes.res.object) return new modhooks.manifest(); return Protobuf.decode<modhooks.manifest>(callRes.res.object, modhooks.manifest.decode); } }