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

View File

@ -1,7 +1,7 @@
use std::result::Result;
use std::sync::Arc;
use swap::{
api::{
cli::api::{
request::{
BalanceArgs, BuyXmrArgs, GetHistoryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs,
WithdrawBtcArgs,
@ -25,7 +25,7 @@ impl<T, E: ToString> ToStringResult<T> for Result<T, E> {
/// This macro is used to create boilerplate functions as tauri commands
/// that simply delegate handling to the respective request type.
///
///
/// # Example
/// ```ignored
/// tauri_command!(get_balance, BalanceArgs);
@ -43,8 +43,8 @@ macro_rules! tauri_command {
async fn $fn_name(
context: tauri::State<'_, Arc<Context>>,
args: $request_name,
) -> Result<<$request_name as swap::api::request::Request>::Response, String> {
<$request_name as swap::api::request::Request>::request(args, context.inner().clone())
) -> Result<<$request_name as swap::cli::api::request::Request>::Response, String> {
<$request_name as swap::cli::api::request::Request>::request(args, context.inner().clone())
.await
.to_string_result()
}

View File

@ -5,6 +5,7 @@ mod event_loop;
mod list_sellers;
pub mod tracing;
pub mod transport;
pub mod api;
pub use behaviour::{Behaviour, OutEvent};
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 {
fn emit_tauri_event<S: Serialize + Clone>(
&self,
event: &str,
payload: S,
) -> Result<()>;
fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>;
fn emit_swap_progress_event(&self, swap_id: Uuid, event: TauriSwapProgressEvent) {
let _ = self.emit_tauri_event(
@ -42,21 +38,13 @@ pub trait TauriEmitter {
}
impl TauriEmitter for TauriHandle {
fn emit_tauri_event<S: Serialize + Clone>(
&self,
event: &str,
payload: S,
) -> Result<()> {
fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
self.emit_tauri_event(event, payload)
}
}
impl TauriEmitter for Option<TauriHandle> {
fn emit_tauri_event<S: Serialize + Clone>(
&self,
event: &str,
payload: S,
) -> Result<()> {
fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
match self {
Some(tauri) => tauri.emit_tauri_event(event, payload),
None => Ok(()),

View File

@ -1,10 +1,9 @@
use crate::api::request::{
buy_xmr, cancel_and_refund, export_bitcoin_wallet, get_balance, get_config, get_history,
list_sellers, monero_recovery, resume_swap, start_daemon, withdraw_btc, BalanceArgs,
BuyXmrArgs, CancelAndRefundArgs, ExportBitcoinWalletArgs, GetConfigArgs, GetHistoryArgs,
ListSellersArgs, MoneroRecoveryArgs, Request, ResumeSwapArgs, StartDaemonArgs, WithdrawBtcArgs,
use crate::cli::api::request::{
BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, ExportBitcoinWalletArgs, GetConfigArgs,
GetHistoryArgs, ListSellersArgs, MoneroRecoveryArgs, Request, ResumeSwapArgs, StartDaemonArgs,
WithdrawBtcArgs,
};
use crate::api::Context;
use crate::cli::api::Context;
use crate::bitcoin::{bitcoin_address, Amount};
use crate::monero;
use crate::monero::monero_address;
@ -544,15 +543,14 @@ struct Seller {
mod tests {
use super::*;
use crate::api::api_test::*;
use crate::api::Config;
use crate::cli::api::api_test::*;
use crate::cli::api::Config;
use crate::monero::monero_address::MoneroAddressNetworkMismatch;
const BINARY_NAME: &str = "swap";
const ARGS_DATA_DIR: &str = "/tmp/dir/";
#[tokio::test]
// 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
// 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,
}
#[allow(unused)]
#[derive(Debug)]
enum OutEvent {
Rendezvous(rendezvous::client::Event),

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result;
use uuid::Uuid;
use crate::api::tauri_bindings::TauriHandle;
use crate::cli::api::tauri_bindings::TauriHandle;
use crate::protocol::Database;
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::cli::EventLoopHandle;
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 thiserror::Error;
use tower_http::cors::CorsLayer;

View File

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