From 9275b9b7ecb325a5f11ad252120ea545678f5bca Mon Sep 17 00:00:00 2001 From: Jason-W123 Date: Sun, 9 Feb 2025 20:36:36 +0800 Subject: [PATCH 1/5] fix estimate and command --- main/src/cache.rs | 8 ++--- main/src/deploy/deployer.rs | 20 +++++++++++-- main/src/deploy/mod.rs | 47 ++++++++++++++++++------------ main/src/main.rs | 58 +++++++++++++++++++++++++++++++++++-- 4 files changed, 106 insertions(+), 27 deletions(-) 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..c7ff022 100644 --- a/main/src/deploy/deployer.rs +++ b/main/src/deploy/deployer.rs @@ -6,7 +6,7 @@ use crate::{ check::ContractCheck, macros::*, util::color::{Color, DebugColor, GREY}, - DeployConfig, + DeployConfig, GasFeeConfig, }; use alloy_dyn_abi::{DynSolValue, JsonAbiExt, Specifier}; use alloy_json_abi::{Constructor, StateMutability}; @@ -136,18 +136,32 @@ 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", + client, + gas, + gas_price, + ) + .await?; } if cfg.estimate_gas { return Ok(()); } + let fee_per_gas: u128 = match cfg.check_config.common_cfg.get_max_fee_per_gas_wei()? { + Some(wei) => wei, + None => gas_price.try_into().unwrap_or_default(), + }; + 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 f1c1d2e..401fe41 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,26 @@ 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", client, 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: u128 = match self.check_config.common_cfg.get_max_fee_per_gas_wei()? { + Some(wei) => wei, + None => gas_price.try_into().unwrap_or_default(), + }; + 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 +205,22 @@ 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: u128 = match self.check_config.common_cfg.get_max_fee_per_gas_wei()? { + Some(wei) => wei, + None => gas_price.try_into().unwrap_or_default(), + }; + 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 +238,12 @@ 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, + client: &SignerClient, + gas: U256, + gas_price: U256, +) -> Result<()> { greyln!("estimates"); greyln!("{} tx gas: {}", name, gas.debug_lavender()); greyln!( @@ -256,7 +274,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 +282,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(); @@ -328,11 +347,3 @@ pub fn format_gas(gas: U256) -> String { text.pink() } } - -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"), - } -} diff --git a/main/src/main.rs b/main/src/main.rs index 0d9bc8f..dcc28fb 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"); + } + + 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!( From 783efbf767d9d1879f3c609530575321ae596267 Mon Sep 17 00:00:00 2001 From: Jason-W123 Date: Mon, 10 Feb 2025 14:51:02 +0800 Subject: [PATCH 2/5] remove client --- main/src/deploy/deployer.rs | 1 - main/src/deploy/mod.rs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/main/src/deploy/deployer.rs b/main/src/deploy/deployer.rs index c7ff022..527c5fe 100644 --- a/main/src/deploy/deployer.rs +++ b/main/src/deploy/deployer.rs @@ -142,7 +142,6 @@ pub async fn deploy( if cfg.check_config.common_cfg.verbose || cfg.estimate_gas { super::print_gas_estimate( "deployer deploy, activate, and init", - client, gas, gas_price, ) diff --git a/main/src/deploy/mod.rs b/main/src/deploy/mod.rs index 401fe41..f873e66 100644 --- a/main/src/deploy/mod.rs +++ b/main/src/deploy/mod.rs @@ -140,7 +140,7 @@ impl DeployConfig { let gas_price = client.get_gas_price().await?; if self.check_config.common_cfg.verbose || self.estimate_gas { - print_gas_estimate("deployment", client, gas, gas_price).await?; + print_gas_estimate("deployment", gas, gas_price).await?; } if self.estimate_gas { let nonce = client.get_transaction_count(sender, None).await?; @@ -240,7 +240,6 @@ impl DeployConfig { pub async fn print_gas_estimate( name: &str, - client: &SignerClient, gas: U256, gas_price: U256, ) -> Result<()> { From d89c25c9c96a00191acc76d06777d5aa9c13f9b8 Mon Sep 17 00:00:00 2001 From: Jason-W123 Date: Mon, 10 Feb 2025 14:55:32 +0800 Subject: [PATCH 3/5] add comment --- main/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/src/main.rs b/main/src/main.rs index dcc28fb..9c14ed9 100644 --- a/main/src/main.rs +++ b/main/src/main.rs @@ -413,7 +413,7 @@ fn convert_gwei_to_wei(fee_str: &str) -> Result { let wei = gwei * 1e9; if !wei.is_finite() { - bail!("Overflow occurred in floating point multiplication"); + bail!("Overflow occurred in floating point multiplication of --max-fee-per-gas-gwei converting"); } if wei < 0.0 || wei >= u128::MAX as f64 { From f87a3674fcc1940de62bd16f4433037b05105877 Mon Sep 17 00:00:00 2001 From: Jason-W123 Date: Mon, 10 Feb 2025 14:56:27 +0800 Subject: [PATCH 4/5] fmt --- main/src/deploy/deployer.rs | 7 +------ main/src/deploy/mod.rs | 6 +----- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/main/src/deploy/deployer.rs b/main/src/deploy/deployer.rs index 527c5fe..636464d 100644 --- a/main/src/deploy/deployer.rs +++ b/main/src/deploy/deployer.rs @@ -140,12 +140,7 @@ pub async fn deploy( 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", - gas, - gas_price, - ) - .await?; + super::print_gas_estimate("deployer deploy, activate, and init", gas, gas_price).await?; } if cfg.estimate_gas { return Ok(()); diff --git a/main/src/deploy/mod.rs b/main/src/deploy/mod.rs index f873e66..036ef11 100644 --- a/main/src/deploy/mod.rs +++ b/main/src/deploy/mod.rs @@ -238,11 +238,7 @@ impl DeployConfig { } } -pub async fn print_gas_estimate( - name: &str, - gas: U256, - gas_price: U256, -) -> Result<()> { +pub async fn print_gas_estimate(name: &str, gas: U256, gas_price: U256) -> Result<()> { greyln!("estimates"); greyln!("{} tx gas: {}", name, gas.debug_lavender()); greyln!( From 2926152369db6f3645b00d620b4654408299b57b Mon Sep 17 00:00:00 2001 From: Jason-W123 Date: Mon, 10 Feb 2025 15:18:40 +0800 Subject: [PATCH 5/5] wrap calculate_fee_per_gas --- main/src/deploy/deployer.rs | 8 +++----- main/src/deploy/mod.rs | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/main/src/deploy/deployer.rs b/main/src/deploy/deployer.rs index 636464d..55243e0 100644 --- a/main/src/deploy/deployer.rs +++ b/main/src/deploy/deployer.rs @@ -4,9 +4,10 @@ use super::SignerClient; use crate::{ check::ContractCheck, + deploy::calculate_fee_per_gas, macros::*, util::color::{Color, DebugColor, GREY}, - DeployConfig, GasFeeConfig, + DeployConfig, }; use alloy_dyn_abi::{DynSolValue, JsonAbiExt, Specifier}; use alloy_json_abi::{Constructor, StateMutability}; @@ -146,10 +147,7 @@ pub async fn deploy( return Ok(()); } - let fee_per_gas: u128 = match cfg.check_config.common_cfg.get_max_fee_per_gas_wei()? { - Some(wei) => wei, - None => gas_price.try_into().unwrap_or_default(), - }; + let fee_per_gas = calculate_fee_per_gas(&cfg.check_config.common_cfg, gas_price)?; let receipt = super::run_tx( "deploy_activate_init", diff --git a/main/src/deploy/mod.rs b/main/src/deploy/mod.rs index 036ef11..3660f9b 100644 --- a/main/src/deploy/mod.rs +++ b/main/src/deploy/mod.rs @@ -147,10 +147,7 @@ impl DeployConfig { return Ok(ethers::utils::get_contract_address(sender, nonce)); } - let fee_per_gas: u128 = match self.check_config.common_cfg.get_max_fee_per_gas_wei()? { - Some(wei) => wei, - None => gas_price.try_into().unwrap_or_default(), - }; + let fee_per_gas = calculate_fee_per_gas(&self.check_config.common_cfg, gas_price)?; let receipt = run_tx( "deploy", @@ -211,10 +208,7 @@ impl DeployConfig { greyln!("activation gas estimate: {}", format_gas(gas)); } - let fee_per_gas: u128 = match self.check_config.common_cfg.get_max_fee_per_gas_wei()? { - Some(wei) => wei, - None => gas_price.try_into().unwrap_or_default(), - }; + let fee_per_gas = calculate_fee_per_gas(&self.check_config.common_cfg, gas_price)?; let receipt = run_tx( "activate", @@ -342,3 +336,11 @@ pub fn format_gas(gas: U256) -> String { text.pink() } } + +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) +}