hiero_sdk/contract/
contract_info_query.rs1use hiero_sdk_proto::services;
4use hiero_sdk_proto::services::smart_contract_service_client::SmartContractServiceClient;
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 ContractId,
16 ContractInfo,
17 Error,
18 Query,
19 ToProtobuf,
20 ValidateChecksums,
21};
22
23pub type ContractInfoQuery = Query<ContractInfoQueryData>;
25
26#[derive(Default, Debug, Clone)]
27pub struct ContractInfoQueryData {
28 contract_id: Option<ContractId>,
30}
31
32impl ContractInfoQuery {
33 #[must_use]
35 pub fn get_contract_id(&self) -> Option<ContractId> {
36 self.data.contract_id
37 }
38
39 pub fn contract_id(&mut self, contract_id: ContractId) -> &mut Self {
41 self.data.contract_id = Some(contract_id);
42 self
43 }
44}
45
46impl From<ContractInfoQueryData> for AnyQueryData {
47 #[inline]
48 fn from(data: ContractInfoQueryData) -> Self {
49 Self::ContractInfo(data)
50 }
51}
52
53impl ToQueryProtobuf for ContractInfoQueryData {
54 fn to_query_protobuf(&self, header: services::QueryHeader) -> services::Query {
55 let contract_id = self.contract_id.to_protobuf();
56
57 services::Query {
58 query: Some(services::query::Query::ContractGetInfo(services::ContractGetInfoQuery {
59 contract_id,
60 header: Some(header),
61 })),
62 }
63 }
64}
65
66impl QueryExecute for ContractInfoQueryData {
67 type Response = ContractInfo;
68
69 fn execute(
70 &self,
71 channel: Channel,
72 request: services::Query,
73 ) -> BoxGrpcFuture<'_, services::Response> {
74 Box::pin(async {
75 SmartContractServiceClient::new(channel).get_contract_info(request).await
76 })
77 }
78}
79
80impl ValidateChecksums for ContractInfoQueryData {
81 fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
82 self.contract_id.validate_checksums(ledger_id)
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use expect_test::expect;
89
90 use crate::query::ToQueryProtobuf;
91 use crate::{
92 ContractId,
93 ContractInfoQuery,
94 Hbar,
95 };
96
97 #[test]
98 fn serialize() {
99 expect![[r#"
100 Query {
101 query: Some(
102 ContractGetInfo(
103 ContractGetInfoQuery {
104 header: Some(
105 QueryHeader {
106 payment: None,
107 response_type: AnswerOnly,
108 },
109 ),
110 contract_id: Some(
111 ContractId {
112 shard_num: 0,
113 realm_num: 0,
114 contract: Some(
115 ContractNum(
116 5005,
117 ),
118 ),
119 },
120 ),
121 },
122 ),
123 ),
124 }
125 "#]]
126 .assert_debug_eq(
127 &ContractInfoQuery::new()
128 .contract_id(crate::ContractId {
129 shard: 0,
130 realm: 0,
131 num: 5005,
132 evm_address: None,
133 checksum: None,
134 })
135 .max_payment_amount(Hbar::from_tinybars(100_000))
136 .data
137 .to_query_protobuf(Default::default()),
138 );
139 }
140
141 #[test]
142 fn get_set_contract_id() {
143 let mut query = ContractInfoQuery::new();
144 query.contract_id(ContractId::new(0, 0, 5005));
145
146 assert_eq!(query.get_contract_id(), Some(ContractId::new(0, 0, 5005)));
147 }
148}