Skip to content

Commit

Permalink
fix!: changes balance query (#6158)
Browse files Browse the repository at this point in the history
Description
---
changes balance query to be more in line with you would expect. 
FFI now matches grpc

BREAKING CHANGE
---
This changes the external API for the wallet_ffi and the wallet library
  • Loading branch information
SWvheerden committed Feb 27, 2024
1 parent 19a824d commit 9ccc615
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
Expand Up @@ -264,10 +264,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
Err(e) => return Err(Status::not_found(format!("GetBalance error! {}", e))),
};
Ok(Response::new(GetBalanceResponse {
available_balance: balance
.available_balance
.saturating_sub(balance.time_locked_balance.unwrap_or_default())
.0,
available_balance: balance.available_balance.0,
pending_incoming_balance: balance.pending_incoming_balance.0,
pending_outgoing_balance: balance.pending_outgoing_balance.0,
timelocked_balance: balance.time_locked_balance.unwrap_or_default().0,
Expand Down
Expand Up @@ -53,7 +53,7 @@ impl<B: Backend> Component<B> for Balance {
let available_balance = Spans::from(vec![
Span::styled("Available:", Style::default().fg(Color::Magenta)),
Span::raw(" "),
Span::raw(format!("{}", balance.available_balance.saturating_sub(time_locked))),
Span::raw(format!("{}", balance.available_balance)),
Span::raw(format!(" (Time Locked: {})", time_locked)),
]);
let incoming_balance = Spans::from(vec![
Expand Down
Expand Up @@ -72,8 +72,9 @@ impl BalanceEnquiryDebouncer {
if let Ok(balance) = self.output_manager_service.get_balance().await {
trace!(
target: LOG_TARGET,
"Initial balance: available {}, incoming {}, outgoing {}",
"Initial balance: available {}, time-locked {}, incoming {}, outgoing {}",
balance.available_balance,
balance.time_locked_balance.unwrap_or(0.into()),
balance.pending_incoming_balance,
balance.pending_outgoing_balance
);
Expand Down
Expand Up @@ -394,7 +394,7 @@ impl OutputSql {
let balance_query_result = if let Some(current_tip) = current_tip_for_time_lock_calculation {
let balance_query = sql_query(
"SELECT coalesce(sum(value), 0) as amount, 'available_balance' as category \
FROM outputs WHERE status = ? \
FROM outputs WHERE status = ? AND maturity <= ? AND script_lock_height <= ? \
UNION ALL \
SELECT coalesce(sum(value), 0) as amount, 'time_locked_balance' as category \
FROM outputs WHERE status = ? AND maturity > ? OR script_lock_height > ? \
Expand All @@ -407,6 +407,8 @@ impl OutputSql {
)
// available_balance
.bind::<diesel::sql_types::Integer, _>(OutputStatus::Unspent as i32)
.bind::<diesel::sql_types::BigInt, _>(current_tip as i64)
.bind::<diesel::sql_types::BigInt, _>(current_tip as i64)
// time_locked_balance
.bind::<diesel::sql_types::Integer, _>(OutputStatus::Unspent as i32)
.bind::<diesel::sql_types::BigInt, _>(current_tip as i64)
Expand Down
Expand Up @@ -163,7 +163,7 @@ pub async fn test_db_backend<T: OutputManagerBackend + 'static>(backend: T) {

let balance = db.get_balance(Some(3)).unwrap();
assert_eq!(balance, Balance {
available_balance,
available_balance: available_balance - time_locked_balance,
time_locked_balance: Some(time_locked_balance),
pending_incoming_balance,
pending_outgoing_balance
Expand Down

0 comments on commit 9ccc615

Please sign in to comment.