Skip to content

Commit

Permalink
[aptos-cli] Add flag to turn on max gas estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Sep 8, 2022
1 parent 399ed72 commit 93a0ee2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,18 @@ pub struct GasOptions {
pub max_gas: Option<u64>,
}

const DEFAULT_MAX_GAS: u64 = 50000;

/// Common options for interacting with an account for a validator
#[derive(Debug, Default, Parser)]
pub struct TransactionOptions {
/// Estimate maximum gas via simulation
///
/// This will simulate the transaction, and use the simulated actual amount of gas
/// to be used as the max gas. If disabled, and no max gas provided, 50000 will be used
/// as the max gas
#[clap(long)]
pub(crate) estimate_max_gas: bool,
#[clap(flatten)]
pub(crate) private_key_options: PrivateKeyInputOptions,
#[clap(flatten)]
Expand Down Expand Up @@ -1145,7 +1154,7 @@ impl TransactionOptions {

let max_gas = if let Some(max_gas) = self.gas_options.max_gas {
max_gas
} else {
} else if self.estimate_max_gas {
let simulated_txn = self
.simulate_transaction(payload.clone(), Some(gas_unit_price), amount_transfer)
.await?;
Expand All @@ -1156,6 +1165,9 @@ impl TransactionOptions {
)));
}
simulated_txn.info.gas_used.0
} else {
// TODO: Remove once simulation is stabilized and can handle all cases
DEFAULT_MAX_GAS
};

if ask_to_confirm_price {
Expand Down
1 change: 1 addition & 0 deletions crates/aptos/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ impl CliTestFramework {
rest_options: self.rest_options(),
gas_options: gas_options.unwrap_or_default(),
prompt_options: PromptOptions::yes(),
estimate_max_gas: true,
..Default::default()
}
}
Expand Down

0 comments on commit 93a0ee2

Please sign in to comment.