use hedera_proto::services;
use hedera_proto::services::crypto_service_client::CryptoServiceClient;
use tonic::transport::Channel;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
AccountId,
BoxGrpcFuture,
Error,
LedgerId,
NftId,
TokenId,
Transaction,
ValidateChecksums,
};
pub type AccountAllowanceDeleteTransaction = Transaction<AccountAllowanceDeleteTransactionData>;
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase", default))]
pub struct AccountAllowanceDeleteTransactionData {
nft_allowances: Vec<NftRemoveAllowance>,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct NftRemoveAllowance {
pub token_id: TokenId,
pub owner_account_id: AccountId,
pub serials: Vec<i64>,
}
impl AccountAllowanceDeleteTransaction {
#[must_use]
pub fn get_nft_allowances(&self) -> &[NftRemoveAllowance] {
&self.data().nft_allowances
}
pub fn delete_all_token_nft_allowances(
&mut self,
nft_id: NftId,
owner_account_id: AccountId,
) -> &mut Self {
let data = self.data_mut();
let owner_account_id = owner_account_id;
if let Some(allowance) = data.nft_allowances.iter_mut().find(|allowance| {
allowance.token_id == nft_id.token_id && allowance.owner_account_id == owner_account_id
}) {
allowance.serials.push(nft_id.serial as i64);
} else {
data.nft_allowances.push(NftRemoveAllowance {
token_id: nft_id.token_id,
serials: vec![nft_id.serial as i64],
owner_account_id,
});
}
self
}
}
impl TransactionData for AccountAllowanceDeleteTransactionData {}
impl TransactionExecute for AccountAllowanceDeleteTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { CryptoServiceClient::new(channel).delete_allowances(request).await })
}
}
impl ValidateChecksums for AccountAllowanceDeleteTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
for allowance in &self.nft_allowances {
allowance.token_id.validate_checksums(ledger_id)?;
allowance.owner_account_id.validate_checksums(ledger_id)?;
}
Ok(())
}
}
impl ToTransactionDataProtobuf for AccountAllowanceDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::CryptoDeleteAllowance(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for AccountAllowanceDeleteTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::CryptoDeleteAllowance(self.to_protobuf())
}
}
impl From<AccountAllowanceDeleteTransactionData> for AnyTransactionData {
fn from(transaction: AccountAllowanceDeleteTransactionData) -> Self {
Self::AccountAllowanceDelete(transaction)
}
}
impl FromProtobuf<services::CryptoDeleteAllowanceTransactionBody>
for AccountAllowanceDeleteTransactionData
{
fn from_protobuf(pb: services::CryptoDeleteAllowanceTransactionBody) -> crate::Result<Self> {
Ok(Self { nft_allowances: Vec::from_protobuf(pb.nft_allowances)? })
}
}
impl ToProtobuf for AccountAllowanceDeleteTransactionData {
type Protobuf = services::CryptoDeleteAllowanceTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
services::CryptoDeleteAllowanceTransactionBody {
nft_allowances: self.nft_allowances.to_protobuf(),
}
}
}
impl FromProtobuf<services::NftRemoveAllowance> for NftRemoveAllowance {
fn from_protobuf(pb: services::NftRemoveAllowance) -> crate::Result<Self>
where
Self: Sized,
{
Ok(Self {
token_id: TokenId::from_protobuf(pb_getf!(pb, token_id)?)?,
owner_account_id: AccountId::from_protobuf(pb_getf!(pb, owner)?)?,
serials: pb.serial_numbers,
})
}
}
impl ToProtobuf for NftRemoveAllowance {
type Protobuf = services::NftRemoveAllowance;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
token_id: Some(self.token_id.to_protobuf()),
owner: Some(self.owner_account_id.to_protobuf()),
serial_numbers: self.serials.clone(),
}
}
}