From 417a949037b7c33895e34081d9acd23ddd4a10f8 Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Thu, 17 Apr 2025 13:46:28 +0100 Subject: [PATCH] fix: cache coins for remote backend when loading gui --- liana-gui/src/lianalite/login.rs | 41 ++++++++++++++++++++++---------- liana-gui/src/main.rs | 8 ++++--- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/liana-gui/src/lianalite/login.rs b/liana-gui/src/lianalite/login.rs index efa61e20..06366681 100644 --- a/liana-gui/src/lianalite/login.rs +++ b/liana-gui/src/lianalite/login.rs @@ -8,9 +8,13 @@ use liana_ui::{ icon, theme, widget::*, }; +use lianad::commands::ListCoinsResult; use crate::{ - app::settings::{AuthConfig, Settings, SettingsError, WalletSetting}, + app::{ + cache::coins_to_cache, + settings::{AuthConfig, Settings, SettingsError, WalletSetting}, + }, daemon::DaemonError, }; @@ -68,7 +72,16 @@ pub enum Message { // redirect to the installer with the remote backend connection. Install(Option), // redirect to the app runner with the remote backend connection. - Run(Result<(BackendWalletClient, api::Wallet), Error>), + Run( + Result< + ( + BackendWalletClient, + api::Wallet, + /* coins to cache */ ListCoinsResult, + ), + Error, + >, + ), } #[derive(Debug, Clone)] @@ -83,7 +96,7 @@ pub enum ViewMessage { #[derive(Debug, Clone)] pub enum BackendState { NoWallet(BackendClient), - WalletExists(BackendWalletClient, api::Wallet), + WalletExists(BackendWalletClient, api::Wallet, ListCoinsResult), } pub struct LianaLiteLogin { @@ -182,10 +195,11 @@ impl LianaLiteLogin { Ok(BackendState::NoWallet(_)) => { self.auth_error = Some("No wallet found for the given email"); } - Ok(BackendState::WalletExists(client, wallet)) => { - return Task::perform(async move { (client, wallet) }, |(c, w)| { - Message::Run(Ok((c, w))) - }); + Ok(BackendState::WalletExists(client, wallet, coins)) => { + return Task::perform( + async move { (client, wallet, coins) }, + |(c, w, coins)| Message::Run(Ok((c, w, coins))), + ); } Err(e) => { self.connection_error = Some(e); @@ -323,7 +337,7 @@ impl LianaLiteLogin { Ok(BackendState::NoWallet(client)) => { return Task::perform(async move { Some(client) }, Message::Install); } - Ok(BackendState::WalletExists(client, wallet)) => { + Ok(BackendState::WalletExists(client, wallet, coins)) => { let datadir = self.datadir.clone(); let network = self.network; return Task::perform( @@ -337,7 +351,7 @@ impl LianaLiteLogin { ) .await?; - Ok((client, wallet)) + Ok((client, wallet, coins)) }, Message::Run, ); @@ -545,10 +559,12 @@ pub async fn connect( if wallet_id.is_empty() { let first = wallets.first().cloned().ok_or(DaemonError::NoAnswer)?; let (wallet_client, wallet) = client.connect_wallet(first); - Ok(BackendState::WalletExists(wallet_client, wallet)) + let coins = coins_to_cache(Arc::new(wallet_client.clone())).await?; + Ok(BackendState::WalletExists(wallet_client, wallet, coins)) } else if let Some(wallet) = wallets.into_iter().find(|w| w.id == wallet_id) { let (wallet_client, wallet) = client.connect_wallet(wallet); - Ok(BackendState::WalletExists(wallet_client, wallet)) + let coins = coins_to_cache(Arc::new(wallet_client.clone())).await?; + Ok(BackendState::WalletExists(wallet_client, wallet, coins)) } else { Ok(BackendState::NoWallet(client)) } @@ -571,7 +587,8 @@ pub async fn connect_with_refresh_token( .find(|w| w.id == wallet_id) { let (wallet_client, wallet) = client.connect_wallet(wallet); - Ok(BackendState::WalletExists(wallet_client, wallet)) + let coins = coins_to_cache(Arc::new(wallet_client.clone())).await?; + Ok(BackendState::WalletExists(wallet_client, wallet, coins)) } else { Ok(BackendState::NoWallet(client)) } diff --git a/liana-gui/src/main.rs b/liana-gui/src/main.rs index a9c97e0b..50cae804 100644 --- a/liana-gui/src/main.rs +++ b/liana-gui/src/main.rs @@ -19,7 +19,7 @@ extern crate serde_json; use liana::miniscript::bitcoin; use liana_ui::{component::text, font, image, theme, widget::Element}; -use lianad::config::Config as DaemonConfig; +use lianad::{commands::ListCoinsResult, config::Config as DaemonConfig}; use liana_gui::{ app::{self, cache::Cache, config::default_datadir, wallet::Wallet, App}, @@ -272,7 +272,7 @@ impl GUI { self.state = State::Installer(Box::new(install)); command.map(|msg| Message::Install(Box::new(msg))) } - login::Message::Run(Ok((backend_client, wallet))) => { + login::Message::Run(Ok((backend_client, wallet, coins))) => { let config = app::Config::from_file( &l.datadir .join(l.network.to_string()) @@ -288,6 +288,7 @@ impl GUI { let (app, command) = create_app_with_remote_backend( backend_client, wallet, + coins, l.datadir.clone(), l.network, config, @@ -463,6 +464,7 @@ impl GUI { pub fn create_app_with_remote_backend( remote_backend: BackendWalletClient, wallet: api::Wallet, + coins: ListCoinsResult, datadir: PathBuf, network: bitcoin::Network, config: app::Config, @@ -498,7 +500,7 @@ pub fn create_app_with_remote_backend( App::new( Cache { network, - coins: Vec::new(), + coins: coins.coins, rescan_progress: None, sync_progress: 1.0, // Remote backend is always synced datadir_path: datadir.clone(),