Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sol-types): un-break decode revert #457

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 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,8 +411,8 @@ 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)]
Expand Down Expand Up @@ -462,14 +462,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
12 changes: 11 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,16 @@ where
}
}

impl<T: SolInterface + fmt::Display> RevertReason<T> {
/// Returns the reason for a revert as a string.
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
Loading