@veive-io/mod-execution-as
Version:
Veive smart account execution module
105 lines (95 loc) • 4.29 kB
text/typescript
import { System, Protobuf, StringBytes } from "@koinos/sdk-as";
import { modexecution } from "./proto/modexecution";
export class ModExecution {
_contractId: Uint8Array;
/**
* Create an instance of a ModExecution contract
* @example
* ```ts
* const contract = new ModExecution(Base58.decode("1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe"));
* ```
*/
constructor(contractId: Uint8Array) {
this._contractId = contractId;
}
/**
* Executes the specified operation.
*
* This method is called to perform a specific operation, which may include
* smart contract calls or other actions defined in the execution module.
* @external
*/
execute(args: modexecution.execute_args): void {
const argsBuffer = Protobuf.encode(args, modexecution.execute_args.encode);
const callRes = System.call(this._contractId, 0xbe6bdfe8, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'ModExecution.execute': ${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: modexecution.on_install_args): void {
const argsBuffer = Protobuf.encode(args, modexecution.on_install_args.encode);
const callRes = System.call(this._contractId, 0xd3813539, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'ModExecution.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: modexecution.on_uninstall_args): void {
const argsBuffer = Protobuf.encode(args, modexecution.on_uninstall_args.encode);
const callRes = System.call(this._contractId, 0x3278f284, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'ModExecution.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: modexecution.is_type_args): modexecution.is_type_result {
const argsBuffer = Protobuf.encode(args, modexecution.is_type_args.encode);
const callRes = System.call(this._contractId, 0xb4fc81c5, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'ModExecution.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 modexecution.is_type_result();
return Protobuf.decode<modexecution.is_type_result>(callRes.res.object, modexecution.is_type_result.decode);
}
/**
* @external
* @readonly
*/
manifest(): modexecution.manifest {
const argsBuffer = new Uint8Array(0);
const callRes = System.call(this._contractId, 0x05b3abf2, argsBuffer);
if (callRes.code != 0) {
const errorMessage = `failed to call 'ModExecution.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 modexecution.manifest();
return Protobuf.decode<modexecution.manifest>(callRes.res.object, modexecution.manifest.decode);
}
}