Merge bitcoindevkit/bdk-ffi#148: Add Wallet and TxBuilder tests using test funded wallet

e469dcd32c174e6a3d898f9e61196231f4671159 Add test with funded wallet (Steve Myers)

Pull request description:

  This PR adds an example test using the `bdk` test funded wallet.  The example test makes sure the `bdk-ffi` `TxBuilder` is able to drain a single wallet UTXO to a single address.  More tests can be added as we need them in future PRs.

  Required to complete #141

Top commit has no ACKs.

Tree-SHA512: 780e8cf5b3d3091f3322113f017c5b5524b30a3ac9e18910539c51042740d2809535a947b8d56012076ac5e9ad1abcf707ceaf17651457ea327a0b522fcc1002
This commit is contained in:
Steve Myers 2022-06-14 10:26:54 -07:00
commit cc8a17ef86
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051

View File

@ -331,13 +331,13 @@ fn to_script_pubkey(address: &str) -> Result<Script, BdkError> {
.map_err(|e| BdkError::Generic(e.to_string()))
}
#[derive(Clone)]
#[derive(Clone, Debug)]
enum RbfValue {
Default,
Value(u32),
}
#[derive(Clone)]
#[derive(Clone, Debug)]
struct TxBuilder {
recipients: Vec<(String, u64)>,
fee_rate: Option<f32>,
@ -505,3 +505,70 @@ impl BumpFeeTxBuilder {
}
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
// The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
// These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
// crate.
#[cfg(test)]
mod test {
use crate::{TxBuilder, Wallet};
use bdk::bitcoin::Address;
use bdk::bitcoin::Network::Testnet;
use bdk::wallet::get_funded_wallet;
use std::str::FromStr;
use std::sync::Mutex;
#[test]
fn test_drain_wallet() {
let test_wpkh = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)";
let (funded_wallet, _, _) = get_funded_wallet(test_wpkh);
let test_wallet = Wallet {
wallet_mutex: Mutex::new(funded_wallet),
};
let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string();
let tx_builder = TxBuilder::new()
.drain_wallet()
.drain_to(drain_to_address.clone());
//dbg!(&tx_builder);
assert_eq!(tx_builder.drain_wallet, true);
assert_eq!(tx_builder.drain_to, Some(drain_to_address));
let psbt = tx_builder.finish(&test_wallet).unwrap();
let psbt = psbt.internal.lock().unwrap().clone();
// confirm one input with 50,000 sats
assert_eq!(psbt.inputs.len(), 1);
let input_value = psbt
.inputs
.get(0)
.cloned()
.unwrap()
.non_witness_utxo
.unwrap()
.output
.get(0)
.unwrap()
.value;
assert_eq!(input_value, 50_000 as u64);
// confirm one output to correct address with all sats - fee
assert_eq!(psbt.outputs.len(), 1);
let output_address = Address::from_script(
&psbt
.unsigned_tx
.output
.get(0)
.cloned()
.unwrap()
.script_pubkey,
Testnet,
)
.unwrap();
assert_eq!(
output_address,
Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt").unwrap()
);
let output_value = psbt.unsigned_tx.output.get(0).cloned().unwrap().value;
assert_eq!(output_value, 49_890 as u64); // input - fee
}
}