How to add a Predicate to a Script inputs

I want to set a predicate as an input to a script in the following fashion:

    let predicate = MyPredicate::load_from( predicate_bin_path,).unwrap();
    let script = MyScript::new(wallet, bin_path);
     // First wallet transfers amount to predicate.
     predicate.receive(first_wallet, 500, asset_id, None).await.unwrap();

     // Check predicate balance.
     let balance = first_wallet
         .get_asset_balance(predicate.address(), asset_id)

     assert_eq!(balance, 500);

    let inputs = vec![predicate.encode_data(4096, 4096)];
    let result = script.main().with_inputs(inputs).call().await.unwrap();

However this results in a mismatched types error since predicate.encode_data(4096, 4096) is not of type Input. My question is how to transform a predicate into the Input type ?


You’ll need to provide the coin belonging to the predicate that you want to spend as an Input::CoinPredicate. You can see an example of this here.

We’re working on ways to make this more intuitive. Let me know if you have any questions in the meantime !


One more question, in the predicate_data field sway-applications/ at 0132a8056a21babe810851b49f6630b68558dfd1 · FuelLabs/sway-applications · GitHub would it be possible to insert an address as data, and access that value inside both the predicate and the script ?

Yes - On the Sway side, predicate data is provided as arguments to the main() function. If you also want to access those arguments in the script, you can use this function from the standard library.