Skip to content

Commit

Permalink
Use derived decode and abi/selector for Panic
Browse files Browse the repository at this point in the history
  • Loading branch information
blckngm committed Dec 5, 2023
1 parent 0d3b125 commit 5d01c7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
21 changes: 8 additions & 13 deletions crates/relayer/src/chain/axon/eth_err.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use ethers::abi::{Detokenize, ParamType, Uint};
use ethers::abi::Uint;
use ethers::contract::EthCall;

/// Panic(uint)
/// For decoding and displaying `Panic(uint256)` errors.
///
/// EthError derived decode_with_selector is buggy, so we derive `EthCall` instead. Decode with `AbiDecode::decode`.
#[derive(EthCall)]
pub struct Panic(Uint);

impl Panic {
// Can't get the right selector with `derive(EthError)`, so I implement this manually.
pub fn decode_with_selector(bytes: &[u8]) -> Option<Self> {
let bytes = bytes.strip_prefix(b"\x4e\x48\x7b\x71")?;
let tokens = ethers::abi::decode(&[ParamType::Uint(32)], bytes).ok()?;
Some(Panic(Uint::from_tokens(tokens).ok()?))
}
}

impl std::fmt::Display for Panic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let e = PanicError::from_code(self.0.low_u32());
Expand Down Expand Up @@ -74,7 +69,7 @@ impl std::fmt::Display for PanicError {

#[cfg(test)]
mod test {
use ethers::contract::EthError;
use ethers::{abi::AbiDecode, contract::EthError};

use super::Panic;

Expand All @@ -84,7 +79,7 @@ mod test {
.unwrap(),
)
.unwrap();
if let Some(p) = Panic::decode_with_selector(&revert_data) {
if let Ok(p) = Panic::decode(&revert_data) {
p.to_string()
} else if let Some(s) = String::decode_with_selector(&revert_data) {
s
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer/src/chain/axon/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
event::IbcEventWithHeight,
ibc_contract::OwnableIBCHandlerEvents,
};
use ethers::{contract::ContractError, providers::Middleware, types::H256};
use ethers::{abi::AbiDecode, contract::ContractError, providers::Middleware, types::H256};
use ibc_relayer_types::{
clients::{
ics07_axon::{client_state::AxonClientState, consensus_state::AxonConsensusState},
Expand All @@ -35,7 +35,7 @@ where
{
if let Some(r) = err.decode_revert::<String>() {
eyre::eyre!("Contract call reverted: {r}")
} else if let Some(p) = err.as_revert().and_then(|d| Panic::decode_with_selector(d)) {
} else if let Some(p) = err.as_revert().and_then(|d| Panic::decode(d).ok()) {
eyre::eyre!("Contract call reverted: {p}")
} else {
err.into()
Expand Down

0 comments on commit 5d01c7b

Please sign in to comment.