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

feat!: use total_gas and total_fee from tx status #1574

Merged
merged 26 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a486657
Work in progress
hal3e May 31, 2024
0d7c5bc
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Jun 10, 2024
ee7d7f7
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Jun 12, 2024
7d42695
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Jan 14, 2025
038d8b2
feat: use `total_gas` and `total_fee` from tx status
hal3e Jan 14, 2025
ed3fd56
update total_fee calc
hal3e Jan 15, 2025
a8c9754
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Jan 16, 2025
ffb3d11
resolve all TODOs
hal3e Jan 16, 2025
173b5e9
merge master
hal3e Jan 27, 2025
3a2ec67
merge master
hal3e Feb 3, 2025
441a85d
Work in progress
hal3e Feb 5, 2025
23b6480
update types
hal3e Feb 5, 2025
f2b4506
change var names
hal3e Feb 5, 2025
d812f9d
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 5, 2025
1c09a66
refactor
hal3e Feb 6, 2025
a77ea31
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 10, 2025
f9f6f7b
pr comments
hal3e Feb 11, 2025
fa755cb
update docs
hal3e Feb 11, 2025
4389fc0
Update docs/src/calling-contracts/cost-estimation.md
hal3e Feb 14, 2025
727a7f8
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 14, 2025
13f9638
pr comments
hal3e Feb 14, 2025
b0816cf
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 17, 2025
e9507db
merge master
hal3e Feb 17, 2025
1076a13
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 18, 2025
b88e585
Merge branch 'master' into hal3e/total-fee-from-status
hal3e Feb 24, 2025
b586232
pr comments
hal3e Feb 24, 2025
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
2 changes: 2 additions & 0 deletions docs/src/calling-contracts/cost-estimation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ With the function `estimate_transaction_cost(tolerance: Option<f64>, block_horiz
{{#include ../../../packages/fuels-accounts/src/provider.rs:transaction_cost}}
```

> **Note** `script_gas` reefers to the part of the gas spent on the script execution.

Below are examples that show how to get the estimated transaction cost from single and multi call transactions.

