Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: return error when adjusting for fee #1606

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,3 +2510,34 @@ async fn loader_storage_works_via_proxy() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn adjust_for_fee_errors() -> Result<()> {
setup_program_test!(
Wallets("wallet"),
Abigen(Contract(
name = "MyContract",
project = "e2e/sway/contracts/contract_test"
)),
);

let contract_binary = "sway/contracts/contract_test/out/release/contract_test.bin";

let contract_id = Contract::load_from(contract_binary, LoadConfiguration::default())?
.deploy_if_not_exists(&wallet, TxPolicies::default().with_tip(10_000_000_000_000))
.await?;

// then
let contract_instance = MyContract::new(contract_id, wallet);

let response = contract_instance
.methods()
.read_counter()
.call()
.await?
.value;

assert_eq!(response, 0);

Ok(())
}
18 changes: 12 additions & 6 deletions packages/fuels-accounts/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use fuels_core::types::{
coin::Coin,
coin_type::CoinType,
coin_type_id::CoinTypeId,
errors::Result,
errors::{Context, Result},
input::Input,
message::Message,
transaction::{Transaction, TxPolicies},
Expand Down Expand Up @@ -141,8 +141,9 @@ pub trait ViewOnlyAccount: std::fmt::Debug + Send + Sync + Clone {
Some(base_assets),
)
.await
// if there query fails do nothing
.unwrap_or_default();
.with_context(|| {
format!("failed to get base asset inputs with amount: `{missing_base_amount}`")
})?;

tb.inputs_mut().extend(new_base_inputs);
};
Expand Down Expand Up @@ -189,7 +190,8 @@ pub trait Account: ViewOnlyAccount {
0
};
self.adjust_for_fee(&mut tx_builder, used_base_amount)
.await?;
.await
.context("failed to adjust for fee")?;

let tx = tx_builder.build(provider).await?;
let tx_id = tx.id(consensus_parameters.chain_id());
Expand Down Expand Up @@ -251,7 +253,9 @@ pub trait Account: ViewOnlyAccount {
);

self.add_witnesses(&mut tb)?;
self.adjust_for_fee(&mut tb, balance).await?;
self.adjust_for_fee(&mut tb, balance)
.await
.context("failed to adjust for fee")?;

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

Expand Down Expand Up @@ -289,7 +293,9 @@ pub trait Account: ViewOnlyAccount {
);

self.add_witnesses(&mut tb)?;
self.adjust_for_fee(&mut tb, amount).await?;
self.adjust_for_fee(&mut tb, amount)
.await
.context("failed to adjust for fee")?;

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

Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ impl Provider {
.message_proof(
tx_id,
nonce,
commit_block_id.map(Into::into),
commit_block_id,
commit_block_height.map(Into::into),
)
.await
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fuels_accounts::Account;
use fuels_core::types::{
errors::{error, Result},
errors::{error, Context, Result},
transaction::{ScriptTransaction, TxPolicies},
transaction_builders::{
BuildableTransaction, ScriptTransactionBuilder, TransactionBuilder, VariableOutputPolicy,
Expand Down Expand Up @@ -97,7 +97,10 @@ impl TransactionTuner for ScriptCall {
.await?;

account.add_witnesses(&mut tb)?;
account.adjust_for_fee(&mut tb, 0).await?;
account
.adjust_for_fee(&mut tb, 0)
.await
.context("failed to adjust for fee")?;

tb.build(account.try_provider()?).await
}
Expand Down
7 changes: 5 additions & 2 deletions packages/fuels-programs/src/calls/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use fuels_core::{
offsets::call_script_data_offset,
types::{
bech32::{Bech32Address, Bech32ContractId},
errors::Result,
errors::{Context, Result},
input::Input,
transaction::{ScriptTransaction, TxPolicies},
transaction_builders::{
Expand Down Expand Up @@ -101,7 +101,10 @@ pub(crate) async fn build_tx_from_contract_calls(
.unwrap_or_default();

account.add_witnesses(&mut tb)?;
account.adjust_for_fee(&mut tb, used_base_amount).await?;
account
.adjust_for_fee(&mut tb, used_base_amount)
.await
.context("failed to adjust for fee")?;

tb.build(account.try_provider()?).await
}
Expand Down
7 changes: 5 additions & 2 deletions packages/fuels-programs/src/contract/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fuels_core::{
constants::WORD_SIZE,
types::{
bech32::Bech32ContractId,
errors::{error, Result},
errors::{error, Context, Result},
transaction::TxPolicies,
transaction_builders::{Blob, BlobId, BlobTransactionBuilder, TransactionBuilder},
},
Expand Down Expand Up @@ -140,7 +140,10 @@ impl Contract<Loader<BlobsNotUploaded>> {
.with_tx_policies(tx_policies)
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE);

account.adjust_for_fee(&mut tb, 0).await?;
account
.adjust_for_fee(&mut tb, 0)
.await
.context("failed to adjust for fee")?;
account.add_witnesses(&mut tb)?;

let tx = tb.build(provider).await?;
Expand Down
7 changes: 5 additions & 2 deletions packages/fuels-programs/src/contract/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fuels_core::{
error,
types::{
bech32::Bech32ContractId,
errors::Result,
errors::{Context, Result},
transaction::TxPolicies,
transaction_builders::{Blob, CreateTransactionBuilder},
},
Expand Down Expand Up @@ -157,7 +157,10 @@ impl Contract<Regular> {
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE);

account.add_witnesses(&mut tb)?;
account.adjust_for_fee(&mut tb, 0).await?;
account
.adjust_for_fee(&mut tb, 0)
.await
.context("failed to adjust for fee")?;

let provider = account.try_provider()?;

Expand Down
8 changes: 5 additions & 3 deletions packages/fuels-programs/src/executable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fuels_core::{
types::{
errors::Result,
errors::{Context, Result},
transaction_builders::{Blob, BlobTransactionBuilder},
},
Configurables,
Expand Down Expand Up @@ -174,8 +174,10 @@ impl Executable<Loader> {
.with_blob(self.blob())
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE);

account.adjust_for_fee(&mut tb, 0).await?;

account
.adjust_for_fee(&mut tb, 0)
.await
.context("failed to adjust for fee")?;
account.add_witnesses(&mut tb)?;

let tx = tb.build(provider).await?;
Expand Down
Loading