Skip to content

Commit

Permalink
Merge branch 'master' into hal3e/total-fee-from-status
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e authored Jan 16, 2025
2 parents ed3fd56 + 5ad05ab commit a8c9754
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 27 deletions.
8 changes: 6 additions & 2 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use fuel_tx::{
consensus_parameters::{ConsensusParametersV1, FeeParametersV1},
ConsensusParameters, FeeParameters,
ConsensusParameters, FeeParameters, Output,
};
use fuels::{
core::codec::{calldata, encode_fn_selector, DecoderConfig, EncoderConfig},
Expand Down Expand Up @@ -1707,12 +1707,16 @@ async fn contract_custom_call_no_signatures_strategy() -> Result<()> {

let mut tb = call_handler.transaction_builder().await?;

let base_asset_id = *provider.consensus_parameters().await?.base_asset_id();

let amount = 10;
let consensus_parameters = provider.consensus_parameters().await?;
let new_base_inputs = wallet
.get_asset_inputs_for_amount(*consensus_parameters.base_asset_id(), amount, None)
.get_asset_inputs_for_amount(base_asset_id, amount, None)
.await?;
tb.inputs_mut().extend(new_base_inputs);
tb.outputs_mut()
.push(Output::change(wallet.address().into(), 0, base_asset_id));

// ANCHOR: tb_no_signatures_strategy
let mut tx = tb
Expand Down
1 change: 1 addition & 0 deletions e2e/tests/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ async fn predicate_adjust_fee_persists_message_w_data() -> Result<()> {
TxPolicies::default().with_tip(1),
);
predicate.adjust_for_fee(&mut tb, 1000).await?;

let tx = tb.build(&provider).await?;

assert_eq!(tx.inputs().len(), 2);
Expand Down
3 changes: 3 additions & 0 deletions e2e/tests/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use fuel_tx::Output;
use fuels::{
core::{
codec::{DecoderConfig, EncoderConfig},
Expand Down Expand Up @@ -132,8 +133,10 @@ async fn test_output_variable_estimation() -> Result<()> {
let inputs = wallet
.get_asset_inputs_for_amount(asset_id, amount, None)
.await?;
let output = Output::change(wallet.address().into(), 0, asset_id);
let _ = script_call
.with_inputs(inputs)
.with_outputs(vec![output])
.with_variable_output_policy(VariableOutputPolicy::EstimateMinimum)
.call()
.await?;
Expand Down
19 changes: 9 additions & 10 deletions packages/fuels-accounts/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use fuels_core::types::{

use crate::{
accounts_utils::{
adjust_inputs_outputs, available_base_assets_and_amount, calculate_missing_base_amount,
add_base_change_if_needed, available_base_assets_and_amount, calculate_missing_base_amount,
extract_message_nonce, split_into_utxo_ids_and_nonces,
},
provider::{Provider, ResourceFilter},
Expand Down Expand Up @@ -117,7 +117,8 @@ pub trait ViewOnlyAccount: std::fmt::Debug + Send + Sync + Clone {
excluded_coins: Option<Vec<CoinTypeId>>,
) -> Result<Vec<Input>>;

/// Add base asset inputs to the transaction to cover the estimated fee.
/// Add base asset inputs to the transaction to cover the estimated fee
/// and add a change output for the base asset if needed.
/// Requires contract inputs to be at the start of the transactions inputs vec
/// so that their indexes are retained
async fn adjust_for_fee<Tb: TransactionBuilder + Sync>(
Expand All @@ -141,14 +142,11 @@ pub trait ViewOnlyAccount: std::fmt::Debug + Send + Sync + Clone {
)
.await?;

adjust_inputs_outputs(
tb,
new_base_inputs,
self.address(),
consensus_parameters.base_asset_id(),
);
tb.inputs_mut().extend(new_base_inputs);
};

add_base_change_if_needed(tb, self.address(), consensus_parameters.base_asset_id());

Ok(())
}
}
Expand Down Expand Up @@ -407,10 +405,11 @@ mod tests {
1,
Default::default(),
);
let change = Output::change(wallet.address().into(), 0, Default::default());

ScriptTransactionBuilder::prepare_transfer(
vec![input_coin],
vec![output_coin],
vec![output_coin, change],
Default::default(),
)
};
Expand All @@ -433,7 +432,7 @@ mod tests {
assert_eq!(signature, tx_signature);

// Check if the signature is what we expect it to be
assert_eq!(signature, Signature::from_str("8afd30de7039faa07aac1cf2676970a77dc8ef3f779b44c1510ad7bf58ea56f43727b23142bd7252b79ae2c832e073927f84f6b0857fedf2f6d86e9535e48fd0")?);
assert_eq!(signature, Signature::from_str("faa616776a1c336ef6257f7cb0cb5cd932180e2d15faba5f17481dae1cbcaf314d94617bd900216a6680bccb1ea62438e4ca93b0d5733d33788ef9d79cc24e9f")?);

// Recover the address that signed the transaction
let recovered_address = signature.recover(&message)?;
Expand Down
5 changes: 1 addition & 4 deletions packages/fuels-accounts/src/accounts_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,11 @@ fn is_consuming_utxos(tb: &impl TransactionBuilder) -> bool {
.any(|input| !matches!(input, Input::Contract { .. }))
}

pub fn adjust_inputs_outputs(
pub fn add_base_change_if_needed(
tb: &mut impl TransactionBuilder,
new_base_inputs: impl IntoIterator<Item = Input>,
address: &Bech32Address,
base_asset_id: &AssetId,
) {
tb.inputs_mut().extend(new_base_inputs);

let is_base_change_present = tb.outputs().iter().any(|output| {
matches!(output , Output::Change { asset_id , .. }
if asset_id == base_asset_id)
Expand Down
Loading

0 comments on commit a8c9754

Please sign in to comment.