Learn BDK – Bitcoin Dev Kit

Here we continue trying out BDK and learning to use it with Rust :

By the end of this article our code will do the following :

  • Create a wallet
  • create a transaction
  • sign transaction
  • broadcast to testnet

During testing I had this “Dust” error, so let’s just cover that first…

BDK – Error: OutputBelowDustLimit(0)

When testing on Testnet you may come across errors with the amount you are trying to send such as these :

For instance..Dust

Dust is considered the amount of satoshis equal to or lower than a transaction fee.

❯ cargo run
   Compiling bdk_2 v0.1.0 (/home/rag/Documents/rust/bdk_2)
    Finished dev [unoptimized + debuginfo] target(s) in 3.34s
     Running `/home/rag/Documents/rust/bdk_2/target/debug/bdk_2`
Generated Address: tb1q7w0t936xp5p994qx506xj53gjdcmzjr2mkqghn
Wallet balance in SAT: { immature: 0, trusted_pending: 0, untrusted_pending: 0, confirmed: 566 }
Error: InsufficientFunds { needed: 50113, available: 566 }

❯ cargo run
   Compiling bdk_2 v0.1.0 (/home/rag/Documents/rust/bdk_2)
    Finished dev [unoptimized + debuginfo] target(s) in 3.22s
     Running `/home/rag/Documents/rust/bdk_2/target/debug/bdk_2`
Generated Address: tb1q7w0t936xp5p994qx506xj53gjdcmzjr2mkqghn
Wallet balance in SAT: { immature: 0, trusted_pending: 0, untrusted_pending: 0, confirmed: 566 }
Error: OutputBelowDustLimit(0)

Dust limit = 546

How to get more sats for testing on Testnet

If you want additional test sats, try : https://bitcoinfaucet.uo1.net/

Ok, now we’ve received some test sats, let’s send some back to faucet using our code, we’ll break down the code in a bit…

❯ ## with new test sats
❯ nv main.rs
❯ ## send them back to faucet
❯ cargo run
   Compiling bdk_2 v0.1.0 (/home/rag/Documents/rust/bdk_2)
    Finished dev [unoptimized + debuginfo] target(s) in 3.37s
     Running `/home/rag/Documents/rust/bdk_2/target/debug/bdk_2`
Generated Address: tb1q7w0t936xp5p994qx506xj53gjdcmzjr2mkqghn
Wallet balance in SAT: { immature: 0, trusted_pending: 0, untrusted_pending: 1000, confirmed: 566 }
Transaction details: TransactionDetails {
    transaction: None,
    txid: f20554df5042500effe406cad484c32f02ccaf663f0c1861d79070b3d7105d32,
    received: 357,
    sent: 1566,
    fee: Some(
        209,
    ),
    confirmation_time: None,
}
Transaction Signed: true
Transaction sent! TXID: f20554df5042500effe406cad484c32f02ccaf663f0c1861d79070b3d7105d32.
Explorer URL: https://blockstream.info/testnet/tx/f20554df5042500effe406cad484c32f02ccaf663f0c1861d79070b3d7105d32

Success – Transaction sent!

Webdock – Fast Cloud VPS Linux Hosting

The tx_builder

The tx builder is the part we need to customise per transaction – If you are curious as to what “rbf” means in the code, here is a short explanation :

"RBF" refers to "Replace-By-Fee," a feature in Bitcoin transactions. Replace-By-Fee allows a sender to replace an unconfirmed transaction with a new one that includes a higher transaction fee, enabling faster confirmation. The code snippet you provided is a method called `enable_rbf` in a programming context, presumably related to Bitcoin transactions. When invoked, this method sets a flag (`self.params.rbf`) to enable Replace-By-Fee, and it uses the default value for the `nSequence` field, which is `0xFFFFFFFD`. This value indicates that the transaction can be replaced until it is included in a block.

Essentially, calling this method signifies the intent to enable a transaction to be replaced by a new one with a higher fee, providing flexibility to adjust transaction fees dynamically for faster confirmation in the Bitcoin network. The method returns a mutable reference to the instance, allowing for method chaining in the code.

https://youtu.be/zL9Nu39qft4?si=JBj6qOht4m6I5lPh

Code:

Here you see my code with “1000” to send from my wallet, back to this test faucet address : tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6 (as per screenshot instructions

Conclusion :

Using Rust, BDK and the Bitcoin Testnet (test network) we successfully sent 1000 sats back to the faucet from the wallet that we generated with our code and received test sats to.

We created a transaction, signed it, and broadcast it to Testnet and it was subsequently confirmed. Nice!

├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug

Just show me the full code!

Full code for BDK Rust used here on GitHub

Watch- Bitcoin Dev Kit | BDK code to send transaction | Rust Language