cometbft_rpc/endpoint/
tx.rs

1//! `/tx` endpoint JSON-RPC wrapper
2
3use cometbft::{abci, block, tx, Hash};
4use serde::{Deserialize, Serialize};
5
6use crate::dialect::{self, Dialect};
7use crate::{prelude::*, request::RequestMessage, serializers, Method};
8
9/// Request for finding a transaction by its hash.
10#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
11pub struct Request {
12    /// The hash of the transaction we want to find.
13    ///
14    /// Serialized internally into a base64-encoded string before sending to
15    /// the RPC server.
16    #[serde(with = "serializers::tx_hash_base64")]
17    pub hash: Hash,
18    /// Whether or not to include the proofs of the transaction's inclusion in
19    /// the block.
20    pub prove: bool,
21}
22
23impl Request {
24    /// Constructor.
25    pub fn new(hash: Hash, prove: bool) -> Self {
26        Self { hash, prove }
27    }
28}
29
30impl RequestMessage for Request {
31    fn method(&self) -> Method {
32        Method::Tx
33    }
34}
35
36impl crate::Request<dialect::v0_34::Dialect> for Request {
37    type Response = self::v0_34::DialectResponse;
38}
39
40impl crate::Request<dialect::v1::Dialect> for Request {
41    type Response = Response;
42}
43
44impl<S: Dialect> crate::SimpleRequest<S> for Request
45where
46    Self: crate::Request<S>,
47    Response: From<Self::Response>,
48{
49    type Output = Response;
50}
51
52#[derive(Clone, Debug, Serialize, Deserialize)]
53pub struct Response {
54    /// The hash of the transaction.
55    ///
56    /// Deserialized from a hex-encoded string (there is a discrepancy between
57    /// the format used for the request and the format used for the response in
58    /// the CometBFT RPC).
59    pub hash: Hash,
60    pub height: block::Height,
61    pub index: u32,
62    pub tx_result: abci::types::ExecTxResult,
63    #[serde(with = "serializers::bytes::base64string")]
64    pub tx: Vec<u8>,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub proof: Option<tx::Proof>,
67}
68
69impl crate::Response for Response {}
70
71/// Serialization for /tx endpoint format in CometBFT 0.34
72pub mod v0_34 {
73    use super::Response;
74    use crate::dialect::v0_34::Event;
75    use crate::prelude::*;
76    use crate::{dialect, serializers};
77    use cometbft::{block, tx, Hash};
78    use serde::{Deserialize, Serialize};
79
80    /// RPC dialect helper for serialization of the response.
81    #[derive(Debug, Deserialize, Serialize)]
82    pub struct DialectResponse {
83        /// The hash of the transaction.
84        ///
85        /// Deserialized from a hex-encoded string (there is a discrepancy between
86        /// the format used for the request and the format used for the response in
87        /// the CometBFT RPC).
88        pub hash: Hash,
89        pub height: block::Height,
90        pub index: u32,
91        pub tx_result: dialect::DeliverTx<Event>,
92        #[serde(with = "serializers::bytes::base64string")]
93        pub tx: Vec<u8>,
94        #[serde(skip_serializing_if = "Option::is_none")]
95        pub proof: Option<tx::Proof>,
96    }
97
98    impl crate::Response for DialectResponse {}
99
100    impl From<DialectResponse> for Response {
101        fn from(msg: DialectResponse) -> Self {
102            Self {
103                hash: msg.hash,
104                height: msg.height,
105                index: msg.index,
106                tx_result: msg.tx_result.into(),
107                tx: msg.tx,
108                proof: msg.proof,
109            }
110        }
111    }
112}