-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return view function result with events and used gas (#17)
* return view function result with events and used gas * fmt * clippy * use JsonEvent
- Loading branch information
Showing
13 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ mod output; | |
mod staking; | ||
mod std_coin; | ||
mod table; | ||
mod view_output; | ||
|
||
#[cfg(feature = "testing")] | ||
mod move_unit; |
10 changes: 10 additions & 0 deletions
10
crates/e2e-move-tests/src/tests/view_output.data/pack/Move.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "ViewOutputTests" | ||
version = "0.0.0" | ||
|
||
[dependencies] | ||
InitiaStdlib = { local = "../../../../../../precompile/modules/initia_stdlib" } | ||
|
||
[addresses] | ||
std = "0x1" | ||
test = "0x2" |
22 changes: 22 additions & 0 deletions
22
crates/e2e-move-tests/src/tests/view_output.data/pack/sources/ViewOutputTests.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module test::ViewOutputTests { | ||
use std::event; | ||
use std::string; | ||
use initia_std::type_info; | ||
|
||
#[event] | ||
/// Event emitted when some amount of coins are withdrawn from an Collateral. | ||
struct ViewEvent has drop, store { | ||
type_arg: string::String, | ||
arg: string::String, | ||
} | ||
|
||
#[view] | ||
public fun emit_event<TypeArg>(arg: string::String): string::String { | ||
event::emit(ViewEvent { | ||
type_arg: type_info::type_name<TypeArg>(), | ||
arg, | ||
}); | ||
|
||
arg | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::MoveHarness; | ||
use initia_move_types::json_event::JsonEvents; | ||
use initia_move_types::view_function::ViewFunction; | ||
use move_core_types::identifier::Identifier; | ||
use move_core_types::language_storage::{StructTag, TypeTag}; | ||
use move_core_types::{account_address::AccountAddress, language_storage::ModuleId}; | ||
|
||
#[test] | ||
fn test_view_output() { | ||
let deployer_addr = | ||
AccountAddress::from_hex_literal("0x2").expect("0x2 account should be created"); | ||
let path = "src/tests/view_output.data/pack"; | ||
let mut h = MoveHarness::new(); | ||
|
||
h.initialize(); | ||
|
||
// publish std coin | ||
let output = h | ||
.publish_package(&deployer_addr, path) | ||
.expect("should success"); | ||
h.commit(output, true); | ||
|
||
let module_name = Identifier::from_str("ViewOutputTests").unwrap(); | ||
let module_id = ModuleId::new(deployer_addr, module_name.clone()); | ||
let function_name = Identifier::from_str("emit_event").unwrap(); | ||
let struct_name = Identifier::from_str("ViewEvent").unwrap(); | ||
|
||
let arg_bytes = bcs::to_bytes("hello world").unwrap(); | ||
let out = h | ||
.run_view_function_get_events(ViewFunction::new( | ||
module_id, | ||
function_name, | ||
vec![TypeTag::U256], | ||
vec![arg_bytes], | ||
)) | ||
.expect("should success"); | ||
|
||
assert_eq!(out.ret().as_str(), "\"hello world\""); | ||
assert_eq!( | ||
out.events(), | ||
&JsonEvents::new(vec![( | ||
TypeTag::Struct(Box::new(StructTag { | ||
address: deployer_addr, | ||
module: module_name, | ||
name: struct_name, | ||
type_params: vec![] | ||
})), | ||
"{\"arg\":\"hello world\",\"type_arg\":\"u256\"}".to_string() | ||
)]) | ||
.into_inner(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.