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

test: move decode_revert_reason to alloy and add tests #308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions crates/sol-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ mod impl_core;

mod types;
pub use types::{
data_type as sol_data, ContractError, Encodable, EventTopic, GenericContractError, Panic,
PanicKind, Revert, Selectors, SolCall, SolEnum, SolError, SolEvent, SolInterface, SolStruct,
SolType, TopicList,
data_type as sol_data, decode_revert_reason, ContractError, Encodable, EventTopic,
GenericContractError, Panic, PanicKind, Revert, Selectors, SolCall, SolEnum, SolError,
SolEvent, SolInterface, SolStruct, SolType, TopicList,
};

pub mod utils;
Expand Down
47 changes: 45 additions & 2 deletions crates/sol-types/src/types/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{
token::{PackedSeqToken, TokenSeq, WordToken},
Result, SolType, TokenType, Word,
GenericContractError, Result, SolInterface, SolType, TokenType, Word,
};
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloc::{string::String, vec::Vec};
use alloy_primitives::U256;
use core::{borrow::Borrow, fmt};

Expand Down Expand Up @@ -408,6 +411,24 @@ impl PanicKind {
}
}

/// Returns the revert reason from the given output data. Returns `None` if the
/// content is not a valid abi encoded String or a regular utf8 string (for
/// Vyper reverts).
pub fn decode_revert_reason(out: &[u8]) -> Option<String> {
// Try to decode as a generic contract error.
if let Ok(error) = GenericContractError::decode(out, true) {
return Some(error.to_string())
}

// If that fails, try to decode as a regular string.
if let Ok(decoded_string) = core::str::from_utf8(out) {
return Some(decoded_string.to_string())
}

// If both attempts fail, return None.
None
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -448,4 +469,26 @@ mod tests {
"Panic selector is incorrect"
);
}

#[test]
fn test_decode_solidity_revert_reason() {
let revert = Revert::from("test_revert_reason");
let encoded = revert.encode();
let decoded = decode_revert_reason(&encoded).unwrap();
assert_eq!(decoded, String::from("revert: test_revert_reason"));
}

#[test]
fn test_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"));
}

#[test]
fn test_decode_non_utf8_revert_reason() {
let revert_reason = [0xFF];
let decoded = decode_revert_reason(&revert_reason);
assert_eq!(decoded, None);
}
}
2 changes: 1 addition & 1 deletion crates/sol-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod r#enum;
pub use r#enum::SolEnum;

mod error;
pub use error::{Panic, PanicKind, Revert, SolError};
pub use error::{decode_revert_reason, Panic, PanicKind, Revert, SolError};

mod event;
pub use event::{EventTopic, SolEvent, TopicList};
Expand Down