use hedera_proto::services;
use hedera_proto::services::token_service_client::TokenServiceClient;
use tonic::transport::Channel;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::token::custom_fees::AnyCustomFee;
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
BoxGrpcFuture,
Error,
LedgerId,
TokenId,
Transaction,
ValidateChecksums,
};
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", default))]
pub struct TokenFeeScheduleUpdateTransactionData {
token_id: Option<TokenId>,
custom_fees: Vec<AnyCustomFee>,
}
impl TokenFeeScheduleUpdateTransaction {
#[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
}
#[must_use]
pub fn get_custom_fees(&self) -> &[AnyCustomFee] {
&self.data().custom_fees
}
pub fn custom_fees(
&mut self,
custom_fees: impl IntoIterator<Item = AnyCustomFee>,
) -> &mut Self {
self.data_mut().custom_fees = custom_fees.into_iter().collect();
self
}
}
impl TransactionData for TokenFeeScheduleUpdateTransactionData {}
impl TransactionExecute for TokenFeeScheduleUpdateTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async {
TokenServiceClient::new(channel).update_token_fee_schedule(request).await
})
}
}
impl ValidateChecksums for TokenFeeScheduleUpdateTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.token_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for TokenFeeScheduleUpdateTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::TokenFeeScheduleUpdate(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for TokenFeeScheduleUpdateTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::TokenFeeScheduleUpdate(self.to_protobuf())
}
}
impl From<TokenFeeScheduleUpdateTransactionData> for AnyTransactionData {
fn from(transaction: TokenFeeScheduleUpdateTransactionData) -> Self {
Self::TokenFeeScheduleUpdate(transaction)
}
}
impl FromProtobuf<services::TokenFeeScheduleUpdateTransactionBody>
for TokenFeeScheduleUpdateTransactionData
{
fn from_protobuf(pb: services::TokenFeeScheduleUpdateTransactionBody) -> crate::Result<Self> {
Ok(Self {
token_id: Option::from_protobuf(pb.token_id)?,
custom_fees: Vec::from_protobuf(pb.custom_fees)?,
})
}
}
impl ToProtobuf for TokenFeeScheduleUpdateTransactionData {
type Protobuf = services::TokenFeeScheduleUpdateTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
services::TokenFeeScheduleUpdateTransactionBody {
token_id: self.token_id.to_protobuf(),
custom_fees: self.custom_fees.to_protobuf(),
}
}
}
#[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.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(())
}
}
}