use hedera_proto::services;
use prost::Message;
use time::{
Duration,
OffsetDateTime,
};
use crate::protobuf::ToProtobuf;
use crate::{
AccountId,
FromProtobuf,
Hbar,
Key,
LedgerId,
PublicKey,
StakingInfo,
Tinybar,
};
#[derive(Debug, Clone)]
pub struct AccountInfo {
pub account_id: AccountId,
pub contract_account_id: String,
pub is_deleted: bool,
#[deprecated]
pub proxy_account_id: Option<AccountId>,
pub proxy_received: Hbar,
pub key: Key,
pub balance: Hbar,
#[deprecated]
pub send_record_threshold: Hbar,
#[deprecated]
pub receive_record_threshold: Hbar,
pub is_receiver_signature_required: bool,
pub expiration_time: Option<OffsetDateTime>,
pub auto_renew_period: Option<Duration>,
pub account_memo: String,
pub owned_nfts: u64,
pub max_automatic_token_associations: u32,
pub alias_key: Option<PublicKey>,
pub ethereum_nonce: u64,
pub ledger_id: LedgerId,
pub staking: Option<StakingInfo>,
}
impl AccountInfo {
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::<services::crypto_get_info_response::AccountInfo>::from_bytes(bytes)
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
#[allow(deprecated)]
services::crypto_get_info_response::AccountInfo {
account_id: Some(self.account_id.to_protobuf()),
contract_account_id: self.contract_account_id.clone(),
deleted: self.is_deleted,
proxy_received: self.proxy_received.to_tinybars(),
key: Some(self.key.to_protobuf()),
balance: self.balance.to_tinybars() as u64,
receiver_sig_required: self.is_receiver_signature_required,
expiration_time: self.expiration_time.to_protobuf(),
auto_renew_period: self.auto_renew_period.to_protobuf(),
memo: self.account_memo.clone(),
owned_nfts: self.owned_nfts as i64,
max_automatic_token_associations: self.max_automatic_token_associations as i32,
alias: self.alias_key.as_ref().map(ToProtobuf::to_bytes).unwrap_or_default(),
ledger_id: self.ledger_id.to_bytes(),
ethereum_nonce: self.ethereum_nonce as i64,
staking_info: self.staking.to_protobuf(),
proxy_account_id: self.proxy_account_id.to_protobuf(),
generate_receive_record_threshold: self.receive_record_threshold.to_tinybars() as u64,
generate_send_record_threshold: self.send_record_threshold.to_tinybars() as u64,
live_hashes: Vec::default(),
token_relationships: Vec::default(),
}
.encode_to_vec()
}
}
impl FromProtobuf<services::response::Response> for AccountInfo {
fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
where
Self: Sized,
{
let response = pb_getv!(pb, CryptoGetInfo, services::response::Response);
let info = pb_getf!(response, account_info)?;
Self::from_protobuf(info)
}
}
impl FromProtobuf<services::crypto_get_info_response::AccountInfo> for AccountInfo {
fn from_protobuf(pb: services::crypto_get_info_response::AccountInfo) -> crate::Result<Self>
where
Self: Sized,
{
let key = pb_getf!(pb, key)?;
let account_id = pb_getf!(pb, account_id)?;
let alias_key = PublicKey::from_alias_bytes(&pb.alias)?;
let ledger_id = LedgerId::from_bytes(pb.ledger_id);
let staking = Option::from_protobuf(pb.staking_info)?;
#[allow(deprecated)]
Ok(Self {
ledger_id,
staking,
account_id: AccountId::from_protobuf(account_id)?,
contract_account_id: pb.contract_account_id,
is_deleted: pb.deleted,
proxy_received: Hbar::from_tinybars(pb.proxy_received),
key: Key::from_protobuf(key)?,
balance: Hbar::from_tinybars(pb.balance as Tinybar),
expiration_time: pb.expiration_time.map(Into::into),
auto_renew_period: pb.auto_renew_period.map(Into::into),
account_memo: pb.memo,
owned_nfts: pb.owned_nfts as u64,
max_automatic_token_associations: pb.max_automatic_token_associations as u32,
alias_key,
ethereum_nonce: pb.ethereum_nonce as u64,
is_receiver_signature_required: pb.receiver_sig_required,
proxy_account_id: Option::from_protobuf(pb.proxy_account_id)?,
send_record_threshold: Hbar::from_tinybars(pb.generate_send_record_threshold as i64),
receive_record_threshold: Hbar::from_tinybars(
pb.generate_receive_record_threshold as i64,
),
})
}
}