Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tck): add AccountUpdate method #867

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 132 additions & 5 deletions tck/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::{
use hedera::{
AccountCreateTransaction,
AccountId,
AccountUpdateTransaction,
Client,
EvmAddress,
Hbar,
Expand All @@ -22,7 +23,10 @@ use jsonrpsee::types::{
};
use once_cell::sync::Lazy;
use serde_json::Value;
use time::Duration;
use time::{
Duration,
OffsetDateTime,
};

use crate::errors::from_hedera_error;
use crate::helpers::{
Expand All @@ -32,6 +36,7 @@ use crate::helpers::{
};
use crate::responses::{
AccountCreateResponse,
AccountUpdateResponse,
GenerateKeyResponse,
};

Expand Down Expand Up @@ -93,6 +98,26 @@ pub trait Rpc {
alias: Option<String>,
common_transaction_params: Option<HashMap<String, Value>>,
) -> Result<AccountCreateResponse, ErrorObjectOwned>;

/*
/ Specification:
/ https://github.com/hiero-ledger/hiero-sdk-tck/blob/main/test-specifications/crypto-service/accountUpdateTransaction.md#updateAccount
*/
#[method(name = "updateAccount")]
async fn update_account(
&self,
account_id: Option<String>,
key: Option<String>,
auto_renew_period: Option<i64>,
expiration_time: Option<i64>,
receiver_signature_required: Option<bool>,
memo: Option<String>,
max_auto_token_associations: Option<i64>,
staked_account_id: Option<String>,
staked_node_id: Option<i64>,
decline_staking_reward: Option<bool>,
common_transaction_params: Option<HashMap<String, Value>>,
) -> Result<AccountUpdateResponse, ErrorObjectOwned>;
}

pub struct RpcServerImpl;
Expand Down Expand Up @@ -199,7 +224,6 @@ impl RpcServer for RpcServerImpl {
alias: Option<String>,
common_transaction_params: Option<HashMap<String, Value>>,
) -> Result<AccountCreateResponse, ErrorObjectOwned> {
println!("******************** create_account ********************");
let client = {
let guard = GLOBAL_SDK_CLIENT.lock().unwrap();
guard
Expand All @@ -223,7 +247,6 @@ impl RpcServer for RpcServerImpl {
}

if let Some(initial_balance) = initial_balance {
println!("initial_balance: {initial_balance}");
account_create_tx.initial_balance(Hbar::from_tinybars(initial_balance));
}

Expand Down Expand Up @@ -290,11 +313,115 @@ impl RpcServer for RpcServerImpl {
let tx_receipt =
tx_response.get_receipt(&client).await.map_err(|e| from_hedera_error(e))?;

println!("******************** end create_account ********************");

Ok(AccountCreateResponse {
account_id: tx_receipt.account_id.unwrap().to_string(),
status: tx_receipt.status.as_str_name().to_string(),
})
}

async fn update_account(
&self,
account_id: Option<String>,
key: Option<String>,
auto_renew_period: Option<i64>,
expiration_time: Option<i64>,
receiver_signature_required: Option<bool>,
memo: Option<String>,
max_auto_token_associations: Option<i64>,
staked_account_id: Option<String>,
staked_node_id: Option<i64>,
decline_staking_reward: Option<bool>,
common_transaction_params: Option<HashMap<String, Value>>,
) -> Result<AccountUpdateResponse, ErrorObjectOwned> {
let client = {
let guard = GLOBAL_SDK_CLIENT.lock().unwrap();
guard
.as_ref()
.ok_or_else(|| {
ErrorObject::owned(
INTERNAL_ERROR_CODE,
"Client not initialized".to_string(),
None::<()>,
)
})?
.clone()
};

let mut account_update_tx = AccountUpdateTransaction::new();

if let Some(account_id) = account_id {
account_update_tx.account_id(account_id.parse().unwrap());
}

if let Some(key) = key {
let key = get_hedera_key(&key)?;

account_update_tx.key(key);
}

if let Some(receiver_signature_required) = receiver_signature_required {
account_update_tx.receiver_signature_required(receiver_signature_required);
}

if let Some(auto_renew_period) = auto_renew_period {
account_update_tx.auto_renew_period(Duration::seconds(auto_renew_period));
}

if let Some(expiration_time) = expiration_time {
account_update_tx.expiration_time(
OffsetDateTime::from_unix_timestamp(expiration_time).map_err(|e| {
ErrorObject::owned(INTERNAL_ERROR_CODE, e.to_string(), None::<()>)
})?,
);
}

if let Some(memo) = memo {
account_update_tx.account_memo(memo);
}

if let Some(max_auto_token_associations) = max_auto_token_associations {
account_update_tx.max_automatic_token_associations(max_auto_token_associations as i32);
}

if let Some(staked_account_id) = staked_account_id {
account_update_tx.staked_account_id(
AccountId::from_str(&staked_account_id).map_err(|e| {
ErrorObject::owned(INTERNAL_ERROR_CODE, e.to_string(), None::<()>)
})?,
);
}

if let Some(staked_node_id) = staked_node_id {
account_update_tx.staked_node_id(staked_node_id as u64);
}

if let Some(decline_staking_reward) = decline_staking_reward {
account_update_tx.decline_staking_reward(decline_staking_reward);
}

if let Some(common_transaction_params) = common_transaction_params {
let _ =
fill_common_transaction_params(&mut account_update_tx, &common_transaction_params);

account_update_tx.freeze_with(&client).unwrap();

if let Some(signers) = common_transaction_params.get("signers") {
if let Value::Array(signers) = signers {
for signer in signers {
if let Value::String(signer_str) = signer {
account_update_tx.sign(PrivateKey::from_str_der(signer_str).unwrap());
}
}
}
}
}

let tx_response =
account_update_tx.execute(&client).await.map_err(|e| from_hedera_error(e))?;

let tx_receipt =
tx_response.get_receipt(&client).await.map_err(|e| from_hedera_error(e))?;

Ok(AccountUpdateResponse { status: tx_receipt.status.as_str_name().to_string() })
}
}
6 changes: 6 additions & 0 deletions tck/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub struct AccountCreateResponse {
pub status: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AccountUpdateResponse {
pub status: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GenerateKeyResponse {
Expand Down
Loading