hiero_sdk/schedule/
schedule_sign_transaction.rs1use hiero_sdk_proto::services;
4use hiero_sdk_proto::services::schedule_service_client::ScheduleServiceClient;
5use tonic::transport::Channel;
6
7use crate::protobuf::{
8 FromProtobuf,
9 ToProtobuf,
10};
11use crate::transaction::{
12 AnyTransactionData,
13 ChunkInfo,
14 ToTransactionDataProtobuf,
15 TransactionData,
16 TransactionExecute,
17};
18use crate::{
19 BoxGrpcFuture,
20 Error,
21 ScheduleId,
22 Transaction,
23 ValidateChecksums,
24};
25
26pub type ScheduleSignTransaction = Transaction<ScheduleSignTransactionData>;
28
29#[derive(Debug, Default, Clone)]
30pub struct ScheduleSignTransactionData {
31 schedule_id: Option<ScheduleId>,
32}
33
34impl ScheduleSignTransaction {
35 #[must_use]
37 pub fn get_schedule_id(&self) -> Option<ScheduleId> {
38 self.data().schedule_id
39 }
40
41 pub fn schedule_id(&mut self, id: ScheduleId) -> &mut Self {
43 self.data_mut().schedule_id = Some(id);
44 self
45 }
46}
47
48impl TransactionData for ScheduleSignTransactionData {}
49
50impl TransactionExecute for ScheduleSignTransactionData {
51 fn execute(
52 &self,
53 channel: Channel,
54 request: services::Transaction,
55 ) -> BoxGrpcFuture<'_, services::TransactionResponse> {
56 Box::pin(async { ScheduleServiceClient::new(channel).delete_schedule(request).await })
57 }
58}
59
60impl ValidateChecksums for ScheduleSignTransactionData {
61 fn validate_checksums(&self, ledger_id: &crate::ledger_id::RefLedgerId) -> Result<(), Error> {
62 self.schedule_id.validate_checksums(ledger_id)
63 }
64}
65
66impl ToTransactionDataProtobuf for ScheduleSignTransactionData {
67 fn to_transaction_data_protobuf(
68 &self,
69 chunk_info: &ChunkInfo,
70 ) -> services::transaction_body::Data {
71 let _ = chunk_info.assert_single_transaction();
72
73 let schedule_id = self.schedule_id.to_protobuf();
74
75 services::transaction_body::Data::ScheduleSign(services::ScheduleSignTransactionBody {
76 schedule_id,
77 })
78 }
79}
80
81impl From<ScheduleSignTransactionData> for AnyTransactionData {
82 fn from(transaction: ScheduleSignTransactionData) -> Self {
83 Self::ScheduleSign(transaction)
84 }
85}
86
87impl FromProtobuf<services::ScheduleSignTransactionBody> for ScheduleSignTransactionData {
88 fn from_protobuf(pb: services::ScheduleSignTransactionBody) -> crate::Result<Self> {
89 Ok(Self { schedule_id: Option::from_protobuf(pb.schedule_id)? })
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use expect_test::expect;
96 use hiero_sdk_proto::services;
97
98 use crate::protobuf::{
99 FromProtobuf,
100 ToProtobuf,
101 };
102 use crate::schedule::ScheduleSignTransactionData;
103 use crate::transaction::test_helpers::{
104 check_body,
105 transaction_body,
106 };
107 use crate::{
108 AnyTransaction,
109 ScheduleId,
110 ScheduleSignTransaction,
111 };
112
113 const SCHEDULE_ID: ScheduleId = ScheduleId::new(0, 0, 444);
114
115 fn make_transaction() -> ScheduleSignTransaction {
116 let mut tx = ScheduleSignTransaction::new_for_tests();
117
118 tx.schedule_id(SCHEDULE_ID).freeze().unwrap();
119
120 tx
121 }
122
123 #[test]
124 fn serialize() {
125 let tx = make_transaction();
126
127 let tx = transaction_body(tx);
128
129 let tx = check_body(tx);
130
131 expect![[r#"
132 ScheduleSign(
133 ScheduleSignTransactionBody {
134 schedule_id: Some(
135 ScheduleId {
136 shard_num: 0,
137 realm_num: 0,
138 schedule_num: 444,
139 },
140 ),
141 },
142 )
143 "#]]
144 .assert_debug_eq(&tx)
145 }
146
147 #[test]
148 fn to_from_bytes() {
149 let tx = make_transaction();
150
151 let tx2 = AnyTransaction::from_bytes(&tx.to_bytes().unwrap()).unwrap();
152
153 let tx = transaction_body(tx);
154
155 let tx2 = transaction_body(tx2);
156
157 assert_eq!(tx, tx2);
158 }
159
160 #[test]
161 fn from_proto_body() {
162 let tx =
163 services::ScheduleSignTransactionBody { schedule_id: Some(SCHEDULE_ID.to_protobuf()) };
164
165 let tx = ScheduleSignTransactionData::from_protobuf(tx).unwrap();
166
167 assert_eq!(tx.schedule_id, Some(SCHEDULE_ID));
168 }
169
170 #[test]
171 fn get_set_schedule_id() {
172 let mut tx = ScheduleSignTransaction::new();
173 tx.schedule_id(SCHEDULE_ID);
174
175 assert_eq!(tx.get_schedule_id(), Some(SCHEDULE_ID));
176 }
177
178 #[test]
179 #[should_panic]
180 fn get_set_schedule_id_frozen_panics() {
181 make_transaction().schedule_id(SCHEDULE_ID);
182 }
183}