use hedera_proto::services;
use hedera_proto::services::smart_contract_service_client::SmartContractServiceClient;
use time::{
Duration,
OffsetDateTime,
};
use tonic::transport::Channel;
use crate::protobuf::FromProtobuf;
use crate::staked_id::StakedId;
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
AccountId,
BoxGrpcFuture,
ContractId,
Error,
Key,
LedgerId,
ToProtobuf,
Transaction,
ValidateChecksums,
};
pub type ContractUpdateTransaction = Transaction<ContractUpdateTransactionData>;
#[cfg_attr(feature = "ffi", serde_with::skip_serializing_none)]
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(default, rename_all = "camelCase"))]
pub struct ContractUpdateTransactionData {
contract_id: Option<ContractId>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::TimestampNanoSeconds>>")
)]
expiration_time: Option<OffsetDateTime>,
admin_key: Option<Key>,
#[cfg_attr(
feature = "ffi",
serde(with = "serde_with::As::<Option<serde_with::DurationSeconds<i64>>>")
)]
auto_renew_period: Option<Duration>,
contract_memo: Option<String>,
max_automatic_token_associations: Option<u32>,
auto_renew_account_id: Option<AccountId>,
proxy_account_id: Option<AccountId>,
#[cfg_attr(feature = "ffi", serde(flatten))]
staked_id: Option<StakedId>,
decline_staking_reward: Option<bool>,
}
impl ContractUpdateTransaction {
#[must_use]
pub fn get_contract_id(&self) -> Option<ContractId> {
self.data().contract_id
}
pub fn contract_id(&mut self, contract_id: ContractId) -> &mut Self {
self.data_mut().contract_id = Some(contract_id);
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_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_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_contract_memo(&self) -> Option<&str> {
self.data().contract_memo.as_deref()
}
pub fn contract_memo(&mut self, memo: impl Into<String>) -> &mut Self {
self.data_mut().contract_memo = Some(memo.into());
self
}
#[must_use]
pub fn get_max_automatic_token_associations(&self) -> Option<u32> {
self.data().max_automatic_token_associations
}
pub fn max_automatic_token_associations(&mut self, max: u32) -> &mut Self {
self.data_mut().max_automatic_token_associations = Some(max);
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, account_id: AccountId) -> &mut Self {
self.data_mut().auto_renew_account_id = Some(account_id);
self
}
#[must_use]
pub fn get_proxy_account_id(&self) -> Option<AccountId> {
self.data().proxy_account_id
}
pub fn proxy_account_id(&mut self, id: AccountId) -> &mut Self {
self.data_mut().proxy_account_id = Some(id);
self
}
#[must_use]
pub fn get_staked_account_id(&self) -> Option<AccountId> {
self.data().staked_id.and_then(|id| id.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(|id| id.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 ContractUpdateTransactionData {}
impl TransactionExecute for ContractUpdateTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { SmartContractServiceClient::new(channel).update_contract(request).await })
}
}
impl ValidateChecksums for ContractUpdateTransactionData {
fn validate_checksums(&self, ledger_id: &LedgerId) -> Result<(), Error> {
self.contract_id.validate_checksums(ledger_id)?;
self.auto_renew_account_id.validate_checksums(ledger_id)?;
self.staked_id.validate_checksums(ledger_id)?;
self.proxy_account_id.validate_checksums(ledger_id)
}
}
impl ToTransactionDataProtobuf for ContractUpdateTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::ContractUpdateInstance(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for ContractUpdateTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::ContractUpdateInstance(self.to_protobuf())
}
}
impl FromProtobuf<services::ContractUpdateTransactionBody> for ContractUpdateTransactionData {
#[allow(deprecated)]
fn from_protobuf(pb: services::ContractUpdateTransactionBody) -> crate::Result<Self> {
use services::contract_update_transaction_body::MemoField;
Ok(Self {
contract_id: Option::from_protobuf(pb.contract_id)?,
expiration_time: pb.expiration_time.map(Into::into),
admin_key: Option::from_protobuf(pb.admin_key)?,
auto_renew_period: pb.auto_renew_period.map(Into::into),
contract_memo: pb.memo_field.map(|it| match it {
MemoField::Memo(it) => it,
MemoField::MemoWrapper(it) => it,
}),
max_automatic_token_associations: pb
.max_automatic_token_associations
.map(|it| it as u32),
auto_renew_account_id: Option::from_protobuf(pb.auto_renew_account_id)?,
proxy_account_id: Option::from_protobuf(pb.proxy_account_id)?,
staked_id: Option::from_protobuf(pb.staked_id)?,
decline_staking_reward: pb.decline_reward,
})
}
}
impl ToProtobuf for ContractUpdateTransactionData {
type Protobuf = services::ContractUpdateTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
let contract_id = self.contract_id.to_protobuf();
let expiration_time = self.expiration_time.map(Into::into);
let admin_key = self.admin_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();
let staked_id = self.staked_id.map(|id| match id {
StakedId::NodeId(id) => {
services::contract_update_transaction_body::StakedId::StakedNodeId(id as i64)
}
StakedId::AccountId(id) => {
services::contract_update_transaction_body::StakedId::StakedAccountId(
id.to_protobuf(),
)
}
});
let memo_field = self
.contract_memo
.clone()
.map(services::contract_update_transaction_body::MemoField::MemoWrapper);
#[allow(deprecated)]
services::ContractUpdateTransactionBody {
contract_id,
expiration_time,
admin_key,
proxy_account_id: self.proxy_account_id.to_protobuf(),
auto_renew_period,
max_automatic_token_associations: self
.max_automatic_token_associations
.map(|max| max as i32),
auto_renew_account_id,
decline_reward: self.decline_staking_reward,
staked_id,
file_id: None,
memo_field,
}
}
}
impl From<ContractUpdateTransactionData> for AnyTransactionData {
fn from(transaction: ContractUpdateTransactionData) -> Self {
Self::ContractUpdate(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,
ContractId,
ContractUpdateTransaction,
Key,
PublicKey,
};
const CONTRACT_UPDATE_TRANSACTION_JSON: &str = r#"{
"$type": "contractUpdate",
"contractId": "0.0.1001",
"expirationTime": 1656352251277559886,
"adminKey": {
"single": "302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd"
},
"autoRenewPeriod": 7776000,
"contractMemo": "A contract memo",
"maxAutomaticTokenAssociations": 1024,
"autoRenewAccountId": "0.0.1002",
"stakedAccountId": "0.0.1003",
"declineStakingReward": true
}"#;
const ADMIN_KEY: &str =
"302a300506032b6570032100d1ad76ed9b057a3d3f2ea2d03b41bcd79aeafd611f941924f0f6da528ab066fd";
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = ContractUpdateTransaction::new();
transaction
.contract_id(ContractId::from(1001))
.expiration_time(OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?)
.admin_key(PublicKey::from_str(ADMIN_KEY)?)
.auto_renew_period(Duration::days(90))
.contract_memo("A contract memo")
.max_automatic_token_associations(1024)
.auto_renew_account_id(AccountId::from(1002))
.staked_account_id(AccountId::from(1003))
.decline_staking_reward(true);
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, CONTRACT_UPDATE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction =
serde_json::from_str(CONTRACT_UPDATE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.into_body().data, AnyTransactionData::ContractUpdate(transaction) => transaction);
assert_eq!(data.contract_id.unwrap(), ContractId::from(1001));
assert_eq!(
data.expiration_time.unwrap(),
OffsetDateTime::from_unix_timestamp_nanos(1656352251277559886)?
);
assert_eq!(data.auto_renew_period.unwrap(), Duration::days(90));
assert_eq!(data.contract_memo.unwrap(), "A contract memo");
assert_eq!(data.max_automatic_token_associations.unwrap(), 1024);
assert_eq!(data.decline_staking_reward.unwrap(), true);
let admin_key =
assert_matches!(data.admin_key.unwrap(), Key::Single(public_key) => public_key);
assert_eq!(admin_key, PublicKey::from_str(ADMIN_KEY)?);
assert_eq!(data.auto_renew_account_id, Some(AccountId::from(1002)));
assert_eq!(data.staked_id, Some(AccountId::from(1003).into()));
Ok(())
}
}
}