Skip to content

Commit dde493c

Browse files
authored
feat!: add decode_as_debug_str to ABIDecoder (#1291)
PR: #690 updated `ParamType` and added names for `struct` and `enum` types and their fields/variants. This was later removed in PR: #885 as we completely changed how log decoding is done. This PR returns struct and enum names (also fields/variants) so that users can use `ParamTypes` at runtime to debug log or return receipts. In addition, users are able to go directly from `ProgramABI` and some data to decoded debug. Here is an example how this is used: BREAKING CHANGE: `EnumVariants` are now imported through `param_types::EnumVariants`
1 parent fa17c52 commit dde493c

File tree

24 files changed

+2756
-2116
lines changed

24 files changed

+2756
-2116
lines changed

examples/debugging/src/lib.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ mod tests {
55
use fuel_abi_types::abi::program::ProgramABI;
66
use fuels::{
77
core::{
8-
codec::{calldata, fn_selector, resolve_fn_selector},
8+
codec::{calldata, fn_selector, resolve_fn_selector, ABIDecoder},
99
traits::Parameterize,
1010
},
11+
macros::abigen,
1112
types::{errors::Result, param_types::ParamType, SizedAsciiString},
1213
};
1314

@@ -69,4 +70,103 @@ mod tests {
6970

7071
Ok(())
7172
}
73+
74+
#[test]
75+
fn decoded_debug_matches_rust_debug() -> Result<()> {
76+
abigen!(Contract(
77+
name = "MyContract",
78+
abi = "packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json"
79+
));
80+
81+
let json_abi_file =
82+
"../../packages/fuels/tests/types/contracts/generics/out/debug/generics-abi.json";
83+
let abi_file_contents = std::fs::read_to_string(json_abi_file)?;
84+
85+
let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;
86+
87+
let type_lookup = parsed_abi
88+
.types
89+
.into_iter()
90+
.map(|decl| (decl.type_id, decl))
91+
.collect::<HashMap<_, _>>();
92+
93+
let get_first_fn_argument = |fn_name: &str| {
94+
parsed_abi
95+
.functions
96+
.iter()
97+
.find(|abi_fun| abi_fun.name == fn_name)
98+
.expect("should be there")
99+
.inputs
100+
.first()
101+
.expect("should be there")
102+
};
103+
let decoder = ABIDecoder::default();
104+
105+
{
106+
// simple struct with a single generic parameter
107+
let type_application = get_first_fn_argument("struct_w_generic");
108+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
109+
110+
let expected_struct = SimpleGeneric {
111+
single_generic_param: 123u64,
112+
};
113+
114+
assert_eq!(
115+
format!("{expected_struct:?}"),
116+
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?
117+
);
118+
}
119+
{
120+
// struct that delegates the generic param internally
121+
let type_application = get_first_fn_argument("struct_delegating_generic");
122+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
123+
124+
let expected_struct = PassTheGenericOn {
125+
one: SimpleGeneric {
126+
single_generic_param: SizedAsciiString::<3>::try_from("abc")?,
127+
},
128+
};
129+
130+
assert_eq!(
131+
format!("{expected_struct:?}"),
132+
decoder.decode_as_debug_str(&param_type, &[97, 98, 99])?
133+
);
134+
}
135+
{
136+
// enum with generic in variant
137+
let type_application = get_first_fn_argument("enum_w_generic");
138+
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
139+
140+
let expected_enum = EnumWGeneric::B(10u64);
141+
142+
assert_eq!(
143+
format!("{expected_enum:?}"),
144+
decoder.decode_as_debug_str(
145+
&param_type,
146+
&[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 10]
147+
)?
148+
);
149+
}
150+
{
151+
// logged type
152+
let logged_type = parsed_abi
153+
.logged_types
154+
.as_ref()
155+
.expect("has logs")
156+
.first()
157+
.expect("has log");
158+
159+
let param_type =
160+
ParamType::try_from_type_application(&logged_type.application, &type_lookup)?;
161+
162+
let expected_u8 = 1;
163+
164+
assert_eq!(
165+
format!("{expected_u8}"),
166+
decoder.decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 1])?
167+
);
168+
}
169+
170+
Ok(())
171+
}
72172
}

0 commit comments

Comments
 (0)