@@ -862,8 +862,24 @@ 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+ log .Debugf ("Skipping deposit %s as it expires before " +
876+ "the htlc" , d .OutPoint .String ())
877+
878+ continue
879+ }
880+
881+ deposits = append (deposits , d )
882+ }
867883
868884 // Sort the deposits by amount in descending order, then by
869885 // blocks-until-expiry in ascending order.
@@ -901,6 +917,25 @@ func SelectDeposits(targetAmount btcutil.Amount, deposits []*deposit.Deposit,
901917 selectedAmount , targetAmount )
902918}
903919
920+ // IsSwappable checks if a deposit is swappable. It returns true if the deposit
921+ // is not expired and the htlc is not too close to expiry.
922+ func IsSwappable (confirmationHeight , blockHeight , csvExpiry uint32 ) bool {
923+ // The deposit expiry height is the confirmation height plus the csv
924+ // expiry.
925+ depositExpiryHeight := confirmationHeight + csvExpiry
926+
927+ // The htlc expiry height is the current height plus the htlc
928+ // cltv delta.
929+ htlcExpiryHeight := blockHeight + DefaultLoopInOnChainCltvDelta
930+
931+ // Ensure that the deposit doesn't expire before the htlc.
932+ if depositExpiryHeight < htlcExpiryHeight + DepositHtlcDelta {
933+ return false
934+ }
935+
936+ return true
937+ }
938+
904939// DeduceSwapAmount calculates the swap amount based on the selected amount and
905940// the total deposit amount. It checks if the selected amount leaves a dust
906941// change output or exceeds the total deposits value. Note that if the selected
0 commit comments