use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::token_service_client::TokenServiceClient;
use tonic::transport::Channel;
use crate::entity_id::AutoValidateChecksum;
use crate::protobuf::ToProtobuf;
use crate::token::custom_fees::AnyCustomFee;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
Error,
LedgerId,
TokenId,
Transaction,
TransactionId,
};
pub type TokenFeeScheduleUpdateTransaction = Transaction<TokenFeeScheduleUpdateTransactionData>;
#[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"))]
pub struct TokenFeeScheduleUpdateTransactionData {
token_id: Option<TokenId>,
custom_fees: Vec<AnyCustomFee>,
}
impl TokenFeeScheduleUpdateTransaction {
pub fn token_id(&mut self, token_id: impl Into<TokenId>) -> &mut Self {
self.body.data.token_id = Some(token_id.into());
self
}
pub fn custom_fees(
&mut self,
custom_fees: impl IntoIterator<Item = AnyCustomFee>,
) -> &mut Self {
self.body.data.custom_fees = custom_fees.into_iter().collect();
self
}
}
#[async_trait]
impl TransactionExecute for TokenFeeScheduleUpdateTransactionData {
fn validate_checksums_for_ledger_id(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.token_id.validate_checksum_for_ledger_id(ledger_id)
}
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
TokenServiceClient::new(channel).update_token_fee_schedule(request).await
}
}
impl ToTransactionDataProtobuf for TokenFeeScheduleUpdateTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &TransactionId,
) -> services::transaction_body::Data {
let token_id = self.token_id.to_protobuf();
let custom_fees = self.custom_fees.to_protobuf();
services::transaction_body::Data::TokenFeeScheduleUpdate(
services::TokenFeeScheduleUpdateTransactionBody { token_id, custom_fees },
)
}
}
impl From<TokenFeeScheduleUpdateTransactionData> for AnyTransactionData {
fn from(transaction: TokenFeeScheduleUpdateTransactionData) -> Self {
Self::TokenFeeScheduleUpdate(transaction)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::token::custom_fees::{
CustomFee,
FixedFeeData,
};
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
AccountId,
TokenFeeScheduleUpdateTransaction,
TokenId,
};
const TOKEN_FEE_SCHEDULE_UPDATE_TRANSACTION_JSON: &str = r#"{
"$type": "tokenFeeScheduleUpdate",
"tokenId": "0.0.1001",
"customFees": [
{
"$type": "fixed",
"amount": 1,
"denominatingTokenId": "0.0.7",
"feeCollectorAccountId": "0.0.8",
"allCollectorsAreExempt": false
}
]
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = TokenFeeScheduleUpdateTransaction::new();
transaction.token_id(TokenId::from(1001)).custom_fees([CustomFee {
fee: FixedFeeData { amount: 1, denominating_token_id: TokenId::from(7) }.into(),
fee_collector_account_id: Some(AccountId::from(8)),
all_collectors_are_exempt: false,
}]);
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, TOKEN_FEE_SCHEDULE_UPDATE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(TOKEN_FEE_SCHEDULE_UPDATE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.body.data, AnyTransactionData::TokenFeeScheduleUpdate(transaction) => transaction);
assert_eq!(data.token_id.unwrap(), TokenId::from(1001));
assert_eq!(
data.custom_fees,
[CustomFee {
fee: FixedFeeData { amount: 1, denominating_token_id: TokenId::from(7) }.into(),
fee_collector_account_id: Some(AccountId::from(8)),
all_collectors_are_exempt: false,
}]
);
Ok(())
}
}
}