Skip to content

Commit e0c5d84

Browse files
committed
feat: make max_fee_estimation_tolerance configurable in call handlers and Executable APIs
1 parent 865e00c commit e0c5d84

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

e2e/tests/contracts.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,3 +2880,45 @@ async fn test_returned_method_descriptors_are_valid() -> Result<()> {
28802880

28812881
Ok(())
28822882
}
2883+
2884+
#[tokio::test]
2885+
async fn test_max_fee_estimation_tolerance() -> Result<()> {
2886+
setup_program_test!(
2887+
Wallets("wallet"),
2888+
Abigen(Contract(
2889+
name = "TestContract",
2890+
project = "e2e/sway/contracts/contract_test"
2891+
)),
2892+
Deploy(
2893+
name = "contract_instance",
2894+
contract = "TestContract",
2895+
wallet = "wallet",
2896+
random_salt = false,
2897+
),
2898+
);
2899+
2900+
// Test with default tolerance
2901+
let contract_methods = contract_instance.methods();
2902+
let response_default = contract_methods.get_single(5).call().await?;
2903+
assert_eq!(response_default.value, 5);
2904+
2905+
// Test with custom tolerance (lower than default)
2906+
let custom_tolerance = 0.1; // 10% tolerance
2907+
let response_custom = contract_methods
2908+
.get_single(5)
2909+
.with_max_fee_estimation_tolerance(custom_tolerance)
2910+
.call()
2911+
.await?;
2912+
assert_eq!(response_custom.value, 5);
2913+
2914+
// Test with custom tolerance (higher than default)
2915+
let high_tolerance = 1.0; // 100% tolerance
2916+
let response_high = contract_methods
2917+
.get_single(5)
2918+
.with_max_fee_estimation_tolerance(high_tolerance)
2919+
.call()
2920+
.await?;
2921+
assert_eq!(response_high.value, 5);
2922+
2923+
Ok(())
2924+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct CallHandler<A, C, T> {
4949
cached_tx_id: Option<Bytes32>,
5050
variable_output_policy: VariableOutputPolicy,
5151
unresolved_signers: Vec<Arc<dyn Signer + Send + Sync>>,
52+
max_fee_estimation_tolerance: f32,
5253
}
5354

5455
impl<A, C, T> CallHandler<A, C, T> {
@@ -85,6 +86,17 @@ impl<A, C, T> CallHandler<A, C, T> {
8586
self.unresolved_signers.push(Arc::new(signer));
8687
self
8788
}
89+
90+
/// Sets the max fee estimation tolerance for a given transaction.
91+
/// This tolerance is used as a buffer when estimating the maximum fee.
92+
/// Note that this is a builder method, i.e. use it as a chain:
93+
/// ```ignore
94+
/// my_contract_instance.my_method(...).with_max_fee_estimation_tolerance(0.25).call()
95+
/// ```
96+
pub fn with_max_fee_estimation_tolerance(mut self, max_fee_estimation_tolerance: f32) -> Self {
97+
self.max_fee_estimation_tolerance = max_fee_estimation_tolerance;
98+
self
99+
}
88100
}
89101

90102
impl<A, C, T> CallHandler<A, C, T>
@@ -123,6 +135,7 @@ where
123135
consensus_parameters,
124136
asset_inputs,
125137
&self.account,
138+
self.max_fee_estimation_tolerance,
126139
)?;
127140

128141
tb.add_signers(&self.unresolved_signers)?;
@@ -317,6 +330,7 @@ where
317330
cached_tx_id: None,
318331
variable_output_policy: VariableOutputPolicy::default(),
319332
unresolved_signers: vec![],
333+
max_fee_estimation_tolerance: crate::DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
320334
}
321335
}
322336

@@ -405,6 +419,7 @@ where
405419
cached_tx_id: None,
406420
variable_output_policy: VariableOutputPolicy::default(),
407421
unresolved_signers: vec![],
422+
max_fee_estimation_tolerance: crate::DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
408423
}
409424
}
410425

