From 4769bd9c2326533168211c26ca5a1d726e876874 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 6 Dec 2021 12:25:23 +1100 Subject: [PATCH] Make `WalletInfo` in feed optional In case the wallet sync fails, we replace the value with `None` to indicate to the UI that we don't have an up-to-date wallet information. In case the sync works again at some point, we will replace it with `Some` again. Making this optional also allows us to start up the system without syncing the wallet, a step towards https://github.com/itchysats/itchysats/discussions/753. --- daemon/src/maker.rs | 3 +-- daemon/src/routes_maker.rs | 2 +- daemon/src/routes_taker.rs | 2 +- daemon/src/taker.rs | 4 ++-- daemon/src/to_sse_event.rs | 12 ++++++------ daemon/src/wallet_sync.rs | 6 +++--- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/daemon/src/maker.rs b/daemon/src/maker.rs index 98c36c6..fb72c4c 100644 --- a/daemon/src/maker.rs +++ b/daemon/src/maker.rs @@ -5,7 +5,6 @@ use bdk::{bitcoin, FeeRate}; use clap::{Parser, Subcommand}; use daemon::auth::{self, MAKER_USERNAME}; use daemon::model::cfd::Role; -use daemon::model::WalletInfo; use daemon::seed::Seed; use daemon::tokio_ext::FutureExt; use daemon::{ @@ -205,7 +204,7 @@ async fn main() -> Result<()> { "ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7", )?; - let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::(wallet_info); + let (wallet_feed_sender, wallet_feed_receiver) = watch::channel(Some(wallet_info)); let figment = rocket::Config::figment() .merge(("address", opts.http_address.ip())) diff --git a/daemon/src/routes_maker.rs b/daemon/src/routes_maker.rs index b30daaa..fcb1292 100644 --- a/daemon/src/routes_maker.rs +++ b/daemon/src/routes_maker.rs @@ -29,7 +29,7 @@ pub type Maker = xtra::Address< #[rocket::get("/feed")] pub async fn maker_feed( rx: &State, - rx_wallet: &State>, + rx_wallet: &State>>, _auth: Authenticated, ) -> EventStream![] { let rx = rx.inner(); diff --git a/daemon/src/routes_taker.rs b/daemon/src/routes_taker.rs index a03b39a..7e1e49b 100644 --- a/daemon/src/routes_taker.rs +++ b/daemon/src/routes_taker.rs @@ -25,7 +25,7 @@ type Taker = xtra::Address, - rx_wallet: &State>, + rx_wallet: &State>>, rx_maker_status: &State>, ) -> EventStream![] { let rx = rx.inner(); diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index caf8572..c64cc2e 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -5,7 +5,7 @@ use bdk::{bitcoin, FeeRate}; use clap::{Parser, Subcommand}; use daemon::connection::connect; use daemon::model::cfd::Role; -use daemon::model::{Identity, WalletInfo}; +use daemon::model::Identity; use daemon::seed::Seed; use daemon::tokio_ext::FutureExt; use daemon::{ @@ -204,7 +204,7 @@ async fn main() -> Result<()> { "ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7", )?; - let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::(wallet_info); + let (wallet_feed_sender, wallet_feed_receiver) = watch::channel(Some(wallet_info)); let mut tasks = Tasks::default(); diff --git a/daemon/src/to_sse_event.rs b/daemon/src/to_sse_event.rs index add1e92..eec421e 100644 --- a/daemon/src/to_sse_event.rs +++ b/daemon/src/to_sse_event.rs @@ -46,13 +46,13 @@ pub struct WalletInfo { last_updated_at: Timestamp, } -impl ToSseEvent for model::WalletInfo { +impl ToSseEvent for Option { fn to_sse_event(&self) -> Event { - let wallet_info = WalletInfo { - balance: self.balance, - address: self.address.to_string(), - last_updated_at: self.last_updated_at, - }; + let wallet_info = self.as_ref().map(|wallet_info| WalletInfo { + balance: wallet_info.balance, + address: wallet_info.address.to_string(), + last_updated_at: wallet_info.last_updated_at, + }); Event::json(&wallet_info).event("wallet") } diff --git a/daemon/src/wallet_sync.rs b/daemon/src/wallet_sync.rs index 0ff13ae..0d668ed 100644 --- a/daemon/src/wallet_sync.rs +++ b/daemon/src/wallet_sync.rs @@ -5,7 +5,7 @@ use tokio::sync::watch; use tokio::time::sleep; use xtra::Address; -pub async fn new(wallet: Address, sender: watch::Sender) { +pub async fn new(wallet: Address, sender: watch::Sender>) { loop { sleep(Duration::from_secs(10)).await; @@ -14,10 +14,10 @@ pub async fn new(wallet: Address, sender: watch::Sender info, + Ok(info) => Some(info), Err(e) => { tracing::warn!("Failed to sync wallet: {:#}", e); - continue; + None } };