-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(e2e): properly check errors match (#110)
Follow-up work to #80 We were previously just checking that an error's signature was contained in the JSON-RPC response, but now we check the revert data fully. Also polished the API.
- Loading branch information
1 parent
58a7163
commit 2f98bef
Showing
5 changed files
with
62 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use alloy::{contract::Error, sol_types::SolError}; | ||
|
||
pub trait ErrorExt<E> { | ||
/// Checks that `Self` corresponds to the typed abi-encoded error | ||
/// `expected`. | ||
fn is(&self, expected: E) -> bool; | ||
} | ||
|
||
impl<E: SolError> ErrorExt<E> for Error { | ||
fn is(&self, expected: E) -> bool { | ||
let Self::TransportError(e) = self else { | ||
return false; | ||
}; | ||
|
||
let raw_value = e | ||
.as_error_resp() | ||
.and_then(|payload| payload.data.clone()) | ||
.expect("should extract the error"); | ||
let actual = &raw_value.get().trim_matches('"')[2..]; | ||
let expected = alloy::hex::encode(expected.abi_encode()); | ||
return expected == actual; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use alloy::{rpc::types::eth::TransactionReceipt, sol_types::SolEvent}; | ||
|
||
pub trait EventExt<E> { | ||
/// Asserts the contract emitted the `expected` event. | ||
fn emits(&self, expected: E); | ||
} | ||
|
||
impl<E> EventExt<E> for TransactionReceipt | ||
where | ||
E: SolEvent, | ||
E: PartialEq, | ||
E: std::fmt::Debug, | ||
{ | ||
fn emits(&self, expected: E) { | ||
// Extract all events that are the expected type. | ||
let emitted = self | ||
.inner | ||
.logs() | ||
.iter() | ||
.filter_map(|log| log.log_decode().ok()) | ||
.map(|log| log.inner.data) | ||
.any(|event| expected == event); | ||
|
||
assert!(emitted, "Event {:?} not emitted", expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters