Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Nov 14, 2024
1 parent 87cd1b0 commit 838b6dd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- [Transfer all assets](./cookbook/transfer-all-assets.md)
- [Debugging](./debugging/index.md)
- [The Function selector](./debugging/function-selector.md)
- [Decoding script transactions](./debugging/decoding-script-transactions.md)
- [Glossary](./glossary.md)
- [Contributing](./contributing/CONTRIBUTING.md)
- [Integration tests structure](./contributing/tests-structure.md)
Expand Down
24 changes: 24 additions & 0 deletions docs/src/debugging/decoding-script-transactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Decoding script transactions

The SDK offers some tools that can help you make fuel script transactions more
human readable. You can determine whether the script transaction is:

* calling a contract method(s),
* is a loader script and you can see the blob id
* is neither of the above

In the case of contract call(s), if you have the ABI file, you can also decode
the arguments to the function by making use of the `AbiFormatter`:

```rust,ignore
{{#include ../../../examples/contracts/src/lib.rs:decoding_script_transactions}}
```

prints:

```text
The script called: initialize_counter(42)
```

The `AbiFormatter` can also decode configurables, refer to the rust docs for
more information.
65 changes: 64 additions & 1 deletion examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ mod tests {
use std::{collections::HashSet, time::Duration};

use fuels::{
core::codec::{encode_fn_selector, DecoderConfig, EncoderConfig},
core::codec::{encode_fn_selector, ABIFormatter, DecoderConfig, EncoderConfig},
crypto::SecretKey,
prelude::{LoadConfiguration, NodeConfig, StorageConfiguration},
programs::debug::ScriptType,
test_helpers::{ChainConfig, StateConfig},
tx::TxId,
types::{
errors::{transaction::Reason, Result},
Bits256,
Expand Down Expand Up @@ -1137,4 +1139,65 @@ mod tests {

Ok(())
}

#[tokio::test]
#[allow(unused_variables)]
async fn decoding_script_transactions() -> Result<()> {
use fuels::prelude::*;

setup_program_test!(
Abigen(Contract(
name = "MyContract",
project = "e2e/sway/contracts/contract_test"
)),
Wallets("wallet"),
Deploy(
name = "contract_instance",
contract = "MyContract",
wallet = "wallet"
)
);

let tx_id = contract_instance
.methods()
.initialize_counter(42)
.call()
.await?
.tx_id
.unwrap();

let provider: &Provider = wallet.try_provider()?;

// ANCHOR: decoding_script_transactions
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 = call.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(", ")
);

// ANCHOR_END: decoding_script_transactions
Ok(())
}
}
3 changes: 2 additions & 1 deletion packages/fuels-core/src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
mod abi_decoder;
mod abi_encoder;
pub mod abi_formatter;
mod abi_formatter;
mod function_selector;
mod logs;
mod utils;

pub use abi_decoder::*;
pub use abi_encoder::*;
pub use abi_formatter::*;
pub use function_selector::*;
pub use logs::*;

Expand Down

0 comments on commit 838b6dd

Please sign in to comment.