use hiero_sdk_proto::services;
use crate::hooks::{
FungibleHookCall,
FungibleHookType,
};
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::{
AccountId,
Hbar,
};
#[derive(Debug, Clone)]
pub struct Transfer {
pub account_id: AccountId,
pub amount: Hbar,
pub is_approved: bool,
pub hook_call: Option<FungibleHookCall>,
}
impl FromProtobuf<services::AccountAmount> for Transfer {
fn from_protobuf(pb: services::AccountAmount) -> crate::Result<Self>
where
Self: Sized,
{
let hook_call = match pb.hook_call {
Some(services::account_amount::HookCall::PreTxAllowanceHook(hook)) => {
Some(FungibleHookCall::from_protobuf_with_type(
hook,
FungibleHookType::PreTxAllowanceHook,
)?)
}
Some(services::account_amount::HookCall::PrePostTxAllowanceHook(hook)) => {
Some(FungibleHookCall::from_protobuf_with_type(
hook,
FungibleHookType::PrePostTxAllowanceHook,
)?)
}
None => None,
};
Ok(Self {
account_id: AccountId::from_protobuf(pb_getf!(pb, account_id)?)?,
amount: Hbar::from_tinybars(pb.amount),
is_approved: pb.is_approval,
hook_call,
})
}
}
impl ToProtobuf for Transfer {
type Protobuf = services::AccountAmount;
fn to_protobuf(&self) -> Self::Protobuf {
let hook_call = self.hook_call.as_ref().map(|hook| match hook.hook_type {
FungibleHookType::PreTxAllowanceHook => {
services::account_amount::HookCall::PreTxAllowanceHook(hook.to_protobuf())
}
FungibleHookType::PrePostTxAllowanceHook => {
services::account_amount::HookCall::PrePostTxAllowanceHook(hook.to_protobuf())
}
});
services::AccountAmount {
account_id: Some(self.account_id.to_protobuf()),
amount: self.amount.to_tinybars(),
is_approval: self.is_approved,
hook_call,
}
}
}