Skip to content

Commit

Permalink
Feature: add revert reason to receipt (#717)
Browse files Browse the repository at this point in the history
* 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 <artem.ivanov@dsr-corporation.com>
  • Loading branch information
DenisRybas and Artemkaaas authored Sep 11, 2024
1 parent 135a5b3 commit b1ae801
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async fn send_transaction_with_confirmation_<T: Transport>(
.transaction_receipt(hash)
.await?
.expect("receipt can't be null after wait for confirmations; qed");

Ok(receipt)
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()),
}
}
}
Expand All @@ -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,
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ pub struct Receipt {
/// Effective gas price
#[serde(rename = "effectiveGasPrice")]
pub effective_gas_price: Option<U256>,
/// Transaction revert reason
#[serde(rename = "revertReason")]
pub revert_reason: Option<String>,
}

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
Expand Down

0 comments on commit b1ae801

Please sign in to comment.