diff --git a/main/src/cache.rs b/main/src/cache.rs index b2798bd..d1a7694 100644 --- a/main/src/cache.rs +++ b/main/src/cache.rs @@ -11,9 +11,8 @@ use eyre::{bail, Result}; use CacheManager::CacheManagerErrors; use crate::constants::ARB_WASM_CACHE_ADDRESS; -use crate::deploy::gwei_to_wei; use crate::macros::greyln; -use crate::{CacheBidConfig, CacheStatusConfig, CacheSuggestionsConfig}; +use crate::{CacheBidConfig, CacheStatusConfig, CacheSuggestionsConfig, GasFeeConfig}; sol! { #[sol(rpc)] @@ -178,8 +177,9 @@ pub async fn place_bid(cfg: &CacheBidConfig) -> Result<()> { let cache_manager = CacheManager::new(cache_manager_addr, provider.clone()); let addr = cfg.address.to_fixed_bytes().into(); let mut place_bid_call = cache_manager.placeBid(addr).value(U256::from(cfg.bid)); - if let Some(max_fee) = cfg.max_fee_per_gas_gwei { - place_bid_call = place_bid_call.max_fee_per_gas(gwei_to_wei(max_fee)?); + if let Some(max_fee) = cfg.get_max_fee_per_gas_wei()? { + place_bid_call = place_bid_call.max_fee_per_gas(max_fee); + place_bid_call = place_bid_call.max_priority_fee_per_gas(0); }; greyln!("Checking if contract can be cached..."); diff --git a/main/src/deploy/deployer.rs b/main/src/deploy/deployer.rs index baa1825..55243e0 100644 --- a/main/src/deploy/deployer.rs +++ b/main/src/deploy/deployer.rs @@ -4,6 +4,7 @@ use super::SignerClient; use crate::{ check::ContractCheck, + deploy::calculate_fee_per_gas, macros::*, util::color::{Color, DebugColor, GREY}, DeployConfig, @@ -136,18 +137,23 @@ pub async fn deploy( .estimate_gas(&TypedTransaction::Eip1559(tx.clone()), None) .await .wrap_err("deployment failed during gas estimation")?; + + let gas_price = client.get_gas_price().await?; + if cfg.check_config.common_cfg.verbose || cfg.estimate_gas { - super::print_gas_estimate("deployer deploy, activate, and init", client, gas).await?; + super::print_gas_estimate("deployer deploy, activate, and init", gas, gas_price).await?; } if cfg.estimate_gas { return Ok(()); } + let fee_per_gas = calculate_fee_per_gas(&cfg.check_config.common_cfg, gas_price)?; + let receipt = super::run_tx( "deploy_activate_init", tx, Some(gas), - cfg.check_config.common_cfg.max_fee_per_gas_gwei, + fee_per_gas, client, cfg.check_config.common_cfg.verbose, ) diff --git a/main/src/deploy/mod.rs b/main/src/deploy/mod.rs index 11cd9e6..c1441e1 100644 --- a/main/src/deploy/mod.rs +++ b/main/src/deploy/mod.rs @@ -11,7 +11,7 @@ use crate::{ color::{Color, DebugColor}, sys, }, - DeployConfig, + DeployConfig, GasFeeConfig, }; use alloy_primitives::{Address, U256 as AU256}; use alloy_sol_macro::sol; @@ -137,19 +137,23 @@ impl DeployConfig { .estimate_gas(&TypedTransaction::Eip1559(tx.clone()), None) .await?; + let gas_price = client.get_gas_price().await?; + if self.check_config.common_cfg.verbose || self.estimate_gas { - print_gas_estimate("deployment", client, gas).await?; + print_gas_estimate("deployment", gas, gas_price).await?; } if self.estimate_gas { let nonce = client.get_transaction_count(sender, None).await?; return Ok(ethers::utils::get_contract_address(sender, nonce)); } + let fee_per_gas = calculate_fee_per_gas(&self.check_config.common_cfg, gas_price)?; + let receipt = run_tx( "deploy", tx, Some(gas), - self.check_config.common_cfg.max_fee_per_gas_gwei, + fee_per_gas, client, self.check_config.common_cfg.verbose, ) @@ -198,15 +202,19 @@ impl DeployConfig { .await .map_err(|e| eyre!("did not estimate correctly: {e}"))?; + let gas_price = client.get_gas_price().await?; + if self.check_config.common_cfg.verbose || self.estimate_gas { greyln!("activation gas estimate: {}", format_gas(gas)); } + let fee_per_gas = calculate_fee_per_gas(&self.check_config.common_cfg, gas_price)?; + let receipt = run_tx( "activate", tx, Some(gas), - self.check_config.common_cfg.max_fee_per_gas_gwei, + fee_per_gas, client, self.check_config.common_cfg.verbose, ) @@ -224,8 +232,7 @@ impl DeployConfig { } } -pub async fn print_gas_estimate(name: &str, client: &SignerClient, gas: U256) -> Result<()> { - let gas_price = client.get_gas_price().await?; +pub async fn print_gas_estimate(name: &str, gas: U256, gas_price: U256) -> Result<()> { greyln!("estimates"); greyln!("{} tx gas: {}", name, gas.debug_lavender()); greyln!( @@ -256,7 +263,7 @@ pub async fn run_tx( name: &str, tx: Eip1559TransactionRequest, gas: Option, - max_fee_per_gas_gwei: Option, + max_fee_per_gas_wei: u128, client: &SignerClient, verbose: bool, ) -> Result { @@ -264,9 +271,10 @@ pub async fn run_tx( if let Some(gas) = gas { tx.gas = Some(gas); } - if let Some(max_fee) = max_fee_per_gas_gwei { - tx.max_fee_per_gas = Some(U256::from(gwei_to_wei(max_fee)?)); - } + + tx.max_fee_per_gas = Some(U256::from(max_fee_per_gas_wei)); + tx.max_priority_fee_per_gas = Some(U256::from(0)); + let tx = TypedTransaction::Eip1559(tx); let tx = client.send_transaction(tx, None).await?; let tx_hash = tx.tx_hash(); @@ -329,10 +337,10 @@ pub fn format_gas(gas: U256) -> String { } } -pub fn gwei_to_wei(gwei: u128) -> Result { - let wei_per_gwei: u128 = 10u128.pow(9); - match gwei.checked_mul(wei_per_gwei) { - Some(wei) => Ok(wei), - None => bail!("overflow occurred while converting gwei to wei"), - } +pub fn calculate_fee_per_gas(config: &T, gas_price: U256) -> Result { + let fee_per_gas = match config.get_max_fee_per_gas_wei()? { + Some(wei) => wei, + None => gas_price.try_into().unwrap(), + }; + Ok(fee_per_gas) } diff --git a/main/src/main.rs b/main/src/main.rs index 0d9bc8f..9c14ed9 100644 --- a/main/src/main.rs +++ b/main/src/main.rs @@ -125,7 +125,7 @@ struct CommonConfig { source_files_for_project_hash: Vec, #[arg(long)] /// Optional max fee per gas in gwei units. - max_fee_per_gas_gwei: Option, + max_fee_per_gas_gwei: Option, /// Specifies the features to use when building the Stylus binary. #[arg(long)] features: Option, @@ -161,7 +161,7 @@ pub struct CacheBidConfig { bid: u64, #[arg(long)] /// Optional max fee per gas in gwei units. - max_fee_per_gas_gwei: Option, + max_fee_per_gas_gwei: Option, } #[derive(Args, Clone, Debug)] @@ -395,6 +395,60 @@ impl fmt::Display for CommonConfig { } } +pub trait GasFeeConfig { + fn get_max_fee_per_gas_wei(&self) -> Result>; + fn get_fee_str(&self) -> &Option; +} + +fn convert_gwei_to_wei(fee_str: &str) -> Result { + let gwei = match fee_str.parse::() { + Ok(fee) if fee >= 0.0 => fee, + Ok(_) => bail!("Max fee per gas must be non-negative"), + Err(_) => bail!("Invalid max fee per gas value: {}", fee_str), + }; + + if !gwei.is_finite() { + bail!("Invalid gwei value: must be finite"); + } + + let wei = gwei * 1e9; + if !wei.is_finite() { + bail!("Overflow occurred in floating point multiplication of --max-fee-per-gas-gwei converting"); + } + + if wei < 0.0 || wei >= u128::MAX as f64 { + bail!("Result outside valid range for wei"); + } + + Ok(wei as u128) +} + +impl GasFeeConfig for CommonConfig { + fn get_fee_str(&self) -> &Option { + &self.max_fee_per_gas_gwei + } + + fn get_max_fee_per_gas_wei(&self) -> Result> { + match self.get_fee_str() { + Some(fee_str) => Ok(Some(convert_gwei_to_wei(fee_str)?)), + None => Ok(None), + } + } +} + +impl GasFeeConfig for CacheBidConfig { + fn get_fee_str(&self) -> &Option { + &self.max_fee_per_gas_gwei + } + + fn get_max_fee_per_gas_wei(&self) -> Result> { + match self.get_fee_str() { + Some(fee_str) => Ok(Some(convert_gwei_to_wei(fee_str)?)), + None => Ok(None), + } + } +} + impl fmt::Display for CheckConfig { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(