Skip to content

Commit

Permalink
fix(sol-types): un-break decode revert (#457)
Browse files Browse the repository at this point in the history
* fix(sol-types): un-break decode revert

* fix

* fix
  • Loading branch information
DaniPopes authored Dec 16, 2023
1 parent c59d1ff commit e2798c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 6 additions & 5 deletions crates/sol-types/src/types/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abi::token::{PackedSeqToken, Token, TokenSeq, WordToken},
types::interface::{GenericRevertReason, RevertReason},
types::interface::RevertReason,
Result, SolType, Word,
};
use alloc::{string::String, vec::Vec};
Expand Down Expand Up @@ -411,14 +411,15 @@ impl PanicKind {
/// If successful, it returns the decoded revert reason wrapped in an `Option`.
///
/// If both attempts fail, it returns `None`.
pub fn decode_revert_reason(out: &[u8]) -> Option<GenericRevertReason> {
RevertReason::decode(out)
pub fn decode_revert_reason(out: &[u8]) -> Option<String> {
RevertReason::decode(out).map(|x| x.to_string())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{sol, types::interface::SolInterface};
use alloc::string::ToString;
use alloy_primitives::{address, hex, keccak256};

#[test]
Expand Down Expand Up @@ -462,14 +463,14 @@ mod tests {
let revert = Revert::from("test_revert_reason");
let encoded = revert.abi_encode();
let decoded = decode_revert_reason(&encoded).unwrap();
assert_eq!(decoded, revert.into());
assert_eq!(decoded, revert.to_string());
}

#[test]
fn decode_random_revert_reason() {
let revert_reason = String::from("test_revert_reason");
let decoded = decode_revert_reason(revert_reason.as_bytes()).unwrap();
assert_eq!(decoded, String::from("test_revert_reason").into());
assert_eq!(decoded, "test_revert_reason");
}

#[test]
Expand Down
13 changes: 12 additions & 1 deletion crates/sol-types/src/types/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<T: fmt::Display> fmt::Display for RevertReason<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RevertReason::ContractError(error) => error.fmt(f),
RevertReason::RawString(raw_string) => write!(f, "{}", raw_string),
RevertReason::RawString(raw_string) => f.write_str(raw_string),
}
}
}
Expand Down Expand Up @@ -447,6 +447,17 @@ where
}
}

impl<T: SolInterface + fmt::Display> RevertReason<T> {
/// Returns the reason for a revert as a string.
#[allow(clippy::inherent_to_string_shadow_display)]
pub fn to_string(&self) -> String {
match self {
RevertReason::ContractError(error) => error.to_string(),
RevertReason::RawString(raw_string) => raw_string.clone(),
}
}
}

/// Iterator over the function or error selectors of a [`SolInterface`] type.
///
/// This `struct` is created by the [`selectors`] method on [`SolInterface`].
Expand Down

0 comments on commit e2798c2

Please sign in to comment.