Sync bitcoin wallet before initial max_giveable call

This commit is contained in:
binarybaron 2023-08-24 14:26:28 +02:00 committed by binarybaron
parent 99dfa895bb
commit cff51b341d
2 changed files with 89 additions and 45 deletions

View file

@ -19,7 +19,7 @@ use std::future::Future;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug_span, Instrument, Span, field};
use tracing::{debug_span, field, Instrument, Span};
use uuid::Uuid;
//TODO: Request and Method can be combined into a single enum
@ -71,7 +71,9 @@ pub enum Method {
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::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)
}
@ -81,32 +83,72 @@ impl Method {
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::Config => {
debug_span!("method", name = "Config", log_reference_id = field::Empty)
}
Method::ExportBitcoinWallet => {
debug_span!("method", name = "ExportBitcoinWallet", log_reference_id=field::Empty)
debug_span!(
"method",
name = "ExportBitcoinWallet",
log_reference_id = field::Empty
)
}
Method::GetCurrentSwap => {
debug_span!("method", name = "GetCurrentSwap", log_reference_id=field::Empty)
debug_span!(
"method",
name = "GetCurrentSwap",
log_reference_id = field::Empty
)
}
Method::GetSwapInfo { .. } => {
debug_span!("method", name = "GetSwapInfo", log_reference_id=field::Empty)
debug_span!(
"method",
name = "GetSwapInfo",
log_reference_id = field::Empty
)
}
Method::History => {
debug_span!("method", name = "History", 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)
debug_span!(
"method",
name = "ListSellers",
log_reference_id = field::Empty
)
}
Method::MoneroRecovery { .. } => {
debug_span!("method", name = "MoneroRecovery", log_reference_id=field::Empty)
debug_span!(
"method",
name = "MoneroRecovery",
log_reference_id = field::Empty
)
}
Method::RawHistory => debug_span!("method", name = "RawHistory", 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)
debug_span!(
"method",
name = "StartDaemon",
log_reference_id = field::Empty
)
}
Method::SuspendCurrentSwap => {
debug_span!("method", name = "SuspendCurrentSwap", log_reference_id=field::Empty)
debug_span!(
"method",
name = "SuspendCurrentSwap",
log_reference_id = field::Empty
)
}
Method::WithdrawBtc { .. } => {
debug_span!("method", name = "WithdrawBtc", log_reference_id=field::Empty)
debug_span!(
"method",
name = "WithdrawBtc",
log_reference_id = field::Empty
)
}
};
if let Some(log_reference_id) = log_reference_id {
@ -118,11 +160,17 @@ impl Method {
impl Request {
pub fn new(cmd: Method) -> Request {
Request { cmd, log_reference: None }
Request {
cmd,
log_reference: None,
}
}
pub fn with_id(cmd: Method, id: Option<String>) -> Request {
Request { cmd, log_reference: id }
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
@ -699,21 +747,21 @@ pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
max_giveable_fn: FMG,
sync: FS,
estimate_fee: FFE,
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
) -> Result<(Amount, Amount)>
where
TB: Future<Output = Result<bitcoin::Amount>>,
TB: Future<Output = Result<Amount>>,
FB: Fn() -> TB,
TMG: Future<Output = Result<bitcoin::Amount>>,
TMG: Future<Output = Result<Amount>>,
FMG: Fn() -> TMG,
TS: Future<Output = Result<()>>,
FS: Fn() -> TS,
FFE: Fn(bitcoin::Amount) -> TFE,
TFE: Future<Output = Result<bitcoin::Amount>>,
FFE: Fn(Amount) -> TFE,
TFE: Future<Output = Result<Amount>>,
{
tracing::debug!("Requesting quote");
let bid_quote = bid_quote.await?;
if bid_quote.max_quantity == bitcoin::Amount::ZERO {
if bid_quote.max_quantity == Amount::ZERO {
bail!(ZeroQuoteReceived)
}
@ -724,9 +772,10 @@ where
"Received quote",
);
sync().await?;
let mut max_giveable = max_giveable_fn().await?;
if max_giveable == bitcoin::Amount::ZERO || max_giveable < bid_quote.min_quantity {
if max_giveable == Amount::ZERO || max_giveable < bid_quote.min_quantity {
let deposit_address = get_new_address.await?;
let minimum_amount = bid_quote.min_quantity;
let maximum_amount = bid_quote.max_quantity;

View file

@ -5,11 +5,11 @@ use crate::monero::monero_address;
use crate::{bitcoin, monero};
use anyhow::Result;
use jsonrpsee::server::RpcModule;
use jsonrpsee::types::Params;
use libp2p::core::Multiaddr;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use jsonrpsee::types::Params;
use uuid::Uuid;
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
@ -31,11 +31,10 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
execute_request(
params_raw,
Method::GetSwapInfo {
swap_id: *swap_id,
},
Method::GetSwapInfo { swap_id: *swap_id },
&context,
).await
)
.await
})
.unwrap();
@ -65,13 +64,7 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?;
execute_request(
params_raw,
Method::Resume {
swap_id: *swap_id,
},
&context,
).await
execute_request(params_raw, Method::Resume { swap_id: *swap_id }, &context).await
})
.unwrap();
@ -85,11 +78,10 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
execute_request(
params_raw,
Method::CancelAndRefund {
swap_id: *swap_id,
},
Method::CancelAndRefund { swap_id: *swap_id },
&context,
).await
)
.await
})
.unwrap();
@ -123,7 +115,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
address: withdraw_address,
},
&context,
).await
)
.await
})
.expect("Could not register RPC method withdraw_btc");
module
@ -168,7 +161,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
swap_id: Uuid::new_v4(),
},
&context,
).await
)
.await
})
.unwrap();
module
@ -184,7 +178,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
rendezvous_point: rendezvous_point.clone(),
},
&context,
).await
)
.await
})
.unwrap();
@ -202,15 +197,15 @@ async fn execute_request(
cmd: Method,
context: &Arc<Context>,
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
let params_parsed = params.parse::<HashMap<String, String>>()
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 reference_id = params_parsed.get("log_reference_id");
let request = Request::with_id(cmd, reference_id.map(|s| s.clone()));
request
.call(Arc::clone(context))
.await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
}
}