From 62ad10ac9c748559631f2d6eb2a1a5f8a02e4199 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Mon, 12 Aug 2024 00:13:31 +0200 Subject: [PATCH] wip: Tauri event system --- Cargo.lock | 1 + src-tauri/Cargo.toml | 10 +++---- src-tauri/src/lib.rs | 3 ++- swap/Cargo.toml | 59 +++++++++++++++++++++-------------------- swap/src/api.rs | 14 +++++----- swap/src/rpc/methods.rs | 2 +- 6 files changed, 47 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea46b0e7..7c77ea8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6396,6 +6396,7 @@ dependencies = [ "sqlx", "structopt", "strum", + "tauri", "tempfile", "testcontainers", "thiserror", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7fa632b3..8f12d3bb 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "unstoppableswap-gui-rs" version = "0.0.0" -authors = [ "binarybaron", "einliterflasche", "unstoppableswap" ] +authors = ["binarybaron", "einliterflasche", "unstoppableswap"] edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -9,15 +9,15 @@ description = "GUI for XMR<>BTC Atomic Swaps written in Rust" [lib] name = "unstoppableswap_gui_rs_lib" -crate-type = [ "lib", "cdylib", "staticlib" ] +crate-type = ["lib", "cdylib", "staticlib"] [build-dependencies] -tauri-build = { version = "2.0.0-rc.1", features = [ "config-json5" ] } +tauri-build = { version = "2.0.0-rc.1", features = ["config-json5"] } [dependencies] anyhow = "1" once_cell = "1" -serde = { version = "1", features = [ "derive" ] } +serde = { version = "1", features = ["derive"] } serde_json = "1" swap = { path = "../swap" } -tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ] } +tauri = { version = "2.0.0-rc.1", features = ["config-json5"] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 01e3d1d9..158cbf80 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -89,7 +89,8 @@ fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> true, None, ) - .await? + .await + .unwrap() .with_tauri_handle(app.app_handle().to_owned()); app.manage(Arc::new(context)); diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 2a8fb66e..cb41c25e 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -9,36 +9,7 @@ description = "XMR/BTC trustless atomic swaps." name = "swap" [dependencies] -tauri = { version = "2.0.0-rc.1" } - -[target.'cfg(not(windows))'.dependencies] -tokio-tar = "0.3" - -[target.'cfg(windows)'.dependencies] -zip = "0.5" - -[dev-dependencies] -bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs.git", rev = "80cc8d05db2610d8531011be505b7bee2b5cdf9f" } -get-port = "3" -hyper = "1.3" -jsonrpsee = { version = "0.16.2", features = ["ws-client"] } -mockito = "1.4" -monero-harness = { path = "../monero-harness" } -port_check = "0.2" -proptest = "1" -sequential-test = "0.2.4" -serde_cbor = "0.11" -serial_test = "3.1" -tempfile = "3" -testcontainers = "0.15" - -[build-dependencies] anyhow = "1" -vergen = { version = "8.3", default-features = false, features = [ - "build", - "git", - "git2", -] } async-compression = { version = "0.3", features = ["bzip2", "tokio"] } async-trait = "0.1" atty = "0.2" @@ -150,3 +121,33 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [ url = { version = "2", features = ["serde"] } uuid = { version = "1.9", features = ["serde", "v4"] } void = "1" +tauri = { version = "2.0.0-rc.1", features = ["config-json5"] } + +[target.'cfg(not(windows))'.dependencies] +tokio-tar = "0.3" + +[target.'cfg(windows)'.dependencies] +zip = "0.5" + +[dev-dependencies] +bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs.git", rev = "80cc8d05db2610d8531011be505b7bee2b5cdf9f" } +get-port = "3" +hyper = "1.3" +jsonrpsee = { version = "0.16.2", features = ["ws-client"] } +mockito = "1.4" +monero-harness = { path = "../monero-harness" } +port_check = "0.2" +proptest = "1" +sequential-test = "0.2.4" +serde_cbor = "0.11" +serial_test = "3.1" +tempfile = "3" +testcontainers = "0.15" + +[build-dependencies] +anyhow = "1" +vergen = { version = "8.3", default-features = false, features = [ + "build", + "git", + "git2", +] } diff --git a/swap/src/api.rs b/swap/src/api.rs index 3eb6868e..0d938559 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -7,14 +7,15 @@ use crate::network::rendezvous::XmrBtcNamespace; use crate::protocol::Database; use crate::seed::Seed; use crate::{bitcoin, cli, monero}; -use anyhow::{bail, Context as AnyContext, Error, Result}; +use anyhow::{anyhow, bail, Context as AnyContext, Error, Result}; use futures::future::try_join_all; +use serde::Serialize; use std::fmt; use std::future::Future; use std::net::SocketAddr; use std::path::PathBuf; use std::sync::{Arc, Once}; -use tauri::AppHandle; +use tauri::{AppHandle, Emitter}; use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock}; use tokio::task::JoinHandle; use url::Url; @@ -250,14 +251,15 @@ impl Context { Ok(context) } - pub fn with_tauri_handle(mut self, tauri_handle: Arc) -> Self { - self.tauri_handle = Some(tauri_handle); + pub fn with_tauri_handle(mut self, tauri_handle: AppHandle) -> Self { + self.tauri_handle = Some(Arc::new(tauri_handle)); self } - pub fn emit_tauri_event(mut self, event: &str, payload: S) -> Result<()> { + pub fn emit_tauri_event(self, event: &str, payload: S) -> Result<()> { self.tauri_handle - .ok_or_else(|| anyhow!("Tauri handle not set"))? + .ok_or(anyhow!("Tauri handle not set"))? + .as_ref() .emit(event, payload)?; Ok(()) diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index d50e029a..7c7cc6c0 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -1,7 +1,7 @@ use crate::api::request::{ buy_xmr, cancel_and_refund, get_balance, get_current_swap, get_history, get_raw_states, get_swap_info, list_sellers, monero_recovery, resume_swap, suspend_current_swap, withdraw_btc, - BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, Method, + BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, MoneroRecoveryArgs, ResumeArgs, WithdrawBtcArgs, }; use crate::api::Context;