From 09ea626d81c60e5256eafcda65c38602aa788cde Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Tue, 6 Aug 2024 01:35:04 +0800 Subject: [PATCH] feat: cast send print revert reason(data) --- crates/cast/bin/cmd/estimate.rs | 11 ++++++++- crates/cast/bin/tx.rs | 21 ++++++++++++++--- crates/cast/src/lib.rs | 40 ++++++++++++++++++++++++++------- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/crates/cast/bin/cmd/estimate.rs b/crates/cast/bin/cmd/estimate.rs index d16cfac0f599..629b585536e9 100644 --- a/crates/cast/bin/cmd/estimate.rs +++ b/crates/cast/bin/cmd/estimate.rs @@ -11,6 +11,7 @@ use foundry_cli::{ use foundry_common::ens::NameOrAddress; use foundry_config::Config; use std::str::FromStr; +use cast::add_data_to_err_report; /// CLI arguments for `cast estimate`. #[derive(Debug, Parser)] @@ -104,7 +105,15 @@ impl EstimateArgs { .build_raw(sender) .await?; - let gas = provider.estimate_gas(&tx).block(block.unwrap_or_default()).await?; + let gas = match provider.estimate_gas(&tx).block(block.unwrap_or_default()).await { + Ok(r) => { + r + }, + Err(e) => { + let r = add_data_to_err_report(e); + return Err(r) + } + }; println!("{gas}"); Ok(()) } diff --git a/crates/cast/bin/tx.rs b/crates/cast/bin/tx.rs index 9a731187a570..ac3d48a3939d 100644 --- a/crates/cast/bin/tx.rs +++ b/crates/cast/bin/tx.rs @@ -1,7 +1,7 @@ use alloy_consensus::{SidecarBuilder, SimpleCoder}; use alloy_json_abi::Function; use alloy_network::{AnyNetwork, TransactionBuilder}; -use alloy_primitives::{hex, Address, Bytes, TxKind}; +use alloy_primitives::{Address, Bytes, hex, TxKind}; use alloy_provider::Provider; use alloy_rpc_types::{TransactionInput, TransactionRequest}; use alloy_serde::WithOtherFields; @@ -256,7 +256,14 @@ where if !self.legacy && (self.tx.max_fee_per_gas.is_none() || self.tx.max_priority_fee_per_gas.is_none()) { - let estimate = self.provider.estimate_eip1559_fees(None).await?; + let estimate = match self.provider.estimate_eip1559_fees(None).await { + Ok(gas_estimate) => { + gas_estimate + }, + Err(e) => { + return Err(cast::add_data_to_err_report(e)) + } + }; if !self.legacy { if self.tx.max_fee_per_gas.is_none() { @@ -270,7 +277,15 @@ where } if self.tx.gas.is_none() { - self.tx.gas = Some(self.provider.estimate_gas(&self.tx).await?); + let resp = self.provider.estimate_gas(&self.tx).await; + match resp { + Ok(gas_estimate) => { + self.tx.gas = Some(gas_estimate); + }, + Err(e) => { + return Err(cast::add_data_to_err_report(e)) + } + } } if self.tx.nonce.is_none() { diff --git a/crates/cast/src/lib.rs b/crates/cast/src/lib.rs index 46610d6303b7..9a6100424e2e 100644 --- a/crates/cast/src/lib.rs +++ b/crates/cast/src/lib.rs @@ -6,9 +6,9 @@ use alloy_dyn_abi::{DynSolType, DynSolValue, FunctionExt}; use alloy_json_abi::Function; use alloy_network::AnyNetwork; use alloy_primitives::{ - hex, - utils::{keccak256, ParseUnits, Unit}, - Address, Keccak256, TxHash, TxKind, B256, I256, U256, + Address, + B256, + hex, I256, Keccak256, TxHash, TxKind, U256, utils::{keccak256, ParseUnits, Unit}, }; use alloy_provider::{ network::eip2718::{Decodable2718, Encodable2718}, @@ -18,11 +18,11 @@ use alloy_rlp::Decodable; use alloy_rpc_types::{BlockId, BlockNumberOrTag, Filter, TransactionRequest}; use alloy_serde::WithOtherFields; use alloy_sol_types::sol; -use alloy_transport::Transport; +use alloy_transport::{Transport, TransportErrorKind}; use base::{Base, NumberWithBase, ToBase}; use chrono::DateTime; use evm_disassembler::{disassemble_bytes, disassemble_str, format_operations}; -use eyre::{Context, ContextCompat, Result}; +use eyre::{Context, ContextCompat, Report as ErrReport, Report, Result}; use foundry_block_explorers::Client; use foundry_common::{ abi::{encode_function_args, get_func}, @@ -44,7 +44,7 @@ use std::{ sync::atomic::{AtomicBool, Ordering}, }; use tokio::signal::ctrl_c; - +use alloy_json_rpc::RpcError; use foundry_common::abi::encode_function_args_packed; pub use foundry_evm::*; @@ -129,8 +129,15 @@ where func: Option<&Function>, block: Option, ) -> Result { - let res = self.provider.call(req).block(block.unwrap_or_default()).await?; - + let res = match self.provider.call(req).block(block.unwrap_or_default()).await { + Ok(r) => { + r + }, + Err(e) => { + let r = add_data_to_err_report(e); + return Err(r) + } + }; let mut decoded = vec![]; if let Some(func) = func { @@ -2151,3 +2158,20 @@ mod tests { ) } } + +pub fn add_data_to_err_report(e: RpcError) -> Report { + match e { + RpcError::ErrorResp(error_payload) => { + let err_str = format!( + "{} with data: {}", + error_payload, + error_payload.data.as_ref() + .map_or_else(String::new, |err_data| err_data.get().trim_matches('"').to_string()) + ); + return ErrReport::msg(err_str) + } + _ => { + return e.into() + } + }; +} \ No newline at end of file