Allow API caller to retrieve last synced bitcoin balane and avoid costly sync

pull/1276/head
binarybaron 7 months ago
parent 8bd242c825
commit 98d8685a8e

@ -50,7 +50,9 @@ pub enum Method {
amount: Option<Amount>,
address: bitcoin::Address,
},
Balance,
Balance {
force_refresh: bool,
},
ListSellers {
rendezvous_point: Multiaddr,
},
@ -69,7 +71,7 @@ pub enum Method {
impl Method {
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
let span = match self {
Method::Balance => {
Method::Balance { .. } => {
debug_span!(
"method",
method_name = "Balance",
@ -260,7 +262,7 @@ impl Request {
state2.cancel_timelock,
state2.punish_timelock,
))
}else {
} else {
None
}
} else {
@ -702,18 +704,29 @@ impl Request {
Ok(json!({}))
}
Method::Balance => {
Method::Balance { force_refresh } => {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
.context("Could not get Bitcoin wallet")?;
bitcoin_wallet.sync().await?;
if force_refresh {
bitcoin_wallet.sync().await?;
}
let bitcoin_balance = bitcoin_wallet.balance().await?;
tracing::info!(
balance = %bitcoin_balance,
"Checked Bitcoin balance",
);
if force_refresh {
tracing::info!(
balance = %bitcoin_balance,
"Checked Bitcoin balance",
);
} else {
tracing::debug!(
balance = %bitcoin_balance,
"Current Bitcoin balance as of last sync",
);
}
Ok(json!({
"balance": bitcoin_balance.to_sat()

@ -109,7 +109,9 @@ where
(context, request)
}
CliCommand::Balance { bitcoin } => {
let request = Request::new(Method::Balance);
let request = Request::new(Method::Balance {
force_refresh: true,
});
let context = Context::build(
Some(bitcoin),

@ -34,8 +34,14 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
.await
})?;
module.register_async_method("get_bitcoin_balance", |params, context| async move {
execute_request(params, Method::Balance, &context).await
module.register_async_method("get_bitcoin_balance", |params_raw, context| async move {
let params: HashMap<String, bool> = params_raw.parse()?;
let force_refresh = *params.get("force_refresh").ok_or_else(|| {
jsonrpsee_core::Error::Custom("Does not contain force_refresh".to_string())
})?;
execute_request(params_raw, Method::Balance { force_refresh }, &context).await
})?;
module.register_async_method("get_history", |params, context| async move {

Loading…
Cancel
Save