Skip to content

Commit

Permalink
Add to preview mint as well
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Feb 1, 2025
1 parent 13a1727 commit 3a8048c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
23 changes: 16 additions & 7 deletions harbor-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::fedimint_client::{
spawn_invoice_receive_subscription, spawn_onchain_payment_subscription,
spawn_onchain_receive_subscription, FederationInviteOrId, FedimintClient,
};
use crate::metadata::{get_federation_metadata, FederationMeta, CACHE};
use crate::metadata::{get_federation_metadata, FederationData, FederationMeta, CACHE};
use anyhow::anyhow;
use bip39::Mnemonic;
use bitcoin::address::NetworkUnchecked;
Expand Down Expand Up @@ -126,10 +126,16 @@ pub enum CoreUIMsg {
TransferFailure(String),
// todo probably want a way to incrementally add items to the history
TransactionHistoryUpdated(Vec<TransactionItem>),
FederationBalanceUpdated { id: FederationId, balance: Amount },
FederationBalanceUpdated {
id: FederationId,
balance: Amount,
},
AddFederationFailed(String),
RemoveFederationFailed(String),
FederationInfo(ClientConfig),
FederationInfo {
config: ClientConfig,
metadata: FederationMeta,
},
AddFederationSuccess,
RemoveFederationSuccess,
FederationListNeedsUpdate,
Expand Down Expand Up @@ -472,7 +478,7 @@ impl HarborCore {
pub async fn get_federation_info(
&self,
invite_code: InviteCode,
) -> anyhow::Result<ClientConfig> {
) -> anyhow::Result<(ClientConfig, FederationMeta)> {
log::info!("Getting federation info for invite code: {invite_code}");
let download = Instant::now();
let config = {
Expand All @@ -499,7 +505,9 @@ impl HarborCore {
download.elapsed().as_millis()
);

Ok(config)
let metadata = get_federation_metadata(FederationData::Config(&config)).await;

Ok((config, metadata))
}

pub async fn add_federation(&self, invite_code: InviteCode) -> anyhow::Result<()> {
Expand Down Expand Up @@ -594,8 +602,9 @@ impl HarborCore {
tokio::task::spawn(async move {
let mut w = CACHE.write().await;
for client in needs_metadata {
let metadata = get_federation_metadata(&client).await;
w.insert(client.federation_id(), metadata);
let id = client.federation_id();
let metadata = get_federation_metadata(FederationData::Client(&client)).await;
w.insert(id, metadata);
}
drop(w);

Expand Down
43 changes: 32 additions & 11 deletions harbor-client/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::http::make_get_request;
use bitcoin::secp256k1::PublicKey;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::ClientConfig;
use fedimint_core::config::FederationId;
use fedimint_core::module::serde_json;
use log::error;
Expand All @@ -13,6 +14,27 @@ use tokio::sync::RwLock;
pub(crate) static CACHE: Lazy<RwLock<HashMap<FederationId, FederationMeta>>> =
Lazy::new(|| RwLock::new(HashMap::new()));

pub(crate) enum FederationData<'a> {
Client(&'a ClientHandleArc),
Config(&'a ClientConfig),
}

impl FederationData<'_> {
pub(crate) fn get_meta(&self, str: &str) -> Option<String> {
match self {
FederationData::Client(c) => c.get_meta(str),
FederationData::Config(c) => c.meta(str).ok().flatten(),
}
}

pub(crate) fn federation_id(&self) -> FederationId {
match self {
FederationData::Client(c) => c.federation_id(),
FederationData::Config(c) => c.global.calculate_federation_id(),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct FederationMetaConfig {
#[serde(flatten)]
Expand Down Expand Up @@ -50,14 +72,14 @@ impl FederationMeta {
}
}

pub(crate) async fn get_federation_metadata(fedimint_client: &ClientHandleArc) -> FederationMeta {
let meta_external_url = fedimint_client.get_meta("meta_external_url");
pub(crate) async fn get_federation_metadata(data: FederationData<'_>) -> FederationMeta {
let meta_external_url = data.get_meta("meta_external_url");
let config: Option<FederationMeta> = match meta_external_url.as_ref() {
None => None,
Some(url) => match make_get_request::<FederationMetaConfig>(url).await {
Ok(m) => m
.federations
.get(&fedimint_client.federation_id().to_string())
.get(&data.federation_id().to_string())
.cloned(),
Err(e) => {
error!("Error fetching external metadata: {}", e);
Expand All @@ -69,35 +91,34 @@ pub(crate) async fn get_federation_metadata(fedimint_client: &ClientHandleArc) -
FederationMeta {
meta_external_url, // Already set...
federation_name: merge_values(
fedimint_client.get_meta("federation_name").clone(),
data.get_meta("federation_name").clone(),
config.as_ref().and_then(|c| c.federation_name.clone()),
),
federation_expiry_timestamp: merge_values(
fedimint_client.get_meta("federation_expiry_timestamp"),
data.get_meta("federation_expiry_timestamp"),
config
.as_ref()
.and_then(|c| c.federation_expiry_timestamp.clone()),
),
welcome_message: merge_values(
fedimint_client.get_meta("welcome_message"),
data.get_meta("welcome_message"),
config.as_ref().and_then(|c| c.welcome_message.clone()),
),
vetted_gateways: config.as_ref().and_then(|c| c.vetted_gateways.clone()),
federation_icon_url: merge_values(
fedimint_client.get_meta("federation_icon_url"),
data.get_meta("federation_icon_url"),
config.as_ref().and_then(|c| c.federation_icon_url.clone()),
),
preview_message: merge_values(
fedimint_client.get_meta("preview_message"),
data.get_meta("preview_message"),
config.as_ref().and_then(|c| c.preview_message.clone()),
),
popup_end_timestamp: merge_values(
fedimint_client.get_meta("popup_end_timestamp"),
data.get_meta("popup_end_timestamp"),
config.as_ref().and_then(|c| c.popup_end_timestamp.clone()),
),
popup_countdown_message: merge_values(
fedimint_client
.get_meta("popup_countdown_message")
data.get_meta("popup_countdown_message")
.map(|v| v.to_string()),
config
.as_ref()
Expand Down
5 changes: 3 additions & 2 deletions harbor-ui/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,9 @@ async fn process_core(core_handle: &mut CoreHandle, core: &HarborCore) {
core.msg(msg.id, CoreUIMsg::AddFederationFailed(e.to_string()))
.await;
}
Ok(config) => {
core.msg(msg.id, CoreUIMsg::FederationInfo(config)).await;
Ok((config, metadata)) => {
core.msg(msg.id, CoreUIMsg::FederationInfo { config, metadata })
.await;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion harbor-ui/src/components/federation_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn h_federation_item(item: &FederationItem, invite_code: Option<String>) ->
balance,
guardians,
module_kinds: _, // We don't care about module kinds
metadata: _, // todo use
metadata,
} = item;

let name_row = row![map_icon(SvgIcon::People, 24., 24.), text(name).size(24)]
Expand All @@ -33,6 +33,10 @@ pub fn h_federation_item(item: &FederationItem, invite_code: Option<String>) ->
column = column.push(text(guardian_text).size(18).style(subtitle));
}

if let Some(welcome) = metadata.welcome_message.as_ref() {
column = column.push(text(welcome).size(18).style(subtitle));
}

match invite_code {
// Preview mode with Add button
Some(code) => {
Expand Down
4 changes: 2 additions & 2 deletions harbor-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ impl HarborWallet {
let (_, task) = self.send_from_ui(UICoreMsg::FederationListNeedsUpdate);
task
}
CoreUIMsg::FederationInfo(config) => {
CoreUIMsg::FederationInfo { config, metadata } => {
let id = config.calculate_federation_id();
let name = config.meta::<String>("federation_name");
let guardians: Vec<String> = config
Expand All @@ -918,7 +918,7 @@ impl HarborWallet {
balance: 0,
guardians: Some(guardians),
module_kinds: Some(module_kinds),
metadata: Default::default(),
metadata,
};

self.peek_federation_item = Some(item);
Expand Down

0 comments on commit 3a8048c

Please sign in to comment.