Skip to content

Commit b4ebb19

Browse files
committed
loopdb+sweepbatcher: add GetParentBatch and TotalSwept calls
1 parent c094ad4 commit b4ebb19

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

loopdb/sqlc/batch.sql.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/batch.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ INSERT INTO sweeps (
6262
amt = $5,
6363
completed = $6;
6464

65+
-- name: GetParentBatch :one
66+
SELECT
67+
sweep_batches.*
68+
FROM
69+
sweep_batches
70+
JOIN
71+
sweeps ON sweep_batches.id = sweeps.batch_id
72+
WHERE
73+
sweeps.swap_hash = $1
74+
AND
75+
sweeps.completed = TRUE
76+
AND
77+
sweep_batches.confirmed = TRUE;
78+
79+
-- name: GetBatchSweptAmount :one
80+
SELECT
81+
SUM(amt) AS total
82+
FROM
83+
sweeps
84+
WHERE
85+
batch_id = $1
86+
AND
87+
completed = TRUE;
6588

6689
-- name: GetBatchSweeps :many
6790
SELECT

sweepbatcher/store.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ type BaseDB interface {
2222
GetBatchSweeps(ctx context.Context, batchID int32) (
2323
[]sqlc.GetBatchSweepsRow, error)
2424

25+
// GetBatchSweptAmount returns the total amount of sats swept by a
26+
// (confirmed) batch.
27+
GetBatchSweptAmount(ctx context.Context, batchID int32) (int64, error)
28+
2529
// GetSweepStatus returns true if the sweep has been completed.
2630
GetSweepStatus(ctx context.Context, swapHash []byte) (bool, error)
2731

32+
// GetParentBatch fetches the parent batch of a completed sweep.
33+
GetParentBatch(ctx context.Context, swapHash []byte) (sqlc.SweepBatch,
34+
error)
35+
2836
// GetSwapUpdates fetches all the updates for a swap.
2937
GetSwapUpdates(ctx context.Context, swapHash []byte) (
3038
[]sqlc.SwapUpdate, error)
@@ -148,6 +156,34 @@ func (s *SQLStore) FetchBatchSweeps(ctx context.Context, id int32) (
148156
return sweeps, nil
149157
}
150158

159+
// TotalSweptAmount returns the total amount swept by a (confirmed) batch.
160+
func (s *SQLStore) TotalSweptAmount(ctx context.Context, id int32) (
161+
btcutil.Amount, error) {
162+
163+
amt, err := s.baseDb.GetBatchSweptAmount(ctx, id)
164+
if err != nil {
165+
return 0, err
166+
}
167+
168+
return btcutil.Amount(amt), nil
169+
}
170+
171+
// GetParentBatch fetches the parent batch of a completed sweep.
172+
func (s *SQLStore) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
173+
*dbBatch, error) {
174+
175+
batch, err := s.baseDb.GetParentBatch(ctx, swapHash[:])
176+
if err != nil {
177+
return nil, err
178+
}
179+
180+
if err != nil {
181+
return nil, err
182+
}
183+
184+
return convertBatchRow(batch), nil
185+
}
186+
151187
// UpsertSweep inserts a sweep into the database, or updates an existing sweep
152188
// if it already exists.
153189
func (s *SQLStore) UpsertSweep(ctx context.Context, sweep *dbSweep) error {

sweepbatcher/store_mock.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"sort"
77

8+
"github.com/btcsuite/btcd/btcutil"
89
"github.com/lightningnetwork/lnd/lntypes"
910
)
1011

@@ -123,3 +124,44 @@ func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
123124
_, ok := s.sweeps[id]
124125
return ok
125126
}
127+
128+
// GetParentBatch returns the parent batch of a swap.
129+
func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
130+
*dbBatch, error) {
131+
132+
for _, sweep := range s.sweeps {
133+
if sweep.SwapHash == swapHash {
134+
batch, ok := s.batches[sweep.BatchID]
135+
if !ok {
136+
return nil, errors.New("batch not found")
137+
}
138+
return &batch, nil
139+
}
140+
}
141+
142+
return nil, errors.New("batch not found")
143+
}
144+
145+
// TotalSweptAmount returns the total amount of BTC that has been swept from a
146+
// batch.
147+
func (s *StoreMock) TotalSweptAmount(ctx context.Context, batchID int32) (
148+
btcutil.Amount, error) {
149+
150+
batch, ok := s.batches[batchID]
151+
if !ok {
152+
return 0, errors.New("batch not found")
153+
}
154+
155+
if batch.State != batchConfirmed && batch.State != batchClosed {
156+
return 0, nil
157+
}
158+
159+
var total btcutil.Amount
160+
for _, sweep := range s.sweeps {
161+
if sweep.BatchID == batchID {
162+
total += sweep.Amount
163+
}
164+
}
165+
166+
return 0, nil
167+
}

sweepbatcher/sweep_batcher.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ type BatcherStore interface {
7272
// GetSweepStatus returns the completed status of the sweep.
7373
GetSweepStatus(ctx context.Context, swapHash lntypes.Hash) (
7474
bool, error)
75+
76+
// GetParentBatch returns the parent batch of a (completed) sweep.
77+
GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
78+
*dbBatch, error)
79+
80+
// TotalSweptAmount returns the total amount swept by a (confirmed)
81+
// batch.
82+
TotalSweptAmount(ctx context.Context, id int32) (btcutil.Amount, error)
7583
}
7684

7785
// MuSig2SignSweep is a function that can be used to sign a sweep transaction

0 commit comments

Comments
 (0)