Skip to main content

hiero_sdk/schedule/
schedule_delete_transaction.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use hiero_sdk_proto::services;
4use hiero_sdk_proto::services::schedule_service_client::ScheduleServiceClient;
5use tonic::transport::Channel;
6
7use crate::ledger_id::RefLedgerId;
8use crate::protobuf::{
9    FromProtobuf,
10    ToProtobuf,
11};
12use crate::transaction::{
13    AnyTransactionData,
14    ChunkInfo,
15    ToSchedulableTransactionDataProtobuf,
16    ToTransactionDataProtobuf,
17    TransactionData,
18    TransactionExecute,
19};
20use crate::{
21    BoxGrpcFuture,
22    Error,
23    ScheduleId,
24    Transaction,
25    ValidateChecksums,
26};
27
28/// Marks a schedule in the network's action queue as deleted. Must be signed
29/// by the admin key of the target schedule. A deleted schedule cannot
30/// receive any additional signing keys, nor will it be executed.
31pub type ScheduleDeleteTransaction = Transaction<ScheduleDeleteTransactionData>;
32
33#[derive(Debug, Default, Clone)]
34pub struct ScheduleDeleteTransactionData {
35    schedule_id: Option<ScheduleId>,
36}
37
38impl ScheduleDeleteTransaction {
39    /// Returns the schedule to delete.
40    #[must_use]
41    pub fn get_schedule_id(&self) -> Option<ScheduleId> {
42        self.data().schedule_id
43    }
44
45    /// Sets the schedule to delete.
46    pub fn schedule_id(&mut self, id: ScheduleId) -> &mut Self {
47        self.data_mut().schedule_id = Some(id);
48        self
49    }
50}
51impl TransactionData for ScheduleDeleteTransactionData {}
52
53impl TransactionExecute for ScheduleDeleteTransactionData {
54    fn execute(
55        &self,
56        channel: Channel,
57        request: services::Transaction,
58    ) -> BoxGrpcFuture<'_, services::TransactionResponse> {
59        Box::pin(async { ScheduleServiceClient::new(channel).delete_schedule(request).await })
60    }
61}
62
63impl ValidateChecksums for ScheduleDeleteTransactionData {
64    fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
65        self.schedule_id.validate_checksums(ledger_id)
66    }
67}
68
69impl ToTransactionDataProtobuf for ScheduleDeleteTransactionData {
70    fn to_transaction_data_protobuf(
71        &self,
72        chunk_info: &ChunkInfo,
73    ) -> services::transaction_body::Data {
74        let _ = chunk_info.assert_single_transaction();
75
76        services::transaction_body::Data::ScheduleDelete(self.to_protobuf())
77    }
78}
79
80impl ToSchedulableTransactionDataProtobuf for ScheduleDeleteTransactionData {
81    fn to_schedulable_transaction_data_protobuf(
82        &self,
83    ) -> services::schedulable_transaction_body::Data {
84        services::schedulable_transaction_body::Data::ScheduleDelete(self.to_protobuf())
85    }
86}
87
88impl From<ScheduleDeleteTransactionData> for AnyTransactionData {
89    fn from(transaction: ScheduleDeleteTransactionData) -> Self {
90        Self::ScheduleDelete(transaction)
91    }
92}
93
94impl FromProtobuf<services::ScheduleDeleteTransactionBody> for ScheduleDeleteTransactionData {
95    fn from_protobuf(pb: services::ScheduleDeleteTransactionBody) -> crate::Result<Self> {
96        Ok(Self { schedule_id: Option::from_protobuf(pb.schedule_id)? })
97    }
98}
99
100impl ToProtobuf for ScheduleDeleteTransactionData {
101    type Protobuf = services::ScheduleDeleteTransactionBody;
102
103    fn to_protobuf(&self) -> Self::Protobuf {
104        services::ScheduleDeleteTransactionBody { schedule_id: self.schedule_id.to_protobuf() }
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use expect_test::expect;
111    use hiero_sdk_proto::services;
112
113    use super::ScheduleDeleteTransactionData;
114    use crate::protobuf::{
115        FromProtobuf,
116        ToProtobuf,
117    };
118    use crate::transaction::test_helpers::{
119        check_body,
120        transaction_body,
121    };
122    use crate::{
123        AnyTransaction,
124        ScheduleDeleteTransaction,
125        ScheduleId,
126    };
127
128    const SCHEDULE_ID: ScheduleId = ScheduleId::new(0, 0, 444);
129
130    fn make_transaction() -> ScheduleDeleteTransaction {
131        let mut tx = ScheduleDeleteTransaction::new_for_tests();
132
133        tx.schedule_id(SCHEDULE_ID).freeze().unwrap();
134
135        tx
136    }
137
138    #[test]
139    fn serialize() {
140        let tx = make_transaction();
141
142        let tx = transaction_body(tx);
143
144        let tx = check_body(tx);
145
146        expect![[r#"
147            ScheduleDelete(
148                ScheduleDeleteTransactionBody {
149                    schedule_id: Some(
150                        ScheduleId {
151                            shard_num: 0,
152                            realm_num: 0,
153                            schedule_num: 444,
154                        },
155                    ),
156                },
157            )
158        "#]]
159        .assert_debug_eq(&tx)
160    }
161
162    #[test]
163    fn to_from_bytes() {
164        let tx = make_transaction();
165
166        let tx2 = AnyTransaction::from_bytes(&tx.to_bytes().unwrap()).unwrap();
167
168        let tx = transaction_body(tx);
169
170        let tx2 = transaction_body(tx2);
171
172        assert_eq!(tx, tx2);
173    }
174
175    #[test]
176    fn from_proto_body() {
177        let tx = services::ScheduleDeleteTransactionBody {
178            schedule_id: Some(SCHEDULE_ID.to_protobuf()),
179        };
180
181        let tx = ScheduleDeleteTransactionData::from_protobuf(tx).unwrap();
182
183        assert_eq!(tx.schedule_id, Some(SCHEDULE_ID));
184    }
185
186    #[test]
187    fn get_set_schedule_id() {
188        let mut tx = ScheduleDeleteTransaction::new();
189        tx.schedule_id(SCHEDULE_ID);
190
191        assert_eq!(tx.get_schedule_id(), Some(SCHEDULE_ID));
192    }
193
194    #[test]
195    #[should_panic]
196    fn get_set_schedule_id_frozen_panics() {
197        make_transaction().schedule_id(SCHEDULE_ID);
198    }
199}