Skip to content

Commit

Permalink
chore: bump rust bdk to alpha 11
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed May 13, 2024
1 parent ac15ed7 commit b88c022
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class LiveTxBuilderTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total.sat > 0uL)

val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val psbt: Psbt = TxBuilder()
Expand All @@ -54,9 +54,9 @@ class LiveTxBuilderTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total.sat > 0uL)

val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.TESTNET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class LiveWalletTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")
val balance: Balance = wallet.getBalance()
println("Balance: $balance")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total.sat > 0uL)

println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
Expand All @@ -55,10 +55,10 @@ class LiveWalletTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")
println("New address: ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address}")

assert(wallet.getBalance().total > 0uL) {
assert(wallet.getBalance().total.sat > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address} and try again."
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OfflineWalletTest {

assertEquals(
expected = 0uL,
actual = wallet.getBalance().total
actual = wallet.getBalance().total.sat
)
}
}
20 changes: 10 additions & 10 deletions bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ path = "uniffi-bindgen.rs"
default = ["uniffi/cli"]

[dependencies]
bdk = { version = "1.0.0-alpha.10", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.12.0", default-features = false, features = ["std", "blocking", "blocking-https-rustls"] }
bdk_file_store = { version = "0.10.0" }
bdk = { version = "1.0.0-alpha.11", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.13.0", default-features = false, features = ["std", "blocking", "blocking-https-rustls"] }
bdk_file_store = { version = "0.11.0" }

uniffi = { version = "=0.26.1" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
Expand Down
21 changes: 13 additions & 8 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ interface WalletCreationError {
NotInitialized();
LoadedGenesisDoesNotMatch(string expected, string got);
LoadedNetworkDoesNotMatch(Network expected, Network? got);
LoadedDescriptorDoesNotMatch(string got, KeychainKind keychain);
};

// ------------------------------------------------------------------------
Expand All @@ -203,17 +204,17 @@ dictionary AddressInfo {
};

dictionary Balance {
u64 immature;
Amount immature;

u64 trusted_pending;
Amount trusted_pending;

u64 untrusted_pending;
Amount untrusted_pending;

u64 confirmed;
Amount confirmed;

u64 trusted_spendable;
Amount trusted_spendable;

u64 total;
Amount total;
};

dictionary LocalOutput {
Expand Down Expand Up @@ -460,8 +461,8 @@ dictionary ScriptAmount {
};

dictionary SentAndReceivedValues {
u64 sent;
u64 received;
Amount sent;
Amount received;
};

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -543,6 +544,10 @@ dictionary OutPoint {
u32 vout;
};

dictionary Amount {
u64 sat;
};

interface FeeRate {
[Name=from_sat_per_vb, Throws=FeeRateError]
constructor(u64 sat_per_vb);
Expand Down
20 changes: 20 additions & 0 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ use std::io::Cursor;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

use bdk::bitcoin::Amount as BdkAmount;

pub struct Amount {
pub sat: u64,
}

impl From<BdkAmount> for Amount {
fn from(amount: BdkAmount) -> Self {
Amount {
sat: amount.to_sat(),
}
}
}

impl From<Amount> for BdkAmount {
fn from(amount: Amount) -> Self {
BdkAmount::from_sat(amount.sat)
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script(pub(crate) BdkScriptBuf);

Expand Down
18 changes: 18 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bdk::bitcoin::address::Error as BdkAddressError;
use bdk::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk::KeychainKind;

// ------------------------------------------------------------------------
// error definitions
Expand Down Expand Up @@ -434,6 +435,9 @@ pub enum WalletCreationError {
expected: Network,
got: Option<Network>,
},

#[error("loaded descriptor '{got}' does not match what was provided '{keychain:?}'")]
LoadedDescriptorDoesNotMatch { got: String, keychain: KeychainKind },
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -902,6 +906,12 @@ impl From<NewOrLoadError> for WalletCreationError {
NewOrLoadError::LoadedNetworkDoesNotMatch { expected, got } => {
WalletCreationError::LoadedNetworkDoesNotMatch { expected, got }
}
NewOrLoadError::LoadedDescriptorDoesNotMatch { got, keychain } => {
WalletCreationError::LoadedDescriptorDoesNotMatch {
got: format!("{:?}", got),
keychain,
}
}
}
}
}
Expand All @@ -921,6 +931,7 @@ mod test {
use crate::OutPoint;
use crate::SignerError;
use bdk::bitcoin::Network;
use bdk::KeychainKind;

#[test]
fn test_error_address() {
Expand Down Expand Up @@ -1605,6 +1616,13 @@ mod test {
},
"loaded network type is not bitcoin, got Some(Testnet)".to_string(),
),
(
WalletCreationError::LoadedDescriptorDoesNotMatch {
got: "def".to_string(),
keychain: KeychainKind::External,
},
"loaded descriptor 'def' does not match what was provided 'External'".to_string(),
),
];

for (error, expected) in errors {
Expand Down
1 change: 1 addition & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::Amount;
use crate::bitcoin::FeeRate;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
Expand Down
26 changes: 14 additions & 12 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use bdk::LocalOutput as BdkLocalOutput;

use std::sync::{Arc, Mutex};

use crate::bitcoin::Amount;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChainPosition {
Confirmed { height: u32, timestamp: u64 },
Expand Down Expand Up @@ -65,23 +67,23 @@ impl From<BdkAddressInfo> for AddressInfo {
}

pub struct Balance {
pub immature: u64,
pub trusted_pending: u64,
pub untrusted_pending: u64,
pub confirmed: u64,
pub trusted_spendable: u64,
pub total: u64,
pub immature: Amount,
pub trusted_pending: Amount,
pub untrusted_pending: Amount,
pub confirmed: Amount,
pub trusted_spendable: Amount,
pub total: Amount,
}

impl From<BdkBalance> for Balance {
fn from(bdk_balance: BdkBalance) -> Self {
Balance {
immature: bdk_balance.immature,
trusted_pending: bdk_balance.trusted_pending,
untrusted_pending: bdk_balance.untrusted_pending,
confirmed: bdk_balance.confirmed,
trusted_spendable: bdk_balance.trusted_spendable(),
total: bdk_balance.total(),
immature: bdk_balance.immature.into(),
trusted_pending: bdk_balance.trusted_pending.into(),
untrusted_pending: bdk_balance.untrusted_pending.into(),
confirmed: bdk_balance.confirmed.into(),
trusted_spendable: bdk_balance.trusted_spendable().into(),
total: bdk_balance.total().into(),
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
Expand Down Expand Up @@ -102,8 +103,11 @@ impl Wallet {
}

pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues {
let (sent, received): (u64, u64) = self.get_wallet().sent_and_received(&tx.into());
SentAndReceivedValues { sent, received }
let (sent, received) = self.get_wallet().sent_and_received(&tx.into());
SentAndReceivedValues {
sent: Amount::from(sent),
received: Amount::from(received),
}
}

pub fn transactions(&self) -> Vec<CanonicalTx> {
Expand Down Expand Up @@ -152,8 +156,8 @@ impl Wallet {
}

pub struct SentAndReceivedValues {
pub sent: u64,
pub received: u64,
pub sent: Amount,
pub received: Amount,
}

pub struct Update(pub(crate) BdkUpdate);
Expand Down Expand Up @@ -315,7 +319,7 @@ impl TxBuilder {
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx();
for (script, amount) in &self.recipients {
tx_builder.add_recipient(script.clone(), *amount);
tx_builder.add_recipient(script.clone(), Amount { sat: *amount }.into());
}
tx_builder.change_policy(self.change_policy);
if !self.utxos.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class LiveTxBuilderTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total.sat > 0uL)

val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val psbt: Psbt = TxBuilder()
Expand All @@ -55,9 +55,9 @@ class LiveTxBuilderTest {
val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL)
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("Balance: ${wallet.getBalance().total.sat}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total.sat > 0uL)

val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.SIGNET)
Expand Down
Loading

0 comments on commit b88c022

Please sign in to comment.