How do I auto-calculate the number of variable outputs?

Hi everyone. I’m writing Spark’s Rust order matcher, and I’ve encountered an issue while matching orders. Previously, I’ve been matching orders one-to-one, one sell and one buy. Right now I’ve switched to matching many-to-many.
The issue is with setting the number of variable outputs for the transaction. AFAIK, there used to be a function that auto-calculated the number of variable outputs so you didn’t have to do it manually, but then it stopped working?.. Right now we’re using .append_variable_outputs(usize), and while it’s constant (2) for a one-to-one match, it’s not as easy for a many-to-many match. Is there any way I can auto-calculate the number of variable outputs to not have to introduce an extra parameter for it in a contract ABI function?

Thank you for the help!

The method that auto-calculates output variables is described here:

https://rust.fuel.network/v0.61.0/calling-contracts/tx-dependency-estimation.html#transaction-dependency-estimation

If you have discovered an issue while using it, feel free to share the details and we’ll look into it.

Thank you, but unfortunately it doesn’t work. So I’m trying to run estimation this way:

        self.instance
            .methods()
            .match_orders_many(sell_order_ids, buy_order_ids)
            .with_tx_policies(TxPolicies::default())
            .estimate_tx_dependencies(Some(3))
            .await?
            .call()
            .await

I’m getting the standard Revert transaction error: Revert in this case, the same I was getting when there was the old copy-pasted code with .add_variable_output(2) instead of estimate. As soon as I change the estimate call to .add_variable_output(20) (the vectors I’m trying to match contain 3 sells and 3 buys), it works without any issues. So I unfortunately don’t know why this estimation doesn’t work for me, maybe I’m doing something wrong? But I’m getting revert transaction if I try to use it.

estimate_tx_dependencies will need 1 simulation for each tx dependency it needs to resolve.
The total number of tx dependencies in this case is the number of output variables needed plus the number of other contracts you interact with (e.g if the contract method you’re calling includes calls to other contracts).

If you use estimate_tx_dependencies(Some(3)) you are limiting it to 3 simulation attempts. If your call includes more than 3 dependencies, estimate_tx_dependencies won’t be able to resolve those.

The number of attempts should be set high enough to reflect the maximum expected number of dependencies. Also note that currently each simulation attempt incurs a network call and some overhead on the node side. There are plans to alleviate this in the future by running localised simulations but this is not a high priority item at the moment.

I see. Thank you for the explanation! I’ve tried running .estimate_tx_dependencies(None) and comparing it to .append_variable_outputs((sells.len() + buys.len()) * 3) with two vectors of 5x5 matching orders (5 sells and 5 buys), and the match with estimation takes 50 seconds, while the approximated .append_variable_output() takes only 27. That’s a bit too much overhead IMO. We’ll be using the estimated variable output count for now, and hopefully sometime when the estimation gets optimized, it would be possible to switch to it at that point.

Again, thanks so much for all your help, @mujkica !