cometbft_rpc/endpoint/
tx_search.rs

1//! `/tx_search` endpoint JSON-RPC wrapper
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    dialect::{self, Dialect},
7    prelude::*,
8    request::RequestMessage,
9    serializers, Method, Order,
10};
11
12pub use super::tx;
13
14/// Request for searching for transactions with their results.
15#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
16pub struct Request {
17    pub query: String,
18    pub prove: bool,
19    #[serde(with = "serializers::from_str")]
20    pub page: u32,
21    #[serde(with = "serializers::from_str")]
22    pub per_page: u8,
23    pub order_by: Order,
24}
25
26impl Request {
27    /// Constructor.
28    pub fn new(
29        query: impl ToString,
30        prove: bool,
31        page: u32,
32        per_page: u8,
33        order_by: Order,
34    ) -> Self {
35        Self {
36            query: query.to_string(),
37            prove,
38            page,
39            per_page,
40            order_by,
41        }
42    }
43}
44
45impl RequestMessage for Request {
46    fn method(&self) -> Method {
47        Method::TxSearch
48    }
49}
50
51impl crate::Request<dialect::v0_34::Dialect> for Request {
52    type Response = self::v0_34::DialectResponse;
53}
54
55impl crate::Request<dialect::v1::Dialect> for Request {
56    type Response = Response;
57}
58
59impl<S: Dialect> crate::SimpleRequest<S> for Request
60where
61    Self: crate::Request<S>,
62    Response: From<Self::Response>,
63{
64    type Output = Response;
65}
66
67#[derive(Clone, Debug, Serialize, Deserialize)]
68pub struct Response {
69    pub txs: Vec<tx::Response>,
70    #[serde(with = "serializers::from_str")]
71    pub total_count: u32,
72}
73
74impl crate::Response for Response {}
75
76/// Serialization for /tx_search endpoint format in CometBFT 0.34
77pub mod v0_34 {
78    use super::{tx, Response};
79    use crate::prelude::*;
80    use crate::serializers;
81    use serde::{Deserialize, Serialize};
82
83    /// RPC dialect helper for serialization of the response.
84    #[derive(Debug, Deserialize, Serialize)]
85    pub struct DialectResponse {
86        pub txs: Vec<tx::v0_34::DialectResponse>,
87        #[serde(with = "serializers::from_str")]
88        pub total_count: u32,
89    }
90
91    impl crate::Response for DialectResponse {}
92
93    impl From<DialectResponse> for Response {
94        fn from(msg: DialectResponse) -> Self {
95            Self {
96                txs: msg.txs.into_iter().map(Into::into).collect(),
97                total_count: msg.total_count,
98            }
99        }
100    }
101}