"Predicate data" tutorial from The Fuel Rust SDK Book doesn't work

Hi guys
Following this tutorial Predicate data - The Fuel Rust SDK I inserted the code into my code editor and it gives me that error

if I’m trying to run it I have this output

Executing task: cargo test --package predicates-test --test integration_tests -- just_test --exact --nocapture 

   Compiling predicates-test v0.1.0 (/Users/alexey/projects/fuel/predicates-test)
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> tests/harness.rs:62:45
   |
62 |     let predicate_data: Vec<u8> = predicate.encode_data(42_u64);
   |                                             ^^^^^^^^^^^ ------ argument of type `u64` unexpected
   |
note: associated function defined here
  --> tests/harness.rs:7:1
   |
7  | / abigen!(Predicate(
8  | |     name = "Predicate",
9  | |     abi = "out/debug/predicates-test-abi.json"
10 | | ));
   | |__^
   = note: this error originates in the macro `abigen` (in Nightly builds, run with -Z macro-backtrace for more info)
help: remove the extra argument
   |
62 |     let predicate_data: Vec<u8> = predicate.encode_data();
   |                                                        ~~

error[E0308]: mismatched types
  --> tests/harness.rs:62:35
   |
62 |     let predicate_data: Vec<u8> = predicate.encode_data(42_u64);
   |                         -------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Vec<u8>`, found `Predicate`
   |                         |
   |                         expected due to this
   |
   = note: expected struct `Vec<u8>`
              found struct `Predicate`

error[E0308]: mismatched types
   --> tests/harness.rs:73:13
    |
67  |         .spend_predicate(
    |          --------------- arguments to this method are incorrect
...
73  |             Some(predicate_data),
    |             ^^^^^^^^^^^^^^^^^^^^ expected `UnresolvedBytes`, found `Option<Vec<u8>>`
    |
    = note: expected struct `UnresolvedBytes`
                 found enum `Option<Vec<u8>>`
note: associated function defined here
   --> /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/fuels-signers-0.37.1/src/wallet.rs:552:18
    |
552 |     pub async fn spend_predicate(
    |                  ^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0061, E0308.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `predicates-test` due to 3 previous errors

 *  The terminal process "cargo 'test', '--package', 'predicates-test', '--test', 'integration_tests', '--', 'just_test', '--exact', '--nocapture'" terminated with exit code: 101. 
 *  Terminal will be reused by tasks, press any key to close it. 

Thats my toolchain

Default host: aarch64-apple-darwin
fuelup home: /Users/alexey/.fuelup

installed toolchains
--------------------
beta-2-aarch64-apple-darwin (default)
beta-3-aarch64-apple-darwin
latest-aarch64-apple-darwin
latest-2023-02-18-aarch64-apple-darwin

active toolchain
-----------------
beta-2-aarch64-apple-darwin (default)
  forc : 0.31.1
    - forc-client
      - forc-deploy : 0.31.1
      - forc-run : 0.31.1
    - forc-doc : 0.31.1
    - forc-explore : 0.28.1
    - forc-fmt : 0.31.1
    - forc-index : 0.1.13
    - forc-lsp : 0.31.1
    - forc-tx - not found
    - forc-wallet : 0.1.2
  fuel-core : 0.15.3
  fuel-indexer : 0.1.13

I’m tried on force version 0.35 and 0.37
And result is the same

Check out the updated tutorial, your link was for v0.33
https://rust.fuel.network/v0.37.0/predicates/predicate-data.html#predicate-data

Thanks
Also in the example, we have a lot of signs like “?” but my compiler doesn’t like that too much. How to allow to use of “?” characters?

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:33:75
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
33 |     let predicate = Predicate::load_from( "out/debug/predicates-test.bin")?;
   |                                                                           ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::types::errors::Error>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:46:15
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
46 |         .await?;
   |               ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::types::errors::Error>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:50:24
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
50 |         .get_provider()?
   |                        ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::signers::wallet::WalletError>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:52:15
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
52 |         .await?;
   |               ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::signers::provider::ProviderError>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:59:64
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
59 |     let predicate_data: Vec<u8> = predicate.encode_data(42_u64)?;
   |                                                                ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<_>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:73:15
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
73 |         .await?;
   |               ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::types::errors::Error>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:77:24
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
77 |         .get_provider()?
   |                        ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::signers::wallet::WalletError>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:79:15
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
79 |         .await?;
   |               ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::signers::provider::ProviderError>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> tests/harness.rs:84:77
   |
11 | #[tokio::test]
   | -------------- this function should return `Result` or `Option` to accept `?`
...
84 |     let balance = second_wallet.get_asset_balance(&AssetId::default()).await?;
   |                                                                             ^ cannot use the `?` operator in an async block that returns `()`
   |
   = help: the trait `FromResidual<Result<Infallible, fuels::types::errors::Error>>` is not implemented for `()`

Some errors have detailed explanations: E0061, E0277, E0308.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `predicates-test` due to 12 previous errors
1 Like

And also in the example, I have an error like


He wants to see 2 arguments right here, but if I provide 2 arguments I have the same error as in my first try

@furnic
Check it, please

Full code you can find here

1 Like

The ? operator in Rust can only be used in a function which returns a Result type.
You can either change your test function signature to return a result, or replace usage of ? with .unwrap();

The api for working with predicates has changed. Following along with the most recent docs, we can see that:

  • Sending funds to the predicate is now done via predicate.receive, i.e:
    predicate.receive(first_wallet, 500, asset_id, None).await?;

  • Encoding data and spending funds is done like this:

         predicate
            .encode_data(4096, 4096)
            .spend(second_wallet, amount_to_unlock, asset_id, None)
            .await?;

Full example here: Predicate data - The Fuel Rust SDK
You can also read the release notes for Fuels v0.34.0 on this page: Releases · FuelLabs/fuels-rs · GitHub

I’m just copying and pasting the example, and I having this error

1 Like

You can see the full context for the example from the docs in this file: fuels-rs/lib.rs at master · FuelLabs/fuels-rs · GitHub .

The examples that use ? instead of unwrap won’t work if you copy and paste outside of a function that returns a Result type.

2 Likes

Yes, but somehow that doesn’t work for me

After this fixed code became valid, there is no warnings or errors but when I’m running the code I have this error

running 1 test
thread 'just_test' panicked at 'called `Result::unwrap()` on an `Err` value: ProviderError("Response errors; Invalid transaction: The transaction contains a predicate which failed to validate: TransactionId(0xbb4b81fe6e74a6823fbd0d0a239a0890f99ff177a8baacb3a69c68dac986e277)")', tests/harness.rs:55:10

I have tried to change encode_data(4096, 4096) to encode_data(4096_u32, 4096_u64) but result is the same

The code is here

I would try modifying your predicate to take 2 params of the same type, otherwise I would expect the comparison with == to fail…

Remade the predicate the following code

And added types on the predicate call

After the rebuild of the predicate, I have this output

  Compiled library "core".
  Compiled library "std".
  Compiled predicate "predicates-test".
  Bytecode size is 44 bytes.
  Predicate root: 0x4189b4e1542f0fc48595b3e009bc18f2151be7af29a2def960263d67d287d326

And the error is still the same

^^/p/f/predicates-test >>> cargo test                                                                                    (master+4) 16:07:49 
    Finished test [unoptimized + debuginfo] target(s) in 0.35s
     Running tests/harness.rs (target/debug/deps/integration_tests-235965a24c31f178)

running 1 test
test just_test ... FAILED

failures:

---- just_test stdout ----
thread 'just_test' panicked at 'called `Result::unwrap()` on an `Err` value: ProviderError("Response errors; Invalid transaction: The transaction contains a predicate which failed to validate: TransactionId(0xe078c783fed0be84cdc178493e707051d1134a07919613e0a61a3e16057f32d0)")', tests/harness.rs:55:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    just_test

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.21s

error: test failed, to rerun pass `--test integration_tests`

I would try increasing the tx gas limit as the next step in debugging.

I’ve tried running the example from your repo and had no problems. Have you updated your toolchain since the initial post?

For reference, I’ve managed to run it with the following:

forc : 0.35.0
fuel-core : 0.17.0
2 Likes

That works on testnet beta-3, thanks!

2 Likes

thank you for tutorial

1 Like

I will make an article about predicates after eth porto
You can see more tutorials here

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.