Skip to content

Commit c59e4cd

Browse files
refactor: extract swap/src/monero/wallet.rs to new monero-wallet crate (#803)
* refactor(bitcoin-wallet): Arc<Box<dyn Trait>> -> Arc<dyn Trait> * refactor: extract swap/src/monero/wallet.rs to new monero-wallet crate Closes #798
1 parent e7ae08d commit c59e4cd

File tree

10 files changed

+134
-55
lines changed

10 files changed

+134
-55
lines changed

Cargo.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"monero-rpc-pool",
1010
"monero-sys",
1111
"monero-tests",
12+
"monero-wallet",
1213
"src-tauri",
1314
"swap",
1415
"swap-asb",

bitcoin-wallet/src/wallet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ use tokio::sync::watch;
3838
use tokio::sync::Mutex as TokioMutex;
3939
use tracing::{debug_span, Instrument};
4040

41-
pub type TauriHandle = Option<Arc<Box<dyn BitcoinTauriHandle>>>;
41+
pub type TauriHandle = Option<Arc<dyn BitcoinTauriHandle>>;
4242
pub trait BitcoinTauriHandle: Send + Sync {
4343
/// let progress_handle = tauri_handle.new_background_process_with_initial_progress(
4444
/// TauriBackgroundProgress::FullScanningBitcoinWallet,
4545
/// TauriBitcoinFullScanProgress::Unknown,
4646
/// );
47-
fn start_full_scan(&self) -> Arc<Box<dyn BitcoinTauriBackgroundTask>>;
47+
fn start_full_scan(&self) -> Arc<dyn BitcoinTauriBackgroundTask>;
4848

4949
/// let background_process_handle = self
5050
/// .tauri_handle
5151
/// .new_background_process_with_initial_progress(
5252
/// TauriBackgroundProgress::SyncingBitcoinWallet,
5353
/// TauriBitcoinSyncProgress::Unknown,
5454
/// );
55-
fn start_sync(&self) -> Arc<Box<dyn BitcoinTauriBackgroundTask>>;
55+
fn start_sync(&self) -> Arc<dyn BitcoinTauriBackgroundTask>;
5656
}
5757
pub trait BitcoinTauriBackgroundTask: Send + Sync {
5858
/// progress_handle_clone.update(TauriBitcoinFullScanProgress::Known {

monero-wallet/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "monero-wallet"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
# Error handling
8+
anyhow = { workspace = true }
9+
10+
# Other stuff
11+
monero = { workspace = true }
12+
monero-sys = { path = "../monero-sys" }
13+
swap-core = { path = "../swap-core" }
14+
throttle = { path = "../throttle" }
15+
time = "0.3"
16+
uuid = { workspace = true }
17+
18+
# Tokio
19+
tokio = { workspace = true, features = ["process", "fs", "net", "parking_lot", "rt"] }
20+
21+
# Tracing
22+
tracing = { workspace = true }
23+
tracing-appender = "0.2"
24+
tracing-subscriber = { workspace = true }
25+
26+
# monero-oxide
27+
monero-oxide-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git", package = "monero-rpc" }
28+
monero-simple-request-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git" }
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,31 @@ use throttle::{throttle, Throttle};
1616
use tokio::sync::RwLock;
1717
use 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<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
};
@@ -367,7 +372,7 @@ impl Wallets {
367372
&self,
368373
swap_id: Uuid,
369374
spend_key: monero::PrivateKey,
370-
view_key: super::PrivateViewKey,
375+
view_key: PrivateViewKey,
371376
tx_lock_id: TxHash,
372377
) -> Result<Arc<Wallet>> {
373378
// Derive wallet address from the keys

swap/Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ bdk = { workspace = true }
1717
bdk_wallet = { workspace = true }
1818
bitcoin = { workspace = true }
1919

20-
# Bitcoin Wallet
20+
# Wallets
2121
bitcoin-wallet = { path = "../bitcoin-wallet" }
22+
monero-wallet = { path = "../monero-wallet" }
2223

2324
# Tor
2425
arti-client = { workspace = true, features = ["static-sqlite", "tokio", "rustls", "onion-service-service"] }
@@ -70,7 +71,6 @@ swap-p2p = { path = "../swap-p2p" }
7071
swap-proptest = { path = "../swap-proptest" }
7172
swap-serde = { path = "../swap-serde" }
7273
tauri = { version = "2.0", features = ["config-json5"], optional = true, default-features = false }
73-
throttle = { path = "../throttle" }
7474
time = "0.3"
7575
url = { workspace = true }
7676
uuid = { workspace = true, features = ["serde"] }
@@ -96,10 +96,6 @@ serde_with = { version = "1", features = ["macros"] }
9696
strum = { workspace = true, features = ["derive"] }
9797
typeshare = { workspace = true }
9898

99-
# monero-oxide
100-
monero-oxide-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git", package = "monero-rpc" }
101-
monero-simple-request-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git" }
102-
10399
# Database
104100
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio-rustls"] }
105101

swap/src/cli/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ mod builder {
730730
daemon.clone(),
731731
env_config.monero_network,
732732
false,
733-
self.tauri_handle.clone(),
733+
self.tauri_handle.clone().map(|th| th.into()),
734734
wallet,
735735
wallet_database,
736736
)

swap/src/cli/api/tauri_bindings.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -454,26 +454,22 @@ struct BitcoinTauriHandle(TauriHandle);
454454

455455
impl Into<bitcoin_wallet::TauriHandle> for TauriHandle {
456456
fn into(self) -> bitcoin_wallet::TauriHandle {
457-
Some(Arc::new(Box::new(BitcoinTauriHandle(self))))
457+
Some(Arc::new(BitcoinTauriHandle(self)))
458458
}
459459
}
460460

461461
impl bitcoin_wallet::BitcoinTauriHandle for BitcoinTauriHandle {
462-
fn start_full_scan(&self) -> Arc<Box<dyn bitcoin_wallet::BitcoinTauriBackgroundTask>> {
463-
Arc::new(Box::new(
464-
self.0.new_background_process_with_initial_progress(
465-
TauriBackgroundProgress::FullScanningBitcoinWallet,
466-
TauriBitcoinFullScanProgress::Unknown,
467-
),
462+
fn start_full_scan(&self) -> Arc<dyn bitcoin_wallet::BitcoinTauriBackgroundTask> {
463+
Arc::new(self.0.new_background_process_with_initial_progress(
464+
TauriBackgroundProgress::FullScanningBitcoinWallet,
465+
TauriBitcoinFullScanProgress::Unknown,
468466
))
469467
}
470468

471-
fn start_sync(&self) -> Arc<Box<dyn bitcoin_wallet::BitcoinTauriBackgroundTask>> {
472-
Arc::new(Box::new(
473-
self.0.new_background_process_with_initial_progress(
474-
TauriBackgroundProgress::SyncingBitcoinWallet,
475-
TauriBitcoinSyncProgress::Unknown,
476-
),
469+
fn start_sync(&self) -> Arc<dyn bitcoin_wallet::BitcoinTauriBackgroundTask> {
470+
Arc::new(self.0.new_background_process_with_initial_progress(
471+
TauriBackgroundProgress::SyncingBitcoinWallet,
472+
TauriBitcoinSyncProgress::Unknown,
477473
))
478474
}
479475
}
@@ -505,6 +501,41 @@ impl bitcoin_wallet::BitcoinTauriBackgroundTask
505501
}
506502
}
507503

504+
struct MoneroTauriHandle(TauriHandle);
505+
506+
impl Into<monero_wallet::TauriHandle> for TauriHandle {
507+
fn into(self) -> monero_wallet::TauriHandle {
508+
Arc::new(MoneroTauriHandle(self))
509+
}
510+
}
511+
512+
impl monero_wallet::MoneroTauriHandle for MoneroTauriHandle {
513+
fn balance_change(&self, total_balance: monero::Amount, unlocked_balance: monero::Amount) {
514+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
515+
MoneroWalletUpdate::BalanceChange(GetMoneroBalanceResponse {
516+
total_balance,
517+
unlocked_balance,
518+
}),
519+
))
520+
}
521+
522+
fn history_update(&self, transactions: Vec<monero_sys::TransactionInfo>) {
523+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
524+
MoneroWalletUpdate::HistoryUpdate(GetMoneroHistoryResponse { transactions }),
525+
))
526+
}
527+
528+
fn sync_progress(&self, current_block: u64, target_block: u64, progress_percentage: f32) {
529+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
530+
MoneroWalletUpdate::SyncProgress(GetMoneroSyncProgressResponse {
531+
current_block,
532+
target_block,
533+
progress_percentage,
534+
}),
535+
))
536+
}
537+
}
538+
508539
impl Display for ApprovalRequest {
509540
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510541
match self.request {

swap/src/common/tracing_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ mod crates {
223223
pub const OUR_CRATES: &[&str] = &[
224224
// Library crates
225225
"bitcoin_wallet",
226+
"monero_wallet",
226227
"swap_p2p",
227228
"swap_env",
228229
"swap_core",

swap/src/monero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod wallet;
1+
pub use monero_wallet as wallet;
22
pub mod wallet_rpc;
33

44
pub use ::monero::network::Network;

0 commit comments

Comments
 (0)