use hedera_proto::services;
use hedera_proto::services::crypto_service_client::CryptoServiceClient;
use time::{
Duration,
OffsetDateTime,
};
use tonic::transport::Channel;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::staked_id::StakedId;
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
AccountId,
BoxGrpcFuture,
Error,
Key,
LedgerId,
Transaction,
ValidateChecksums,
};
pub type AccountUpdateTransaction = Transaction<AccountUpdateTransactionData>;
#[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 AccountUpdateTransactionData {
account_id: Option<AccountId>,
key: Option<Key>,
receiver_signature_required: Option<bool>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::DurationSeconds<i64>>>")
)]
auto_renew_period: Option<Duration>,
auto_renew_account_id: Option<AccountId>,
#[deprecated]
proxy_account_id: Option<AccountId>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::TimestampNanoSeconds>>")
)]
expiration_time: Option<OffsetDateTime>,
account_memo: Option<String>,
max_automatic_token_associations: Option<u16>,
#[cfg_attr(feature = "ffi", serde(flatten))]
staked_id: Option<StakedId>,
decline_staking_reward: Option<bool>,
}
impl AccountUpdateTransaction {
#[must_use]
pub fn get_account_id(&self) -> Option<AccountId> {
self.data().account_id
}
pub fn account_id(&mut self, id: AccountId) -> &mut Self {
self.data_mut().account_id = Some(id);
self
}
#[must_use]
pub fn get_expiration_time(&self) -> Option<OffsetDateTime> {
self.data().expiration_time
}
pub fn expiration_time(&mut self, at: OffsetDateTime) -> &mut Self {
self.data_mut().expiration_time = Some(at);
self
}
#[must_use]
pub fn get_key(&self) -> Option<&Key> {
self.data().key.as_ref()
}
pub fn key(&mut self, key: impl Into<Key>) -> &mut Self {
self.data_mut().key = Some(key.into());
self
}
#[must_use]
pub fn get_receiver_signature_required(&self) -> Option<bool> {
self.data().receiver_signature_required
}
pub fn receiver_signature_required(&mut self, required: bool) -> &mut Self {
self.data_mut().receiver_signature_required = Some(required);
self
}
#[deprecated]
#[allow(deprecated)]
#[must_use]
pub fn get_proxy_account_id(&self) -> Option<AccountId> {
self.data().proxy_account_id
}
#[deprecated]
#[allow(deprecated)]
pub fn proxy_account_id(&mut self, proxy_account_id: AccountId) -> &mut Self {
self.data_mut().proxy_account_id = Some(proxy_account_id);
self
}
#[must_use]
pub fn get_auto_renew_period(&self) -> Option<Duration> {
self.data().auto_renew_period
}
pub fn auto_renew_period(&mut self, period: Duration) -> &mut Self {
self.data_mut().auto_renew_period = Some(period);
self
}
#[must_use]
pub fn get_auto_renew_account_id(&self) -> Option<AccountId> {
self.data().auto_renew_account_id
}
pub fn auto_renew_account_id(&mut self, id: AccountId) -> &mut Self {
self.data_mut().auto_renew_account_id = Some(id);
self
}
#[must_use]
pub fn get_account_memo(&self) -> Option<&str> {
self.data().account_memo.as_deref()
}
pub fn account_memo(&mut self, memo: impl Into<String>) -> &mut Self {
self.data_mut().account_memo = Some(memo.into());
self
}
#[must_use]
pub fn get_max_automatic_token_associations(&self) -> Option<u16> {
self.data().max_automatic_token_associations
}
pub fn max_automatic_token_associations(&mut self, amount: u16) -> &mut Self {
self.data_mut().max_automatic_token_associations = Some(amount);
self
}
#[must_use]
pub fn get_staked_account_id(&self) -> Option<AccountId> {
self.data().staked_id.and_then(|it| it.to_account_id())
}
pub fn staked_account_id(&mut self, id: AccountId) -> &mut Self {
self.data_mut().staked_id = Some(id.into());
self
}
#[must_use]
pub fn get_staked_node_id(&self) -> Option<u64> {
self.data().staked_id.and_then(|it| it.to_node_id())
}
pub fn staked_node_id(&mut self, id: u64) -> &mut Self {
self.data_mut().staked_id = Some(id.into());
self
}
#[must_use]
pub fn get_decline_staking_reward(&self) -> Option<bool> {
self.data().decline_staking_reward
}
pub fn decline_staking_reward(&mut self, decline: bool) -> &mut Self {
self.data_mut().decline_staking_reward = Some(decline);
self
}
}
impl TransactionData for AccountUpdateTransactionData {}
impl TransactionExecute for AccountUpdateTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { CryptoServiceClient::new(channel).update_account(request).await })
}
}
impl ValidateChecksums for AccountUpdateTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.account_id.validate_checksums(ledger_id)?;
self.staked_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for AccountUpdateTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::CryptoUpdateAccount(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for AccountUpdateTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::CryptoUpdateAccount(self.to_protobuf())
}
}
impl From<AccountUpdateTransactionData> for AnyTransactionData {
fn from(transaction: AccountUpdateTransactionData) -> Self {
Self::AccountUpdate(transaction)
}
}
impl FromProtobuf<services::CryptoUpdateTransactionBody> for AccountUpdateTransactionData {
#[allow(deprecated)]
fn from_protobuf(pb: services::CryptoUpdateTransactionBody) -> crate::Result<Self> {
use services::crypto_update_transaction_body::ReceiverSigRequiredField;
let receiver_signature_required = pb.receiver_sig_required_field.map(|it| match it {
ReceiverSigRequiredField::ReceiverSigRequired(it) => it,
ReceiverSigRequiredField::ReceiverSigRequiredWrapper(it) => it,
});
Ok(Self {
account_id: Option::from_protobuf(pb.account_id_to_update)?,
key: Option::from_protobuf(pb.key)?,
receiver_signature_required,
auto_renew_period: pb.auto_renew_period.map(Into::into),
auto_renew_account_id: Option::from_protobuf(pb.auto_renew_account)?,
proxy_account_id: Option::from_protobuf(pb.proxy_account_id)?,
expiration_time: pb.expiration_time.map(Into::into),
account_memo: pb.memo,
max_automatic_token_associations: pb
.max_automatic_token_associations
.map(|it| it as u16),
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
})
}
}
impl ToProtobuf for AccountUpdateTransactionData {
type Protobuf = services::CryptoUpdateTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
let account_id = self.account_id.to_protobuf();
let key = self.key.to_protobuf();
let auto_renew_period = self.auto_renew_period.to_protobuf();
let auto_renew_account = self.auto_renew_account_id.to_protobuf();
let expiration_time = self.expiration_time.to_protobuf();
let receiver_signature_required = self.receiver_signature_required.map(|required| {
services::crypto_update_transaction_body::ReceiverSigRequiredField::ReceiverSigRequiredWrapper(required)
});
let staked_id = self.staked_id.map(|id| match id {
StakedId::NodeId(id) => {
services::crypto_update_transaction_body::StakedId::StakedNodeId(id as i64)
}
StakedId::AccountId(id) => {
services::crypto_update_transaction_body::StakedId::StakedAccountId(
id.to_protobuf(),
)
}
});
#[allow(deprecated)]
services::CryptoUpdateTransactionBody {
account_id_to_update: account_id,
key,
proxy_account_id: self.proxy_account_id.to_protobuf(),
proxy_fraction: 0,
auto_renew_period,
auto_renew_account,
expiration_time,
memo: self.account_memo.clone(),
max_automatic_token_associations: self.max_automatic_token_associations.map(Into::into),
decline_reward: self.decline_staking_reward,
send_record_threshold_field: None,
receive_record_threshold_field: None,
receiver_sig_required_field: receiver_signature_required,
staked_id,
virtual_address_update: None, }
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use std::str::FromStr;
use assert_matches::assert_matches;
use time::{
Duration,
OffsetDateTime,
};
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
AccountId,
AccountUpdateTransaction,
Key,
PublicKey,
};
const ACCOUNT_UPDATE_TRANSACTION_JSON: &str = r#"{
"$type": "accountUpdate",
"accountId": "0.0.1001",
"key": {
"single": "302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd"
},
"receiverSignatureRequired": true,
"autoRenewPeriod": 7776000,
"proxyAccountId": "0.0.3141",
"expirationTime": 1656352251277559886,
"accountMemo": "An account memo",
"maxAutomaticTokenAssociations": 256,
"stakedAccountId": "0.0.1002",
"declineStakingReward": false
}"#;
const KEY: &str =
"302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd";
#[test]
#[allow(deprecated)]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = AccountUpdateTransaction::new();
transaction
.account_id(AccountId::from(1001))
.expiration_time(OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?)
.key(PublicKey::from_str(KEY)?)
.receiver_signature_required(true)
.auto_renew_period(Duration::days(90))
.account_memo("An account memo")
.max_automatic_token_associations(256)
.staked_node_id(7)
.staked_account_id(AccountId::from(1002))
.proxy_account_id(AccountId::from(3141))
.decline_staking_reward(false);
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, ACCOUNT_UPDATE_TRANSACTION_JSON);
Ok(())
}
#[test]
#[allow(deprecated)]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(ACCOUNT_UPDATE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.data(), AnyTransactionData::AccountUpdate(transaction) => transaction);
assert_eq!(
data.expiration_time.unwrap(),
OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?
);
assert_eq!(data.receiver_signature_required.unwrap(), true);
assert_eq!(data.auto_renew_period.unwrap(), Duration::days(90));
assert_eq!(data.account_memo.as_deref(), Some("An account memo"));
assert_eq!(data.max_automatic_token_associations.unwrap(), 256);
assert_eq!(data.decline_staking_reward.unwrap(), false);
assert_eq!(data.account_id, Some(AccountId::from(1001)));
assert_eq!(data.staked_id, Some(AccountId::from(1002).into()));
assert_eq!(data.proxy_account_id, Some(AccountId::from(3141)));
let key = assert_matches!(data.key, Some(Key::Single(public_key)) => public_key);
assert_eq!(key, PublicKey::from_str(KEY)?);
Ok(())
}
}
}