|
| 1 | +use ethers::abi::ethabi::AbiError; |
| 2 | +use std::collections::BTreeMap; |
| 3 | + |
| 4 | +/// Macro to extend the contract error mapping by aggregating errors from multiple contracts. |
| 5 | +/// Each contract is specified as a pair: `[snake_case, ABI_IDENTIFIER]` where: |
| 6 | +/// - `snake_case` is the contract name converted to snake case. |
| 7 | +/// - `ABI_IDENTIFIER` is the contract name in uppercase with `_ABI` appended. |
| 8 | +/// |
| 9 | +/// This macro builds a lazily initialized static map (`MAP`) that associates Solidity error selectors |
| 10 | +/// (first 4 bytes of an error signature, hex-encoded) with their corresponding `AbiError`. |
| 11 | +#[macro_export] |
| 12 | +macro_rules! extend_contract_error_mapping { |
| 13 | + ($([$snake_case:ident, $abi:ident]),* $(,)?) => { |
| 14 | + lazy_static::lazy_static! { |
| 15 | + pub(crate) static ref MAP: ::std::collections::BTreeMap<String, ethers::abi::ethabi::AbiError> = { |
| 16 | + let mut errors = ::std::collections::BTreeMap::default(); |
| 17 | + |
| 18 | + $( |
| 19 | + $crate::error_parser::extend_errors(&mut errors, $crate::gen::$snake_case::$snake_case::$abi.errors.clone()); |
| 20 | + )* |
| 21 | + |
| 22 | + errors |
| 23 | + }; |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const SOLIDITY_SELECTOR_BYTE_SIZE: usize = 4; |
| 29 | + |
| 30 | +/// Extends the provided error map with errors from a contract’s error collection. |
| 31 | +/// For each error, it extracts the Solidity selector (first 4 bytes of the error signature), |
| 32 | +/// hex-encodes it, and maps that selector to a clone of the `AbiError`. |
| 33 | +/// |
| 34 | +/// If a selector already exists in the map, a warning is logged. |
| 35 | +pub fn extend_errors( |
| 36 | + map: &mut BTreeMap<String, AbiError>, |
| 37 | + contract_errors: BTreeMap<String, Vec<AbiError>>, |
| 38 | +) { |
| 39 | + for (_, v) in contract_errors.iter() { |
| 40 | + for e in v { |
| 41 | + // solidity selector is only the first 4 bytes of the signature |
| 42 | + let selector = const_hex::hex::encode(&e.signature().0[0..SOLIDITY_SELECTOR_BYTE_SIZE]); |
| 43 | + map.insert(selector, e.clone()); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, PartialEq, thiserror::Error)] |
| 49 | +pub enum ParseContractError { |
| 50 | + #[error("error bytes shorter than 4 bytes for solidity contract selector")] |
| 51 | + ErrorBytesTooShort, |
| 52 | + #[error("error string not hex format: {0}")] |
| 53 | + ErrorNotHexStr(String), |
| 54 | + #[error("error selector not found in contract error map: {selector}")] |
| 55 | + ErrorNotFound { selector: String }, |
| 56 | +} |
| 57 | + |
| 58 | +pub struct ContractErrorParser {} |
| 59 | + |
| 60 | +impl ContractErrorParser { |
| 61 | + pub fn parse_from_bytes(bytes: &[u8]) -> Result<String, ParseContractError> { |
| 62 | + if bytes.len() < SOLIDITY_SELECTOR_BYTE_SIZE { |
| 63 | + return Err(ParseContractError::ErrorBytesTooShort); |
| 64 | + } |
| 65 | + |
| 66 | + let selector = const_hex::hex::encode(&bytes[0..4]); |
| 67 | + |
| 68 | + let Some(error) = crate::gen::MAP.get(&selector) else { |
| 69 | + return Err(ParseContractError::ErrorNotFound { selector }); |
| 70 | + }; |
| 71 | + if let Err(e) = error.decode(bytes) { |
| 72 | + tracing::warn!("contract error selector found: {selector}, but decode failed: {e}"); |
| 73 | + } |
| 74 | + |
| 75 | + Ok(error.name.clone()) |
| 76 | + } |
| 77 | + |
| 78 | + pub fn parse_from_hex_str(err: &str) -> Result<String, ParseContractError> { |
| 79 | + let bytes = const_hex::hex::decode(err) |
| 80 | + .map_err(|e| ParseContractError::ErrorNotHexStr(e.to_string()))?; |
| 81 | + Self::parse_from_bytes(bytes.as_slice()) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use crate::error_parser::{ContractErrorParser, ParseContractError}; |
| 88 | + use const_hex::hex; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_parse_error_ok() { |
| 92 | + // selector for "BottomUpCheckpointAlreadySubmitted" error |
| 93 | + let err_bytes = hex::decode("d6bb62dd").unwrap(); |
| 94 | + |
| 95 | + assert_eq!( |
| 96 | + ContractErrorParser::parse_from_bytes(err_bytes.as_ref()).unwrap(), |
| 97 | + "BottomUpCheckpointAlreadySubmitted".to_string() |
| 98 | + ); |
| 99 | + |
| 100 | + // selector for "FunctionNotFound" error |
| 101 | + let err_bytes = |
| 102 | + hex::decode("5416eb98611941f900000000000000000000000000000000000000000000000000000000") |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + assert_eq!( |
| 106 | + ContractErrorParser::parse_from_bytes(err_bytes.as_ref()).unwrap(), |
| 107 | + "FunctionNotFound".to_string() |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_parse_error_not_found() { |
| 113 | + // a random error selector |
| 114 | + let err_bytes = hex::decode("a6bb62dd").unwrap(); |
| 115 | + |
| 116 | + assert_eq!( |
| 117 | + ContractErrorParser::parse_from_bytes(err_bytes.as_ref()), |
| 118 | + Err(ParseContractError::ErrorNotFound { |
| 119 | + selector: "a6bb62dd".to_string() |
| 120 | + }) |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments