Skip to content

Commit 59b2b8a

Browse files
committed
Drop Node::onchain_balance from public API
We already have `Node::spendable_onchain_balance` and `Node::total_onchain_balance` exposed, so the general `onchain_balance` is kind of redudant. As it is also not exposed (and not easily exposable) in bindings, we can make the bindings and Rust APIs more homogeneous by dropping the call. Plus, we probably should avoid exposing any BDK types in the public API until BDK 1.0 landed and we made the switch, as many breaking changes will be coming with it.
1 parent 13808ab commit 59b2b8a

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,9 +1145,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
11451145
Ok(funding_address)
11461146
}
11471147

1148-
/// Retrieve the current on-chain balance.
1149-
pub fn onchain_balance(&self) -> Result<bdk::Balance, Error> {
1150-
self.wallet.get_balance()
1148+
/// Retrieve the currently spendable on-chain balance in satoshis.
1149+
pub fn spendable_onchain_balance_sats(&self) -> Result<u64, Error> {
1150+
Ok(self.wallet.get_balance().map(|bal| bal.get_spendable())?)
1151+
}
1152+
1153+
/// Retrieve the current total on-chain balance in satoshis.
1154+
pub fn total_onchain_balance_sats(&self) -> Result<u64, Error> {
1155+
Ok(self.wallet.get_balance().map(|bal| bal.get_total())?)
11511156
}
11521157

11531158
/// Send an on-chain payment to the given address.
@@ -1177,16 +1182,6 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
11771182
self.wallet.send_to_address(address, None)
11781183
}
11791184

1180-
/// Retrieve the currently spendable on-chain balance in satoshis.
1181-
pub fn spendable_onchain_balance_sats(&self) -> Result<u64, Error> {
1182-
Ok(self.wallet.get_balance().map(|bal| bal.get_spendable())?)
1183-
}
1184-
1185-
/// Retrieve the current total on-chain balance in satoshis.
1186-
pub fn total_onchain_balance_sats(&self) -> Result<u64, Error> {
1187-
Ok(self.wallet.get_balance().map(|bal| bal.get_total())?)
1188-
}
1189-
11901185
/// Retrieve a list of known channels.
11911186
pub fn list_channels(&self) -> Vec<ChannelDetails> {
11921187
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()

src/test/functional_tests.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ fn channel_full_cycle() {
3434
);
3535
node_a.sync_wallets().unwrap();
3636
node_b.sync_wallets().unwrap();
37-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
38-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
37+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
38+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
3939

4040
// Check we haven't got any events yet
4141
assert_eq!(node_a.next_event(), None);
@@ -77,12 +77,11 @@ fn channel_full_cycle() {
7777
node_b.sync_wallets().unwrap();
7878

7979
let onchain_fee_buffer_sat = 1500;
80-
let node_a_balance = node_a.onchain_balance().unwrap();
8180
let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
8281
let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
83-
assert!(node_a_balance.get_spendable() < node_a_upper_bound_sat);
84-
assert!(node_a_balance.get_spendable() > node_a_lower_bound_sat);
85-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
82+
assert!(node_a.spendable_onchain_balance_sats().unwrap() < node_a_upper_bound_sat);
83+
assert!(node_a.spendable_onchain_balance_sats().unwrap() > node_a_lower_bound_sat);
84+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
8685

8786
expect_event!(node_a, ChannelReady);
8887

@@ -215,10 +214,10 @@ fn channel_full_cycle() {
215214
let node_a_upper_bound_sat =
216215
(premine_amount_sat - funding_amount_sat) + (funding_amount_sat - sum_of_all_payments_sat);
217216
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
218-
assert!(node_a.onchain_balance().unwrap().get_spendable() > node_a_lower_bound_sat);
219-
assert!(node_a.onchain_balance().unwrap().get_spendable() < node_a_upper_bound_sat);
217+
assert!(node_a.spendable_onchain_balance_sats().unwrap() > node_a_lower_bound_sat);
218+
assert!(node_a.spendable_onchain_balance_sats().unwrap() < node_a_upper_bound_sat);
220219
let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
221-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), expected_final_amount_node_b_sat);
220+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), expected_final_amount_node_b_sat);
222221

223222
// Check we handled all events
224223
assert_eq!(node_a.next_event(), None);
@@ -260,8 +259,8 @@ fn channel_open_fails_when_funds_insufficient() {
260259
);
261260
node_a.sync_wallets().unwrap();
262261
node_b.sync_wallets().unwrap();
263-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
264-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
262+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
263+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
265264

266265
println!("\nA -- connect_open_channel -> B");
267266
assert_eq!(
@@ -302,13 +301,13 @@ fn start_stop_reinit() {
302301
let expected_amount = Amount::from_sat(100000);
303302

304303
premine_and_distribute_funds(&bitcoind, &electrsd, vec![funding_address], expected_amount);
305-
assert_eq!(node.onchain_balance().unwrap().get_total(), 0);
304+
assert_eq!(node.total_onchain_balance_sats().unwrap(), 0);
306305

307306
node.start().unwrap();
308307
assert_eq!(node.start(), Err(Error::AlreadyRunning));
309308

310309
node.sync_wallets().unwrap();
311-
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
310+
assert_eq!(node.spendable_onchain_balance_sats().unwrap(), expected_amount.to_sat());
312311

313312
node.stop().unwrap();
314313
assert_eq!(node.stop(), Err(Error::NotRunning));
@@ -328,13 +327,13 @@ fn start_stop_reinit() {
328327
reinitialized_node.start().unwrap();
329328

330329
assert_eq!(
331-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
330+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
332331
expected_amount.to_sat()
333332
);
334333

335334
reinitialized_node.sync_wallets().unwrap();
336335
assert_eq!(
337-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
336+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
338337
expected_amount.to_sat()
339338
);
340339

@@ -369,7 +368,7 @@ fn onchain_spend_receive() {
369368

370369
node_a.sync_wallets().unwrap();
371370
node_b.sync_wallets().unwrap();
372-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), 100000);
371+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), 100000);
373372

374373
assert_eq!(Err(Error::InsufficientFunds), node_a.send_to_onchain_address(&addr_b, 1000));
375374

@@ -380,9 +379,9 @@ fn onchain_spend_receive() {
380379
node_a.sync_wallets().unwrap();
381380
node_b.sync_wallets().unwrap();
382381

383-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), 1000);
384-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 98000);
385-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
382+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), 1000);
383+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 98000);
384+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
386385

387386
let addr_b = node_b.new_funding_address().unwrap();
388387
let txid = node_a.send_all_to_onchain_address(&addr_b).unwrap();
@@ -392,9 +391,9 @@ fn onchain_spend_receive() {
392391
node_a.sync_wallets().unwrap();
393392
node_b.sync_wallets().unwrap();
394393

395-
assert_eq!(node_a.onchain_balance().unwrap().get_total(), 0);
396-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 99000);
397-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
394+
assert_eq!(node_a.total_onchain_balance_sats().unwrap(), 0);
395+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 99000);
396+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
398397
}
399398

400399
#[test]

0 commit comments

Comments
 (0)