Skip to content

Commit 2f254c0

Browse files
committed
feat: account totals endpoint handler
Signed-off-by: William Hankins <[email protected]>
1 parent f979bf3 commit 2f254c0

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

modules/rest_blockfrost/src/handlers/accounts.rs

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::Arc;
33

44
use crate::handlers_config::HandlersConfig;
55
use crate::types::{
6-
AccountAddressREST, AccountRewardREST, AccountUTxOREST, AccountWithdrawalREST, AmountList,
7-
DelegationUpdateREST, RegistrationUpdateREST,
6+
AccountAddressREST, AccountRewardREST, AccountTotalsREST, AccountUTxOREST,
7+
AccountWithdrawalREST, AmountList, DelegationUpdateREST, RegistrationUpdateREST,
88
};
99
use acropolis_common::messages::{Message, RESTResponse, StateQuery, StateQueryResponse};
1010
use acropolis_common::queries::accounts::{AccountsStateQuery, AccountsStateQueryResponse};
@@ -563,7 +563,7 @@ pub async fn handle_account_assets_blockfrost(
563563
.await?;
564564

565565
let Some(addresses) = addresses else {
566-
return Ok(RESTResponse::with_text(404, "Account not found"));
566+
return Err(RESTError::not_found("Account not found"));
567567
};
568568

569569
// Get utxos from address state
@@ -621,11 +621,74 @@ pub async fn handle_account_assets_blockfrost(
621621

622622
/// Handle `/accounts/{stake_address}/addresses/total` Blockfrost-compatible endpoint
623623
pub async fn handle_account_totals_blockfrost(
624-
_context: Arc<Context<Message>>,
625-
_params: Vec<String>,
626-
_handlers_config: Arc<HandlersConfig>,
624+
context: Arc<Context<Message>>,
625+
params: Vec<String>,
626+
handlers_config: Arc<HandlersConfig>,
627627
) -> Result<RESTResponse, RESTError> {
628-
Err(RESTError::not_implemented("Account totals not implemented"))
628+
let account = parse_stake_address(&params)?;
629+
630+
// Get addresses from historical accounts state
631+
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
632+
AccountsStateQuery::GetAccountAssociatedAddresses {
633+
account: account.clone(),
634+
},
635+
)));
636+
let addresses = query_state(
637+
&context,
638+
&handlers_config.historical_accounts_query_topic,
639+
msg,
640+
|message| match message {
641+
Message::StateQueryResponse(StateQueryResponse::Accounts(
642+
AccountsStateQueryResponse::AccountAssociatedAddresses(addresses),
643+
)) => Ok(Some(addresses)),
644+
Message::StateQueryResponse(StateQueryResponse::Accounts(
645+
AccountsStateQueryResponse::Error(QueryError::NotFound { .. }),
646+
)) => Ok(None),
647+
Message::StateQueryResponse(StateQueryResponse::Addresses(
648+
AddressStateQueryResponse::Error(e),
649+
)) => Err(e),
650+
_ => Err(QueryError::internal_error(
651+
"Unexpected message type while retrieving account addresses",
652+
)),
653+
},
654+
)
655+
.await?;
656+
657+
let Some(addresses) = addresses else {
658+
return Err(RESTError::not_found("Account not found"));
659+
};
660+
661+
// Get totals from address state
662+
let msg = Arc::new(Message::StateQuery(StateQuery::Addresses(
663+
AddressStateQuery::GetAddressesTotals { addresses },
664+
)));
665+
let totals = query_state(
666+
&context,
667+
&handlers_config.addresses_query_topic,
668+
msg,
669+
|message| match message {
670+
Message::StateQueryResponse(StateQueryResponse::Addresses(
671+
AddressStateQueryResponse::AddressesTotals(utxos),
672+
)) => Ok(utxos),
673+
Message::StateQueryResponse(StateQueryResponse::Addresses(
674+
AddressStateQueryResponse::Error(e),
675+
)) => Err(e),
676+
_ => Err(QueryError::internal_error(
677+
"Unexpected message type while retrieving account totals",
678+
)),
679+
},
680+
)
681+
.await?;
682+
683+
let rest_response = AccountTotalsREST {
684+
stake_address: account.to_string()?,
685+
received_sum: totals.received.into(),
686+
sent_sum: totals.sent.into(),
687+
tx_count: totals.tx_count,
688+
};
689+
690+
let json = serde_json::to_string_pretty(&rest_response)?;
691+
Ok(RESTResponse::with_json(200, &json))
629692
}
630693

631694
/// Handle `/accounts/{stake_address}/utxos` Blockfrost-compatible endpoint
@@ -659,7 +722,7 @@ pub async fn handle_account_utxos_blockfrost(
659722
.await?;
660723

661724
let Some(addresses) = addresses else {
662-
return Ok(RESTResponse::with_text(404, "Account not found"));
725+
return Err(RESTError::not_found("Account not found"));
663726
};
664727

665728
// Get utxos from address state

modules/rest_blockfrost/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ pub struct AccountUTxOREST {
939939
}
940940

941941
#[derive(serde::Serialize)]
942-
pub struct _AccountTotalsREST {
942+
pub struct AccountTotalsREST {
943943
pub stake_address: String,
944944
pub received_sum: AmountList,
945945
pub sent_sum: AmountList,

0 commit comments

Comments
 (0)