use hedera_proto::services;
use hedera_proto::services::file_service_client::FileServiceClient;
use hedera_proto::services::smart_contract_service_client::SmartContractServiceClient;
use tonic::transport::Channel;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
BoxGrpcFuture,
ContractId,
Error,
FileId,
LedgerId,
Transaction,
ValidateChecksums,
};
pub type SystemUndeleteTransaction = Transaction<SystemUndeleteTransactionData>;
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(default, rename_all = "camelCase"))]
pub struct SystemUndeleteTransactionData {
file_id: Option<FileId>,
contract_id: Option<ContractId>,
}
impl SystemUndeleteTransaction {
#[must_use]
pub fn get_contract_id(&self) -> Option<ContractId> {
self.data().contract_id
}
pub fn contract_id(&mut self, id: impl Into<ContractId>) -> &mut Self {
let data = self.data_mut();
data.file_id = None;
data.contract_id = Some(id.into());
self
}
#[must_use]
pub fn get_file_id(&self) -> Option<FileId> {
self.data().file_id
}
pub fn file_id(&mut self, id: impl Into<FileId>) -> &mut Self {
let data = self.data_mut();
data.contract_id = None;
data.file_id = Some(id.into());
self
}
}
impl TransactionData for SystemUndeleteTransactionData {}
impl TransactionExecute for SystemUndeleteTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async move {
if self.file_id.is_some() {
FileServiceClient::new(channel).system_undelete(request).await
} else {
SmartContractServiceClient::new(channel).system_undelete(request).await
}
})
}
}
impl ValidateChecksums for SystemUndeleteTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.contract_id.validate_checksums(ledger_id)?;
self.file_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for SystemUndeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::SystemUndelete(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for SystemUndeleteTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::SystemUndelete(self.to_protobuf())
}
}
impl From<SystemUndeleteTransactionData> for AnyTransactionData {
fn from(transaction: SystemUndeleteTransactionData) -> Self {
Self::SystemUndelete(transaction)
}
}
impl FromProtobuf<services::SystemUndeleteTransactionBody> for SystemUndeleteTransactionData {
fn from_protobuf(pb: services::SystemUndeleteTransactionBody) -> crate::Result<Self> {
use services::system_undelete_transaction_body::Id;
let (file_id, contract_id) = match pb.id {
Some(Id::FileId(it)) => (Some(FileId::from_protobuf(it)?), None),
Some(Id::ContractId(it)) => (None, Some(ContractId::from_protobuf(it)?)),
None => (None, None),
};
Ok(Self { file_id, contract_id })
}
}
impl ToProtobuf for SystemUndeleteTransactionData {
type Protobuf = services::SystemUndeleteTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
let contract_id = self.contract_id.to_protobuf();
let file_id = self.file_id.to_protobuf();
let id = match (contract_id, file_id) {
(Some(contract_id), _) => {
Some(services::system_undelete_transaction_body::Id::ContractId(contract_id))
}
(_, Some(file_id)) => {
Some(services::system_undelete_transaction_body::Id::FileId(file_id))
}
_ => None,
};
services::SystemUndeleteTransactionBody { id }
}
}