@@ -84,6 +84,10 @@ type Querier interface {
8484 // hash.
8585 DepositIDsForSwapHash (ctx context.Context ,
8686 swapHash []byte ) ([][]byte , error )
87+
88+ // DepositsForSwapHash retrieves all deposits for a given swap hash.
89+ DepositsForSwapHash (ctx context.Context ,
90+ swapHash []byte ) ([]sqlc.DepositsForSwapHashRow , error )
8791}
8892
8993// BaseDB is the interface that contains all the queries generated by sqlc for
@@ -122,10 +126,15 @@ func (s *SqlStore) GetLoopInByHash(ctx context.Context,
122126
123127 var (
124128 err error
125- row sqlc.GetStaticAddressLoopInSwapRow
129+ swap sqlc.GetStaticAddressLoopInSwapRow
126130 updates []sqlc.StaticAddressSwapUpdate
127131 )
128- row , err = s .baseDB .GetStaticAddressLoopInSwap (ctx , swapHash [:])
132+ swap , err = s .baseDB .GetStaticAddressLoopInSwap (ctx , swapHash [:])
133+ if err != nil {
134+ return nil , err
135+ }
136+
137+ deposits , err := s .baseDB .DepositsForSwapHash (ctx , swapHash [:])
129138 if err != nil {
130139 return nil , err
131140 }
@@ -135,9 +144,7 @@ func (s *SqlStore) GetLoopInByHash(ctx context.Context,
135144 return nil , err
136145 }
137146
138- return toStaticAddressLoopIn (
139- ctx , s .network , row , updates ,
140- )
147+ return toStaticAddressLoopIn (ctx , s .network , swap , deposits , updates )
141148}
142149
143150// GetStaticAddressLoopInSwapsByStates returns all static address loop-ins from
@@ -165,6 +172,11 @@ func (s *SqlStore) GetStaticAddressLoopInSwapsByStates(ctx context.Context,
165172
166173 loopIns := make ([]* StaticAddressLoopIn , 0 , len (rows ))
167174 for _ , row := range rows {
175+ deposits , err := s .baseDB .DepositsForSwapHash (ctx , row .SwapHash )
176+ if err != nil {
177+ return nil , err
178+ }
179+
168180 updates , err = s .baseDB .GetLoopInSwapUpdates (
169181 ctx , row .SwapHash ,
170182 )
@@ -174,7 +186,7 @@ func (s *SqlStore) GetStaticAddressLoopInSwapsByStates(ctx context.Context,
174186
175187 loopIn , err = toStaticAddressLoopIn (
176188 ctx , s .network , sqlc .GetStaticAddressLoopInSwapRow (row ),
177- updates ,
189+ deposits , updates ,
178190 )
179191 if err != nil {
180192 return nil , err
@@ -206,6 +218,14 @@ func toStrings(states []fsm.StateType) []string {
206218func (s * SqlStore ) CreateLoopIn (ctx context.Context ,
207219 loopIn * StaticAddressLoopIn ) error {
208220
221+ if loopIn == nil {
222+ return errors .New ("loop-in cannot be nil" )
223+ }
224+
225+ if len (loopIn .Deposits ) == 0 {
226+ return errors .New ("loop-in must have at least one deposit" )
227+ }
228+
209229 swapArgs := sqlc.InsertSwapParams {
210230 SwapHash : loopIn .SwapHash [:],
211231 Preimage : loopIn .SwapPreimage [:],
@@ -274,8 +294,24 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
274294 return err
275295 }
276296
297+ // Map each deposit to the swap hash in the
298+ // deposit_to_swap table. This allows us to track which
299+ // deposits are used for which swaps.
300+ for _ , d := range loopIn .Deposits {
301+ err = q .MapDepositToSwap (
302+ ctx , sqlc.MapDepositToSwapParams {
303+ DepositID : d .ID [:],
304+ SwapHash : loopIn .SwapHash [:],
305+ },
306+ )
307+ if err != nil {
308+ return err
309+ }
310+ }
311+
277312 return q .InsertStaticAddressMetaUpdate (ctx , updateArgs )
278- })
313+ },
314+ )
279315}
280316
281317// UpdateLoopIn updates the loop-in in the database.
@@ -407,44 +443,45 @@ func (s *SqlStore) DepositIDsForSwapHash(ctx context.Context,
407443
408444// toStaticAddressLoopIn converts sql rows to an instant out struct.
409445func toStaticAddressLoopIn (_ context.Context , network * chaincfg.Params ,
410- row sqlc.GetStaticAddressLoopInSwapRow ,
446+ swap sqlc.GetStaticAddressLoopInSwapRow ,
447+ deposits []sqlc.DepositsForSwapHashRow ,
411448 updates []sqlc.StaticAddressSwapUpdate ) (* StaticAddressLoopIn , error ) {
412449
413- swapHash , err := lntypes .MakeHash (row .SwapHash )
450+ swapHash , err := lntypes .MakeHash (swap .SwapHash )
414451 if err != nil {
415452 return nil , err
416453 }
417454
418- swapPreImage , err := lntypes .MakePreimage (row .Preimage )
455+ swapPreImage , err := lntypes .MakePreimage (swap .Preimage )
419456 if err != nil {
420457 return nil , err
421458 }
422459
423- clientKey , err := btcec .ParsePubKey (row .SenderScriptPubkey )
460+ clientKey , err := btcec .ParsePubKey (swap .SenderScriptPubkey )
424461 if err != nil {
425462 return nil , err
426463 }
427464
428- serverKey , err := btcec .ParsePubKey (row .ReceiverScriptPubkey )
465+ serverKey , err := btcec .ParsePubKey (swap .ReceiverScriptPubkey )
429466 if err != nil {
430467 return nil , err
431468 }
432469
433470 var htlcTimeoutSweepTxHash * chainhash.Hash
434- if row .HtlcTimeoutSweepTxID .Valid {
471+ if swap .HtlcTimeoutSweepTxID .Valid {
435472 htlcTimeoutSweepTxHash , err = chainhash .NewHashFromStr (
436- row .HtlcTimeoutSweepTxID .String ,
473+ swap .HtlcTimeoutSweepTxID .String ,
437474 )
438475 if err != nil {
439476 return nil , err
440477 }
441478 }
442479
443480 depositOutpoints := strings .Split (
444- row .DepositOutpoints , outpointSeparator ,
481+ swap .DepositOutpoints , outpointSeparator ,
445482 )
446483
447- timeoutAddressString := row .HtlcTimeoutSweepAddress
484+ timeoutAddressString := swap .HtlcTimeoutSweepAddress
448485 var timeoutAddress btcutil.Address
449486 if timeoutAddressString != "" {
450487 timeoutAddress , err = btcutil .DecodeAddress (
@@ -455,33 +492,68 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
455492 }
456493 }
457494
495+ depositList := make ([]* deposit.Deposit , 0 , len (deposits ))
496+ for _ , d := range deposits {
497+ id := deposit.ID {}
498+ err = id .FromByteSlice (d .DepositID )
499+ if err != nil {
500+ return nil , err
501+ }
502+
503+ sqlcDeposit := sqlc.Deposit {
504+ DepositID : id [:],
505+ TxHash : d .TxHash ,
506+ Amount : d .Amount ,
507+ OutIndex : d .OutIndex ,
508+ ConfirmationHeight : d .ConfirmationHeight ,
509+ TimeoutSweepPkScript : d .TimeoutSweepPkScript ,
510+ ExpirySweepTxid : d .ExpirySweepTxid ,
511+ FinalizedWithdrawalTx : d .FinalizedWithdrawalTx ,
512+ }
513+
514+ sqlcDepositUpdate := sqlc.DepositUpdate {
515+ DepositID : id [:],
516+ UpdateState : d .UpdateState .String ,
517+ UpdateTimestamp : d .UpdateTimestamp .Time ,
518+ }
519+ deposit , err := deposit .ToDeposit (
520+ sqlcDeposit , sqlcDepositUpdate ,
521+ )
522+ if err != nil {
523+ return nil , err
524+ }
525+
526+ depositList = append (depositList , deposit )
527+ }
528+
458529 loopIn := & StaticAddressLoopIn {
459530 SwapHash : swapHash ,
460531 SwapPreimage : swapPreImage ,
461- HtlcCltvExpiry : row .CltvExpiry ,
462- MaxSwapFee : btcutil .Amount (row .MaxSwapFee ),
463- InitiationHeight : uint32 (row .InitiationHeight ),
464- InitiationTime : row .InitiationTime ,
532+ HtlcCltvExpiry : swap .CltvExpiry ,
533+ MaxSwapFee : btcutil .Amount (swap .MaxSwapFee ),
534+ InitiationHeight : uint32 (swap .InitiationHeight ),
535+ InitiationTime : swap .InitiationTime ,
465536 ProtocolVersion : version .AddressProtocolVersion (
466- row .ProtocolVersion ,
537+ swap .ProtocolVersion ,
467538 ),
468- Label : row .Label ,
539+ Label : swap .Label ,
469540 ClientPubkey : clientKey ,
470541 ServerPubkey : serverKey ,
471542 HtlcKeyLocator : keychain.KeyLocator {
472- Family : keychain .KeyFamily (row .ClientKeyFamily ),
473- Index : uint32 (row .ClientKeyIndex ),
543+ Family : keychain .KeyFamily (swap .ClientKeyFamily ),
544+ Index : uint32 (swap .ClientKeyIndex ),
474545 },
475- SwapInvoice : row .SwapInvoice ,
476- PaymentTimeoutSeconds : uint32 (row .PaymentTimeoutSeconds ),
477- LastHop : row .LastHop ,
478- QuotedSwapFee : btcutil .Amount (row .QuotedSwapFeeSatoshis ),
546+ SwapInvoice : swap .SwapInvoice ,
547+ PaymentTimeoutSeconds : uint32 (swap .PaymentTimeoutSeconds ),
548+ LastHop : swap .LastHop ,
549+ QuotedSwapFee : btcutil .Amount (swap .QuotedSwapFeeSatoshis ),
479550 DepositOutpoints : depositOutpoints ,
480551 HtlcTxFeeRate : chainfee .SatPerKWeight (
481- row .HtlcTxFeeRateSatKw ,
552+ swap .HtlcTxFeeRateSatKw ,
482553 ),
483554 HtlcTimeoutSweepAddress : timeoutAddress ,
484555 HtlcTimeoutSweepTxHash : htlcTimeoutSweepTxHash ,
556+ Deposits : depositList ,
485557 }
486558
487559 if len (updates ) > 0 {
0 commit comments