mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-07 14:02:32 -04:00
Pass down tracing span to API calls and allow for log_reference_id to be set
This commit is contained in:
parent
bbcfffab6d
commit
ed7792f7e7
4 changed files with 287 additions and 256 deletions
|
@ -19,12 +19,13 @@ use std::future::Future;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tracing::{debug_span, Instrument};
|
use tracing::{debug_span, Instrument, Span, field};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub cmd: Method,
|
pub cmd: Method,
|
||||||
|
pub log_reference: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
@ -66,21 +67,83 @@ pub enum Method {
|
||||||
SuspendCurrentSwap,
|
SuspendCurrentSwap,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Method {
|
||||||
|
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
|
||||||
|
let span = match self {
|
||||||
|
Method::Balance => debug_span!("method", name = "Balance", log_reference_id=field::Empty),
|
||||||
|
Method::BuyXmr { swap_id, .. } => {
|
||||||
|
debug_span!("method", name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::CancelAndRefund { swap_id } => {
|
||||||
|
debug_span!("method", name="CancelAndRefund", swap_id=%swap_id, log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::Resume { swap_id } => {
|
||||||
|
debug_span!("method", name="Resume", swap_id=%swap_id, log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::Config => debug_span!("method", name = "Config", log_reference_id=field::Empty),
|
||||||
|
Method::ExportBitcoinWallet => {
|
||||||
|
debug_span!("method", name = "ExportBitcoinWallet", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::GetCurrentSwap => {
|
||||||
|
debug_span!("method", name = "GetCurrentSwap", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::GetSwapInfo { .. } => {
|
||||||
|
debug_span!("method", name = "GetSwapInfo", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::History => debug_span!("method", name = "History", log_reference_id=field::Empty),
|
||||||
|
Method::ListSellers { .. } => {
|
||||||
|
debug_span!("method", name = "ListSellers", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::MoneroRecovery { .. } => {
|
||||||
|
debug_span!("method", name = "MoneroRecovery", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::RawHistory => debug_span!("method", name = "RawHistory", log_reference_id=field::Empty),
|
||||||
|
Method::StartDaemon { .. } => {
|
||||||
|
debug_span!("method", name = "StartDaemon", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::SuspendCurrentSwap => {
|
||||||
|
debug_span!("method", name = "SuspendCurrentSwap", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
Method::WithdrawBtc { .. } => {
|
||||||
|
debug_span!("method", name = "WithdrawBtc", log_reference_id=field::Empty)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(log_reference_id) = log_reference_id {
|
||||||
|
span.record("log_reference_id", &log_reference_id.as_str());
|
||||||
|
}
|
||||||
|
span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub fn new(cmd: Method) -> Request {
|
pub fn new(cmd: Method) -> Request {
|
||||||
Request { cmd }
|
Request { cmd, log_reference: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_cmd(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
pub fn with_id(cmd: Method, id: Option<String>) -> Request {
|
||||||
|
Request { cmd, log_reference: id }
|
||||||
|
}
|
||||||
|
|
||||||
|
// We pass the outer tracing span down to this function such that it can be passed down to other spawned tokio tasks
|
||||||
|
// This ensures that tasks like the event_loop are all part of the same tracing span
|
||||||
|
async fn handle_cmd(
|
||||||
|
self,
|
||||||
|
context: Arc<Context>,
|
||||||
|
) -> Result<serde_json::Value> {
|
||||||
match self.cmd {
|
match self.cmd {
|
||||||
Method::SuspendCurrentSwap => {
|
Method::SuspendCurrentSwap => {
|
||||||
context.swap_lock.send_suspend_signal().await?;
|
|
||||||
let swap_id = context.swap_lock.get_current_swap_id().await;
|
let swap_id = context.swap_lock.get_current_swap_id().await;
|
||||||
|
|
||||||
|
if swap_id.is_some() {
|
||||||
|
context.swap_lock.send_suspend_signal().await?;
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
"swapId": swap_id,
|
"success": true,
|
||||||
"success": true
|
"swapId": swap_id.unwrap()
|
||||||
}))
|
}))
|
||||||
|
} else {
|
||||||
|
bail!("No swap is currently running")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Method::GetSwapInfo { swap_id } => {
|
Method::GetSwapInfo { swap_id } => {
|
||||||
let bitcoin_wallet = context
|
let bitcoin_wallet = context
|
||||||
|
@ -106,7 +169,7 @@ impl Request {
|
||||||
|
|
||||||
let start_date = context.db.get_swap_start_date(swap_id).await?;
|
let start_date = context.db.get_swap_start_date(swap_id).await?;
|
||||||
|
|
||||||
let state_name = format!("{:?}", swap_state);
|
let state_name = format!("{}", swap_state);
|
||||||
|
|
||||||
// variable timelock: Option<Result<ExpiredTimelocks>>
|
// variable timelock: Option<Result<ExpiredTimelocks>>
|
||||||
let timelock = match swap_state {
|
let timelock = match swap_state {
|
||||||
|
@ -132,6 +195,7 @@ impl Request {
|
||||||
|
|
||||||
// Add txids
|
// Add txids
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
|
"swapId": swap_id,
|
||||||
"seller": {
|
"seller": {
|
||||||
"peerId": peerId.to_string(),
|
"peerId": peerId.to_string(),
|
||||||
"addresses": addresses
|
"addresses": addresses
|
||||||
|
@ -140,6 +204,7 @@ impl Request {
|
||||||
"startDate": start_date,
|
"startDate": start_date,
|
||||||
// If none return null, if some unwrap and return as json
|
// If none return null, if some unwrap and return as json
|
||||||
"timelock": timelock.map(|tl| tl.map(|tl| json!(tl)).unwrap_or(json!(null))).unwrap_or(json!(null)),
|
"timelock": timelock.map(|tl| tl.map(|tl| json!(tl)).unwrap_or(json!(null))).unwrap_or(json!(null)),
|
||||||
|
// Use display to get the string representation of the state
|
||||||
"stateName": state_name,
|
"stateName": state_name,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -195,7 +260,7 @@ impl Request {
|
||||||
|
|
||||||
let (event_loop, mut event_loop_handle) =
|
let (event_loop, mut event_loop_handle) =
|
||||||
EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
||||||
let event_loop = tokio::spawn(event_loop.run());
|
let event_loop = tokio::spawn(event_loop.run().instrument(Span::current()));
|
||||||
|
|
||||||
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
|
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
|
||||||
let estimate_fee = |amount| bitcoin_wallet.estimate_fee(TxLock::weight(), amount);
|
let estimate_fee = |amount| bitcoin_wallet.estimate_fee(TxLock::weight(), amount);
|
||||||
|
@ -278,12 +343,138 @@ impl Request {
|
||||||
.release_swap_lock()
|
.release_swap_lock()
|
||||||
.await
|
.await
|
||||||
.expect("Could not release swap lock");
|
.expect("Could not release swap lock");
|
||||||
});
|
}.instrument(Span::current()));
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
"swapId": swap_id.to_string(),
|
"swapId": swap_id.to_string(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Method::Resume { swap_id } => {
|
||||||
|
context.swap_lock.acquire_swap_lock(swap_id).await?;
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
tokio::select! {
|
||||||
|
_ = async {
|
||||||
|
let seller_peer_id = context.db.get_peer_id(swap_id).await?;
|
||||||
|
let seller_addresses = context.db.get_addresses(seller_peer_id).await?;
|
||||||
|
|
||||||
|
let seed = context
|
||||||
|
.config
|
||||||
|
.seed
|
||||||
|
.as_ref()
|
||||||
|
.context("Could not get seed")?
|
||||||
|
.derive_libp2p_identity();
|
||||||
|
|
||||||
|
let behaviour = cli::Behaviour::new(
|
||||||
|
seller_peer_id,
|
||||||
|
context.config.env_config,
|
||||||
|
Arc::clone(
|
||||||
|
context
|
||||||
|
.bitcoin_wallet
|
||||||
|
.as_ref()
|
||||||
|
.context("Could not get Bitcoin wallet")?,
|
||||||
|
),
|
||||||
|
(seed.clone(), context.config.namespace),
|
||||||
|
);
|
||||||
|
let mut swarm = swarm::cli(
|
||||||
|
seed.clone(),
|
||||||
|
context
|
||||||
|
.config
|
||||||
|
.tor_socks5_port
|
||||||
|
.context("Could not get Tor SOCKS5 port")?,
|
||||||
|
behaviour,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let our_peer_id = swarm.local_peer_id();
|
||||||
|
|
||||||
|
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
||||||
|
|
||||||
|
for seller_address in seller_addresses {
|
||||||
|
swarm
|
||||||
|
.behaviour_mut()
|
||||||
|
.add_address(seller_peer_id, seller_address);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (event_loop, event_loop_handle) =
|
||||||
|
EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
||||||
|
let handle = tokio::spawn(event_loop.run().instrument(Span::current()));
|
||||||
|
|
||||||
|
let monero_receive_address = context.db.get_monero_address(swap_id).await?;
|
||||||
|
let swap = Swap::from_db(
|
||||||
|
Arc::clone(&context.db),
|
||||||
|
swap_id,
|
||||||
|
Arc::clone(
|
||||||
|
context
|
||||||
|
.bitcoin_wallet
|
||||||
|
.as_ref()
|
||||||
|
.context("Could not get Bitcoin wallet")?,
|
||||||
|
),
|
||||||
|
Arc::clone(
|
||||||
|
context
|
||||||
|
.monero_wallet
|
||||||
|
.as_ref()
|
||||||
|
.context("Could not get Monero wallet")?,
|
||||||
|
),
|
||||||
|
context.config.env_config,
|
||||||
|
event_loop_handle,
|
||||||
|
monero_receive_address,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
event_loop_result = handle => {
|
||||||
|
event_loop_result?;
|
||||||
|
},
|
||||||
|
swap_result = bob::run(swap) => {
|
||||||
|
swap_result?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok::<(), anyhow::Error>(())
|
||||||
|
} => {
|
||||||
|
()
|
||||||
|
},
|
||||||
|
_ = context.swap_lock.listen_for_swap_force_suspension() => {
|
||||||
|
tracing::info!("Shutdown signal received, exiting");
|
||||||
|
()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context
|
||||||
|
.swap_lock
|
||||||
|
.release_swap_lock()
|
||||||
|
.await
|
||||||
|
.expect("Could not release swap lock");
|
||||||
|
}.instrument(Span::current()));
|
||||||
|
Ok(json!({
|
||||||
|
"result": "ok",
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
Method::CancelAndRefund { swap_id } => {
|
||||||
|
let bitcoin_wallet = context
|
||||||
|
.bitcoin_wallet
|
||||||
|
.as_ref()
|
||||||
|
.context("Could not get Bitcoin wallet")?;
|
||||||
|
|
||||||
|
context.swap_lock.acquire_swap_lock(swap_id).await?;
|
||||||
|
|
||||||
|
let state = cli::cancel_and_refund(
|
||||||
|
swap_id,
|
||||||
|
Arc::clone(bitcoin_wallet),
|
||||||
|
Arc::clone(&context.db),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
context
|
||||||
|
.swap_lock
|
||||||
|
.release_swap_lock()
|
||||||
|
.await
|
||||||
|
.expect("Could not release swap lock");
|
||||||
|
|
||||||
|
state.map(|state| {
|
||||||
|
json!({
|
||||||
|
"result": state,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
Method::History => {
|
Method::History => {
|
||||||
let swaps = context.db.all().await?;
|
let swaps = context.db.all().await?;
|
||||||
let mut vec: Vec<(Uuid, String)> = Vec::new();
|
let mut vec: Vec<(Uuid, String)> = Vec::new();
|
||||||
|
@ -377,132 +568,6 @@ impl Request {
|
||||||
"balance": bitcoin_balance.to_sat()
|
"balance": bitcoin_balance.to_sat()
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
Method::Resume { swap_id } => {
|
|
||||||
context.swap_lock.acquire_swap_lock(swap_id).await?;
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
|
||||||
tokio::select! {
|
|
||||||
_ = async {
|
|
||||||
let seller_peer_id = context.db.get_peer_id(swap_id).await?;
|
|
||||||
let seller_addresses = context.db.get_addresses(seller_peer_id).await?;
|
|
||||||
|
|
||||||
let seed = context
|
|
||||||
.config
|
|
||||||
.seed
|
|
||||||
.as_ref()
|
|
||||||
.context("Could not get seed")?
|
|
||||||
.derive_libp2p_identity();
|
|
||||||
|
|
||||||
let behaviour = cli::Behaviour::new(
|
|
||||||
seller_peer_id,
|
|
||||||
context.config.env_config,
|
|
||||||
Arc::clone(
|
|
||||||
context
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.context("Could not get Bitcoin wallet")?,
|
|
||||||
),
|
|
||||||
(seed.clone(), context.config.namespace),
|
|
||||||
);
|
|
||||||
let mut swarm = swarm::cli(
|
|
||||||
seed.clone(),
|
|
||||||
context
|
|
||||||
.config
|
|
||||||
.tor_socks5_port
|
|
||||||
.context("Could not get Tor SOCKS5 port")?,
|
|
||||||
behaviour,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let our_peer_id = swarm.local_peer_id();
|
|
||||||
|
|
||||||
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
|
||||||
|
|
||||||
for seller_address in seller_addresses {
|
|
||||||
swarm
|
|
||||||
.behaviour_mut()
|
|
||||||
.add_address(seller_peer_id, seller_address);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) =
|
|
||||||
EventLoop::new(swap_id, swarm, seller_peer_id)?;
|
|
||||||
let handle = tokio::spawn(event_loop.run());
|
|
||||||
|
|
||||||
let monero_receive_address = context.db.get_monero_address(swap_id).await?;
|
|
||||||
let swap = Swap::from_db(
|
|
||||||
Arc::clone(&context.db),
|
|
||||||
swap_id,
|
|
||||||
Arc::clone(
|
|
||||||
context
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.context("Could not get Bitcoin wallet")?,
|
|
||||||
),
|
|
||||||
Arc::clone(
|
|
||||||
context
|
|
||||||
.monero_wallet
|
|
||||||
.as_ref()
|
|
||||||
.context("Could not get Monero wallet")?,
|
|
||||||
),
|
|
||||||
context.config.env_config,
|
|
||||||
event_loop_handle,
|
|
||||||
monero_receive_address,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
tokio::select! {
|
|
||||||
event_loop_result = handle => {
|
|
||||||
event_loop_result?;
|
|
||||||
},
|
|
||||||
swap_result = bob::run(swap) => {
|
|
||||||
swap_result?;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok::<(), anyhow::Error>(())
|
|
||||||
} => {
|
|
||||||
()
|
|
||||||
},
|
|
||||||
_ = context.swap_lock.listen_for_swap_force_suspension() => {
|
|
||||||
tracing::info!("Shutdown signal received, exiting");
|
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
context
|
|
||||||
.swap_lock
|
|
||||||
.release_swap_lock()
|
|
||||||
.await
|
|
||||||
.expect("Could not release swap lock");
|
|
||||||
});
|
|
||||||
Ok(json!({
|
|
||||||
"result": "ok",
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
Method::CancelAndRefund { swap_id } => {
|
|
||||||
let bitcoin_wallet = context
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.context("Could not get Bitcoin wallet")?;
|
|
||||||
|
|
||||||
context.swap_lock.acquire_swap_lock(swap_id).await?;
|
|
||||||
|
|
||||||
let state = cli::cancel_and_refund(
|
|
||||||
swap_id,
|
|
||||||
Arc::clone(bitcoin_wallet),
|
|
||||||
Arc::clone(&context.db),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
context
|
|
||||||
.swap_lock
|
|
||||||
.release_swap_lock()
|
|
||||||
.await
|
|
||||||
.expect("Could not release swap lock");
|
|
||||||
|
|
||||||
state.map(|state| {
|
|
||||||
json!({
|
|
||||||
"result": state,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Method::ListSellers { rendezvous_point } => {
|
Method::ListSellers { rendezvous_point } => {
|
||||||
let rendezvous_node_peer_id = rendezvous_point
|
let rendezvous_node_peer_id = rendezvous_point
|
||||||
.extract_peer_id()
|
.extract_peer_id()
|
||||||
|
@ -608,13 +673,11 @@ impl Request {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
|
||||||
// If the swap ID is set, we add it to the span
|
let method_span = self.cmd.get_tracing_span(self.log_reference.clone()).clone();
|
||||||
let call_span = debug_span!(
|
|
||||||
"cmd",
|
|
||||||
method = ?self.cmd,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.handle_cmd(context).instrument(call_span).await
|
self.handle_cmd(context)
|
||||||
|
.instrument(method_span)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -627,6 +690,7 @@ fn qr_code(value: &impl ToString) -> Result<String> {
|
||||||
.build();
|
.build();
|
||||||
Ok(qr_code)
|
Ok(qr_code)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
|
pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
|
||||||
json: bool,
|
json: bool,
|
||||||
bid_quote: impl Future<Output = Result<BidQuote>>,
|
bid_quote: impl Future<Output = Result<BidQuote>>,
|
||||||
|
|
|
@ -24,6 +24,7 @@ use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::sync::{watch, Mutex};
|
use tokio::sync::{watch, Mutex};
|
||||||
|
use tracing::{Instrument, Span};
|
||||||
|
|
||||||
const SLED_TREE_NAME: &str = "default_tree";
|
const SLED_TREE_NAME: &str = "default_tree";
|
||||||
|
|
||||||
|
@ -192,7 +193,7 @@ impl Wallet {
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
});
|
}.instrument(Span::current()));
|
||||||
|
|
||||||
Subscription {
|
Subscription {
|
||||||
receiver,
|
receiver,
|
||||||
|
|
|
@ -22,7 +22,6 @@ pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>) -> Result<()> {
|
||||||
fmt::layer()
|
fmt::layer()
|
||||||
.with_ansi(false)
|
.with_ansi(false)
|
||||||
.with_target(false)
|
.with_target(false)
|
||||||
.with_span_events(fmt::format::FmtSpan::FULL)
|
|
||||||
.json()
|
.json()
|
||||||
.with_writer(appender),
|
.with_writer(appender),
|
||||||
);
|
);
|
||||||
|
|
|
@ -9,73 +9,93 @@ use libp2p::core::Multiaddr;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use jsonrpsee::types::Params;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
||||||
let mut module = RpcModule::new(context);
|
let mut module = RpcModule::new(context);
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("suspend_current_swap", |_, context| async move {
|
.register_async_method("suspend_current_swap", |params, context| async move {
|
||||||
execute_request(Method::SuspendCurrentSwap, &context).await
|
execute_request(params, Method::SuspendCurrentSwap, &context).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("get_swap_info", |params, context| async move {
|
.register_async_method("get_swap_info", |params_raw, context| async move {
|
||||||
let params: HashMap<String, Uuid> = params.parse()?;
|
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||||
|
|
||||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
get_swap_info(*swap_id, &context).await
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::GetSwapInfo {
|
||||||
|
swap_id: *swap_id,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("get_bitcoin_balance", |_, context| async move {
|
.register_async_method("get_bitcoin_balance", |params, context| async move {
|
||||||
get_bitcoin_balance(&context).await
|
execute_request(params, Method::Balance, &context).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("get_history", |_, context| async move {
|
.register_async_method("get_history", |params, context| async move {
|
||||||
get_history(&context).await
|
execute_request(params, Method::History, &context).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("get_raw_history", |_, context| async move {
|
.register_async_method("get_raw_history", |params, context| async move {
|
||||||
get_raw_history(&context).await
|
execute_request(params, Method::RawHistory, &context).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("resume_swap", |params, context| async move {
|
.register_async_method("resume_swap", |params_raw, context| async move {
|
||||||
let params: HashMap<String, Uuid> = params.parse()?;
|
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||||
|
|
||||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
resume_swap(*swap_id, &context).await
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::Resume {
|
||||||
|
swap_id: *swap_id,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("cancel_refund_swap", |params, context| async move {
|
.register_async_method("cancel_refund_swap", |params_raw, context| async move {
|
||||||
let params: HashMap<String, Uuid> = params.parse()?;
|
let params: HashMap<String, Uuid> = params_raw.parse()?;
|
||||||
|
|
||||||
let swap_id = params.get("swap_id").ok_or_else(|| {
|
let swap_id = params.get("swap_id").ok_or_else(|| {
|
||||||
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
cancel_and_refund_swap(*swap_id, &context).await
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::CancelAndRefund {
|
||||||
|
swap_id: *swap_id,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("withdraw_btc", |params, context| async move {
|
.register_async_method("withdraw_btc", |params_raw, context| async move {
|
||||||
let params: HashMap<String, String> = params.parse()?;
|
let params: HashMap<String, String> = params_raw.parse()?;
|
||||||
|
|
||||||
let amount = if let Some(amount_str) = params.get("amount") {
|
let amount = if let Some(amount_str) = params.get("amount") {
|
||||||
Some(
|
Some(
|
||||||
|
@ -96,12 +116,19 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
||||||
let withdraw_address =
|
let withdraw_address =
|
||||||
bitcoin_address::validate(withdraw_address, context.config.is_testnet)?;
|
bitcoin_address::validate(withdraw_address, context.config.is_testnet)?;
|
||||||
|
|
||||||
withdraw_btc(withdraw_address, amount, &context).await
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::WithdrawBtc {
|
||||||
|
amount,
|
||||||
|
address: withdraw_address,
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
).await
|
||||||
})
|
})
|
||||||
.expect("Could not register RPC method withdraw_btc");
|
.expect("Could not register RPC method withdraw_btc");
|
||||||
module
|
module
|
||||||
.register_async_method("buy_xmr", |params, context| async move {
|
.register_async_method("buy_xmr", |params_raw, context| async move {
|
||||||
let params: HashMap<String, String> = params.parse()?;
|
let params: HashMap<String, String> = params_raw.parse()?;
|
||||||
|
|
||||||
let bitcoin_change_address = bitcoin::Address::from_str(
|
let bitcoin_change_address = bitcoin::Address::from_str(
|
||||||
params.get("bitcoin_change_address").ok_or_else(|| {
|
params.get("bitcoin_change_address").ok_or_else(|| {
|
||||||
|
@ -132,29 +159,38 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
||||||
})?)
|
})?)
|
||||||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||||
|
|
||||||
buy_xmr(
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::BuyXmr {
|
||||||
bitcoin_change_address,
|
bitcoin_change_address,
|
||||||
monero_receive_address,
|
monero_receive_address,
|
||||||
seller,
|
seller,
|
||||||
|
swap_id: Uuid::new_v4(),
|
||||||
|
},
|
||||||
&context,
|
&context,
|
||||||
)
|
).await
|
||||||
.await
|
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
module
|
module
|
||||||
.register_async_method("list_sellers", |params, context| async move {
|
.register_async_method("list_sellers", |params_raw, context| async move {
|
||||||
let params: HashMap<String, Multiaddr> = params.parse()?;
|
let params: HashMap<String, Multiaddr> = params_raw.parse()?;
|
||||||
let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| {
|
let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| {
|
||||||
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
|
jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
list_sellers(rendezvous_point.clone(), &context).await
|
execute_request(
|
||||||
|
params_raw,
|
||||||
|
Method::ListSellers {
|
||||||
|
rendezvous_point: rendezvous_point.clone(),
|
||||||
|
},
|
||||||
|
&context,
|
||||||
|
).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_async_method("get_current_swap", |_, context| async move {
|
.register_async_method("get_current_swap", |params, context| async move {
|
||||||
get_current_swap(&context).await
|
execute_request(params, Method::GetCurrentSwap, &context).await
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -162,88 +198,19 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute_request(
|
async fn execute_request(
|
||||||
|
params: Params<'static>,
|
||||||
cmd: Method,
|
cmd: Method,
|
||||||
context: &Arc<Context>,
|
context: &Arc<Context>,
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
||||||
let request = Request::new(cmd);
|
let params_parsed = params.parse::<HashMap<String, String>>()
|
||||||
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
|
||||||
|
|
||||||
|
let reference_id = params_parsed
|
||||||
|
.get("log_reference_id");
|
||||||
|
|
||||||
|
let request = Request::with_id(cmd, reference_id.map(|s| s.clone()));
|
||||||
request
|
request
|
||||||
.call(Arc::clone(context))
|
.call(Arc::clone(context))
|
||||||
.await
|
.await
|
||||||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
|
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_current_swap(
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::GetCurrentSwap, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_bitcoin_balance(
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::Balance, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_history(context: &Arc<Context>) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::History, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_raw_history(
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::RawHistory, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_swap_info(
|
|
||||||
swap_id: Uuid,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::GetSwapInfo { swap_id }, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn resume_swap(
|
|
||||||
swap_id: Uuid,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::Resume { swap_id }, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn cancel_and_refund_swap(
|
|
||||||
swap_id: Uuid,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::CancelAndRefund { swap_id }, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn withdraw_btc(
|
|
||||||
address: bitcoin::Address,
|
|
||||||
amount: Option<bitcoin::Amount>,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::WithdrawBtc { amount, address }, context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn buy_xmr(
|
|
||||||
bitcoin_change_address: bitcoin::Address,
|
|
||||||
monero_receive_address: monero::Address,
|
|
||||||
seller: Multiaddr,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(
|
|
||||||
Method::BuyXmr {
|
|
||||||
seller,
|
|
||||||
swap_id: Uuid::new_v4(),
|
|
||||||
bitcoin_change_address,
|
|
||||||
monero_receive_address,
|
|
||||||
},
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn list_sellers(
|
|
||||||
rendezvous_point: Multiaddr,
|
|
||||||
context: &Arc<Context>,
|
|
||||||
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
|
|
||||||
execute_request(Method::ListSellers { rendezvous_point }, context).await
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue