Skip to content

Commit

Permalink
Merge pull request #29 from OffchainLabs/minor-improvements-jan-2024
Browse files Browse the repository at this point in the history
Minor Fixes for UX
  • Loading branch information
rauljordan authored Feb 1, 2024
2 parents a83ab3f + a34dcdc commit a2ac0dd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ pub async fn run_checks(cfg: CheckConfig) -> eyre::Result<bool> {
opt_level: project::OptLevel::default(),
nightly: cfg.nightly,
rebuild: true,
skip_contract_size_check: cfg.skip_contract_size_check,
})
.map_err(|e| eyre!("failed to build project to WASM: {e}"))?,
};
println!("Reading WASM file at {}", wasm_file_path.display().grey());

let (precompressed_bytes, init_code) = project::compress_wasm(&wasm_file_path)
.map_err(|e| eyre!("failed to get compressed WASM bytes: {e}"))?;
let (precompressed_bytes, init_code) =
project::compress_wasm(&wasm_file_path, cfg.skip_contract_size_check)
.map_err(|e| eyre!("failed to get compressed WASM bytes: {e}"))?;

let precompressed_size = FileByteSize::new(precompressed_bytes.len() as u64);
println!("Uncompressed WASM size: {precompressed_size}");
Expand Down
7 changes: 5 additions & 2 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use ethers::types::{Eip1559TransactionRequest, H160, U256};
use ethers::utils::{get_contract_address, to_checksum};
use ethers::utils::{format_ether, get_contract_address, to_checksum};
use ethers::{middleware::SignerMiddleware, providers::Middleware, signers::Signer};
use eyre::{bail, eyre};

Expand Down Expand Up @@ -99,6 +99,7 @@ programs to Stylus chains here https://docs.arbitrum.io/stylus/stylus-quickstart
to_checksum(&addr, None),
);
}
println!("Address has ETH Balance: {}", format_ether(balance).mint());
}

// The folder at which to output the transaction data bytes.
Expand All @@ -117,10 +118,12 @@ programs to Stylus chains here https://docs.arbitrum.io/stylus/stylus-quickstart
opt_level: project::OptLevel::default(),
nightly: cfg.check_cfg.nightly,
rebuild: false, // The check step at the start of this command rebuilt.
skip_contract_size_check: cfg.check_cfg.skip_contract_size_check,
})
.map_err(|e| eyre!("could not build project to WASM: {e}"))?,
};
let (_, init_code) = project::compress_wasm(&wasm_file_path)?;
let (_, init_code) =
project::compress_wasm(&wasm_file_path, cfg.check_cfg.skip_contract_size_check)?;
println!("");
println!("{}", "====DEPLOYMENT====".grey());
println!(
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ pub struct CheckConfig {
/// Whether to use Rust nightly.
#[arg(long)]
nightly: bool,
/// Whether to skip the contract size check. In case the contract size is exceeded, a warning
/// will be emitted, but the overall cargo stylus check command will not fail.
#[arg(long)]
skip_contract_size_check: bool,
}

#[derive(Args, Clone, Debug)]
Expand Down
11 changes: 9 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct BuildConfig {
pub opt_level: OptLevel,
pub nightly: bool,
pub rebuild: bool,
pub skip_contract_size_check: bool,
}

#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn build_project_dylib(cfg: BuildConfig) -> Result<PathBuf> {
})
.ok_or(BuildError::NoWasmFound { path: release_path })?;

