use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::crypto_service_client::CryptoServiceClient;
use tonic::transport::Channel;
use crate::entity_id::AutoValidateChecksum;
use crate::protobuf::ToProtobuf;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
Error,
LedgerId,
Transaction,
};
pub type AccountDeleteTransaction = Transaction<AccountDeleteTransactionData>;
#[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(default, rename_all = "camelCase"))]
pub struct AccountDeleteTransactionData {
pub transfer_account_id: Option<AccountId>,
pub account_id: Option<AccountId>,
}
impl AccountDeleteTransaction {
pub fn account_id(&mut self, id: AccountId) -> &mut Self {
self.body.data.account_id = Some(id);
self
}
pub fn transfer_account_id(&mut self, id: AccountId) -> &mut Self {
self.body.data.transfer_account_id = Some(id);
self
}
}
#[async_trait]
impl TransactionExecute for AccountDeleteTransactionData {
fn validate_checksums_for_ledger_id(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.transfer_account_id.validate_checksum_for_ledger_id(ledger_id)?;
self.account_id.validate_checksum_for_ledger_id(ledger_id)
}
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
CryptoServiceClient::new(channel).crypto_delete(request).await
}
}
impl ToTransactionDataProtobuf for AccountDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &crate::TransactionId,
) -> services::transaction_body::Data {
let account_id = self.account_id.to_protobuf();
let transfer_account_id = self.transfer_account_id.to_protobuf();
services::transaction_body::Data::CryptoDelete(services::CryptoDeleteTransactionBody {
transfer_account_id,
delete_account_id: account_id,
})
}
}
impl From<AccountDeleteTransactionData> for AnyTransactionData {
fn from(transaction: AccountDeleteTransactionData) -> Self {
Self::AccountDelete(transaction)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
AccountDeleteTransaction,
AccountId,
};
const ACCOUNT_DELETE_TRANSACTION_JSON: &str = r#"{
"$type": "accountDelete",
"transferAccountId": "0.0.1001",
"accountId": "0.0.1002"
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = AccountDeleteTransaction::new();
transaction
.transfer_account_id(AccountId::from(1001))
.account_id(AccountId::from(1002));
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, ACCOUNT_DELETE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(ACCOUNT_DELETE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.body.data, AnyTransactionData::AccountDelete(transaction) => transaction);
assert_eq!(data.transfer_account_id, Some(AccountId::from(1001)));
assert_eq!(data.account_id, Some(AccountId::from(1002)));
Ok(())
}
}
}