Skip to content

Commit 8ca24c7

Browse files
refactor: extract swap/src/monero/wallet.rs to new monero-wallet crate
Closes #798
1 parent 3df7e81 commit 8ca24c7

File tree

9 files changed

+122
-39
lines changed

9 files changed

+122
-39
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",

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"] }
@@ -71,7 +72,6 @@ swap-p2p = { path = "../swap-p2p" }
7172
swap-proptest = { path = "../swap-proptest" }
7273
swap-serde = { path = "../swap-serde" }
7374
tauri = { version = "2.0", features = ["config-json5"], optional = true, default-features = false }
74-
throttle = { path = "../throttle" }
7575
time = "0.3"
7676
url = { workspace = true }
7777
uuid = { workspace = true, features = ["serde"] }
@@ -97,10 +97,6 @@ serde_with = { version = "1", features = ["macros"] }
9797
strum = { workspace = true, features = ["derive"] }
9898
typeshare = { workspace = true }
9999

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

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,41 @@ impl bitcoin_wallet::BitcoinTauriBackgroundTask
501501
}
502502
}
503503

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+
504539
impl Display for ApprovalRequest {
505540
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
506541
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)