use hiero_sdk_proto::services;
use crate::{
AccountId,
FixedFee,
FixedFeeData,
FromProtobuf,
ToProtobuf,
TokenId,
};
#[derive(Debug, Hash, PartialEq, Eq, Clone, Default)]
pub struct CustomFixedFee {
pub amount: u64,
pub denominating_token_id: Option<TokenId>,
pub fee_collector_account_id: Option<AccountId>,
}
impl CustomFixedFee {
pub fn new(
amount: u64,
denominating_token_id: Option<TokenId>,
fee_collector_account_id: Option<AccountId>,
) -> Self {
Self { amount, denominating_token_id, fee_collector_account_id }
}
}
impl CustomFixedFee {
pub(crate) fn to_fixed_fee_protobuf(&self) -> services::FixedFee {
services::FixedFee {
amount: self.amount as i64,
denominating_token_id: self.denominating_token_id.to_protobuf(),
}
}
}
impl From<CustomFixedFee> for FixedFee {
fn from(v: CustomFixedFee) -> Self {
Self {
fee: FixedFeeData {
amount: v.amount as i64,
denominating_token_id: v.denominating_token_id,
},
fee_collector_account_id: v.fee_collector_account_id,
all_collectors_are_exempt: false,
}
}
}
impl ToProtobuf for CustomFixedFee {
type Protobuf = services::FixedCustomFee;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
fixed_fee: Some(services::FixedFee {
amount: self.amount as i64,
denominating_token_id: self.denominating_token_id.to_protobuf(),
}),
fee_collector_account_id: self.fee_collector_account_id.to_protobuf(),
}
}
}
impl FromProtobuf<services::FixedCustomFee> for CustomFixedFee {
fn from_protobuf(pb: services::FixedCustomFee) -> crate::Result<Self> {
let fee = FixedFeeData::from_protobuf(pb.fixed_fee.unwrap())?;
Ok(Self {
amount: fee.amount as u64,
denominating_token_id: fee.denominating_token_id,
fee_collector_account_id: Option::from_protobuf(pb.fee_collector_account_id)?,
})
}
}
impl FromProtobuf<services::FixedFee> for CustomFixedFee {
fn from_protobuf(pb: services::FixedFee) -> crate::Result<Self> {
let fee = FixedFeeData::from_protobuf(pb)?;
Ok(Self {
amount: fee.amount as u64,
denominating_token_id: fee.denominating_token_id,
fee_collector_account_id: None,
})
}
}