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: script transaction human readable formatting #1542

Open
wants to merge 43 commits into
base: master
Choose a base branch
from

Conversation

segfault-magnet
Copy link
Contributor

@segfault-magnet segfault-magnet commented Nov 11, 2024

Release notes

In this release, we:

  • Added tools to help with creating a human readable view of fuel script transactions.

Summary

Users can now demystify what is contained in a fuel transaction. If it is a contract call the user can get the method name and other parameters. If given the ABI the user can also see the arguments the contract method was called with.

Originally intended for the block explorer to make the tx view more human readable.

Example of how a script tx can now be decoded in runtime:

        let TransactionType::Script(tx) = provider
            .get_transaction_by_id(&tx_id)
            .await?
            .unwrap()
            .transaction
        else {
            panic!("Transaction is not a script transaction");
        };

        let ScriptType::ContractCall(calls) = ScriptType::detect(tx.script(), tx.script_data())?
        else {
            panic!("Script is not a contract call");
        };

        let json_abi = std::fs::read_to_string(
            "../../e2e/sway/contracts/contract_test/out/release/contract_test-abi.json",
        )?;
        let abi_formatter = ABIFormatter::from_json_abi(&json_abi)?;

        let call = &calls[0];
        let fn_selector = callalso.decode_fn_selector()?;
        let decoded_args = abi_formatter.decode_fn_args(&fn_selector, &call.encoded_args)?;

        eprintln!(
            "The script called: {fn_selector}({})",
            decoded_args.join(", ")
        );

Which prints:

The script called: initialize_counter(42)

If you don't have the ABI you can still get the contract method name and information such as gas forwarded etc.

Multicalls are supported.

It can also handle configurables, example of decoding a sway script:

    let decoder = ABIFormatter::from_json_abi(&abi)?;

    let ScriptType::Other(desc) = ScriptType::detect(&tb.script, &tb.script_data).unwrap() else {
        panic!("expected a script")
    };

    assert_eq!(
        decoder.decode_fn_args("main", &desc.data)?,
        vec!["MyStruct { number: 10, boolean: false }"]
    );

    assert_eq!(
        decoder
            .decode_configurables(desc.data_section().unwrap())
            .unwrap(),
        vec![
            ("A_NUMBER".to_owned(), "11".to_owned()),
            (
                "MY_STRUCT".to_owned(),
                "MyStruct { number: 10, boolean: true }".to_owned()
            ),
        ]
    );

When given a loader script we can get the blob id:

    let ScriptType::Loader { script, blob_id } = ScriptType::detect(&script, &script_data).unwrap()
    else {
        panic!("expected a loader script")
    };

    assert_eq!(blob_id, expected_blob_id);

The code is wasm compatible.

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

@segfault-magnet segfault-magnet self-assigned this Nov 13, 2024
@segfault-magnet segfault-magnet added the enhancement New feature or request label Nov 13, 2024
@segfault-magnet segfault-magnet marked this pull request as ready for review November 14, 2024 15:44
@segfault-magnet segfault-magnet changed the title Feat/script analyzing feat: script transaction human readable formatting Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

detect whether a script tx is a contract call / is a loader script
1 participant