@@ -22,7 +22,7 @@ import (
2222 "github.com/lightningnetwork/lnd/lnwallet/chainfee"
2323)
2424
25- const outpointSeparator = ";"
25+ const OutpointSeparator = ";"
2626
2727var (
2828 // ErrInvalidOutpoint is returned when an outpoint contains the outpoint
@@ -88,6 +88,11 @@ type Querier interface {
8888 // DepositsForSwapHash retrieves all deposits for a given swap hash.
8989 DepositsForSwapHash (ctx context.Context ,
9090 swapHash []byte ) ([]sqlc.DepositsForSwapHashRow , error )
91+
92+ // OverrideSelectedSwapAmount updates the selected swap amount for
93+ // a given swap hash.
94+ OverrideSelectedSwapAmount (ctx context.Context ,
95+ params sqlc.OverrideSelectedSwapAmountParams ) error
9196}
9297
9398// BaseDB is the interface that contains all the queries generated by sqlc for
@@ -226,11 +231,16 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
226231 return errors .New ("loop-in must have at least one deposit" )
227232 }
228233
234+ amountRequested := int64 (loopIn .TotalDepositAmount ())
235+ if loopIn .SelectedAmount > 0 {
236+ amountRequested = int64 (loopIn .SelectedAmount )
237+ }
238+
229239 swapArgs := sqlc.InsertSwapParams {
230240 SwapHash : loopIn .SwapHash [:],
231241 Preimage : loopIn .SwapPreimage [:],
232242 InitiationTime : loopIn .InitiationTime ,
233- AmountRequested : int64 ( loopIn . TotalDepositAmount ()) ,
243+ AmountRequested : amountRequested ,
234244 CltvExpiry : loopIn .HtlcCltvExpiry ,
235245 MaxSwapFee : int64 (loopIn .MaxSwapFee ),
236246 InitiationHeight : int32 (loopIn .InitiationHeight ),
@@ -250,13 +260,13 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
250260 // If so, we reject the loop-in to prevent potential issues with
251261 // parsing.
252262 for _ , outpoint := range loopIn .DepositOutpoints {
253- if strings .Contains (outpoint , outpointSeparator ) {
263+ if strings .Contains (outpoint , OutpointSeparator ) {
254264 return ErrInvalidOutpoint
255265 }
256266 }
257267
258268 joinedOutpoints := strings .Join (
259- loopIn .DepositOutpoints , outpointSeparator ,
269+ loopIn .DepositOutpoints , OutpointSeparator ,
260270 )
261271 staticAddressLoopInParams := sqlc.InsertStaticAddressLoopInParams {
262272 SwapHash : loopIn .SwapHash [:],
@@ -266,6 +276,7 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
266276 HtlcTimeoutSweepAddress : loopIn .HtlcTimeoutSweepAddress .String (),
267277 HtlcTxFeeRateSatKw : int64 (loopIn .HtlcTxFeeRate ),
268278 DepositOutpoints : joinedOutpoints ,
279+ SelectedAmount : int64 (loopIn .SelectedAmount ),
269280 PaymentTimeoutSeconds : int32 (loopIn .PaymentTimeoutSeconds ),
270281 }
271282
@@ -350,6 +361,27 @@ func (s *SqlStore) UpdateLoopIn(ctx context.Context,
350361 )
351362}
352363
364+ func (s * SqlStore ) BatchUpdateSelectedSwapAmounts (ctx context.Context ,
365+ updateAmounts map [lntypes.Hash ]btcutil.Amount ) error {
366+
367+ return s .baseDB .ExecTx (ctx , loopdb .NewSqlWriteOpts (),
368+ func (q Querier ) error {
369+ for swapHash , amount := range updateAmounts {
370+ err := q .OverrideSelectedSwapAmount (
371+ ctx , sqlc.OverrideSelectedSwapAmountParams {
372+ SwapHash : swapHash [:],
373+ SelectedAmount : int64 (amount ),
374+ },
375+ )
376+ if err != nil {
377+ return err
378+ }
379+ }
380+
381+ return nil
382+ })
383+ }
384+
353385// IsStored returns true if a swap with the given hash is stored in the
354386// database, false otherwise.
355387func (s * SqlStore ) IsStored (ctx context.Context , swapHash lntypes.Hash ) (bool ,
@@ -478,7 +510,7 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
478510 }
479511
480512 depositOutpoints := strings .Split (
481- swap .DepositOutpoints , outpointSeparator ,
513+ swap .DepositOutpoints , OutpointSeparator ,
482514 )
483515
484516 timeoutAddressString := swap .HtlcTimeoutSweepAddress
@@ -548,6 +580,7 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
548580 LastHop : swap .LastHop ,
549581 QuotedSwapFee : btcutil .Amount (swap .QuotedSwapFeeSatoshis ),
550582 DepositOutpoints : depositOutpoints ,
583+ SelectedAmount : btcutil .Amount (swap .SelectedAmount ),
551584 HtlcTxFeeRate : chainfee .SatPerKWeight (
552585 swap .HtlcTxFeeRateSatKw ,
553586 ),
0 commit comments