use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::consensus_service_client::ConsensusServiceClient;
use time::{
Duration,
OffsetDateTime,
};
use tonic::transport::Channel;
use crate::entity_id::AutoValidateChecksum;
use crate::protobuf::ToProtobuf;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
Error,
Key,
LedgerId,
TopicId,
Transaction,
TransactionId,
};
pub type TopicUpdateTransaction = Transaction<TopicUpdateTransactionData>;
#[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 TopicUpdateTransactionData {
topic_id: Option<TopicId>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::TimestampNanoSeconds>>")
)]
expiration_time: Option<OffsetDateTime>,
topic_memo: Option<String>,
admin_key: Option<Key>,
submit_key: Option<Key>,
#[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>,
}
impl TopicUpdateTransaction {
#[must_use]
pub fn get_topic_id(&self) -> Option<TopicId> {
self.data().topic_id
}
pub fn topic_id(&mut self, id: impl Into<TopicId>) -> &mut Self {
self.data_mut().topic_id = Some(id.into());
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_topic_memo(&self) -> Option<&str> {
self.data().topic_memo.as_deref()
}
pub fn topic_memo(&mut self, memo: impl Into<String>) -> &mut Self {
self.data_mut().topic_memo = Some(memo.into());
self
}
#[must_use]
pub fn get_admin_key(&self) -> Option<&Key> {
self.data().admin_key.as_ref()
}
pub fn admin_key(&mut self, key: impl Into<Key>) -> &mut Self {
self.data_mut().admin_key = Some(key.into());
self
}
#[must_use]
pub fn get_submit_key(&self) -> Option<&Key> {
self.data().submit_key.as_ref()
}
pub fn submit_key(&mut self, key: impl Into<Key>) -> &mut Self {
self.data_mut().submit_key = Some(key.into());
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
}
}
#[async_trait]
impl TransactionExecute for TopicUpdateTransactionData {
fn validate_checksums_for_ledger_id(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.topic_id.validate_checksum_for_ledger_id(ledger_id)?;
self.auto_renew_account_id.validate_checksum_for_ledger_id(ledger_id)
}
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
ConsensusServiceClient::new(channel).update_topic(request).await
}
}
impl ToTransactionDataProtobuf for TopicUpdateTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &TransactionId,
) -> services::transaction_body::Data {
let topic_id = self.topic_id.to_protobuf();
let expiration_time = self.expiration_time.map(Into::into);
let admin_key = self.admin_key.to_protobuf();
let submit_key = self.submit_key.to_protobuf();
let auto_renew_period = self.auto_renew_period.map(Into::into);
let auto_renew_account_id = self.auto_renew_account_id.to_protobuf();
services::transaction_body::Data::ConsensusUpdateTopic(
services::ConsensusUpdateTopicTransactionBody {
auto_renew_account: auto_renew_account_id,
memo: self.topic_memo.clone(),
expiration_time,
topic_id,
admin_key,
submit_key,
auto_renew_period,
},
)
}
}
impl From<TopicUpdateTransactionData> for AnyTransactionData {
fn from(transaction: TopicUpdateTransactionData) -> Self {
Self::TopicUpdate(transaction)
}
}
#[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,
Key,
PublicKey,
TopicId,
TopicUpdateTransaction,
};
const TOPIC_UPDATE_TRANSACTION_JSON: &str = r#"{
"$type": "topicUpdate",
"topicId": "0.0.1001",
"expirationTime": 1656352251277559886,
"topicMemo": "A topic memo",
"adminKey": {
"single": "302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd"
},
"submitKey": {
"single": "302a300506032b6570032100b5b4d9351ebdf266ef3989aed4fd8f0cfcf24b75ba3d0df19cd3946771b40500"
},
"autoRenewPeriod": 7776000,
"autoRenewAccountId": "0.0.1001"
}"#;
const ADMIN_KEY: &str =
"302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd";
const SUBMIT_KEY: &str =
"302a300506032b6570032100b5b4d9351ebdf266ef3989aed4fd8f0cfcf24b75ba3d0df19cd3946771b40500";
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = TopicUpdateTransaction::new();
transaction
.topic_id(TopicId::from(1001))
.expiration_time(OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?)
.topic_memo("A topic memo")
.admin_key(PublicKey::from_str(ADMIN_KEY)?)
.submit_key(PublicKey::from_str(SUBMIT_KEY)?)
.auto_renew_period(Duration::days(90))
.auto_renew_account_id(AccountId::from(1001));
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, TOPIC_UPDATE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction = serde_json::from_str(TOPIC_UPDATE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.into_body().data, AnyTransactionData::TopicUpdate(transaction) => transaction);
assert_eq!(data.topic_id.unwrap(), TopicId::from(1001));
assert_eq!(
data.expiration_time.unwrap(),
OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?
);
assert_eq!(data.topic_memo.unwrap(), "A topic memo");
assert_eq!(data.auto_renew_period.unwrap(), Duration::days(90));
let admin_key =
assert_matches!(data.admin_key.unwrap(), Key::Single(public_key) => public_key);
assert_eq!(admin_key, PublicKey::from_str(ADMIN_KEY)?);
let submit_key =
assert_matches!(data.submit_key.unwrap(), Key::Single(public_key) => public_key);
assert_eq!(submit_key, PublicKey::from_str(SUBMIT_KEY)?);
assert_eq!(data.auto_renew_account_id, Some(AccountId::from(1001)));
Ok(())
}
}
}