use hedera_proto::services;
use hedera_proto::services::token_service_client::TokenServiceClient;
use tonic::transport::Channel;
use crate::ledger_id::RefLedgerId;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
AccountId,
BoxGrpcFuture,
Error,
TokenId,
Transaction,
ValidateChecksums,
};
pub type TokenGrantKycTransaction = Transaction<TokenGrantKycTransactionData>;
#[derive(Debug, Clone, Default)]
pub struct TokenGrantKycTransactionData {
account_id: Option<AccountId>,
token_id: Option<TokenId>,
}
impl TokenGrantKycTransaction {
#[must_use]
pub fn get_account_id(&self) -> Option<AccountId> {
self.data().account_id
}
pub fn account_id(&mut self, account_id: AccountId) -> &mut Self {
self.data_mut().account_id = Some(account_id);
self
}
#[must_use]
pub fn get_token_id(&self) -> Option<TokenId> {
self.data().token_id
}
pub fn token_id(&mut self, token_id: impl Into<TokenId>) -> &mut Self {
self.data_mut().token_id = Some(token_id.into());
self
}
}
impl TransactionData for TokenGrantKycTransactionData {}
impl TransactionExecute for TokenGrantKycTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async {
TokenServiceClient::new(channel).grant_kyc_to_token_account(request).await
})
}
}
impl ValidateChecksums for TokenGrantKycTransactionData {
fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
self.account_id.validate_checksums(ledger_id)?;
self.token_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for TokenGrantKycTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::TokenGrantKyc(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for TokenGrantKycTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::TokenGrantKyc(self.to_protobuf())
}
}
impl From<TokenGrantKycTransactionData> for AnyTransactionData {
fn from(transaction: TokenGrantKycTransactionData) -> Self {
Self::TokenGrantKyc(transaction)
}
}
impl FromProtobuf<services::TokenGrantKycTransactionBody> for TokenGrantKycTransactionData {
fn from_protobuf(pb: services::TokenGrantKycTransactionBody) -> crate::Result<Self> {
Ok(Self {
account_id: Option::from_protobuf(pb.account)?,
token_id: Option::from_protobuf(pb.token)?,
})
}
}
impl ToProtobuf for TokenGrantKycTransactionData {
type Protobuf = services::TokenGrantKycTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
services::TokenGrantKycTransactionBody {
token: self.token_id.to_protobuf(),
account: self.account_id.to_protobuf(),
}
}
}