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

Conversation

hal3e
Copy link
Contributor

@hal3e hal3e commented Mar 4, 2024

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:

// Read the 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)?;

// Parse into `ProgramABI`
let parsed_abi: ProgramABI = serde_json::from_str(&abi_file_contents)?;

// Create type lookup (id, TypeDeclaration)
let type_lookup = parsed_abi
    .types
    .into_iter()
    .map(|decl| (decl.type_id, decl))
    .collect::<HashMap<_, _>>();

// Helper function to extract first argument from a contract function
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")
};

// Get TypeApplication
let type_application = get_first_fn_argument("struct_w_generic");
// Get corresponding ParamType
let param_type = ParamType::try_from_type_application(type_application, &type_lookup)?;
// Get debug output (String)
let debug_output =  ABIDecoder::default().decode_as_debug_str(&param_type, &[0, 0, 0, 0, 0, 0, 0, 123])?;

There are actually not so many changes but I refactored the param_types.rs file into a new module with several files to separate the logical units and their tests.

BREAKING CHANGE: EnumVariants are now imported through param_types::EnumVariants

  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary labels.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@hal3e hal3e added tech-debt Improves code quality or safety dev-experience labels Mar 4, 2024
@hal3e hal3e self-assigned this Mar 4, 2024
Copy link
Contributor

@segfault-magnet segfault-magnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pair of (String, ParamType) comes up a lot.

Wdyt about giving it a name? Maybe Member so that it can refer both to struct fields and enum variants.

@kayagokalp
Copy link
Member

This is great! I think it would be very useful for my tooling side decoding needs. Would it possible to also encode similarly given a param type and value to encode?

@hal3e hal3e changed the title feat: add decode_debug to ParamType feat: add decode_as_debug_str to ABIDecoder Mar 7, 2024
@hal3e hal3e changed the title feat: add decode_as_debug_str to ABIDecoder feat!: add decode_as_debug_str to ABIDecoder Mar 7, 2024
@hal3e hal3e requested a review from segfault-magnet March 7, 2024 10:42
Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! This is useful enough, in my opinion, to have a small section in our docs. Thoughts? It can be done in a follow-up, not necessarily in this PR.

@hal3e
Copy link
Contributor Author

hal3e commented Mar 12, 2024

Great work! This is useful enough, in my opinion, to have a small section in our docs. Thoughts? It can be done in a follow-up, not necessarily in this PR.

Here is the issue for that: #1296

@hal3e hal3e merged commit dde493c into master Mar 12, 2024
41 checks passed
@hal3e hal3e deleted the hal3e/decode-debug branch March 12, 2024 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev-experience tech-debt Improves code quality or safety
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants