@@ -16,12 +16,31 @@ use throttle::{throttle, Throttle};
1616use tokio:: sync:: RwLock ;
1717use uuid:: Uuid ;
1818
19- use crate :: cli:: api:: {
20- request:: { GetMoneroBalanceResponse , GetMoneroHistoryResponse , GetMoneroSyncProgressResponse } ,
21- tauri_bindings:: { MoneroWalletUpdate , TauriEmitter , TauriEvent , TauriHandle } ,
22- } ;
23-
24- use super :: { BlockHeight , TxHash , WatchRequest } ;
19+ use swap_core:: monero:: primitives:: { Amount , BlockHeight , PrivateViewKey , TxHash , WatchRequest } ;
20+
21+ pub type TauriHandle = Arc < Box < dyn MoneroTauriHandle > > ;
22+ pub trait MoneroTauriHandle : Send + Sync {
23+ /// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
24+ /// MoneroWalletUpdate::BalanceChange(GetMoneroBalanceResponse {
25+ /// total_balance, unlocked_balance,
26+ /// })
27+ /// ));
28+ fn balance_change ( & self , total_balance : Amount , unlocked_balance : Amount ) ;
29+
30+ /// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
31+ /// MoneroWalletUpdate::HistoryUpdate(
32+ /// GetMoneroHistoryResponse { transactions }
33+ /// ),
34+ /// ));
35+ fn history_update ( & self , transactions : Vec < monero_sys:: TransactionInfo > ) ;
36+
37+ /// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
38+ /// MoneroWalletUpdate::SyncProgress(GetMoneroSyncProgressResponse {
39+ /// current_block, target_block, progress_percentage,
40+ /// }),
41+ /// ));
42+ fn sync_progress ( & self , current_block : u64 , target_block : u64 , progress_percentage : f32 ) ;
43+ }
2544
2645/// Entrance point to the Monero blockchain.
2746/// You can use this struct to open specific wallets and monitor the blockchain.
@@ -84,13 +103,7 @@ impl TauriWalletListener {
84103 }
85104 } ;
86105
87- let response = GetMoneroBalanceResponse {
88- total_balance : total_balance. into ( ) ,
89- unlocked_balance : unlocked_balance. into ( ) ,
90- } ;
91- tauri. emit_unified_event ( TauriEvent :: MoneroWalletUpdate (
92- MoneroWalletUpdate :: BalanceChange ( response) ,
93- ) ) ;
106+ tauri. balance_change ( total_balance. into ( ) , unlocked_balance. into ( ) ) ;
94107 } ) ;
95108 }
96109 } ;
@@ -111,11 +124,8 @@ impl TauriWalletListener {
111124 return ;
112125 }
113126 } ;
114- let response = GetMoneroHistoryResponse { transactions } ;
115127
116- tauri. emit_unified_event ( TauriEvent :: MoneroWalletUpdate (
117- MoneroWalletUpdate :: HistoryUpdate ( response) ,
118- ) ) ;
128+ tauri. history_update ( transactions) ;
119129 } ) ;
120130 }
121131 } ;
@@ -138,16 +148,11 @@ impl TauriWalletListener {
138148 } ;
139149
140150 let progress_percentage = sync_progress. percentage ( ) ;
141-
142- let response = GetMoneroSyncProgressResponse {
143- current_block : sync_progress. current_block ,
144- target_block : sync_progress. target_block ,
145- progress_percentage : progress_percentage,
146- } ;
147-
148- tauri. emit_unified_event ( TauriEvent :: MoneroWalletUpdate (
149- MoneroWalletUpdate :: SyncProgress ( response) ,
150- ) ) ;
151+ tauri. sync_progress (
152+ sync_progress. current_block ,
153+ sync_progress. target_block ,
154+ progress_percentage,
155+ ) ;
151156 } ) ;
152157 }
153158 } ;
@@ -223,15 +228,17 @@ impl Wallets {
223228 ///
224229 /// The main wallet will be kept alive and synced, other wallets are
225230 /// opened and closed on demand.
226- pub async fn new (
231+ pub async fn new < T : Into < TauriHandle > > (
227232 wallet_dir : PathBuf ,
228233 main_wallet_name : String ,
229234 daemon : Daemon ,
230235 network : Network ,
231236 regtest : bool ,
232- tauri_handle : Option < TauriHandle > ,
237+ tauri_handle : Option < T > ,
233238 wallet_database : Option < Arc < monero_sys:: Database > > ,
234239 ) -> Result < Self > {
240+ let tauri_handle = tauri_handle. map ( |th| th. into ( ) ) ;
241+
235242 let main_wallet = Wallet :: open_or_create (
236243 wallet_dir. join ( & main_wallet_name) . display ( ) . to_string ( ) ,
237244 daemon. clone ( ) ,
@@ -290,15 +297,16 @@ impl Wallets {
290297
291298 /// Create a new `Wallets` instance with an existing wallet as the main wallet.
292299 /// This is used when we want to use a user-selected wallet instead of creating a new one.
293- pub async fn new_with_existing_wallet (
300+ pub async fn new_with_existing_wallet < T : Into < TauriHandle > > (
294301 wallet_dir : PathBuf ,
295302 daemon : Daemon ,
296303 network : Network ,
297304 regtest : bool ,
298- tauri_handle : Option < TauriHandle > ,
305+ tauri_handle : Option < T > ,
299306 existing_wallet : Wallet ,
300307 wallet_database : Option < Arc < monero_sys:: Database > > ,
301308 ) -> Result < Self > {
309+ let tauri_handle = tauri_handle. map ( |th| th. into ( ) ) ;
302310 // TODO: This code is duplicated in [`Wallets::new`]. Unify it.
303311
304312 if regtest {
@@ -367,7 +375,7 @@ impl Wallets {
367375 & self ,
368376 swap_id : Uuid ,
369377 spend_key : monero:: PrivateKey ,
370- view_key : super :: PrivateViewKey ,
378+ view_key : PrivateViewKey ,
371379 tx_lock_id : TxHash ,
372380 ) -> Result < Arc < Wallet > > {
373381 // Derive wallet address from the keys
0 commit comments