Skip to main content

hiero_sdk/topic/
topic_info_query.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use hiero_sdk_proto::services;
4use hiero_sdk_proto::services::consensus_service_client::ConsensusServiceClient;
5use tonic::transport::Channel;
6
7use crate::ledger_id::RefLedgerId;
8use crate::query::{
9    AnyQueryData,
10    QueryExecute,
11    ToQueryProtobuf,
12};
13use crate::{
14    BoxGrpcFuture,
15    Error,
16    Query,
17    ToProtobuf,
18    TopicId,
19    TopicInfo,
20    ValidateChecksums,
21};
22
23/// Retrieve the latest state of a topic.
24pub type TopicInfoQuery = Query<TopicInfoQueryData>;
25
26#[derive(Default, Clone, Debug)]
27pub struct TopicInfoQueryData {
28    topic_id: Option<TopicId>,
29}
30
31impl From<TopicInfoQueryData> for AnyQueryData {
32    #[inline]
33    fn from(data: TopicInfoQueryData) -> Self {
34        Self::TopicInfo(data)
35    }
36}
37
38impl TopicInfoQuery {
39    /// Returns the topic to retrieve info about.
40    #[must_use]
41    pub fn get_topic_id(&self) -> Option<TopicId> {
42        self.data.topic_id
43    }
44
45    /// Sets the topic to retrieve info about.
46    pub fn topic_id(&mut self, id: impl Into<TopicId>) -> &mut Self {
47        self.data.topic_id = Some(id.into());
48        self
49    }
50}
51
52impl ToQueryProtobuf for TopicInfoQueryData {
53    fn to_query_protobuf(&self, header: services::QueryHeader) -> services::Query {
54        let topic_id = self.topic_id.to_protobuf();
55
56        services::Query {
57            query: Some(services::query::Query::ConsensusGetTopicInfo(
58                services::ConsensusGetTopicInfoQuery { topic_id, header: Some(header) },
59            )),
60        }
61    }
62}
63
64impl QueryExecute for TopicInfoQueryData {
65    type Response = TopicInfo;
66
67    fn execute(
68        &self,
69        channel: Channel,
70        request: services::Query,
71    ) -> BoxGrpcFuture<'_, services::Response> {
72        Box::pin(async { ConsensusServiceClient::new(channel).get_topic_info(request).await })
73    }
74}
75
76impl ValidateChecksums for TopicInfoQueryData {
77    fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
78        self.topic_id.validate_checksums(ledger_id)
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use expect_test::expect;
85
86    use crate::query::ToQueryProtobuf;
87    use crate::{
88        Hbar,
89        TopicId,
90        TopicInfoQuery,
91    };
92
93    #[test]
94    fn serialize() {
95        expect![[r#"
96            Query {
97                query: Some(
98                    ConsensusGetTopicInfo(
99                        ConsensusGetTopicInfoQuery {
100                            header: Some(
101                                QueryHeader {
102                                    payment: None,
103                                    response_type: AnswerOnly,
104                                },
105                            ),
106                            topic_id: Some(
107                                TopicId {
108                                    shard_num: 0,
109                                    realm_num: 0,
110                                    topic_num: 5005,
111                                },
112                            ),
113                        },
114                    ),
115                ),
116            }
117        "#]]
118        .assert_debug_eq(
119            &TopicInfoQuery::new()
120                .topic_id(TopicId::new(0, 0, 5005))
121                .max_payment_amount(Hbar::from_tinybars(100_000))
122                .data
123                .to_query_protobuf(Default::default()),
124        )
125    }
126
127    #[test]
128    fn get_set_topic_id() {
129        let mut query = TopicInfoQuery::new();
130        query.topic_id(TopicId::new(0, 0, 5005));
131
132        assert_eq!(query.get_topic_id(), Some(TopicId::new(0, 0, 5005)));
133    }
134}