Skip to content

Commit

Permalink
feat: support stakedNodeId in addition to stakedAccountId in AccountC…
Browse files Browse the repository at this point in the history
…reate and AccountUpdate transactions
  • Loading branch information
mehcode committed Jun 2, 2022
1 parent db2a527 commit e59a585
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 111 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ time = { version = "0.3.9", features = ["serde"] }
tokio = { version = "1.18.1" }
hedera-proto = { path = "../../protobufs/rust", features = ["serde", "time_0_3"] }
serde_json = "1.0.79"
serde_with = { version = "1.12.1", features = ["hex", "base64"] }
serde_with = { version = "1.12.1", features = ["hex", "base64", "time_0_3"] }
tonic = { version = "0.7.1", features = ["compression"] }
async-trait = "0.1.53"
prost = "0.10.1"
Expand Down
33 changes: 29 additions & 4 deletions sdk/rust/src/account/account_create_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct AccountCreateTransactionData {
pub receiver_signature_required: bool,

/// The account is charged to extend its expiration date every this many seconds.
#[serde_as(as = "Option<DurationSeconds>")]
#[serde_as(as = "Option<DurationSeconds<i64>>")]
pub auto_renew_period: Option<Duration>,

/// The memo associated with the account.
Expand All @@ -47,8 +47,13 @@ pub struct AccountCreateTransactionData {
pub max_automatic_token_associations: u16,

/// ID of the account to which this account is staking.
/// This is mutually exclusive with `staked_node_id`.
pub staked_account_id: Option<AccountIdOrAlias>,

/// ID of the node this account is staked to.
/// This is mutually exclusive with `staked_account_id`.
pub staked_node_id: Option<u64>,

/// If true, the account declines receiving a staking reward. The default value is false.
pub decline_staking_reward: bool,
}
Expand All @@ -63,6 +68,7 @@ impl Default for AccountCreateTransactionData {
account_memo: String::new(),
max_automatic_token_associations: 0,
staked_account_id: None,
staked_node_id: None,
decline_staking_reward: false,
}
}
Expand Down Expand Up @@ -106,11 +112,19 @@ impl AccountCreateTransaction {
}

/// Set the ID of the account to which this account is staking.
/// This is mutually exclusive with `staked_node_id`.
pub fn staked_account_id(&mut self, id: impl Into<AccountIdOrAlias>) -> &mut Self {
self.body.data.staked_account_id = Some(id.into());
self
}

/// Set the ID of the node to which this account is staking.
/// This is mutually exclusive with `staked_account_id`.
pub fn staked_node_id(&mut self, id: u64) -> &mut Self {
self.body.data.staked_node_id = Some(id);
self
}

