From b1ae801236fbd0a37c91db5dfbccf3a15950ccd0 Mon Sep 17 00:00:00 2001 From: Denis Rybas <61991100+DenisRybas@users.noreply.github.com> Date: Wed, 11 Sep 2024 17:51:07 +0300 Subject: [PATCH] Feature: add revert reason to receipt (#717) * added revert reason to receipt * Revert_reason code cleanup * added docs for is_txn_reverted method * removed async from decode_revert_reason * removed revert_reason decoding --------- Co-authored-by: artem.ivanov --- src/confirm.rs | 2 ++ src/error.rs | 8 +++++++- src/types/transaction.rs | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/confirm.rs b/src/confirm.rs index 900110cb..f84941de 100644 --- a/src/confirm.rs +++ b/src/confirm.rs @@ -86,6 +86,7 @@ async fn send_transaction_with_confirmation_( .transaction_receipt(hash) .await? .expect("receipt can't be null after wait for confirmations; qed"); + Ok(receipt) } @@ -163,6 +164,7 @@ mod tests { logs_bloom: Default::default(), transaction_type: None, effective_gas_price: Default::default(), + revert_reason: None, }; let poll_interval = Duration::from_secs(0); diff --git a/src/error.rs b/src/error.rs index d858d688..24f0776b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -47,13 +47,17 @@ pub enum Error { /// web3 internal error #[display(fmt = "Internal Web3 error")] Internal, + /// Transaction reverted + #[display(fmt = "Transaction reverted: {}", _0)] + #[from(ignore)] + Revert(String), } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use self::Error::*; match *self { - Unreachable | Decoder(_) | InvalidResponse(_) | Transport { .. } | Internal => None, + Unreachable | Decoder(_) | InvalidResponse(_) | Transport { .. } | Internal | Revert(_) => None, Rpc(ref e) => Some(e), Io(ref e) => Some(e), Recovery(ref e) => Some(e), @@ -79,6 +83,7 @@ impl Clone for Error { Io(e) => Io(IoError::from(e.kind())), Recovery(e) => Recovery(e.clone()), Internal => Internal, + Revert(s) => Revert(s.clone()), } } } @@ -94,6 +99,7 @@ impl PartialEq for Error { (Rpc(a), Rpc(b)) => a == b, (Io(a), Io(b)) => a.kind() == b.kind(), (Recovery(a), Recovery(b)) => a == b, + (Revert(a), Revert(b)) => a == b, _ => false, } } diff --git a/src/types/transaction.rs b/src/types/transaction.rs index e481979a..71fa554f 100644 --- a/src/types/transaction.rs +++ b/src/types/transaction.rs @@ -108,6 +108,16 @@ pub struct Receipt { /// Effective gas price #[serde(rename = "effectiveGasPrice")] pub effective_gas_price: Option, + /// Transaction revert reason + #[serde(rename = "revertReason")] + pub revert_reason: Option, +} + +impl Receipt { + /// Checks transaction execution reverted + pub fn is_txn_reverted(&self) -> bool { + self.status == Some(0.into()) + } } /// Raw bytes of a signed, but not yet sent transaction