1use std::collections::BTreeMap;
2use std::sync::Arc;
3use std::time::Duration;
45use async_trait::async_trait;
6use fedimint_core::bitcoin::Network;
7use fedimint_core::core::ModuleKind;
8use fedimint_core::module::ApiAuth;
9use fedimint_core::module::audit::AuditSummary;
10use fedimint_core::session_outcome::SessionStatusV2;
11use fedimint_core::util::SafeUrl;
12use fedimint_core::{Feerate, PeerId};
1314use crate::{DynServerModule, ServerModule};
1516pub type DynDashboardApi = Arc<dyn IDashboardApi + Send + Sync + 'static>;
1718/// Interface for guardian dashboard API in a running federation
19#[async_trait]
20pub trait IDashboardApi {
21/// Get the guardian's authentication details
22async fn auth(&self) -> ApiAuth;
2324/// Get the guardian ID
25async fn guardian_id(&self) -> PeerId;
2627/// Get a map of peer IDs to guardian names
28async fn guardian_names(&self) -> BTreeMap<PeerId, String>;
2930/// Get the federation name
31async fn federation_name(&self) -> String;
3233/// Get the current active session count
34async fn session_count(&self) -> u64;
3536/// Get items in a given session
37async fn get_session_status(&self, session_idx: u64) -> SessionStatusV2;
3839/// The time it took to order our last proposal in the current session
40async fn consensus_ord_latency(&self) -> Option<Duration>;
4142/// Returns a map of peer ID to estimated round trip time
43async fn p2p_connection_status(&self) -> BTreeMap<PeerId, Option<Duration>>;
4445/// Get the federation invite code to share with users
46async fn federation_invite_code(&self) -> String;
4748/// Get the federation audit summary
49async fn federation_audit(&self) -> AuditSummary;
5051/// Get the url of the bitcoin rpc
52async fn bitcoin_rpc_url(&self) -> SafeUrl;
5354/// Get the status of the bitcoin backend
55async fn bitcoin_rpc_status(&self) -> Option<ServerBitcoinRpcStatus>;
5657/// Get reference to a server module instance by module kind
58fn get_module_by_kind(&self, kind: ModuleKind) -> Option<&DynServerModule>;
5960/// Create a trait object
61fn into_dyn(self) -> DynDashboardApi
62where
63Self: Sized + Send + Sync + 'static,
64 {
65 Arc::new(self)
66 }
67}
6869/// Extension trait for IDashboardApi providing type-safe module access
70pub trait DashboardApiModuleExt {
71/// Get a typed reference to a server module instance by kind
72fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
73}
7475impl DashboardApiModuleExt for DynDashboardApi {
76fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
77self.get_module_by_kind(M::module_kind())?
78.as_any()
79 .downcast_ref::<M>()
80 }
81}
8283#[derive(Debug, Clone)]
84pub struct ServerBitcoinRpcStatus {
85pub network: Network,
86pub block_count: u64,
87pub fee_rate: Feerate,
88pub sync_percentage: Option<f64>,
89}