use hedera_proto::services;
use prost::Message;
use time::{
Duration,
OffsetDateTime,
};
use crate::protobuf::ToProtobuf;
use crate::{
AccountId,
ContractId,
FromProtobuf,
Key,
LedgerId,
StakingInfo,
};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct ContractInfo {
pub contract_id: ContractId,
pub account_id: AccountId,
pub contract_account_id: String,
pub admin_key: Option<Key>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::TimestampNanoSeconds>>")
)]
pub expiration_time: Option<OffsetDateTime>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::DurationSeconds<i64>>>")
)]
pub auto_renew_period: Option<Duration>,
pub storage: u64,
pub contract_memo: String,
pub balance: u64,
pub is_deleted: bool,
pub auto_renew_account_id: Option<AccountId>,
pub max_automatic_token_associations: u32,
pub ledger_id: LedgerId,
pub staking_info: Option<StakingInfo>,
}
impl ContractInfo {
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::<services::contract_get_info_response::ContractInfo>::from_bytes(bytes)
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
#[allow(deprecated)]
services::contract_get_info_response::ContractInfo {
contract_id: Some(self.contract_id.to_protobuf()),
account_id: Some(self.account_id.to_protobuf()),
contract_account_id: self.contract_account_id.clone(),
admin_key: self.admin_key.to_protobuf(),
expiration_time: self.expiration_time.to_protobuf(),
auto_renew_period: self.auto_renew_period.to_protobuf(),
storage: self.storage as i64,
memo: self.contract_memo.clone(),
balance: self.balance,
deleted: self.is_deleted,
ledger_id: self.ledger_id.to_bytes(),
auto_renew_account_id: self.auto_renew_account_id.to_protobuf(),
max_automatic_token_associations: self.max_automatic_token_associations as i32,
staking_info: self.staking_info.to_protobuf(),
token_relationships: Vec::new(),
}
.encode_to_vec()
}
}
impl FromProtobuf<services::response::Response> for ContractInfo {
#[allow(deprecated)]
fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
where
Self: Sized,
{
let response = pb_getv!(pb, ContractGetInfo, services::response::Response);
let info = pb_getf!(response, contract_info)?;
Self::from_protobuf(info)
}
}
impl FromProtobuf<services::contract_get_info_response::ContractInfo> for ContractInfo {
#[allow(deprecated)]
fn from_protobuf(pb: services::contract_get_info_response::ContractInfo) -> crate::Result<Self>
where
Self: Sized,
{
let contract_id = pb_getf!(pb, contract_id)?;
let account_id = pb_getf!(pb, account_id)?;
let expiration_time = pb.expiration_time.map(Into::into);
let auto_renew_period = pb.auto_renew_period.map(Into::into);
let auto_renew_account_id = Option::from_protobuf(pb.auto_renew_account_id)?;
let admin_key = Option::from_protobuf(pb.admin_key)?;
let ledger_id = LedgerId::from_bytes(pb.ledger_id);
let staking_info = Option::from_protobuf(pb.staking_info)?;
Ok(Self {
contract_id: ContractId::from_protobuf(contract_id)?,
account_id: AccountId::from_protobuf(account_id)?,
contract_account_id: pb.contract_account_id,
is_deleted: pb.deleted,
balance: pb.balance,
expiration_time,
auto_renew_period,
auto_renew_account_id,
contract_memo: pb.memo,
max_automatic_token_associations: pb.max_automatic_token_associations as u32,
admin_key,
storage: pb.storage as u64,
ledger_id,
staking_info,
})
}
}