diff --git a/swap/src/api.rs b/swap/src/api.rs index 68d16877..8f3db7bb 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -205,7 +205,7 @@ fn env_config_from(testnet: bool) -> EnvConfig { pub mod api_test { use super::*; use crate::api::request::{Method, Params, Request}; - + use libp2p::Multiaddr; use std::str::FromStr; use tokio::sync::broadcast; @@ -264,34 +264,50 @@ pub mod api_test { } }; - Request::new(tx.subscribe(), Method::BuyXmr, Params { - seller: Some(seller), - bitcoin_change_address: Some(bitcoin_change_address), - monero_receive_address: Some(monero_receive_address), - swap_id: Some(Uuid::new_v4()), - ..Default::default() - }) + Request::new( + tx.subscribe(), + Method::BuyXmr, + Params { + seller: Some(seller), + bitcoin_change_address: Some(bitcoin_change_address), + monero_receive_address: Some(monero_receive_address), + swap_id: Some(Uuid::new_v4()), + ..Default::default() + }, + ) } pub fn resume(tx: broadcast::Sender<()>) -> Request { - Request::new(tx.subscribe(), Method::Resume, Params { - swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), - ..Default::default() - }) + Request::new( + tx.subscribe(), + Method::Resume, + Params { + swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), + ..Default::default() + }, + ) } pub fn cancel(tx: broadcast::Sender<()>) -> Request { - Request::new(tx.subscribe(), Method::CancelAndRefund, Params { - swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), - ..Default::default() - }) + Request::new( + tx.subscribe(), + Method::CancelAndRefund, + Params { + swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), + ..Default::default() + }, + ) } pub fn refund(tx: broadcast::Sender<()>) -> Request { - Request::new(tx.subscribe(), Method::CancelAndRefund, Params { - swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), - ..Default::default() - }) + Request::new( + tx.subscribe(), + Method::CancelAndRefund, + Params { + swap_id: Some(Uuid::from_str(SWAP_ID).unwrap()), + ..Default::default() + }, + ) } } } diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index 6483175d..e4360c7a 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -18,8 +18,8 @@ use std::future::Future; use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; -use tokio::sync::broadcast; use tokio::sync::broadcast::Receiver; +use tokio::sync::Mutex; use tracing::{debug_span, Instrument}; use uuid::Uuid; @@ -63,7 +63,7 @@ impl Shutdown { #[derive(Debug)] pub struct Shutdown { shutdown: bool, - notify: broadcast::Receiver<()>, + notify: Receiver<()>, } impl PartialEq for Shutdown { @@ -332,7 +332,10 @@ impl Request { } Method::StartDaemon => { // Default to 127.0.0.1:1234 - let server_address = self.params.server_address.unwrap_or("127.0.0.1:1234".parse().unwrap()); + let server_address = self + .params + .server_address + .unwrap_or("127.0.0.1:1234".parse().unwrap()); let (_, server_handle) = rpc::run_server(server_address, Arc::clone(&context)).await?; diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index a68e8b04..e3c664b1 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -78,12 +78,16 @@ where let bitcoin_change_address = bitcoin_address::validate(bitcoin_change_address, is_testnet)?; - let request = Request::new(rx.subscribe(), Method::BuyXmr, Params { - bitcoin_change_address: Some(bitcoin_change_address), - monero_receive_address: Some(monero_receive_address), - seller: Some(seller), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::BuyXmr, + Params { + bitcoin_change_address: Some(bitcoin_change_address), + monero_receive_address: Some(monero_receive_address), + seller: Some(seller), + ..Default::default() + }, + ); let context = Context::build( Some(bitcoin), @@ -136,10 +140,14 @@ where monero, tor, } => { - let request = Request::new(rx.subscribe(), Method::StartDaemon, Params { - server_address, - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::StartDaemon, + Params { + server_address, + ..Default::default() + }, + ); let context = Context::build( Some(bitcoin), @@ -162,11 +170,15 @@ where } => { let address = bitcoin_address::validate(address, is_testnet)?; - let request = Request::new(rx.subscribe(), Method::WithdrawBtc, Params { - amount, - address: Some(address), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::WithdrawBtc, + Params { + amount, + address: Some(address), + ..Default::default() + }, + ); let context = Context::build( Some(bitcoin), @@ -188,10 +200,14 @@ where monero, tor, } => { - let request = Request::new(rx.subscribe(), Method::Resume, Params { - swap_id: Some(swap_id), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::Resume, + Params { + swap_id: Some(swap_id), + ..Default::default() + }, + ); let context = Context::build( Some(bitcoin), @@ -212,10 +228,14 @@ where bitcoin, tor, } => { - let request = Request::new(rx.subscribe(), Method::CancelAndRefund, Params { - swap_id: Some(swap_id), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::CancelAndRefund, + Params { + swap_id: Some(swap_id), + ..Default::default() + }, + ); let context = Context::build( Some(bitcoin), @@ -235,10 +255,14 @@ where rendezvous_point, tor, } => { - let request = Request::new(rx.subscribe(), Method::ListSellers, Params { - rendezvous_point: Some(rendezvous_point), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::ListSellers, + Params { + rendezvous_point: Some(rendezvous_point), + ..Default::default() + }, + ); let context = Context::build( None, @@ -279,10 +303,14 @@ where CliCommand::MoneroRecovery { swap_id: SwapId { swap_id }, } => { - let request = Request::new(rx.subscribe(), Method::MoneroRecovery, Params { - swap_id: Some(swap_id), - ..Default::default() - }); + let request = Request::new( + rx.subscribe(), + Method::MoneroRecovery, + Params { + swap_id: Some(swap_id), + ..Default::default() + }, + ); let context = Context::build(None, None, None, data, is_testnet, debug, json, None, rx).await?; @@ -540,7 +568,6 @@ struct Seller { #[cfg(test)] mod tests { use super::*; - use crate::api::api_test::*; use crate::api::Config; diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 1f98b50e..5d7d4285 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -13,21 +13,25 @@ use uuid::Uuid; pub fn register_modules(context: Arc) -> RpcModule> { let mut module = RpcModule::new(context); + module .register_async_method("get_bitcoin_balance", |_, context| async move { get_bitcoin_balance(&context).await }) .expect("Could not register RPC method get_bitcoin_balance"); + module .register_async_method("get_history", |_, context| async move { get_history(&context).await }) .expect("Could not register RPC method get_history"); + module .register_async_method("get_raw_history", |_, context| async move { get_raw_history(&context).await }) .expect("Could not register RPC method get_history"); + module .register_async_method("get_seller", |params, context| async move { let params: HashMap = params.parse()?; @@ -39,6 +43,7 @@ pub fn register_modules(context: Arc) -> RpcModule> { get_seller(*swap_id, &context).await }) .expect("Could not register RPC method get_seller"); + module .register_async_method("get_swap_start_date", |params, context| async move { let params: HashMap = params.parse()?; @@ -50,6 +55,7 @@ pub fn register_modules(context: Arc) -> RpcModule> { get_swap_start_date(*swap_id, &context).await }) .expect("Could not register RPC method get_swap_start_date"); + module .register_async_method("resume_swap", |params, context| async move { let params: HashMap = params.parse()?; diff --git a/swap/tests/rpc.rs b/swap/tests/rpc.rs index 2700cef4..6f516624 100644 --- a/swap/tests/rpc.rs +++ b/swap/tests/rpc.rs @@ -1,7 +1,7 @@ -use anyhow::{Result}; +use anyhow::Result; +use jsonrpsee::rpc_params; use jsonrpsee::ws_client::WsClientBuilder; -use jsonrpsee::{rpc_params}; use jsonrpsee_core::client::ClientT; use jsonrpsee_core::params::ObjectParams; @@ -11,10 +11,9 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; use swap::api::request::{Method, Params, Request, Shutdown}; -use swap::api::{Context}; +use swap::api::Context; use swap::cli::command::{Bitcoin, Monero}; - use tokio::sync::broadcast; use uuid::Uuid;