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

Fix Up Estimate Gas #80

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
16 changes: 16 additions & 0 deletions check/src/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ pub async fn activate_contract(cfg: &ActivateConfig) -> Result<()> {
.value(data_fee)
.data(data);
let tx = TypedTransaction::Eip1559(tx);
if cfg.estimate_gas {
let gas = client.estimate_gas(&tx, None).await?;
let gas_price = client.get_gas_price().await?;
greyln!("estimates");
greyln!("activation tx gas: {}", gas.debug_lavender());
greyln!(
"gas price: {} gwei",
format_units(gas_price, "gwei")?.debug_lavender()
);
let total_cost = gas_price.checked_mul(gas).unwrap_or_default();
let eth_estimate = format_units(total_cost, "ether")?;
greyln!(
"activation tx total cost: {} ETH",
eth_estimate.debug_lavender()
);
}
let tx = client.send_transaction(tx, None).await?;
match tx.await? {
Some(receipt) => {
Expand Down
4 changes: 2 additions & 2 deletions check/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn check(cfg: &CheckConfig) -> Result<ContractCheck> {
let address = cfg.contract_address.unwrap_or(H160::random());
let fee = check_activate(code.clone().into(), address, &provider).await?;
let visual_fee = format_data_fee(fee).unwrap_or("???".red());
greyln!("wasm data fee: {visual_fee}");
greyln!("wasm data fee: {visual_fee} ETH");
Ok(ContractCheck::Ready { code, fee })
}

Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn format_file_size(len: usize, mid: u64, max: u64) -> String {
fn format_data_fee(fee: U256) -> Result<String> {
let fee: u64 = (fee / U256::from(1e9)).try_into()?;
let fee: f64 = fee as f64 / 1e9;
let text = format!("Ξ{fee:.6}");
let text = format!("{fee:.6}");
Ok(if fee <= 5e14 {
text.mint()
} else if fee <= 5e15 {
Expand Down
22 changes: 18 additions & 4 deletions check/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use cargo_stylus_util::{
color::{Color, DebugColor},
sys,
};
use ethers::core::utils::format_units;
use ethers::{
core::k256::ecdsa::SigningKey,
middleware::SignerMiddleware,
Expand Down Expand Up @@ -86,6 +87,10 @@ pub async fn deploy(cfg: DeployConfig) -> Result<()> {
.deploy_contract(contract.code(), sender, &client)
.await?;

if cfg.estimate_gas {
return Ok(());
}

match contract {
ContractCheck::Ready { .. } => {
cfg.activate(sender, contract_addr, data_fee, &client)
Expand Down Expand Up @@ -115,7 +120,19 @@ impl DeployConfig {
.await?;

if self.check_config.common_cfg.verbose || self.estimate_gas {
greyln!("deploy gas estimate: {}", format_gas(gas));
let gas_price = client.get_gas_price().await?;
greyln!("estimates");
greyln!("deployment tx gas: {}", gas.debug_lavender());
greyln!(
"gas price: {} gwei",
format_units(gas_price, "gwei")?.debug_lavender()
);
let total_cost = gas_price.checked_mul(gas).unwrap_or_default();
let eth_estimate = format_units(total_cost, "ether")?;
greyln!(
"deployment tx total cost: {} ETH",
eth_estimate.debug_lavender()
);
}
if self.estimate_gas {
let nonce = client.get_transaction_count(sender, None).await?;
Expand Down Expand Up @@ -184,9 +201,6 @@ https://docs.arbitrum.io/stylus/concepts/stylus-cache-manager"#,
if self.check_config.common_cfg.verbose || self.estimate_gas {
greyln!("activation gas estimate: {}", format_gas(gas));
}
if self.estimate_gas {
return Ok(());
}

let receipt = run_tx(
"activate",
Expand Down
3 changes: 3 additions & 0 deletions check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub struct ActivateConfig {
/// Percent to bump the estimated activation data fee by. Default of 20%
#[arg(long, default_value = "20")]
data_fee_bump_percent: u64,
/// Whether or not to just estimate gas without sending a tx.
#[arg(long)]
estimate_gas: bool,
}

#[derive(Args, Clone, Debug)]
Expand Down
Loading