Skip to content

Commit 323c2d7

Browse files
committed
feat: make max_fee_estimation_tolerance configurable in call handlers and Executable APIs
Closes #1591
1 parent 3422bcc commit 323c2d7

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

packages/fuels-programs/src/calls/call_handler.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ use fuels_core::{
2121
},
2222
};
2323

24-
use crate::{
25-
calls::{
26-
CallParameters, ContractCall, Execution, ExecutionType, ScriptCall,
27-
receipt_parser::ReceiptParser,
28-
traits::{ContractDependencyConfigurator, ResponseParser, TransactionTuner},
29-
utils::find_ids_of_missing_contracts,
30-
},
31-
responses::{CallResponse, SubmitResponse},
32-
};
24+
use crate::{calls::{
25+
CallParameters, ContractCall, Execution, ExecutionType, ScriptCall,
26+
receipt_parser::ReceiptParser,
27+
traits::{ContractDependencyConfigurator, ResponseParser, TransactionTuner},
28+
utils::find_ids_of_missing_contracts,
29+
}, responses::{CallResponse, SubmitResponse}, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE};
3330

3431
// Trait implemented by contract instances so that
3532
// they can be passed to the `with_contracts` method
@@ -45,6 +42,7 @@ pub struct CallHandler<A, C, T> {
4542
pub account: A,
4643
pub call: C,
4744
pub tx_policies: TxPolicies,
45+
pub max_fee_estimation_tolerance: f32,
4846
pub log_decoder: LogDecoder,
4947
pub datatype: PhantomData<T>,
5048
decoder_config: DecoderConfig,
@@ -66,6 +64,11 @@ impl<A, C, T> CallHandler<A, C, T> {
6664
self
6765
}
6866

67+
pub fn with_max_fee_estimation_tolerance(mut self, max_fee_estimation_tolerance: f32) -> Self {
68+
self.max_fee_estimation_tolerance = max_fee_estimation_tolerance;
69+
self
70+
}
71+
6972
pub fn with_decoder_config(mut self, decoder_config: DecoderConfig) -> Self {
7073
self.decoder_config = decoder_config;
7174
self.log_decoder.set_decoder_config(decoder_config);
@@ -99,7 +102,7 @@ where
99102
pub async fn transaction_builder(&self) -> Result<ScriptTransactionBuilder> {
100103
let mut tb = self
101104
.call
102-
.transaction_builder(self.tx_policies, self.variable_output_policy, &self.account)
105+
.tx_builder_with_max_fee_est_tolerance(self.tx_policies, self.variable_output_policy, &self.account, self.max_fee_estimation_tolerance)
103106
.await?;
104107

105108
tb.add_signers(&self.unresolved_signers)?;
@@ -288,6 +291,7 @@ where
288291
account,
289292
call,
290293
tx_policies: TxPolicies::default(),
294+
max_fee_estimation_tolerance: DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
291295
log_decoder,
292296
datatype: PhantomData,
293297
decoder_config: DecoderConfig::default(),
@@ -381,6 +385,7 @@ where
381385
account,
382386
call,
383387
tx_policies: TxPolicies::default(),
388+
max_fee_estimation_tolerance: DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
384389
log_decoder,
385390
datatype: PhantomData,
386391
decoder_config: DecoderConfig::default(),
@@ -414,6 +419,7 @@ where
414419
account,
415420
call: vec![],
416421
tx_policies: TxPolicies::default(),
422+
max_fee_estimation_tolerance: DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
417423
log_decoder: LogDecoder::new(Default::default(), Default::default()),
418424
datatype: PhantomData,
419425
decoder_config: DecoderConfig::default(),

packages/fuels-programs/src/calls/traits/transaction_tuner.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
1212
calls::{
1313
ContractCall, ScriptCall,
14-
utils::{build_with_tb, sealed, transaction_builder_from_contract_calls},
14+
utils::{build_with_tb, sealed, transaction_builder_from_contract_calls, tx_builder_from_ct_calls_with_max_fee_est_tolerance},
1515
},
1616
};
1717

@@ -24,6 +24,14 @@ pub trait TransactionTuner: sealed::Sealed {
2424
account: &T,
2525
) -> Result<ScriptTransactionBuilder>;
2626

27+
async fn tx_builder_with_max_fee_est_tolerance<T: Account>(
28+
&self,
29+
tx_policies: TxPolicies,
30+
variable_output_policy: VariableOutputPolicy,
31+
account: &T,
32+
max_fee_estimation_tolerance: f32
33+
) -> Result<ScriptTransactionBuilder>;
34+
2735
async fn build_tx<T: Account>(
2836
&self,
2937
tb: ScriptTransactionBuilder,
@@ -39,11 +47,29 @@ impl TransactionTuner for ContractCall {
3947
variable_output_policy: VariableOutputPolicy,
4048
account: &T,
4149
) -> Result<ScriptTransactionBuilder> {
42-
transaction_builder_from_contract_calls(
50+
tx_builder_from_ct_calls_with_max_fee_est_tolerance(
51+
std::slice::from_ref(self),
52+
tx_policies,
53+
variable_output_policy,
54+
account,
55+
DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
56+
)
57+
.await
58+
}
59+
60+
async fn tx_builder_with_max_fee_est_tolerance<T: Account>(
61+
&self,
62+
tx_policies: TxPolicies,
63+
variable_output_policy: VariableOutputPolicy,
64+
account: &T,
65+
max_fee_estimation_tolerance: f32
66+
) -> Result<ScriptTransactionBuilder> {
67+
tx_builder_from_ct_calls_with_max_fee_est_tolerance(
4368
std::slice::from_ref(self),
4469
tx_policies,
4570
variable_output_policy,
4671
account,
72+
max_fee_estimation_tolerance,
4773
)
4874
.await
4975
}
@@ -64,6 +90,22 @@ impl TransactionTuner for ScriptCall {
6490
tx_policies: TxPolicies,
6591
variable_output_policy: VariableOutputPolicy,
6692
_account: &T,
93+
) -> Result<ScriptTransactionBuilder> {
94+
self.tx_builder_with_max_fee_est_tolerance(
95+
tx_policies,
96+
variable_output_policy,
97+
_account,
98+
DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE
99+
)
100+
.await
101+
}
102+
103+
async fn tx_builder_with_max_fee_est_tolerance<T: Account>(
104+
&self,
105+
tx_policies: TxPolicies,
106+
variable_output_policy: VariableOutputPolicy,
107+
_account: &T,
108+
max_fee_estimation_tolerance: f32
67109
) -> Result<ScriptTransactionBuilder> {
68110
let (inputs, outputs) = self.prepare_inputs_outputs()?;
69111

@@ -75,7 +117,7 @@ impl TransactionTuner for ScriptCall {
75117
.with_inputs(inputs)
76118
.with_outputs(outputs)
77119
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
78-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
120+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance))
79121
}
80122

81123
async fn build_tx<T: Account>(
@@ -108,6 +150,18 @@ impl TransactionTuner for Vec<ContractCall> {
108150
transaction_builder_from_contract_calls(self, tx_policies, variable_output_policy, account)
109151
.await
110152
}
153+
154+
async fn tx_builder_with_max_fee_est_tolerance<T: Account>(
155+
&self,
156+
tx_policies: TxPolicies,
157+
variable_output_policy: VariableOutputPolicy,
158+
account: &T,
159+
max_fee_estimation_tolerance: f32
160+
) -> Result<ScriptTransactionBuilder> {
161+
validate_contract_calls(self)?;
162+
tx_builder_from_ct_calls_with_max_fee_est_tolerance(self, tx_policies, variable_output_policy, account, max_fee_estimation_tolerance)
163+
.await
164+
}
111165

112166
/// Returns the script that executes the contract calls
113167
async fn build_tx<T: Account>(

packages/fuels-programs/src/calls/utils.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ pub(crate) mod sealed {
2929
pub trait Sealed {}
3030
}
3131

32-
/// Creates a [`ScriptTransactionBuilder`] from contract calls.
33-
pub(crate) async fn transaction_builder_from_contract_calls(
32+
/// Creates a [`ScriptTransactionBuilder`] from contract calls with customizable [`max_fee_estimation_tolerance`].
33+
pub(crate) async fn tx_builder_from_ct_calls_with_max_fee_est_tolerance(
3434
calls: &[ContractCall],
3535
tx_policies: TxPolicies,
3636
variable_outputs: VariableOutputPolicy,
3737
account: &impl Account,
38+
max_fee_estimation_tolerance: f32
3839
) -> Result<ScriptTransactionBuilder> {
3940
let calls_instructions_len = compute_calls_instructions_len(calls);
4041
let provider = account.try_provider()?;
@@ -75,7 +76,17 @@ pub(crate) async fn transaction_builder_from_contract_calls(
7576
.with_inputs(inputs)
7677
.with_outputs(outputs)
7778
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
78-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
79+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance))
80+
}
81+
82+
/// Creates a [`ScriptTransactionBuilder`] from contract calls.
83+
pub(crate) async fn transaction_builder_from_contract_calls(
84+
calls: &[ContractCall],
85+
tx_policies: TxPolicies,
86+
variable_outputs: VariableOutputPolicy,
87+
account: &impl Account,
88+
) -> Result<ScriptTransactionBuilder> {
89+
tx_builder_from_ct_calls_with_max_fee_est_tolerance(calls, tx_policies, variable_outputs, account, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE).await
7990
}
8091

8192
/// Creates a [`ScriptTransaction`] from contract calls. The internal [Transaction] is

packages/fuels-programs/src/executable.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ impl Executable<Loader> {
167167
pub async fn upload_blob(
168168
&self,
169169
account: impl fuels_accounts::Account,
170+
) -> Result<Option<TxResponse>> {
171+
self.upload_blob_with_max_fee_est_tolerance(account, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE).await
172+
}
173+
174+
pub async fn upload_blob_with_max_fee_est_tolerance(
175+
&self,
176+
account: impl fuels_accounts::Account,
177+
max_fee_estimation_tolerance: f32,
170178
) -> Result<Option<TxResponse>> {
171179
let blob = self.blob();
172180
let provider = account.try_provider()?;
@@ -178,7 +186,7 @@ impl Executable<Loader> {
178186

179187
let mut tb = BlobTransactionBuilder::default()
180188
.with_blob(self.blob())
181-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE);
189+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance);
182190

183191
account
184192
.adjust_for_fee(&mut tb, 0)

0 commit comments

Comments
 (0)