refactor(tauri, swap): move rpc api to cli/api

This commit is contained in:
binarybaron 2024-08-26 15:40:50 +02:00
parent 5425a83871
commit 349035d321
No known key found for this signature in database
GPG key ID: 99B75D3E1476A26E
15 changed files with 1231 additions and 46 deletions

23
Cargo.lock generated
View file

@ -2252,7 +2252,7 @@ version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6063efb63db582968fb7df72e1ae68aa6360dcfb0a75143f34fc7d616bad75e" checksum = "c6063efb63db582968fb7df72e1ae68aa6360dcfb0a75143f34fc7d616bad75e"
dependencies = [ dependencies = [
"proc-macro-crate 1.3.1", "proc-macro-crate 1.1.3",
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3761,7 +3761,7 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "424f6e86263cd5294cbd7f1e95746b95aca0e0d66bff31e5a40d6baa87b4aa99" checksum = "424f6e86263cd5294cbd7f1e95746b95aca0e0d66bff31e5a40d6baa87b4aa99"
dependencies = [ dependencies = [
"proc-macro-crate 1.3.1", "proc-macro-crate 1.1.3",
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3897,7 +3897,7 @@ version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799"
dependencies = [ dependencies = [
"proc-macro-crate 1.3.1", "proc-macro-crate 1.1.3",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@ -4550,12 +4550,12 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "1.3.1" version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a"
dependencies = [ dependencies = [
"once_cell", "thiserror",
"toml_edit 0.19.15", "toml 0.5.11",
] ]
[[package]] [[package]]
@ -7054,6 +7054,15 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "toml"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.7.8" version = "0.7.8"

View file

@ -1,7 +1,7 @@
use std::result::Result; use std::result::Result;
use std::sync::Arc; use std::sync::Arc;
use swap::{ use swap::{
api::{ cli::api::{
request::{ request::{
BalanceArgs, BuyXmrArgs, GetHistoryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs, BalanceArgs, BuyXmrArgs, GetHistoryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs,
WithdrawBtcArgs, WithdrawBtcArgs,
@ -43,8 +43,8 @@ macro_rules! tauri_command {
async fn $fn_name( async fn $fn_name(
context: tauri::State<'_, Arc<Context>>, context: tauri::State<'_, Arc<Context>>,
args: $request_name, args: $request_name,
) -> Result<<$request_name as swap::api::request::Request>::Response, String> { ) -> Result<<$request_name as swap::cli::api::request::Request>::Response, String> {
<$request_name as swap::api::request::Request>::request(args, context.inner().clone()) <$request_name as swap::cli::api::request::Request>::request(args, context.inner().clone())
.await .await
.to_string_result() .to_string_result()
} }

View file

@ -5,6 +5,7 @@ mod event_loop;
mod list_sellers; mod list_sellers;
pub mod tracing; pub mod tracing;
pub mod transport; pub mod transport;
pub mod api;
pub use behaviour::{Behaviour, OutEvent}; pub use behaviour::{Behaviour, OutEvent};
pub use cancel_and_refund::{cancel, cancel_and_refund, refund}; pub use cancel_and_refund::{cancel, cancel_and_refund, refund};

1188
swap/src/cli/api/request.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -27,11 +27,7 @@ impl TauriHandle {
} }
pub trait TauriEmitter { pub trait TauriEmitter {
fn emit_tauri_event<S: Serialize + Clone>( fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>;
&self,
event: &str,
payload: S,
) -> Result<()>;
fn emit_swap_progress_event(&self, swap_id: Uuid, event: TauriSwapProgressEvent) { fn emit_swap_progress_event(&self, swap_id: Uuid, event: TauriSwapProgressEvent) {
let _ = self.emit_tauri_event( let _ = self.emit_tauri_event(
@ -42,21 +38,13 @@ pub trait TauriEmitter {
} }
impl TauriEmitter for TauriHandle { impl TauriEmitter for TauriHandle {
fn emit_tauri_event<S: Serialize + Clone>( fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
&self,
event: &str,
payload: S,
) -> Result<()> {
self.emit_tauri_event(event, payload) self.emit_tauri_event(event, payload)
} }
} }
impl TauriEmitter for Option<TauriHandle> { impl TauriEmitter for Option<TauriHandle> {
fn emit_tauri_event<S: Serialize + Clone>( fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
&self,
event: &str,
payload: S,
) -> Result<()> {
match self { match self {
Some(tauri) => tauri.emit_tauri_event(event, payload), Some(tauri) => tauri.emit_tauri_event(event, payload),
None => Ok(()), None => Ok(()),

View file

@ -1,10 +1,9 @@
use crate::api::request::{ use crate::cli::api::request::{
buy_xmr, cancel_and_refund, export_bitcoin_wallet, get_balance, get_config, get_history, BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, ExportBitcoinWalletArgs, GetConfigArgs,
list_sellers, monero_recovery, resume_swap, start_daemon, withdraw_btc, BalanceArgs, GetHistoryArgs, ListSellersArgs, MoneroRecoveryArgs, Request, ResumeSwapArgs, StartDaemonArgs,
BuyXmrArgs, CancelAndRefundArgs, ExportBitcoinWalletArgs, GetConfigArgs, GetHistoryArgs, WithdrawBtcArgs,
ListSellersArgs, MoneroRecoveryArgs, Request, ResumeSwapArgs, StartDaemonArgs, WithdrawBtcArgs,
}; };
use crate::api::Context; use crate::cli::api::Context;
use crate::bitcoin::{bitcoin_address, Amount}; use crate::bitcoin::{bitcoin_address, Amount};
use crate::monero; use crate::monero;
use crate::monero::monero_address; use crate::monero::monero_address;
@ -544,15 +543,14 @@ struct Seller {
mod tests { mod tests {
use super::*; use super::*;
use crate::api::api_test::*; use crate::cli::api::api_test::*;
use crate::api::Config; use crate::cli::api::Config;
use crate::monero::monero_address::MoneroAddressNetworkMismatch; use crate::monero::monero_address::MoneroAddressNetworkMismatch;
const BINARY_NAME: &str = "swap"; const BINARY_NAME: &str = "swap";
const ARGS_DATA_DIR: &str = "/tmp/dir/"; const ARGS_DATA_DIR: &str = "/tmp/dir/";
#[tokio::test] #[tokio::test]
// this test is very long, however it just checks that various CLI arguments sets the // this test is very long, however it just checks that various CLI arguments sets the
// internal Context and Request properly. It is unlikely to fail and splitting it in various // internal Context and Request properly. It is unlikely to fail and splitting it in various
// tests would require to run the tests sequantially which is very slow (due to the context // tests would require to run the tests sequantially which is very slow (due to the context

View file

@ -73,6 +73,7 @@ pub enum Status {
Unreachable, Unreachable,
} }
#[allow(unused)]
#[derive(Debug)] #[derive(Debug)]
enum OutEvent { enum OutEvent {
Rendezvous(rendezvous::client::Event), Rendezvous(rendezvous::client::Event),

View file

@ -264,6 +264,7 @@ mod wire {
#[serde(transparent)] #[serde(transparent)]
pub struct TickerUpdate(Vec<TickerField>); pub struct TickerUpdate(Vec<TickerField>);
#[allow(unused)]
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum TickerField { pub enum TickerField {
@ -277,6 +278,7 @@ mod wire {
ask: Vec<RateElement>, ask: Vec<RateElement>,
} }
#[allow(unused)]
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum RateElement { pub enum RateElement {

View file

@ -16,7 +16,6 @@
missing_copy_implementations missing_copy_implementations
)] )]
pub mod api;
pub mod asb; pub mod asb;
pub mod bitcoin; pub mod bitcoin;
pub mod cli; pub mod cli;

View file

@ -13,5 +13,5 @@ pub mod tor_transport;
pub mod transfer_proof; pub mod transfer_proof;
pub mod transport; pub mod transport;
#[cfg(any(test, feature = "test"))] #[cfg(test)]
pub mod test; pub mod test;

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use uuid::Uuid; use uuid::Uuid;
use crate::api::tauri_bindings::TauriHandle; use crate::cli::api::tauri_bindings::TauriHandle;
use crate::protocol::Database; use crate::protocol::Database;
use crate::{bitcoin, cli, env, monero}; use crate::{bitcoin, cli, env, monero};

View file

@ -1,4 +1,4 @@
use crate::api::tauri_bindings::{TauriEmitter, TauriHandle, TauriSwapProgressEvent}; use crate::cli::api::tauri_bindings::{TauriEmitter, TauriHandle, TauriSwapProgressEvent};
use crate::bitcoin::{ExpiredTimelocks, TxCancel, TxRefund}; use crate::bitcoin::{ExpiredTimelocks, TxCancel, TxRefund};
use crate::cli::EventLoopHandle; use crate::cli::EventLoopHandle;
use crate::network::cooperative_xmr_redeem_after_punish::Response::{Fullfilled, Rejected}; use crate::network::cooperative_xmr_redeem_after_punish::Response::{Fullfilled, Rejected};

View file

@ -1,4 +1,4 @@
use crate::api::Context; use crate::cli::api::Context;
use std::net::SocketAddr; use std::net::SocketAddr;
use thiserror::Error; use thiserror::Error;
use tower_http::cors::CorsLayer; use tower_http::cors::CorsLayer;

View file

@ -1,10 +1,9 @@
use crate::api::request::{ use crate::cli::api::request::{
get_current_swap, get_history, get_raw_states, get_current_swap, get_history, get_raw_states, suspend_current_swap, BalanceArgs, BuyXmrArgs,
suspend_current_swap, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, MoneroRecoveryArgs, Request,
BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, ResumeSwapArgs, WithdrawBtcArgs,
MoneroRecoveryArgs, Request, ResumeSwapArgs, WithdrawBtcArgs,
}; };
use crate::api::Context; use crate::cli::api::Context;
use crate::bitcoin::bitcoin_address; use crate::bitcoin::bitcoin_address;
use crate::monero::monero_address; use crate::monero::monero_address;
use crate::{bitcoin, monero}; use crate::{bitcoin, monero};