Skip to content

Commit

Permalink
feat: cast send print revert reason(data)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuiweixie committed Aug 5, 2024
1 parent 0b73b42 commit 09ea626
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
11 changes: 10 additions & 1 deletion crates/cast/bin/cmd/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
}
Expand Down
21 changes: 18 additions & 3 deletions crates/cast/bin/tx.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
40 changes: 32 additions & 8 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand All @@ -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::*;

Expand Down Expand Up @@ -129,8 +129,15 @@ where
func: Option<&Function>,
block: Option<BlockId>,
) -> Result<String> {
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 {
Expand Down Expand Up @@ -2151,3 +2158,20 @@ mod tests {
)
}
}

pub fn add_data_to_err_report(e: RpcError<TransportErrorKind>) -> 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()
}
};
}

0 comments on commit 09ea626

Please sign in to comment.