/// Set to true, the account declines receiving a staking reward. The default value is false.
pub fn decline_staking_reward(&mut self, decline: bool) -> &mut Self {
self.body.data.decline_staking_reward = decline;
Expand All @@ -137,9 +151,20 @@ impl ToTransactionDataProtobuf for AccountCreateTransactionData {
) -> services::transaction_body::Data {
let key = self.key.as_ref().map(Key::to_protobuf);
let auto_renew_period = self.auto_renew_period.as_ref().map(Duration::to_protobuf);
let staked_id = self.staked_account_id.as_ref().map(|id| {
services::crypto_create_transaction_body::StakedId::StakedAccountId(id.to_protobuf())
});

let staked_id = match (&self.staked_account_id, self.staked_node_id) {
(_, Some(node_id)) => Some(
services::crypto_create_transaction_body::StakedId::StakedNodeId(node_id as i64),
),

(Some(account_id), _) => {
Some(services::crypto_create_transaction_body::StakedId::StakedAccountId(
account_id.to_protobuf(),
))
}

_ => None,
};

services::transaction_body::Data::CryptoCreateAccount(
#[allow(deprecated)]
Expand Down
37 changes: 31 additions & 6 deletions sdk/rust/src/account/account_update_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct AccountUpdateTransactionData {
pub receiver_signature_required: Option<bool>,

/// The account is charged to extend its expiration date every this many seconds.
#[serde_as(as = "Option<DurationSeconds>")]
#[serde_as(as = "Option<DurationSeconds<i64>>")]
pub auto_renew_period: Option<Duration>,

/// The new expiration time to extend to (ignored if equal to or before the current one).
Expand All @@ -53,8 +53,13 @@ pub struct AccountUpdateTransactionData {
pub max_automatic_token_associations: Option<u16>,

/// ID of the account to which this account is staking.
/// This is mutually exclusive with `staked_node_id`.
pub staked_account_id: Option<AccountIdOrAlias>,

/// ID of the node this account is staked to.
/// This is mutually exclusive with `staked_account_id`.
pub staked_node_id: Option<u64>,

/// If true, the account declines receiving a staking reward. The default value is false.
pub decline_staking_reward: Option<bool>,
}
Expand Down Expand Up @@ -103,11 +108,19 @@ impl AccountUpdateTransaction {
}

/// Set the ID of the account to which this account is staking.
/// This is mutually exclusive with `staked_node_id`.
pub fn staked_account_id(&mut self, id: impl Into<AccountIdOrAlias>) -> &mut Self {
self.body.data.staked_account_id = Some(id.into());
self
}

/// Set the ID of the node to which this account is staking.
/// This is mutually exclusive with `staked_account_id`.
pub fn staked_node_id(&mut self, id: u64) -> &mut Self {
self.body.data.staked_node_id = Some(id);
self
}

/// Set to true, the account declines receiving a staking reward. The default value is false.
pub fn decline_staking_reward(&mut self, decline: bool) -> &mut Self {
self.body.data.decline_staking_reward = Some(decline);
Expand Down Expand Up @@ -141,9 +154,19 @@ impl ToTransactionDataProtobuf for AccountUpdateTransactionData {
services::crypto_update_transaction_body::ReceiverSigRequiredField::ReceiverSigRequiredWrapper(required)
});

let staked_id = self.staked_account_id.as_ref().map(|id| {
services::crypto_update_transaction_body::StakedId::StakedAccountId(id.to_protobuf())
});
let staked_id = match (&self.staked_account_id, self.staked_node_id) {
(_, Some(node_id)) => Some(
services::crypto_update_transaction_body::StakedId::StakedNodeId(node_id as i64),
),

(Some(account_id), _) => {
Some(services::crypto_update_transaction_body::StakedId::StakedAccountId(
account_id.to_protobuf(),
))
}

_ => None,
};

services::transaction_body::Data::CryptoUpdateAccount(
#[allow(deprecated)]
Expand All @@ -155,12 +178,14 @@ impl ToTransactionDataProtobuf for AccountUpdateTransactionData {
auto_renew_period,
expiration_time,
memo: self.account_memo.clone(),
max_automatic_token_associations: self.max_automatic_token_associations.into(),
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: staked_id,
staked_id,
},
)
}
Expand Down
1 change: 0 additions & 1 deletion sdk/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ mod file;
mod key;
mod query;
mod schedule;
mod serde;
mod signature;
mod signer;
mod token;
Expand Down
38 changes: 0 additions & 38 deletions sdk/rust/src/serde/duration.rs

This file was deleted.

49 changes: 0 additions & 49 deletions sdk/rust/src/serde/duration_opt.rs

This file was deleted.

6 changes: 0 additions & 6 deletions sdk/rust/src/serde/mod.rs

This file was deleted.

14 changes: 11 additions & 3 deletions sdk/rust/src/transaction/any.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use hedera_proto::services;
use serde::{Deserialize, Deserializer};
use serde_with::skip_serializing_none;
use serde_with::{serde_as, skip_serializing_none, DurationSeconds};
use time::Duration;
use tonic::transport::Channel;
use tonic::{Response, Status};
Expand Down Expand Up @@ -142,22 +142,30 @@ impl TransactionExecute for AnyTransactionData {
// we create a proxy type that has the same layout but is only for AnyQueryData and does
// derive(Deserialize).

#[serde_as]
#[skip_serializing_none]
#[derive(serde::Deserialize, serde::Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AnyTransactionBody<D> {
#[serde(flatten)]
data: D,

#[serde(default)]
node_account_ids: Option<Vec<AccountId>>,
#[serde(default, with = "crate::serde::duration_opt")]

#[serde_as(as = "Option<DurationSeconds<i64>>")]
#[serde(default)]
transaction_valid_duration: Option<Duration>,

#[serde(default)]
max_transaction_fee: Option<u64>,
#[serde(default, skip_serializing_if = "crate::serde::skip_if_string_empty")]

#[serde(default, skip_serializing_if = "String::is_empty")]
transaction_memo: String,

#[serde(default)]
payer_account_id: Option<AccountId>,

#[serde(default)]
transaction_id: Option<TransactionId>,
}
Expand Down
12 changes: 9 additions & 3 deletions sdk/rust/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::fmt::{Debug, Formatter};

use serde_with::{serde_as, skip_serializing_none, FromInto};
use serde_with::{serde_as, skip_serializing_none, DurationSeconds, FromInto};
use time::Duration;

use crate::execute::execute;
Expand Down Expand Up @@ -42,13 +42,19 @@ where
#[serde(flatten)]
#[serde_as(as = "FromInto<AnyTransactionData>")]
pub(crate) data: D,

pub(crate) node_account_ids: Option<Vec<AccountId>>,
#[serde(with = "crate::serde::duration_opt")]

#[serde_as(as = "Option<DurationSeconds<i64>>")]
pub(crate) transaction_valid_duration: Option<Duration>,

pub(crate) max_transaction_fee: Option<u64>,
#[serde(skip_serializing_if = "crate::serde::skip_if_string_empty")]

#[serde(skip_serializing_if = "String::is_empty")]
pub(crate) transaction_memo: String,

pub(crate) payer_account_id: Option<AccountId>,

pub(crate) transaction_id: Option<TransactionId>,
}

Expand Down
Loading

0 comments on commit e59a585

Please sign in to comment.