@@ -438,6 +453,7 @@ where
438453
cached_tx_id: None,
439454
variable_output_policy: VariableOutputPolicy::default(),
440455
unresolved_signers: vec![],
456+
max_fee_estimation_tolerance: crate::DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
441457
}
442458
}
443459

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub trait TransactionTuner: sealed::Sealed {
2929
consensus_parameters: &ConsensusParameters,
3030
asset_input: Vec<Input>,
3131
account: &T,
32+
max_fee_estimation_tolerance: f32,
3233
) -> Result<ScriptTransactionBuilder>;
3334

3435
async fn build_tx<T: Account>(
@@ -51,6 +52,7 @@ impl TransactionTuner for ContractCall {
5152
consensus_parameters: &ConsensusParameters,
5253
asset_input: Vec<Input>,
5354
account: &T,
55+
max_fee_estimation_tolerance: f32,
5456
) -> Result<ScriptTransactionBuilder> {
5557
transaction_builder_from_contract_calls(
5658
std::slice::from_ref(self),
@@ -59,6 +61,7 @@ impl TransactionTuner for ContractCall {
5961
consensus_parameters,
6062
asset_input,
6163
account,
64+
max_fee_estimation_tolerance,
6265
)
6366
}
6467

@@ -84,6 +87,7 @@ impl TransactionTuner for ScriptCall {
8487
_: &ConsensusParameters,
8588
_: Vec<Input>,
8689
_account: &T,
90+
max_fee_estimation_tolerance: f32,
8791
) -> Result<ScriptTransactionBuilder> {
8892
let (inputs, outputs) = self.prepare_inputs_outputs()?;
8993

@@ -95,7 +99,7 @@ impl TransactionTuner for ScriptCall {
9599
.with_inputs(inputs)
96100
.with_outputs(outputs)
97101
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
98-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
102+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance))
99103
}
100104

101105
async fn build_tx<T: Account>(
@@ -128,6 +132,7 @@ impl TransactionTuner for Vec<ContractCall> {
128132
consensus_parameters: &ConsensusParameters,
129133
asset_input: Vec<Input>,
130134
account: &T,
135+
max_fee_estimation_tolerance: f32,
131136
) -> Result<ScriptTransactionBuilder> {
132137
validate_contract_calls(self)?;
133138

@@ -138,6 +143,7 @@ impl TransactionTuner for Vec<ContractCall> {
138143
consensus_parameters,
139144
asset_input,
140145
account,
146+
max_fee_estimation_tolerance,
141147
)
142148
}
143149

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(crate) fn transaction_builder_from_contract_calls(
3737
consensus_parameters: &ConsensusParameters,
3838
asset_inputs: Vec<Input>,
3939
account: &impl Account,
40+
max_fee_estimation_tolerance: f32,
4041
) -> Result<ScriptTransactionBuilder> {
4142
let calls_instructions_len = compute_calls_instructions_len(calls);
4243
let data_offset = call_script_data_offset(consensus_parameters, calls_instructions_len)?;
@@ -63,7 +64,7 @@ pub(crate) fn transaction_builder_from_contract_calls(
6364
.with_inputs(inputs)
6465
.with_outputs(outputs)
6566
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
66-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
67+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance))
6768
}
6869

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

packages/fuels-programs/src/executable.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ 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_tolerance(account, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE).await
172+
}
173+
174+
/// If not previously uploaded, uploads a blob containing the original executable code minus the data section,
175+
/// using the specified max fee estimation tolerance.
176+
pub async fn upload_blob_with_tolerance(
177+
&self,
178+
account: impl fuels_accounts::Account,
179+
max_fee_estimation_tolerance: f32,
170180
) -> Result<Option<TxResponse>> {
171181
let blob = self.blob();
172182
let provider = account.try_provider()?;
@@ -178,7 +188,7 @@ impl Executable<Loader> {
178188

179189
let mut tb = BlobTransactionBuilder::default()
180190
.with_blob(self.blob())
181-
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE);
191+
.with_max_fee_estimation_tolerance(max_fee_estimation_tolerance);
182192

183193
account
184194
.adjust_for_fee(&mut tb, 0)

0 commit comments

Comments
 (0)