@@ -862,8 +862,25 @@ func (m *Manager) GetAllSwaps(ctx context.Context) ([]*StaticAddressLoopIn,
862862// are needed to cover the amount requested without leaving a dust change. It
863863// returns an error if the sum of deposits minus dust is less than the requested
864864// amount.
865- func SelectDeposits (targetAmount btcutil.Amount , deposits []* deposit.Deposit ,
866- csvExpiry uint32 , blockHeight uint32 ) ([]* deposit.Deposit , error ) {
865+ func SelectDeposits (targetAmount btcutil.Amount ,
866+ unfilteredDeposits []* deposit.Deposit , csvExpiry uint32 ,
867+ blockHeight uint32 ) ([]* deposit.Deposit , error ) {
868+
869+ // Filter out deposits that are too close to expiry to be swapped.
870+ var deposits []* deposit.Deposit
871+ for _ , d := range unfilteredDeposits {
872+ if ! IsSwappable (
873+ uint32 (d .ConfirmationHeight ), blockHeight , csvExpiry ,
874+ ) {
875+
876+ log .Debugf ("Skipping deposit %s as it expires before " +
877+ "the htlc" , d .OutPoint .String ())
878+
879+ continue
880+ }
881+
882+ deposits = append (deposits , d )
883+ }
867884
868885 // Sort the deposits by amount in descending order, then by
869886 // blocks-until-expiry in ascending order.
@@ -901,6 +918,25 @@ func SelectDeposits(targetAmount btcutil.Amount, deposits []*deposit.Deposit,
901918 selectedAmount , targetAmount )
902919}
903920
921+ // IsSwappable checks if a deposit is swappable. It returns true if the deposit
922+ // is not expired and the htlc is not too close to expiry.
923+ func IsSwappable (confirmationHeight , blockHeight , csvExpiry uint32 ) bool {
924+ // The deposit expiry height is the confirmation height plus the csv
925+ // expiry.
926+ depositExpiryHeight := confirmationHeight + csvExpiry
927+
928+ // The htlc expiry height is the current height plus the htlc
929+ // cltv delta.
930+ htlcExpiryHeight := blockHeight + DefaultLoopInOnChainCltvDelta
931+
932+ // Ensure that the deposit doesn't expire before the htlc.
933+ if depositExpiryHeight < htlcExpiryHeight + DepositHtlcDelta {
934+ return false
935+ }
936+
937+ return true
938+ }
939+
904940// DeduceSwapAmount calculates the swap amount based on the selected amount and
905941// the total deposit amount. It checks if the selected amount leaves a dust
906942// change output or exceeds the total deposits value. Note that if the selected
0 commit comments