Skip to content

Commit 05ce7da

Browse files
committed
refactor: restructure balance
1 parent e5ded1a commit 05ce7da

File tree

15 files changed

+54
-71
lines changed

15 files changed

+54
-71
lines changed

bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class LiveTxBuilderTest {
1414
val esploraClient = EsploraClient("https://mempool.space/testnet/api")
1515
val update = esploraClient.scan(wallet, 10uL, 1uL)
1616
wallet.applyUpdate(update)
17-
println("Balance: ${wallet.getBalance().total()}")
17+
println("Balance: ${wallet.getBalance().total}")
1818

19-
assert(wallet.getBalance().total() > 0uL)
19+
assert(wallet.getBalance().total > 0uL)
2020

2121
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
2222
val psbt: PartiallySignedTransaction = TxBuilder()

bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ class LiveWalletTest {
1212
val descriptor: Descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
1313
val wallet: Wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
1414
val esploraClient: EsploraClient = EsploraClient("https://mempool.space/testnet/api")
15-
// val esploraClient = EsploraClient("https://blockstream.info/testnet/api")
1615
val update = esploraClient.scan(wallet, 10uL, 1uL)
1716
wallet.applyUpdate(update)
18-
println("Balance: ${wallet.getBalance().total()}")
17+
println("Balance: ${wallet.getBalance().total}")
1918
val balance: Balance = wallet.getBalance()
2019
println("Balance: $balance")
2120

22-
assert(wallet.getBalance().total() > 0uL)
21+
assert(wallet.getBalance().total > 0uL)
2322
}
2423

2524
@Test
@@ -30,10 +29,10 @@ class LiveWalletTest {
3029
val update = esploraClient.scan(wallet, 10uL, 1uL)
3130

3231
wallet.applyUpdate(update)
33-
println("Balance: ${wallet.getBalance().total()}")
32+
println("Balance: ${wallet.getBalance().total}")
3433
println("New address: ${wallet.getAddress(AddressIndex.New).address}")
3534

36-
assert(wallet.getBalance().total() > 0uL) {
35+
assert(wallet.getBalance().total > 0uL) {
3736
"Wallet balance must be greater than 0! Please send funds to ${wallet.getAddress(AddressIndex.New).address} and try again."
3837
}
3938

bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineWalletTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class OfflineWalletTest {
5050

5151
assertEquals(
5252
expected = 0uL,
53-
actual = wallet.getBalance().total()
53+
actual = wallet.getBalance().total
5454
)
5555
}
5656
}

bdk-ffi/src/bdk.udl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ enum ChangeSpendPolicy {
4949
"ChangeForbidden"
5050
};
5151

52-
interface Balance {
53-
u64 immature();
52+
dictionary Balance {
53+
u64 immature;
5454

55-
u64 trusted_pending();
55+
u64 trusted_pending;
5656

57-
u64 untrusted_pending();
57+
u64 untrusted_pending;
5858

59-
u64 confirmed();
59+
u64 confirmed;
6060

61-
u64 trusted_spendable();
61+
u64 trusted_spendable;
6262

63-
u64 total();
63+
u64 total;
6464
};
6565

6666
dictionary AddressInfo {

bdk-ffi/src/lib.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -177,47 +177,33 @@ impl From<&BdkAddressIndex> for AddressIndex {
177177
// }
178178

179179
pub struct Balance {
180-
pub inner: BdkBalance,
180+
// All coinbase outputs not yet matured
181+
pub immature: u64,
182+
/// Unconfirmed UTXOs generated by a wallet tx
183+
pub trusted_pending: u64,
184+
/// Unconfirmed UTXOs received from an external wallet
185+
pub untrusted_pending: u64,
186+
/// Confirmed and immediately spendable balance
187+
pub confirmed: u64,
188+
/// Get sum of trusted_pending and confirmed coins
189+
pub trusted_spendable: u64,
190+
/// Get the whole balance visible to the wallet
191+
pub total: u64,
181192
}
182193

183-
impl Balance {
184-
/// All coinbase outputs not yet matured.
185-
fn immature(&self) -> u64 {
186-
self.inner.immature
187-
}
188-
189-
/// Unconfirmed UTXOs generated by a wallet tx.
190-
fn trusted_pending(&self) -> u64 {
191-
self.inner.trusted_pending
192-
}
193-
194-
/// Unconfirmed UTXOs received from an external wallet.
195-
fn untrusted_pending(&self) -> u64 {
196-
self.inner.untrusted_pending
197-
}
198-
199-
/// Confirmed and immediately spendable balance.
200-
fn confirmed(&self) -> u64 {
201-
self.inner.confirmed
202-
}
203-
204-
/// Get sum of trusted_pending and confirmed coins.
205-
fn trusted_spendable(&self) -> u64 {
206-
self.inner.trusted_spendable()
207-
}
208-
209-
/// Get the whole balance visible to the wallet.
210-
fn total(&self) -> u64 {
211-
self.inner.total()
194+
impl From<BdkBalance> for Balance {
195+
fn from(bdk_balance: BdkBalance) -> Self {
196+
Balance {
197+
immature: bdk_balance.immature,
198+
trusted_pending: bdk_balance.trusted_pending,
199+
untrusted_pending: bdk_balance.untrusted_pending,
200+
confirmed: bdk_balance.confirmed,
201+
trusted_spendable: bdk_balance.trusted_spendable(),
202+
total: bdk_balance.total(),
203+
}
212204
}
213205
}
214206

215-
// impl From<BdkBalance> for Balance {
216-
// fn from(bdk_balance: BdkBalance) -> Self {
217-
// Balance { inner: bdk_balance }
218-
// }
219-
// }
220-
221207
// /// A transaction output, which defines new coins to be created from old ones.
222208
// #[derive(Debug, Clone)]
223209
// pub struct TxOut {

bdk-ffi/src/wallet.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ impl Wallet {
5555
.into()
5656
}
5757

58-
// TODO 16: Why is the Arc required here?
59-
pub fn get_balance(&self) -> Arc<Balance> {
60-
let bdk_balance = self.get_wallet().get_balance();
61-
let balance = Balance { inner: bdk_balance };
62-
Arc::new(balance)
58+
pub fn get_balance(&self) -> Balance {
59+
let bdk_balance: bdk::wallet::Balance = self.get_wallet().get_balance();
60+
Balance::from(bdk_balance)
6361
}
6462

6563
pub fn apply_update(&self, update: Arc<Update>) -> Result<(), BdkError> {

bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveTxBuilderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class LiveTxBuilderTest {
1212
val esploraClient = EsploraClient("https://mempool.space/testnet/api")
1313
val update = esploraClient.scan(wallet, 10uL, 1uL)
1414
wallet.applyUpdate(update)
15-
println("Balance: ${wallet.getBalance().total()}")
15+
println("Balance: ${wallet.getBalance().total}")
1616

17-
assert(wallet.getBalance().total() > 0uL)
17+
assert(wallet.getBalance().total > 0uL)
1818

1919
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
2020
val psbt: PartiallySignedTransaction = TxBuilder()

bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveWalletTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class LiveWalletTest {
1212
// val esploraClient = EsploraClient("https://blockstream.info/testnet/api")
1313
val update = esploraClient.scan(wallet, 10uL, 1uL)
1414
wallet.applyUpdate(update)
15-
println("Balance: ${wallet.getBalance().total()}")
15+
println("Balance: ${wallet.getBalance().total}")
1616

17-
assert(wallet.getBalance().total() > 0uL)
17+
assert(wallet.getBalance().total > 0uL)
1818
}
1919

2020
@Test
@@ -25,10 +25,10 @@ class LiveWalletTest {
2525
val update = esploraClient.scan(wallet, 10uL, 1uL)
2626

2727
wallet.applyUpdate(update)
28-
println("Balance: ${wallet.getBalance().total()}")
28+
println("Balance: ${wallet.getBalance().total}")
2929
println("New address: ${wallet.getAddress(AddressIndex.New).address.asString()}")
3030

31-
assert(wallet.getBalance().total() > 0uL) {
31+
assert(wallet.getBalance().total > 0uL) {
3232
"Wallet balance must be greater than 0! Please send funds to ${wallet.getAddress(AddressIndex.New).address} and try again."
3333
}
3434

bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/OfflineWalletTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class OfflineWalletTest {
4747

4848
assertEquals(
4949
expected = 0uL,
50-
actual = wallet.getBalance().total()
50+
actual = wallet.getBalance().total
5151
)
5252
}
5353
}

bdk-python/tests/test_live_tx_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_tx_builder(self):
2121
)
2222
wallet.apply_update(update)
2323

24-
self.assertGreater(wallet.get_balance().total(), 0)
24+
self.assertGreater(wallet.get_balance().total, 0)
2525

2626
recipient = bdk.Address(
2727
address = "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",

0 commit comments

Comments
 (0)