BDK Coin Selection
bdk_coin_select is a zero-dependency tool to help you select inputs for making Bitcoin (ticker: BTC) transactions.
⚠ This work is only ready to use by those who expect (potentially catastrophic) bugs and will have the time to investigate them and contribute back to this crate.
Synopis
use FromStr;
use ;
use ;
let recipient_addr: Address = "tb1pvjf9t34fznr53u5tqhejz4nr69luzkhlvsdsdfq9pglutrpve2xq7hps46"
.
.unwrap
.assume_checked;
let outputs = vec!;
let target = Target ;
let candidates = vec!;
// You can now select coins!
let mut coin_selector = new;
coin_selector.select;
assert!;
println!;
coin_selector.select;
assert!;
// Now we need to know if we need a change output to drain the excess if we overshot too much
//
// We don't need to know exactly which change output we're going to use yet but we assume it's a taproot output
// that we'll use a keyspend to spend from.
let drain_weights = TR_KEYSPEND;
// Our policy is to only add a change output if the value is over 1_000 sats
let change_policy = min_value;
let change = coin_selector.drain;
if change.is_some else
Automatic selection with Branch and Bound
You can use methods such as [CoinSelector::select] to manually select coins, or methods such as
[CoinSelector::select_until_target_met] for a rudimentary automatic selection. Probably you want
to use [CoinSelector::run_bnb] to do this in a smart way.
Built-in metrics are provided in the [metrics] submodule. Currently, only the
LowestFee metric is considered stable. Note you can try and write your own
metric by implementing the [BnbMetric] yourself but we don't recommend this.
use FromStr;
use ;
use LowestFee;
use ;
let recipient_addr: Address = "tb1pvjf9t34fznr53u5tqhejz4nr69luzkhlvsdsdfq9pglutrpve2xq7hps46"
.
.unwrap
.assume_checked;
let outputs = vec!;
let candidates = ;
let drain_weights = default;
// You could determine this by looking at the user's transaction history and taking an average of the feerate.
let long_term_feerate = from_sat_per_vb;
let mut coin_selector = new;
let target = Target ;
// The change output must be at least this size to be relayed.
// To choose it you need to know the kind of script pubkey on your change txout.
// Here we assume it's a taproot output
let dust_limit = TR_DUST_RELAY_MIN_VALUE;
// We use a change policy that introduces a change output if doing so reduces
// the "waste" (i.e. adding change doesn't increase the fees we'd pay if we factor in the cost to spend the output later on).
let change_policy = min_value_and_waste;
// The LowestFee metric tries make selections that minimize your total fees paid over time.
let metric = LowestFee ;
// We run the branch and bound algorithm with a max round limit of 100,000.
match coin_selector.run_bnb ;
let selection = coin_selector
.apply_selection
.;
let change = coin_selector.drain;
println!;
println!;
Minimum Supported Rust Version (MSRV)
This library is compiles on rust v1.54 and above