tendermint_rpc/endpoint/
tx.rs

1//! `/tx` endpoint JSON-RPC wrapper
2
3use serde::{Deserialize, Serialize};
4use tendermint::{abci, block, tx, Hash};
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::v0_37::Dialect> for Request {
41    type Response = Response;
42}
43
44impl crate::Request<dialect::v0_38::Dialect> for Request {
45    type Response = Response;
46}
47
48impl<S: Dialect> crate::SimpleRequest<S> for Request
49where
50    Self: crate::Request<S>,
51    Response: From<Self::Response>,
52{
53    type Output = Response;
54}
55
56#[derive(Clone, Debug, Serialize, Deserialize)]
57pub struct Response {
58    /// The hash of the transaction.
59    ///
60    /// Deserialized from a hex-encoded string (there is a discrepancy between
61    /// the format used for the request and the format used for the response in
62    /// the Tendermint RPC).
63    pub hash: Hash,
64    pub height: block::Height,
65    pub index: u32,
66    pub tx_result: abci::types::ExecTxResult,
67    #[serde(with = "serializers::bytes::base64string")]
68    pub tx: Vec<u8>,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub proof: Option<tx::Proof>,
71}
72
73impl crate::Response for Response {}
74
75/// Serialization for /tx endpoint format in Tendermint 0.34
76pub mod v0_34 {
77    use super::Response;
78    use crate::dialect::v0_34::Event;
79    use crate::prelude::*;
80    use crate::{dialect, serializers};
81    use serde::{Deserialize, Serialize};
82    use tendermint::{block, tx, Hash};
83
84    /// RPC dialect helper for serialization of the response.
85    #[derive(Debug, Deserialize, Serialize)]
86    pub struct DialectResponse {
87        /// The hash of the transaction.
88        ///
89        /// Deserialized from a hex-encoded string (there is a discrepancy between
90        /// the format used for the request and the format used for the response in
91        /// the Tendermint RPC).
92        pub hash: Hash,
93        pub height: block::Height,
94        pub index: u32,
95        pub tx_result: dialect::DeliverTx<Event>,
96        #[serde(with = "serializers::bytes::base64string")]
97        pub tx: Vec<u8>,
98        #[serde(skip_serializing_if = "Option::is_none")]
99        pub proof: Option<tx::Proof>,
100    }
101
102    impl crate::Response for DialectResponse {}
103
104    impl From<DialectResponse> for Response {
105        fn from(msg: DialectResponse) -> Self {
106            Self {
107                hash: msg.hash,
108                height: msg.height,
109                index: msg.index,
110                tx_result: msg.tx_result.into(),
111                tx: msg.tx,
112                proof: msg.proof,
113            }
114        }
115    }
116}