1
1
use std:: {
2
- cell:: RefCell ,
3
2
cmp:: min,
4
3
collections:: BTreeMap ,
5
4
default:: Default ,
@@ -235,7 +234,7 @@ impl<'a, Cs: CoinSelectionAlgorithm> TxBuilderSpacesUtils<'a, Cs> for TxBuilder<
235
234
placeholder. auction . outpoint . vout as u8 ,
236
235
& offer,
237
236
) ?)
238
- . expect ( "compressed psbt script bytes" ) ;
237
+ . expect ( "compressed psbt script bytes" ) ;
239
238
240
239
let carrier = ScriptBuf :: new_op_return ( & compressed_psbt) ;
241
240
@@ -895,23 +894,25 @@ impl Builder {
895
894
}
896
895
}
897
896
898
- /// A coin selection algorithm that guarantees required utxos are ordered first
899
- /// appends any funding/change outputs to the end of the selected utxos
900
- /// also enables adding additional optional foreign utxos for funding
897
+ /// A coin selection algorithm that :
898
+ /// 1. Guarantees required utxos are ordered first appending
899
+ /// any funding/change outputs to the end of the selected utxos.
900
+ /// 2. Excludes all dust outputs to avoid accidentally spending space utxos
901
+ /// 3. Enables adding additional output exclusions
901
902
#[ derive( Debug , Clone ) ]
902
903
pub struct SpacesAwareCoinSelection {
903
904
pub default_algorithm : DefaultCoinSelectionAlgorithm ,
904
- // Additional UTXOs to fund the transaction
905
- pub other_optional_utxos : RefCell < Vec < WeightedUtxo > > ,
906
905
// Exclude outputs
907
906
pub exclude_outputs : Vec < OutPoint > ,
908
907
}
909
908
910
909
impl SpacesAwareCoinSelection {
911
- pub fn new ( optional_utxos : Vec < WeightedUtxo > , excluded : Vec < OutPoint > ) -> Self {
910
+ // Will skip any outputs with value less than the dust threshold
911
+ // to avoid accidentally spending space outputs
912
+ pub const DUST_THRESHOLD : Amount = Amount :: from_sat ( 1200 ) ;
913
+ pub fn new ( excluded : Vec < OutPoint > ) -> Self {
912
914
Self {
913
915
default_algorithm : DefaultCoinSelectionAlgorithm :: default ( ) ,
914
- other_optional_utxos : RefCell :: new ( optional_utxos) ,
915
916
exclude_outputs : excluded,
916
917
}
917
918
}
@@ -931,12 +932,10 @@ impl CoinSelectionAlgorithm for SpacesAwareCoinSelection {
931
932
. map ( |w| w. utxo . clone ( ) )
932
933
. collect :: < Vec < _ > > ( ) ;
933
934
934
- // Extend optional outputs
935
- optional_utxos. extend ( self . other_optional_utxos . borrow ( ) . iter ( ) . cloned ( ) ) ;
936
-
937
- // Filter out excluded outputs
935
+ // Filter out UTXOs that are either explicitly excluded or below the dust threshold
938
936
optional_utxos. retain ( |weighted_utxo| {
939
- !self
937
+ weighted_utxo. utxo . txout ( ) . value > SpacesAwareCoinSelection :: DUST_THRESHOLD
938
+ && !self
940
939
. exclude_outputs
941
940
. contains ( & weighted_utxo. utxo . outpoint ( ) )
942
941
} ) ;
@@ -952,9 +951,6 @@ impl CoinSelectionAlgorithm for SpacesAwareCoinSelection {
952
951
let mut optional = Vec :: with_capacity ( result. selected . len ( ) - required. len ( ) ) ;
953
952
for utxo in result. selected . drain ( ..) {
954
953
if !required. iter ( ) . any ( |u| u == & utxo) {
955
- self . other_optional_utxos
956
- . borrow_mut ( )
957
- . retain ( |x| x. utxo != utxo) ;
958
954
optional. push ( utxo) ;
959
955
}
960
956
}
0 commit comments