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(forge): force optimism and arbitrum to --slow mode #2046

Merged
merged 4 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions cli/src/cmd/forge/script/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
*,
};
use crate::{
cmd::{forge::script::receipts::wait_for_receipts, has_different_gas_calc},
cmd::{forge::script::receipts::wait_for_receipts, has_batch_support, has_different_gas_calc},
init_progress,
opts::WalletType,
update_progress,
Expand All @@ -15,6 +15,7 @@ use ethers::{
types::transaction::eip2718::TypedTransaction,
utils::format_units,
};
use eyre::ContextCompat;
use foundry_config::Chain;
use futures::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
Expand All @@ -39,14 +40,13 @@ impl ScriptArgs {
.collect();

let local_wallets = self.wallets.find_all(provider.clone(), required_addresses).await?;
if local_wallets.is_empty() {
eyre::bail!("Error accessing local wallet when trying to send onchain transaction, did you set a private key, mnemonic or keystore?")
}
let chain = local_wallets.values().last().wrap_err("Error accessing local wallet when trying to send onchain transaction, did you set a private key, mnemonic or keystore?")?.chain_id();

// We only wait for a transaction receipt before sending the next transaction, if there
// is more than one signer. There would be no way of assuring their order
// otherwise.
let sequential_broadcast = local_wallets.len() != 1 || self.slow;
// otherwise. Or if the chain does not support batched transactions (eg. Arbitrum).
let sequential_broadcast =
local_wallets.len() != 1 || self.slow || !has_batch_support(chain);

// Make a one-time gas price estimation
let (gas_price, eip1559_fees) = {
Expand All @@ -72,7 +72,7 @@ impl ScriptArgs {

let mut tx = tx.clone();

tx.set_chain_id(signer.chain_id());
tx.set_chain_id(chain);

if let Some(gas_price) = self.with_gas_price {
tx.set_gas_price(gas_price);
Expand Down Expand Up @@ -294,7 +294,7 @@ impl ScriptArgs {
println!("\n==========================");
println!("\nEstimated total gas used for script: {}", total_gas);
println!(
"\nAmount required: {} ETH",
"\nEstimated amount required: {} ETH",
format_units(total_gas.saturating_mul(per_gas), 18)
.unwrap_or_else(|_| "[Could not calculate]".to_string())
.trim_end_matches('0')
Expand Down Expand Up @@ -325,14 +325,29 @@ impl fmt::Display for BroadcastError {
/// transaction hash that can be used on a later run with `--resume`.
async fn broadcast<T, U>(
signer: &SignerMiddleware<T, U>,
legacy_or_1559: TypedTransaction,
mut legacy_or_1559: TypedTransaction,
) -> Result<TxHash, BroadcastError>
where
T: Middleware,
U: Signer,
{
tracing::debug!("sending transaction: {:?}", legacy_or_1559);

// Chains which use `eth_estimateGas` are being sent sequentially and require their gas to be
// re-estimated right before broadcasting.
if has_different_gas_calc(signer.signer().chain_id()) {
// If we don't, some RPCs might just return our estimated gas anyway.
legacy_or_1559.set_gas(0);

legacy_or_1559.set_gas(
signer
.provider()
.estimate_gas(&legacy_or_1559)
.await
.map_err(|err| BroadcastError::Simple(err.to_string()))?,
);
}

// Signing manually so we skip `fill_transaction` and its `eth_createAccessList` request.
let signature = signer
.sign_transaction(
Expand Down
4 changes: 3 additions & 1 deletion cli/src/cmd/forge/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ impl ScriptArgs {
println!("{}", Paint::red("Script failed."));
}

println!("Gas used: {}", result.gas);
if script_config.evm_opts.fork_url.is_none() {
println!("Gas used: {}", result.gas);
}

if !result.returned.is_empty() {
println!("\n== Return ==");
Expand Down
20 changes: 16 additions & 4 deletions cli/src/cmd/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethers::{
},
};

use foundry_config::Chain as ConfigChain;
use foundry_utils::Retry;

use std::{collections::BTreeMap, path::PathBuf};
Expand Down Expand Up @@ -198,8 +199,19 @@ macro_rules! update_progress {

/// True if the network calculates gas costs differently.
pub fn has_different_gas_calc(chain: u64) -> bool {
matches!(
Chain::try_from(chain).unwrap_or(Chain::Mainnet),
Chain::Arbitrum | Chain::ArbitrumTestnet
)
if let ConfigChain::Named(chain) = ConfigChain::from(chain) {
return matches!(chain, Chain::Arbitrum | Chain::ArbitrumTestnet)
}
false
}

/// True if it supports broadcasting in batches.
pub fn has_batch_support(chain: u64) -> bool {
if let ConfigChain::Named(chain) = ConfigChain::from(chain) {
return !matches!(
chain,
Chain::Arbitrum | Chain::ArbitrumTestnet | Chain::Optimism | Chain::OptimismKovan
)
}
true
}