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 issue #137

Closed
wants to merge 5 commits into from
Closed
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"

[workspace.dependencies]
alloy-primitives = "=0.7.7"
alloy-dyn-abi = "=0.7.7"
alloy-json-abi = "=0.7.7"
alloy-sol-macro = "=0.7.7"
alloy-sol-types = "=0.7.7"
Expand Down
1 change: 0 additions & 1 deletion main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ nightly = []

[dependencies]
alloy-primitives.workspace = true
alloy-dyn-abi.workspace = true
alloy-json-abi.workspace = true
alloy-sol-macro.workspace = true
alloy-sol-types.workspace = true
Expand Down
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
103 changes: 44 additions & 59 deletions main/src/deploy/mod.rs → main/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use crate::{
check::{self, ContractCheck},
constants::ARB_WASM_H160,
export_abi,
macros::*,
DeployConfig,
};
use crate::{
util::{
color::{Color, DebugColor},
sys,
},
DeployConfig,
GasFeeConfig,
};
use alloy_primitives::{Address, U256 as AU256};
use alloy_sol_macro::sol;
Expand All @@ -27,8 +29,6 @@ use ethers::{
};
use eyre::{bail, eyre, Result, WrapErr};

mod deployer;

sol! {
interface ArbWasm {
function activateProgram(address program)
Expand All @@ -47,11 +47,6 @@ pub async fn deploy(cfg: DeployConfig) -> Result<()> {
.expect("cargo stylus check failed");
let verbose = cfg.check_config.common_cfg.verbose;

let constructor = export_abi::get_constructor_signature()?;
let deployer_args = constructor
.map(|constructor| deployer::parse_constructor_args(&cfg, &constructor, &contract))
.transpose()?;

let client = sys::new_provider(&cfg.check_config.common_cfg.endpoint)?;
let chain_id = client.get_chainid().await.expect("failed to get chain id");

Expand All @@ -64,8 +59,7 @@ pub async fn deploy(cfg: DeployConfig) -> Result<()> {
greyln!("sender address: {}", sender.debug_lavender());
}

let data_fee = contract.suggest_fee()
+ alloy_ethers_typecast::ethers_u256_to_alloy(cfg.experimental_constructor_value);
let data_fee = contract.suggest_fee();

if let ContractCheck::Ready { .. } = &contract {
// check balance early
Expand All @@ -88,10 +82,6 @@ pub async fn deploy(cfg: DeployConfig) -> Result<()> {
}
}

if let Some(deployer_args) = deployer_args {
return deployer::deploy(&cfg, deployer_args, sender, &client).await;
}

let contract_addr = cfg
.deploy_contract(contract.code(), sender, &client)
.await?;
Expand All @@ -115,7 +105,13 @@ cargo stylus activate --address {}"#,
}
ContractCheck::Active { .. } => greyln!("wasm already activated!"),
}
print_cache_notice(contract_addr);
println!("");
let contract_addr = hex::encode(contract_addr);
mintln!(
r#"NOTE: We recommend running cargo stylus cache bid {contract_addr} 0 to cache your activated contract in ArbOS.
Cached contracts benefit from cheaper calls. To read more about the Stylus contract cache, see
https://docs.arbitrum.io/stylus/concepts/stylus-cache-manager"#
);
Ok(())
}

Expand All @@ -136,20 +132,37 @@ impl DeployConfig {
let gas = client
.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?;
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?;
return Ok(ethers::utils::get_contract_address(sender, nonce));
}

let fee_per_gas = 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,
)
Expand Down Expand Up @@ -198,15 +211,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 = 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,
)
Expand All @@ -224,49 +244,22 @@ impl DeployConfig {
}
}

pub async fn print_gas_estimate(name: &str, client: &SignerClient, gas: U256) -> Result<()> {
let gas_price = client.get_gas_price().await?;
greyln!("estimates");
greyln!("{} tx gas: {}", name, 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!(
"{} tx total cost: {} ETH",
name,
eth_estimate.debug_lavender()
);
Ok(())
}

pub fn print_cache_notice(contract_addr: H160) {
let contract_addr = hex::encode(contract_addr);
println!("");
mintln!(
r#"NOTE: We recommend running cargo stylus cache bid {contract_addr} 0 to cache your activated contract in ArbOS.
Cached contracts benefit from cheaper calls. To read more about the Stylus contract cache, see
https://docs.arbitrum.io/stylus/concepts/stylus-cache-manager"#
);
}

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 @@ -328,11 +321,3 @@ pub fn format_gas(gas: U256) -> String {
text.pink()
}
}

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"),
}
}
Loading
Loading