Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add decode_as_debug_str to ABIDecoder #1291

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion examples/debugging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ mod tests {
use fuel_abi_types::abi::program::ProgramABI;
use fuels::{
core::{
codec::{calldata, fn_selector, resolve_fn_selector},
codec::{calldata, fn_selector, resolve_fn_selector, ABIDecoder},
traits::Parameterize,
},
macros::abigen,
types::{errors::Result, param_types::ParamType, SizedAsciiString},
};

Expand Down Expand Up @@ -69,4 +70,103 @@ mod tests {

Ok(())
}

#[test]
fn decoded_debug_matches_rust_debug() -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi = "packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json"
));

let json_abi_file =
"../../packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json";
let abi_file_contents = std::fs::read_to_string(json_abi_file)?;

let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;

let type_lookup = parsed_abi
.types
.into_iter()
.map(|decl| (decl.type_id, decl))
.collect::<HashMap<_, _>>();

let get_first_fn_argument = |fn_name: &str| {
parsed_abi
.functions
.iter()
.find(|abi_fun| abi_fun.name == fn_name)
.expect("should be there")
.inputs
.first()
.expect("should be there")
};
let decoder = ABIDecoder::default();

{
// simple struct with a single generic parameter
let type_application = get_first_fn_argument("struct_w_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_struct = SimpleGeneric {
single_generic_param: 123u64,
};

assert_eq!(
format!("{expected_struct:?}"),
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?
);
}
{
// struct that delegates the generic param internally
let type_application = get_first_fn_argument("struct_delegating_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_struct = PassTheGenericOn {
one: SimpleGeneric {
single_generic_param: SizedAsciiString::<3>::try_from("abc")?,
},
};

assert_eq!(
format!("{expected_struct:?}"),
decoder.decode_as_debug_str(&param_type, &[97, 98, 99])?
);
}
{
// enum with generic in variant
let type_application = get_first_fn_argument("enum_w_generic");
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;

let expected_enum = EnumWGeneric::B(10u64);

assert_eq!(
format!("{expected_enum:?}"),
decoder.decode_as_debug_str(
&param_type,
&[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 10]
)?
);
}
{
// logged type
let logged_type = parsed_abi
.logged_types
.as_ref()
.expect("has logs")
.first()
.expect("has log");

let param_type =
ParamType::try_from_type_application(&logged_type.application, &type_lookup)?;

let expected_u8 = 1;

assert_eq!(
format!("{expected_u8}"),
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 1])?
);
}

Ok(())
}
}
Loading
Loading