@@ -14,7 +14,8 @@ use caryatid_sdk::Context;
1414
1515use crate :: handlers_config:: HandlersConfig ;
1616use crate :: types:: {
17- AccountRewardREST , AccountWithdrawalREST , DelegationUpdateREST , RegistrationUpdateREST ,
17+ AccountAddressREST , AccountRewardREST , AccountWithdrawalREST , DelegationUpdateREST ,
18+ RegistrationUpdateREST ,
1819} ;
1920
2021#[ derive( serde:: Serialize ) ]
@@ -564,6 +565,76 @@ pub async fn handle_account_rewards_blockfrost(
564565 }
565566}
566567
568+ pub async fn handle_account_addresses_blockfrost (
569+ context : Arc < Context < Message > > ,
570+ params : Vec < String > ,
571+ handlers_config : Arc < HandlersConfig > ,
572+ ) -> Result < RESTResponse > {
573+ let account = match parse_stake_address ( & params) {
574+ Ok ( addr) => addr,
575+ Err ( resp) => return Ok ( resp) ,
576+ } ;
577+
578+ // Prepare the message
579+ let msg = Arc :: new ( Message :: StateQuery ( StateQuery :: Accounts (
580+ AccountsStateQuery :: GetAccountAssociatedAddresses { account } ,
581+ ) ) ) ;
582+
583+ // Get addresses from historical accounts state
584+ let addresses = query_state (
585+ & context,
586+ & handlers_config. historical_accounts_query_topic ,
587+ msg,
588+ |message| match message {
589+ Message :: StateQueryResponse ( StateQueryResponse :: Accounts (
590+ AccountsStateQueryResponse :: AccountAssociatedAddresses ( addresses) ,
591+ ) ) => Ok ( Some ( addresses) ) ,
592+ Message :: StateQueryResponse ( StateQueryResponse :: Accounts (
593+ AccountsStateQueryResponse :: NotFound ,
594+ ) ) => Ok ( None ) ,
595+ Message :: StateQueryResponse ( StateQueryResponse :: Accounts (
596+ AccountsStateQueryResponse :: Error ( e) ,
597+ ) ) => Err ( anyhow:: anyhow!(
598+ "Internal server error while retrieving account addresses: {e}"
599+ ) ) ,
600+ _ => Err ( anyhow:: anyhow!(
601+ "Unexpected message type while retrieving account addresses"
602+ ) ) ,
603+ } ,
604+ )
605+ . await ?;
606+
607+ let Some ( addresses) = addresses else {
608+ return Ok ( RESTResponse :: with_text ( 404 , "Account not found" ) ) ;
609+ } ;
610+
611+ let rest_response = match addresses
612+ . iter ( )
613+ . map ( |r| {
614+ Ok :: < _ , anyhow:: Error > ( AccountAddressREST {
615+ address : r. to_string ( ) . map_err ( |e| anyhow ! ( "invalid address: {e}" ) ) ?,
616+ } )
617+ } )
618+ . collect :: < Result < Vec < AccountAddressREST > , _ > > ( )
619+ {
620+ Ok ( v) => v,
621+ Err ( e) => {
622+ return Ok ( RESTResponse :: with_text (
623+ 500 ,
624+ & format ! ( "Failed to convert address entry: {e}" ) ,
625+ ) ) ;
626+ }
627+ } ;
628+
629+ match serde_json:: to_string_pretty ( & rest_response) {
630+ Ok ( json) => Ok ( RESTResponse :: with_json ( 200 , & json) ) ,
631+ Err ( e) => Ok ( RESTResponse :: with_text (
632+ 500 ,
633+ & format ! ( "Internal server error while serializing addresses: {e}" ) ,
634+ ) ) ,
635+ }
636+ }
637+
567638fn parse_stake_address ( params : & [ String ] ) -> Result < StakeAddress , RESTResponse > {
568639 let Some ( stake_key) = params. first ( ) else {
569640 return Err ( RESTResponse :: with_text (
0 commit comments