@@ -878,67 +878,81 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
878
878
879
879
// Now fetch all the AMP sub invoices that were settled since
880
880
// the provided index.
881
- //
882
- // TODO(ziggie): Add limit query for AMP sub invoices.
883
- ampInvoices , err := i .db .FetchSettledAMPSubInvoices (
884
- ctx , sqlc.FetchSettledAMPSubInvoicesParams {
885
- SettleIndexGet : sqldb .SQLInt64 (idx + 1 ),
886
- },
887
- )
888
- if err != nil {
889
- return err
890
- }
881
+ err = queryWithLimit (int64 (idx ), false , i .opts .paginationLimit ,
882
+ //nolint:ll
883
+ func (offset int64 ) (int , int64 , error ) {
884
+ ampInvoices , err := i .db .FetchSettledAMPSubInvoices (
885
+ ctx , sqlc.FetchSettledAMPSubInvoicesParams {
886
+ SettleIndexGet : sqldb .SQLInt64 (
887
+ offset ,
888
+ ),
889
+ NumLimit : int32 (i .opts .paginationLimit ),
890
+ },
891
+ )
892
+ if err != nil {
893
+ return 0 , 0 , err
894
+ }
891
895
892
- for _ , ampInvoice := range ampInvoices {
893
- // Convert the row to a sqlc.Invoice so we can use the
894
- // existing fetchInvoiceData function.
895
- sqlInvoice := sqlc.Invoice {
896
- ID : ampInvoice .ID ,
897
- Hash : ampInvoice .Hash ,
898
- Preimage : ampInvoice .Preimage ,
899
- SettleIndex : ampInvoice .AmpSettleIndex ,
900
- SettledAt : ampInvoice .AmpSettledAt ,
901
- Memo : ampInvoice .Memo ,
902
- AmountMsat : ampInvoice .AmountMsat ,
903
- CltvDelta : ampInvoice .CltvDelta ,
904
- Expiry : ampInvoice .Expiry ,
905
- PaymentAddr : ampInvoice .PaymentAddr ,
906
- PaymentRequest : ampInvoice .PaymentRequest ,
907
- State : ampInvoice .State ,
908
- AmountPaidMsat : ampInvoice .AmountPaidMsat ,
909
- IsAmp : ampInvoice .IsAmp ,
910
- IsHodl : ampInvoice .IsHodl ,
911
- IsKeysend : ampInvoice .IsKeysend ,
912
- CreatedAt : ampInvoice .CreatedAt .UTC (),
913
- }
896
+ if len (ampInvoices ) == 0 {
897
+ return 0 , 0 , nil
898
+ }
914
899
915
- // Fetch the state and HTLCs for this AMP sub invoice.
916
- _ , invoice , err := fetchInvoiceData (
917
- ctx , db , sqlInvoice ,
918
- (* [32 ]byte )(ampInvoice .SetID ), true ,
919
- )
920
- if err != nil {
921
- return fmt .Errorf ("unable to fetch " +
922
- "AMP invoice(id=%d) from db: %w" ,
923
- ampInvoice .ID , err )
924
- }
900
+ for _ , ampInvoice := range ampInvoices {
901
+ // Convert the row to a sqlc.Invoice so we can use the
902
+ // existing fetchInvoiceData function.
903
+ sqlInvoice := sqlc.Invoice {
904
+ ID : ampInvoice .ID ,
905
+ Hash : ampInvoice .Hash ,
906
+ Preimage : ampInvoice .Preimage ,
907
+ SettleIndex : ampInvoice .AmpSettleIndex ,
908
+ SettledAt : ampInvoice .AmpSettledAt ,
909
+ Memo : ampInvoice .Memo ,
910
+ AmountMsat : ampInvoice .AmountMsat ,
911
+ CltvDelta : ampInvoice .CltvDelta ,
912
+ Expiry : ampInvoice .Expiry ,
913
+ PaymentAddr : ampInvoice .PaymentAddr ,
914
+ PaymentRequest : ampInvoice .PaymentRequest ,
915
+ State : ampInvoice .State ,
916
+ AmountPaidMsat : ampInvoice .AmountPaidMsat ,
917
+ IsAmp : ampInvoice .IsAmp ,
918
+ IsHodl : ampInvoice .IsHodl ,
919
+ IsKeysend : ampInvoice .IsKeysend ,
920
+ CreatedAt : ampInvoice .CreatedAt .UTC (),
921
+ }
922
+
923
+ // Fetch the state and HTLCs for this AMP sub invoice.
924
+ _ , invoice , err := fetchInvoiceData (
925
+ ctx , db , sqlInvoice ,
926
+ (* [32 ]byte )(ampInvoice .SetID ), true ,
927
+ )
928
+ if err != nil {
929
+ return 0 , 0 , fmt .Errorf ("unable to fetch " +
930
+ "AMP invoice(id=%d) from db: %w" ,
931
+ ampInvoice .ID , err )
932
+ }
925
933
926
- invoices = append (invoices , * invoice )
934
+ invoices = append (invoices , * invoice )
927
935
928
- processedCount ++
929
- if time .Since (lastLogTime ) >=
930
- invoiceProgressLogInterval {
936
+ processedCount ++
937
+ if time .Since (lastLogTime ) >=
938
+ invoiceProgressLogInterval {
931
939
932
- log .Debugf ("Processed %d settled invoices " +
933
- "including AMP sub invoices which " +
934
- "have a settle index greater than %v" ,
935
- processedCount , idx )
940
+ log .Debugf ("Processed %d settled invoices " +
941
+ "including AMP sub invoices which " +
942
+ "have a settle index greater than %v" ,
943
+ processedCount , idx )
936
944
937
- lastLogTime = time .Now ()
938
- }
939
- }
945
+ lastLogTime = time .Now ()
946
+ }
947
+ }
940
948
941
- return nil
949
+ lastInv := ampInvoices [len (ampInvoices )- 1 ]
950
+ LastIndexOffset := lastInv .SettleIndex .Int64
951
+
952
+ return len (ampInvoices ), LastIndexOffset , nil
953
+ })
954
+
955
+ return err
942
956
}, func () {
943
957
invoices = nil
944
958
})
0 commit comments