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 gas estimate and command issues #138

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions main/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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...");
Expand Down
10 changes: 8 additions & 2 deletions main/src/deploy/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::SignerClient;
use crate::{
check::ContractCheck,
deploy::calculate_fee_per_gas,
macros::*,
util::color::{Color, DebugColor, GREY},
DeployConfig,
Expand Down Expand Up @@ -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,
)
Expand Down
40 changes: 24 additions & 16 deletions main/src/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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!(
Expand Down Expand Up @@ -256,17 +263,18 @@ pub async fn run_tx(
name: &str,
tx: Eip1559TransactionRequest,
gas: Option<U256>,
max_fee_per_gas_gwei: Option<u128>,
max_fee_per_gas_wei: u128,
client: &SignerClient,
verbose: bool,
) -> Result<TransactionReceipt> {
let mut tx = 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();
Expand Down Expand Up @@ -329,10 +337,10 @@ pub fn format_gas(gas: U256) -> String {
}
}

pub fn gwei_to_wei(gwei: u128) -> Result<u128> {
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<T: GasFeeConfig>(config: &T, gas_price: U256) -> Result<u128> {
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)
}
58 changes: 56 additions & 2 deletions main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct CommonConfig {
source_files_for_project_hash: Vec<String>,
#[arg(long)]
/// Optional max fee per gas in gwei units.
max_fee_per_gas_gwei: Option<u128>,
max_fee_per_gas_gwei: Option<String>,
/// Specifies the features to use when building the Stylus binary.
#[arg(long)]
features: Option<String>,
Expand Down Expand Up @@ -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<u128>,
max_fee_per_gas_gwei: Option<String>,
}

#[derive(Args, Clone, Debug)]
Expand Down Expand Up @@ -395,6 +395,60 @@ impl fmt::Display for CommonConfig {
}
}

pub trait GasFeeConfig {
fn get_max_fee_per_gas_wei(&self) -> Result<Option<u128>>;
fn get_fee_str(&self) -> &Option<String>;
}

fn convert_gwei_to_wei(fee_str: &str) -> Result<u128> {
let gwei = match fee_str.parse::<f64>() {
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<String> {
&self.max_fee_per_gas_gwei
}

fn get_max_fee_per_gas_wei(&self) -> Result<Option<u128>> {
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<String> {
&self.max_fee_per_gas_gwei
}

fn get_max_fee_per_gas_wei(&self) -> Result<Option<u128>> {
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!(
Expand Down