From bf83ec74b1e0c28ca65139f5064a519cef292b58 Mon Sep 17 00:00:00 2001 From: jolestar Date: Mon, 17 Jul 2023 17:30:49 +0800 Subject: [PATCH 1/2] [MoveosStd] Define MoveAction in Move --- .../moveos-stdlib/sources/move_action.move | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move diff --git a/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move b/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move new file mode 100644 index 0000000000..08611d6df6 --- /dev/null +++ b/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move @@ -0,0 +1,56 @@ +module moveos_std::move_action{ + + use std::error; + use std::string::{String}; + use std::option::{Self, Option}; + + const EInvalidType: u64 = 1; + + struct ScriptCall has store, copy, drop { + code: vector, + //It is hard to implement TypeTag in Move, so we use String instead + ty_args: vector, + args: vector>, + } + + //TODO define in another module + struct ModuleId has store, copy, drop { + module_address: address, + name: String, + } + + struct FunctionId has store, copy, drop { + module_id: ModuleId, + name: String, + } + + struct FunctionCall has store, copy, drop { + functionId: FunctionId, + ty_args: vector, + args: vector>, + } + + struct ModuleBundle has store, copy, drop { + modules: vector>, + } + + struct MoveAction has store, copy, drop { + script_call: Option, + function_call: Option, + module_bundle: Option, + } + + public fun is_script_call(move_action: &MoveAction): bool { + option::is_some(&move_action.script_call) + } + + public fun into_script_call(move_action: MoveAction): ScriptCall { + assert!(is_script_call(&move_action), error::invalid_argument(EInvalidType)); + option::extract(&mut move_action.script_call) + } + + /// We provide a native function to decode MoveAction from bytes + /// We can not use the `bcs::from_bytes` directly + /// because we can not define a `MoveAction` which has same bcs layout with the MoveAction in Rust + public native fun decode_move_action(bytes: &vector): MoveAction; +} \ No newline at end of file From 4594d15ff4c2fc739122464b596543c330b210f8 Mon Sep 17 00:00:00 2001 From: jolestar Date: Mon, 17 Jul 2023 17:32:17 +0800 Subject: [PATCH 2/2] fixup --- .../moveos-stdlib/sources/move_action.move | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move b/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move index 08611d6df6..4257e769f8 100644 --- a/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move +++ b/moveos/moveos-stdlib/moveos-stdlib/sources/move_action.move @@ -49,6 +49,24 @@ module moveos_std::move_action{ option::extract(&mut move_action.script_call) } + public fun is_function_call(move_action: &MoveAction): bool { + option::is_some(&move_action.function_call) + } + + public fun into_function_call(move_action: MoveAction): FunctionCall { + assert!(is_function_call(&move_action), error::invalid_argument(EInvalidType)); + option::extract(&mut move_action.function_call) + } + + public fun is_module_bundle(move_action: &MoveAction): bool { + option::is_some(&move_action.module_bundle) + } + + public fun into_module_bundle(move_action: MoveAction): ModuleBundle { + assert!(is_module_bundle(&move_action), error::invalid_argument(EInvalidType)); + option::extract(&mut move_action.module_bundle) + } + /// We provide a native function to decode MoveAction from bytes /// We can not use the `bcs::from_bytes` directly /// because we can not define a `MoveAction` which has same bcs layout with the MoveAction in Rust