use hedera_proto::services;
use hedera_proto::services::consensus_service_client::ConsensusServiceClient;
use tonic::transport::Channel;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
BoxGrpcFuture,
Error,
LedgerId,
TopicId,
Transaction,
ValidateChecksums,
};
pub type TopicDeleteTransaction = Transaction<TopicDeleteTransactionData>;
#[cfg_attr(feature = "ffi", serde_with::skip_serializing_none)]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase", default))]
pub struct TopicDeleteTransactionData {
topic_id: Option<TopicId>,
}
impl TopicDeleteTransaction {
#[must_use]
pub fn get_topic_id(&self) -> Option<TopicId> {
self.data().topic_id
}
pub fn topic_id(&mut self, id: impl Into<TopicId>) -> &mut Self {
self.data_mut().topic_id = Some(id.into());
self
}
}
impl TransactionData for TopicDeleteTransactionData {}
impl TransactionExecute for TopicDeleteTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { ConsensusServiceClient::new(channel).delete_topic(request).await })
}
}
impl ValidateChecksums for TopicDeleteTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.topic_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for TopicDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::ConsensusDeleteTopic(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for TopicDeleteTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::ConsensusDeleteTopic(self.to_protobuf())
}
}
impl From<TopicDeleteTransactionData> for AnyTransactionData {
fn from(transaction: TopicDeleteTransactionData) -> Self {
Self::TopicDelete(transaction)
}
}
impl FromProtobuf<services::ConsensusDeleteTopicTransactionBody> for TopicDeleteTransactionData {
fn from_protobuf(pb: services::ConsensusDeleteTopicTransactionBody) -> crate::Result<Self> {
Ok(Self { topic_id: Option::from_protobuf(pb.topic_id)? })
}
}
impl ToProtobuf for TopicDeleteTransactionData {
type Protobuf = services::ConsensusDeleteTopicTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
services::ConsensusDeleteTopicTransactionBody { topic_id: self.topic_id.to_protobuf() }
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
TopicDeleteTransaction,
TopicId,
};
const TOPIC_DELETE_TRANSACTION_JSON: &str = r#"{
"$type": "topicDelete",
"topicId": "0.0.1001"
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = TopicDeleteTransaction::new();
transaction.topic_id(TopicId::from(1001));
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, TOPIC_DELETE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction = serde_json::from_str(TOPIC_DELETE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.data(), AnyTransactionData::TopicDelete(transaction) => transaction);
assert_eq!(data.topic_id.unwrap(), TopicId::from(1001));
Ok(())
}
}
}