@@ -76,7 +76,6 @@ output(s). The other is the weight of spending the drain output later on (the in
76
76
use std :: str :: FromStr ;
77
77
use bdk_coin_select :: {CoinSelector , Candidate , DrainWeights , TXIN_BASE_WEIGHT , ChangePolicy , TR_KEYSPEND_TXIN_WEIGHT };
78
78
use bitcoin :: {Address , Network , Transaction , TxIn , TxOut };
79
- const TR_SATISFACTION_WEIGHT : u32 = 66 ;
80
79
let base_tx = Transaction {
81
80
input : vec! [],
82
81
output : vec! [/* include your recipient outputs here */ ],
@@ -129,19 +128,37 @@ Built-in metrics are provided in the [`metrics`] submodule. Currently, only the
129
128
[ ` LowestFee ` ] ( metrics::LowestFee ) metric is considered stable.
130
129
131
130
``` rust
132
- use bdk_coin_select :: { Candidate , CoinSelector , FeeRate , Target , ChangePolicy };
131
+ use bdk_coin_select :: { Candidate , CoinSelector , FeeRate , Target , TargetFee , ChangePolicy , TR_KEYSPEND_TXIN_WEIGHT };
133
132
use bdk_coin_select :: metrics :: LowestFee ;
134
- let candidates = [];
133
+ let candidates = [
134
+ Candidate {
135
+ input_count : 1 ,
136
+ value : 400_000 ,
137
+ weight : TR_KEYSPEND_TXIN_WEIGHT ,
138
+ is_segwit : true
139
+ },
140
+ Candidate {
141
+ input_count : 1 ,
142
+ value : 200_000 ,
143
+ weight : TR_KEYSPEND_TXIN_WEIGHT ,
144
+ is_segwit : true
145
+ },
146
+ Candidate {
147
+ input_count : 1 ,
148
+ value : 11_000 ,
149
+ weight : TR_KEYSPEND_TXIN_WEIGHT ,
150
+ is_segwit : true
151
+ }
152
+ ];
135
153
let base_weight = 0 ;
136
154
let drain_weights = bdk_coin_select :: DrainWeights :: default ();
137
155
let dust_limit = 0 ;
138
- let long_term_feerate = FeeRate :: default_min_relay_fee ( );
156
+ let long_term_feerate = FeeRate :: from_sat_per_vb ( 10.0 );
139
157
140
158
let mut coin_selector = CoinSelector :: new (& candidates , base_weight );
141
159
142
160
let target = Target {
143
- feerate : FeeRate :: default_min_relay_fee (),
144
- min_fee : 0 ,
161
+ fee : TargetFee :: from_feerate (FeeRate :: from_sat_per_vb (15.0 )),
145
162
value : 210_000 ,
146
163
};
147
164
@@ -151,7 +168,7 @@ let target = Target {
151
168
let change_policy = ChangePolicy :: min_value_and_waste (
152
169
drain_weights ,
153
170
dust_limit ,
154
- target . feerate ,
171
+ target . fee . rate ,
155
172
long_term_feerate ,
156
173
);
157
174
@@ -166,7 +183,11 @@ let metric = LowestFee {
166
183
167
184
// We run the branch and bound algorithm with a max round limit of 100,000.
168
185
match coin_selector . run_bnb (metric , 100_000 ) {
169
- Err (err ) => println! (" failed to find a solution: {}" , err ),
186
+ Err (err ) => {
187
+ println! (" failed to find a solution: {}" , err );
188
+ // fall back to naive selection
189
+ coin_selector . select_until_target_met (target ). expect (" a selection was impossible!" );
190
+ }
170
191
Ok (score ) => {
171
192
println! (" we found a solution with score {}" , score );
172
193
@@ -179,6 +200,7 @@ match coin_selector.run_bnb(metric, 100_000) {
179
200
println! (" We are including a change output of {} value (0 means not change)" , change . value);
180
201
}
181
202
};
203
+
182
204
```
183
205
184
206
## Finalizing a Selection
@@ -195,7 +217,7 @@ match coin_selector.run_bnb(metric, 100_000) {
195
217
use bdk_coin_select :: {CoinSelector , Candidate , DrainWeights , Target , ChangePolicy , TR_KEYSPEND_TXIN_WEIGHT , Drain };
196
218
use bitcoin :: {Amount , TxOut , Address };
197
219
let base_weight = 0_u32 ;
198
- let drain_weights = DrainWeights :: new_tr_keyspend () ;
220
+ let drain_weights = DrainWeights :: TR_KEYSPEND ;
199
221
use core :: str :: FromStr ;
200
222
201
223
// A random target, as an example.
@@ -222,7 +244,7 @@ let candidate_txouts = vec![
222
244
script_pubkey : Address :: from_str (" bc1p0d0rhyynq0awa9m8cqrcr8f5nxqx3aw29w4ru5u9my3h0sfygnzs9khxz8" ). unwrap (). payload. script_pubkey ()
223
245
}
224
246
];
225
- // We transform the candidate txouts into something `CoinSelector` can
247
+ // We transform the candidate txouts into something `CoinSelector` can
226
248
// understand.
227
249
let candidates = candidate_txouts
228
250
. iter ()
@@ -236,7 +258,7 @@ let candidates = candidate_txouts
236
258
237
259
let mut selector = CoinSelector :: new (& candidates , base_weight );
238
260
selector
239
- . select_until_target_met (target , Drain :: none () )
261
+ . select_until_target_met (target )
240
262
. expect (" we've got enough coins" );
241
263
242
264
// Get a list of coins that are selected.
@@ -256,11 +278,5 @@ if drain.is_some() {
256
278
257
279
# Minimum Supported Rust Version (MSRV)
258
280
259
- This library is tested to compile on 1 .54
281
+ This library is compiles on rust v1 .54 and above
260
282
261
- To build with the MSRV, you will need to pin the following dependencies:
262
-
263
- ``` shell
264
- # tempfile 3.7.0 has MSRV 1.63.0+
265
- cargo update -p tempfile --precise " 3.6.0"
266
- ```
0 commit comments