diff --git a/crates/primitives/src/abi.rs b/crates/primitives/src/abi.rs new file mode 100644 index 0000000000..fe89ccc0c6 --- /dev/null +++ b/crates/primitives/src/abi.rs @@ -0,0 +1,16 @@ +//! Eth ABI helpers. +use crate::constants::SELECTOR_LEN; + +/// Returns the revert reason from the given output data, if it's an abi encoded String. Returns +/// `None` if the output is not long enough to contain a function selector or the content is not a +/// valid abi encoded String. +/// +/// **Note:** it's assumed the `out` buffer starts with the call's signature +pub fn decode_revert_reason(out: impl AsRef<[u8]>) -> Option { + use ethers_core::abi::AbiDecode; + let out = out.as_ref(); + if out.len() < SELECTOR_LEN { + return None + } + String::decode(&out[SELECTOR_LEN..]).ok() +} diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index d4c29974bd..6beed25564 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -9,6 +9,7 @@ //! //! This crate contains Ethereum primitive types and helper functions. +pub mod abi; mod account; pub mod basefee; mod bits; diff --git a/crates/rpc/rpc/src/eth/error.rs b/crates/rpc/rpc/src/eth/error.rs index 6f2b10aade..2547e02a2e 100644 --- a/crates/rpc/rpc/src/eth/error.rs +++ b/crates/rpc/rpc/src/eth/error.rs @@ -2,7 +2,7 @@ use crate::result::{internal_rpc_err, invalid_params_rpc_err, rpc_err, rpc_error_with_code}; use jsonrpsee::core::Error as RpcError; -use reth_primitives::{constants::SELECTOR_LEN, Address, Bytes, U256}; +use reth_primitives::{abi::decode_revert_reason, Address, Bytes, U256}; use reth_rpc_types::{error::EthRpcErrorCode, BlockError}; use reth_transaction_pool::error::{InvalidPoolTransactionError, PoolError}; use revm::primitives::{EVMError, ExecutionResult, Halt, OutOfGasError}; @@ -391,15 +391,3 @@ pub(crate) fn ensure_success(result: ExecutionResult) -> EthResult { } } } - -/// Returns the revert reason from the `revm::TransactOut` data, if it's an abi encoded String. -/// -/// **Note:** it's assumed the `out` buffer starts with the call's signature -pub(crate) fn decode_revert_reason(out: impl AsRef<[u8]>) -> Option { - use ethers_core::abi::AbiDecode; - let out = out.as_ref(); - if out.len() < SELECTOR_LEN { - return None - } - String::decode(&out[SELECTOR_LEN..]).ok() -}