```rust,ignore
Expand Down
9 changes: 6 additions & 3 deletions e2e/tests/configurables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ async fn contract_default_configurables() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet.clone());

Expand Down Expand Up @@ -130,7 +131,8 @@ async fn contract_configurables() -> Result<()> {
LoadConfiguration::default().with_configurables(configurables),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet.clone());
// ANCHOR_END: contract_configurables
Expand Down Expand Up @@ -198,7 +200,8 @@ async fn contract_manual_configurables() -> Result<()> {
)?
.with_configurables(configurables)
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet.clone());

Expand Down
73 changes: 48 additions & 25 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ async fn test_contract_call_fee_estimation() -> Result<()> {
let gas_limit = 800;
let tolerance = Some(0.2);
let block_horizon = Some(1);
let expected_gas_used = 960;
let expected_script_gas = 800;
let expected_total_gas = 8463;
let expected_metered_bytes_size = 824;

let estimated_transaction_cost = contract_instance
Expand All @@ -302,7 +303,8 @@ async fn test_contract_call_fee_estimation() -> Result<()> {
.estimate_transaction_cost(tolerance, block_horizon)
.await?;

assert_eq!(estimated_transaction_cost.gas_used, expected_gas_used);
assert_eq!(estimated_transaction_cost.script_gas, expected_script_gas);
assert_eq!(estimated_transaction_cost.total_gas, expected_total_gas);
assert_eq!(
estimated_transaction_cost.metered_bytes_size,
expected_metered_bytes_size
Expand Down Expand Up @@ -331,19 +333,20 @@ async fn contract_call_has_same_estimated_and_used_gas() -> Result<()> {
let tolerance = Some(0.0);
let block_horizon = Some(1);

let estimated_gas_used = contract_methods
let estimated_total_gas = contract_methods
.initialize_counter(42)
.estimate_transaction_cost(tolerance, block_horizon)
.await?
.gas_used;
.total_gas;

let gas_used = contract_methods
.initialize_counter(42)
.call()
.await?
.gas_used;
.tx_status
.total_gas;

assert_eq!(estimated_gas_used, gas_used);
assert_eq!(estimated_total_gas, gas_used);
Ok(())
}

Expand Down Expand Up @@ -373,14 +376,19 @@ async fn mult_call_has_same_estimated_and_used_gas() -> Result<()> {

let tolerance = Some(0.0);
let block_horizon = Some(1);
let estimated_gas_used = multi_call_handler
let estimated_total_gas = multi_call_handler
.estimate_transaction_cost(tolerance, block_horizon)
.await?
.gas_used;
.total_gas;

let gas_used = multi_call_handler.call::<(u64, [u64; 2])>().await?.gas_used;
let total_gas = multi_call_handler
.call::<(u64, [u64; 2])>()
.await?
.tx_status
.total_gas;

assert_eq!(estimated_total_gas, total_gas);

assert_eq!(estimated_gas_used, gas_used);
Ok(())
}

Expand Down Expand Up @@ -703,7 +711,8 @@ async fn setup_output_variable_estimation_test() -> Result<(
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallets[0], TxPolicies::default())
.await?;
.await?
.contract_id;

let mint_asset_id = contract_id.asset_id(&Bits256::zeroed());
let addresses = wallets
Expand Down Expand Up @@ -1754,7 +1763,7 @@ async fn contract_custom_call_no_signatures_strategy() -> Result<()> {

let tx_status = provider.tx_status(&tx_id).await?;

let response = call_handler.get_response_from(tx_status)?;
let response = call_handler.get_response(tx_status)?;

assert_eq!(counter, response.value);

Expand All @@ -1775,7 +1784,8 @@ async fn contract_encoder_config_is_applied() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let instance = TestContract::new(contract_id.clone(), wallet.clone());

Expand Down Expand Up @@ -1984,7 +1994,8 @@ async fn simulations_can_be_made_without_coins() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let provider = wallet.provider().cloned();
let no_funds_wallet = WalletUnlocked::new_random(provider);
Expand Down Expand Up @@ -2015,7 +2026,8 @@ async fn simulations_can_be_made_without_coins_multicall() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let provider = wallet.provider().cloned();
let no_funds_wallet = WalletUnlocked::new_random(provider);
Expand Down Expand Up @@ -2074,7 +2086,8 @@ async fn contract_call_with_non_zero_base_asset_id_and_tip() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet.clone());

Expand Down Expand Up @@ -2250,7 +2263,8 @@ async fn blob_contract_deployment() -> Result<()> {
let contract_id = contract
.convert_to_loader(100_000)?
.deploy_if_not_exists(&wallets[0], TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallets[0].clone());

Expand All @@ -2277,7 +2291,8 @@ async fn regular_contract_can_be_deployed() -> Result<()> {
// when
let contract_id = Contract::load_from(contract_binary, LoadConfiguration::default())?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

// then
let contract_instance = MyContract::new(contract_id, wallet);
Expand Down Expand Up @@ -2309,7 +2324,8 @@ async fn unuploaded_loader_can_be_deployed_directly() -> Result<()> {
let contract_id = Contract::load_from(contract_binary, LoadConfiguration::default())?
.convert_to_loader(1024)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

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

Expand Down Expand Up @@ -2342,13 +2358,15 @@ async fn unuploaded_loader_can_upload_blobs_separately_then_deploy() -> Result<(
// if this were an example for the user we'd just call `deploy` on the contract above
// this way we are testing that the blobs were really deployed above, otherwise the following
// would fail

let contract_id = Contract::loader_from_blob_ids(
blob_ids.to_vec(),
contract.salt(),
contract.storage_slots().to_vec(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet);
let response = contract_instance.methods().something().call().await?.value;
Expand Down Expand Up @@ -2380,7 +2398,8 @@ async fn loader_blob_already_uploaded_not_an_issue() -> Result<()> {
// this will try to upload the blobs but skip upon encountering an error
let contract_id = contract
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id, wallet);
let response = contract_instance.methods().something().call().await?.value;
Expand Down Expand Up @@ -2411,13 +2430,15 @@ async fn loader_works_via_proxy() -> Result<()> {
let contract_id = contract
.convert_to_loader(100)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

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

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

let proxy = MyProxy::new(proxy_id, wallet.clone());
proxy
Expand Down Expand Up @@ -2462,7 +2483,8 @@ async fn loader_storage_works_via_proxy() -> Result<()> {
let contract_id = contract
.convert_to_loader(100)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_binary = "sway/contracts/proxy/out/release/proxy.bin";
let proxy_contract = Contract::load_from(contract_binary, LoadConfiguration::default())?;
Expand All @@ -2472,7 +2494,8 @@ async fn loader_storage_works_via_proxy() -> Result<()> {
let proxy_id = proxy_contract
.with_storage_slots(combined_storage_slots)
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let proxy = MyProxy::new(proxy_id, wallet.clone());
proxy
Expand Down
16 changes: 11 additions & 5 deletions e2e/tests/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ async fn test_multi_call_contract_with_contract_logs() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id.clone(), wallet.clone());

Expand Down Expand Up @@ -742,7 +743,8 @@ async fn test_contract_with_contract_logs() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id.clone(), wallet.clone());

Expand Down Expand Up @@ -828,6 +830,7 @@ async fn test_script_logs_with_contract_logs() -> Result<()> {

{
let num_contract_logs = response
.tx_status
.receipts
.iter()
.filter(|receipt| matches!(receipt, Receipt::LogData { id, .. } | Receipt::Log { id, .. } if *id == contract_id))
Expand Down Expand Up @@ -1025,7 +1028,8 @@ async fn test_contract_require_from_contract() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let contract_instance = MyContract::new(contract_id.clone(), wallet.clone());

Expand Down Expand Up @@ -1079,7 +1083,8 @@ async fn test_multi_call_contract_require_from_contract() -> Result<()> {
LoadConfiguration::default(),
)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;

let lib_contract_instance = MyContract::new(contract_id.clone(), wallet.clone());

Expand Down Expand Up @@ -1169,7 +1174,8 @@ async fn test_loader_script_require_from_loader_contract() -> Result<()> {
let contract_id = contract
.convert_to_loader(100_000)?
.deploy_if_not_exists(&wallet, TxPolicies::default())
.await?;
.await?
.contract_id;
let contract_instance = MyContract::new(contract_id, wallet);

let mut script_instance = script_instance;
Expand Down
Loading
Loading