Skip to content

Commit 16ba137

Browse files
authored
refactor: use eyre Error for gas estimation (#3542)
1 parent b9eb261 commit 16ba137

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

cli/src/cmd/forge/script/broadcast.rs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use foundry_common::{estimate_eip1559_fees, try_get_http_provider, RetryProvider
2121
use foundry_config::Chain;
2222
use futures::StreamExt;
2323
use indicatif::{ProgressBar, ProgressStyle};
24-
use std::{cmp::min, fmt, ops::Mul, sync::Arc};
24+
use std::{cmp::min, ops::Mul, sync::Arc};
2525

2626
impl ScriptArgs {
2727
/// Sends the transactions which haven't been broadcasted yet.
@@ -196,20 +196,18 @@ impl ScriptArgs {
196196
signer: &WalletType,
197197
sequential_broadcast: bool,
198198
fork_url: &str,
199-
) -> Result<TxHash, BroadcastError> {
199+
) -> eyre::Result<TxHash> {
200200
let from = tx.from().expect("no sender");
201201

202202
if sequential_broadcast {
203-
let nonce = foundry_utils::next_nonce(*from, fork_url, None).await.map_err(|_| {
204-
BroadcastError::Simple("Not able to query the EOA nonce.".to_string())
205-
})?;
203+
let nonce = foundry_utils::next_nonce(*from, fork_url, None)
204+
.await
205+
.map_err(|_| eyre::eyre!("Not able to query the EOA nonce."))?;
206206

207207
let tx_nonce = tx.nonce().expect("no nonce");
208208

209209
if nonce != *tx_nonce {
210-
return Err(BroadcastError::Simple(
211-
"EOA nonce changed unexpectedly while sending transactions.".to_string(),
212-
))
210+
eyre::bail!("EOA nonce changed unexpectedly while sending transactions.")
213211
}
214212
}
215213

@@ -349,14 +347,14 @@ impl ScriptArgs {
349347
}
350348
/// Uses the signer to submit a transaction to the network. If it fails, it tries to retrieve
351349
/// the transaction hash that can be used on a later run with `--resume`.
352-
async fn broadcast<T, U>(
350+
async fn broadcast<T, S>(
353351
&self,
354-
signer: &SignerMiddleware<T, U>,
352+
signer: &SignerMiddleware<T, S>,
355353
mut legacy_or_1559: TypedTransaction,
356-
) -> Result<TxHash, BroadcastError>
354+
) -> eyre::Result<TxHash>
357355
where
358-
T: Middleware,
359-
U: Signer,
356+
T: Middleware + 'static,
357+
S: Signer + 'static,
360358
{
361359
tracing::debug!("sending transaction: {:?}", legacy_or_1559);
362360

@@ -377,14 +375,11 @@ impl ScriptArgs {
377375
*legacy_or_1559.from().expect("Tx should have a `from`."),
378376
)
379377
.await
380-
.map_err(|err| BroadcastError::Simple(err.to_string()))?;
378+
.wrap_err_with(|| "Failed to sign transaction")?;
381379

382380
// Submit the raw transaction
383-
let pending = signer
384-
.provider()
385-
.send_raw_transaction(legacy_or_1559.rlp_signed(&signature))
386-
.await
387-
.map_err(|err| BroadcastError::Simple(err.to_string()))?;
381+
let pending =
382+
signer.provider().send_raw_transaction(legacy_or_1559.rlp_signed(&signature)).await?;
388383

389384
Ok(pending.tx_hash())
390385
}
@@ -393,36 +388,18 @@ impl ScriptArgs {
393388
&self,
394389
tx: &mut TypedTransaction,
395390
provider: &Provider<T>,
396-
) -> Result<(), BroadcastError>
391+
) -> eyre::Result<()>
397392
where
398393
T: JsonRpcClient,
399394
{
400395
tx.set_gas(
401396
provider
402397
.estimate_gas(tx, None)
403398
.await
404-
.wrap_err_with(|| format!("Failed to estimate gas for tx: {}", tx.sighash()))
405-
.map_err(|err| BroadcastError::Simple(err.to_string()))? *
399+
.wrap_err_with(|| format!("Failed to estimate gas for tx: {:?}", tx.sighash()))? *
406400
self.gas_estimate_multiplier /
407401
100,
408402
);
409403
Ok(())
410404
}
411405
}
412-
413-
#[derive(thiserror::Error, Debug, Clone)]
414-
pub enum BroadcastError {
415-
Simple(String),
416-
ErrorWithTxHash(String, TxHash),
417-
}
418-
419-
impl fmt::Display for BroadcastError {
420-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421-
match self {
422-
BroadcastError::Simple(err) => write!(f, "{err}"),
423-
BroadcastError::ErrorWithTxHash(err, tx_hash) => {
424-
write!(f, "\nFailed to wait for transaction {tx_hash:?}:\n{err}")
425-
}
426-
}
427-
}
428-
}

0 commit comments

Comments
 (0)