if let Err(e) = compress_wasm(&wasm_file_path) {
if let Err(e) = compress_wasm(&wasm_file_path, cfg.skip_contract_size_check) {
if let Some(BuildError::MaxCompressedSizeExceeded { got, .. }) = e.downcast_ref() {
match cfg.opt_level {
OptLevel::S => {
Expand All @@ -127,6 +128,7 @@ https://github.com/OffchainLabs/cargo-stylus/blob/main/OPTIMIZING_BINARIES.md"#,
opt_level: OptLevel::Z,
nightly: cfg.nightly,
rebuild: true,
skip_contract_size_check: cfg.skip_contract_size_check,
});
}
OptLevel::Z => {
Expand All @@ -141,6 +143,7 @@ https://github.com/OffchainLabs/cargo-stylus/blob/main/OPTIMIZING_BINARIES.md"#,
opt_level: OptLevel::Z,
nightly: true,
rebuild: true,
skip_contract_size_check: cfg.skip_contract_size_check,
});
}
return Err(BuildError::ExceedsMaxDespiteBestEffort {
Expand All @@ -157,7 +160,7 @@ https://github.com/OffchainLabs/cargo-stylus/blob/main/OPTIMIZING_BINARIES.md"#,
}

/// Reads a WASM file at a specified path and returns its brotli compressed bytes.
pub fn compress_wasm(wasm_path: &PathBuf) -> Result<(Vec<u8>, Vec<u8>)> {
pub fn compress_wasm(wasm_path: &PathBuf, skip_size_check: bool) -> Result<(Vec<u8>, Vec<u8>)> {
let wasm_file_bytes = std::fs::read(wasm_path).map_err(|e| {
eyre!(
"could not read WASM file at target path {}: {e}",
Expand All @@ -177,6 +180,10 @@ pub fn compress_wasm(wasm_path: &PathBuf) -> Result<(Vec<u8>, Vec<u8>)> {
let mut deploy_ready_code = hex::decode(EOF_PREFIX).unwrap();
deploy_ready_code.extend(compressed_bytes);

if skip_size_check {
return Ok((wasm_bytes.to_vec(), deploy_ready_code));
}

let precompressed_size = ByteSize::b(wasm_bytes.len() as u64);
if precompressed_size > MAX_PRECOMPRESSED_WASM_SIZE {
return Err(BuildError::MaxPrecompressedSizeExceeded {
Expand Down
39 changes: 33 additions & 6 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,47 @@ where
let base_fee_gwei = format_units(base_fee, "gwei")
.map_err(|e| eyre!("could not format base fee as gwei: {e}"))?;
println!("Base fee: {} gwei", base_fee_gwei.grey());
if !(estimate_only) {
if !estimate_only {
tx_request.max_fee_per_gas = Some(base_fee);
tx_request.max_priority_fee_per_gas = Some(base_fee);
}

let address = &tx_request
.from
.ok_or(eyre!("no sender address specified for tx"))?;
let balance = client
.get_balance(*address, None)
.await
.map_err(|e| eyre!("could not get sender balance: {e}"))?;

let typed = TypedTransaction::Eip1559(tx_request.clone());
let estimated = client
.estimate_gas(&typed, None)
let estimated = client.estimate_gas(&typed, None).await.map_err(|e| {
if e.to_string().contains("gas required exceeds allowance") {
return eyre!(
"not enough funds to transact, only had ETH balance of {}",
format_ether(balance)
);
}
eyre!("could not estimate gas: {e}")
})?;

let estimate_gas_price = client
.get_gas_price()
.await
.map_err(|e| eyre!("could not estimate gas {e}"))?;
.map_err(|e| eyre!("could not estimate gas price for tx: {e}"))?;

let total_estimated_cost = estimated
.checked_mul(estimate_gas_price)
.ok_or(eyre!("could not multiply estimated gas cost by gas price"))?;

let estimate_gas_price = format_units(estimate_gas_price, "gwei")
.map_err(|e| eyre!("could not format gas price to gwei: {e}"))?;

println!(
"Estimated gas for {tx_kind}: {} gas units",
estimated.mint()
"Estimations for {tx_kind}: gas price (gwei): {}, gas units: {}, total ETH cost: {}",
estimate_gas_price.mint(),
estimated.mint(),
format_ether(total_estimated_cost).mint(),
);

if estimate_only {
Expand Down

0 comments on commit a2ac0dd

Please sign in to comment.