@@ -8,16 +8,17 @@ use acropolis_common::queries::blocks::{
88 BlocksStateQuery , BlocksStateQueryResponse , TransactionHashes ,
99} ;
1010use acropolis_common:: queries:: utils:: query_state;
11- use acropolis_common:: queries:: utxos:: UTxOStateQuery ;
11+ use acropolis_common:: queries:: utxos:: { UTxOStateQuery , UTxOStateQueryResponse } ;
1212use acropolis_common:: serialization:: { Bech32Conversion , Bech32WithHrp } ;
13- use acropolis_common:: { DRepChoice , StakeAddress } ;
13+ use acropolis_common:: { DRepChoice , Datum , ReferenceScript , StakeAddress } ;
1414use anyhow:: { anyhow, Result } ;
15+ use blake2:: { Blake2b512 , Digest } ;
1516use caryatid_sdk:: Context ;
1617
1718use crate :: handlers_config:: HandlersConfig ;
1819use crate :: types:: {
19- AccountAddressREST , AccountRewardREST , AccountWithdrawalREST , DelegationUpdateREST ,
20- RegistrationUpdateREST ,
20+ AccountAddressREST , AccountRewardREST , AccountUTxOREST , AccountWithdrawalREST ,
21+ DelegationUpdateREST , RegistrationUpdateREST ,
2122} ;
2223
2324#[ derive( serde:: Serialize ) ]
@@ -833,17 +834,12 @@ pub async fn handle_account_utxos_blockfrost(
833834 ) ) ) ;
834835 let utxo_identifiers = query_state (
835836 & context,
836- & handlers_config. historical_accounts_query_topic ,
837+ & handlers_config. addresses_query_topic ,
837838 msg,
838839 |message| match message {
839840 Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
840841 AddressStateQueryResponse :: AddressesUTxOs ( utxos) ,
841842 ) ) => Ok ( utxos) ,
842- Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
843- AddressStateQueryResponse :: NotFound ,
844- ) ) => Err ( anyhow:: anyhow!(
845- "Internal server error while retrieving account UTxOs: No UTxOs found"
846- ) ) ,
847843 Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
848844 AddressStateQueryResponse :: Error ( e) ,
849845 ) ) => Err ( anyhow:: anyhow!(
@@ -856,33 +852,104 @@ pub async fn handle_account_utxos_blockfrost(
856852 )
857853 . await ?;
858854
855+ // Get TxHashes and BlockHashes from UTxOIdentifiers
856+ let msg = Arc :: new ( Message :: StateQuery ( StateQuery :: Blocks (
857+ BlocksStateQuery :: GetUTxOHashes {
858+ utxo_ids : utxo_identifiers. clone ( ) ,
859+ } ,
860+ ) ) ) ;
861+
862+ let hashes = query_state (
863+ & context,
864+ & handlers_config. blocks_query_topic ,
865+ msg,
866+ |message| match message {
867+ Message :: StateQueryResponse ( StateQueryResponse :: Blocks (
868+ BlocksStateQueryResponse :: UTxOHashes ( utxos) ,
869+ ) ) => Ok ( utxos) ,
870+ Message :: StateQueryResponse ( StateQueryResponse :: Blocks (
871+ BlocksStateQueryResponse :: Error ( e) ,
872+ ) ) => Err ( anyhow:: anyhow!(
873+ "Internal server error while retrieving utxo hashes: {e}"
874+ ) ) ,
875+ _ => Err ( anyhow:: anyhow!(
876+ "Unexpected message type while retrieving account UTxOs"
877+ ) ) ,
878+ } ,
879+ )
880+ . await ?;
881+
859882 // Get UTxO balances from utxo state
860883 let msg = Arc :: new ( Message :: StateQuery ( StateQuery :: UTxOs (
861- UTxOStateQuery :: GetUTxOsMap { utxo_identifiers } ,
884+ UTxOStateQuery :: GetUTxOs {
885+ utxo_identifiers : utxo_identifiers. clone ( ) ,
886+ } ,
862887 ) ) ) ;
863- let balances = query_state (
888+ let entries = query_state (
864889 & context,
865- & handlers_config. historical_accounts_query_topic ,
890+ & handlers_config. utxos_query_topic ,
866891 msg,
867892 |message| match message {
868- Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
869- AddressStateQueryResponse :: AddressesUTxOs ( utxos) ,
870- ) ) => Ok ( Some ( utxos) ) ,
871- Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
872- AddressStateQueryResponse :: NotFound ,
873- ) ) => Ok ( None ) ,
874- Message :: StateQueryResponse ( StateQueryResponse :: Addresses (
875- AddressStateQueryResponse :: Error ( e) ,
893+ Message :: StateQueryResponse ( StateQueryResponse :: UTxOs (
894+ UTxOStateQueryResponse :: UTxOs ( utxos) ,
895+ ) ) => Ok ( utxos) ,
896+ Message :: StateQueryResponse ( StateQueryResponse :: UTxOs (
897+ UTxOStateQueryResponse :: Error ( e) ,
876898 ) ) => Err ( anyhow:: anyhow!(
877- "Internal server error while retrieving account UTxOs : {e}"
899+ "Internal server error while retrieving UTxO entries : {e}"
878900 ) ) ,
879901 _ => Err ( anyhow:: anyhow!(
880- "Unexpected message type while retrieving account UTxOs "
902+ "Unexpected message type while retrieving UTxO entries "
881903 ) ) ,
882904 } ,
883905 )
884906 . await ?;
885- Ok ( RESTResponse :: with_text ( 501 , "Not implemented" ) )
907+
908+ let mut rest_response = Vec :: with_capacity ( entries. len ( ) ) ;
909+ for ( i, entry) in entries. into_iter ( ) . enumerate ( ) {
910+ let tx_hash = hashes. tx_hashes . get ( i) . map ( hex:: encode) . unwrap_or_default ( ) ;
911+ let block_hash = hashes. block_hashes . get ( i) . map ( hex:: encode) . unwrap_or_default ( ) ;
912+ let output_index = utxo_identifiers. get ( i) . map ( |id| id. output_index ( ) ) . unwrap_or ( 0 ) ;
913+ let ( data_hash, inline_datum) = match & entry. datum {
914+ Some ( Datum :: Hash ( h) ) => ( Some ( hex:: encode ( h) ) , None ) ,
915+ Some ( Datum :: Inline ( bytes) ) => ( None , Some ( hex:: encode ( bytes) ) ) ,
916+ None => ( None , None ) ,
917+ } ;
918+ let reference_script_hash = match & entry. reference_script {
919+ Some ( script) => {
920+ let bytes = match script {
921+ ReferenceScript :: Native ( b)
922+ | ReferenceScript :: PlutusV1 ( b)
923+ | ReferenceScript :: PlutusV2 ( b)
924+ | ReferenceScript :: PlutusV3 ( b) => b,
925+ } ;
926+ let mut hasher = Blake2b512 :: new ( ) ;
927+ hasher. update ( bytes) ;
928+ let result = hasher. finalize ( ) ;
929+ Some ( hex:: encode ( & result[ ..32 ] ) )
930+ }
931+ None => None ,
932+ } ;
933+
934+ rest_response. push ( AccountUTxOREST {
935+ address : entry. address . to_string ( ) ?,
936+ tx_hash,
937+ output_index,
938+ amount : entry. value . into ( ) ,
939+ block : block_hash,
940+ data_hash,
941+ inline_datum,
942+ reference_script_hash,
943+ } )
944+ }
945+
946+ match serde_json:: to_string_pretty ( & rest_response) {
947+ Ok ( json) => Ok ( RESTResponse :: with_json ( 200 , & json) ) ,
948+ Err ( e) => Ok ( RESTResponse :: with_text (
949+ 500 ,
950+ & format ! ( "Internal server error while serializing addresses: {e}" ) ,
951+ ) ) ,
952+ }
886953}
887954
888955fn parse_stake_address ( params : & [ String ] ) -> Result < StakeAddress , RESTResponse > {
0 commit comments