@@ -8,6 +8,7 @@ use std::sync::{
88use hedera:: {
99 AccountCreateTransaction ,
1010 AccountId ,
11+ AccountUpdateTransaction ,
1112 Client ,
1213 EvmAddress ,
1314 Hbar ,
@@ -22,7 +23,10 @@ use jsonrpsee::types::{
2223} ;
2324use once_cell:: sync:: Lazy ;
2425use serde_json:: Value ;
25- use time:: Duration ;
26+ use time:: {
27+ Duration ,
28+ OffsetDateTime ,
29+ } ;
2630
2731use crate :: errors:: from_hedera_error;
2832use crate :: helpers:: {
@@ -32,6 +36,7 @@ use crate::helpers::{
3236} ;
3337use crate :: responses:: {
3438 AccountCreateResponse ,
39+ AccountUpdateResponse ,
3540 GenerateKeyResponse ,
3641} ;
3742
@@ -93,6 +98,26 @@ pub trait Rpc {
9398 alias : Option < String > ,
9499 common_transaction_params : Option < HashMap < String , Value > > ,
95100 ) -> Result < AccountCreateResponse , ErrorObjectOwned > ;
101+
102+ /*
103+ / Specification:
104+ / https://github.com/hiero-ledger/hiero-sdk-tck/blob/main/test-specifications/crypto-service/accountUpdateTransaction.md#updateAccount
105+ */
106+ #[ method( name = "updateAccount" ) ]
107+ async fn update_account (
108+ & self ,
109+ account_id : Option < String > ,
110+ key : Option < String > ,
111+ auto_renew_period : Option < i64 > ,
112+ expiration_time : Option < i64 > ,
113+ receiver_signature_required : Option < bool > ,
114+ memo : Option < String > ,
115+ max_auto_token_associations : Option < i64 > ,
116+ staked_account_id : Option < String > ,
117+ staked_node_id : Option < i64 > ,
118+ decline_staking_reward : Option < bool > ,
119+ common_transaction_params : Option < HashMap < String , Value > > ,
120+ ) -> Result < AccountUpdateResponse , ErrorObjectOwned > ;
96121}
97122
98123pub struct RpcServerImpl ;
@@ -199,7 +224,6 @@ impl RpcServer for RpcServerImpl {
199224 alias : Option < String > ,
200225 common_transaction_params : Option < HashMap < String , Value > > ,
201226 ) -> Result < AccountCreateResponse , ErrorObjectOwned > {
202- println ! ( "******************** create_account ********************" ) ;
203227 let client = {
204228 let guard = GLOBAL_SDK_CLIENT . lock ( ) . unwrap ( ) ;
205229 guard
@@ -223,7 +247,6 @@ impl RpcServer for RpcServerImpl {
223247 }
224248
225249 if let Some ( initial_balance) = initial_balance {
226- println ! ( "initial_balance: {initial_balance}" ) ;
227250 account_create_tx. initial_balance ( Hbar :: from_tinybars ( initial_balance) ) ;
228251 }
229252
@@ -290,11 +313,115 @@ impl RpcServer for RpcServerImpl {
290313 let tx_receipt =
291314 tx_response. get_receipt ( & client) . await . map_err ( |e| from_hedera_error ( e) ) ?;
292315
293- println ! ( "******************** end create_account ********************" ) ;
294-
295316 Ok ( AccountCreateResponse {
296317 account_id : tx_receipt. account_id . unwrap ( ) . to_string ( ) ,
297318 status : tx_receipt. status . as_str_name ( ) . to_string ( ) ,
298319 } )
299320 }
321+
322+ async fn update_account (
323+ & self ,
324+ account_id : Option < String > ,
325+ key : Option < String > ,
326+ auto_renew_period : Option < i64 > ,
327+ expiration_time : Option < i64 > ,
328+ receiver_signature_required : Option < bool > ,
329+ memo : Option < String > ,
330+ max_auto_token_associations : Option < i64 > ,
331+ staked_account_id : Option < String > ,
332+ staked_node_id : Option < i64 > ,
333+ decline_staking_reward : Option < bool > ,
334+ common_transaction_params : Option < HashMap < String , Value > > ,
335+ ) -> Result < AccountUpdateResponse , ErrorObjectOwned > {
336+ let client = {
337+ let guard = GLOBAL_SDK_CLIENT . lock ( ) . unwrap ( ) ;
338+ guard
339+ . as_ref ( )
340+ . ok_or_else ( || {
341+ ErrorObject :: owned (
342+ INTERNAL_ERROR_CODE ,
343+ "Client not initialized" . to_string ( ) ,
344+ None :: < ( ) > ,
345+ )
346+ } ) ?
347+ . clone ( )
348+ } ;
349+
350+ let mut account_update_tx = AccountUpdateTransaction :: new ( ) ;
351+
352+ if let Some ( account_id) = account_id {
353+ account_update_tx. account_id ( account_id. parse ( ) . unwrap ( ) ) ;
354+ }
355+
356+ if let Some ( key) = key {
357+ let key = get_hedera_key ( & key) ?;
358+
359+ account_update_tx. key ( key) ;
360+ }
361+
362+ if let Some ( receiver_signature_required) = receiver_signature_required {
363+ account_update_tx. receiver_signature_required ( receiver_signature_required) ;
364+ }
365+
366+ if let Some ( auto_renew_period) = auto_renew_period {
367+ account_update_tx. auto_renew_period ( Duration :: seconds ( auto_renew_period) ) ;
368+ }
369+
370+ if let Some ( expiration_time) = expiration_time {
371+ account_update_tx. expiration_time (
372+ OffsetDateTime :: from_unix_timestamp ( expiration_time) . map_err ( |e| {
373+ ErrorObject :: owned ( INTERNAL_ERROR_CODE , e. to_string ( ) , None :: < ( ) > )
374+ } ) ?,
375+ ) ;
376+ }
377+
378+ if let Some ( memo) = memo {
379+ account_update_tx. account_memo ( memo) ;
380+ }
381+
382+ if let Some ( max_auto_token_associations) = max_auto_token_associations {
383+ account_update_tx. max_automatic_token_associations ( max_auto_token_associations as i32 ) ;
384+ }
385+
386+ if let Some ( staked_account_id) = staked_account_id {
387+ account_update_tx. staked_account_id (
388+ AccountId :: from_str ( & staked_account_id) . map_err ( |e| {
389+ ErrorObject :: owned ( INTERNAL_ERROR_CODE , e. to_string ( ) , None :: < ( ) > )
390+ } ) ?,
391+ ) ;
392+ }
393+
394+ if let Some ( staked_node_id) = staked_node_id {
395+ account_update_tx. staked_node_id ( staked_node_id as u64 ) ;
396+ }
397+
398+ if let Some ( decline_staking_reward) = decline_staking_reward {
399+ account_update_tx. decline_staking_reward ( decline_staking_reward) ;
400+ }
401+
402+ if let Some ( common_transaction_params) = common_transaction_params {
403+ let _ =
404+ fill_common_transaction_params ( & mut account_update_tx, & common_transaction_params) ;
405+
406+ account_update_tx. freeze_with ( & client) . unwrap ( ) ;
407+
408+ if let Some ( signers) = common_transaction_params. get ( "signers" ) {
409+ if let Value :: Array ( signers) = signers {
410+ for signer in signers {
411+ if let Value :: String ( signer_str) = signer {
412+ account_update_tx. sign ( PrivateKey :: from_str_der ( signer_str) . unwrap ( ) ) ;
413+ }
414+ }
415+ }
416+ }
417+ }
418+
419+ let tx_response =
420+ account_update_tx. execute ( & client) . await . map_err ( |e| from_hedera_error ( e) ) ?;
421+
422+ let tx_receipt =
423+ tx_response. get_receipt ( & client) . await . map_err ( |e| from_hedera_error ( e) ) ?;
424+
425+ Ok ( AccountUpdateResponse { status : tx_receipt. status . as_str_name ( ) . to_string ( ) } )
426+ }
300427}
0 commit comments