Skip to content

Commit 1caa9c6

Browse files
committed
loopdb: static address deposit queries
1 parent 8c1ec10 commit 1caa9c6

File tree

7 files changed

+182
-1
lines changed

7 files changed

+182
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS deposits;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- deposits stores historic and unspent static address outputs.
2+
CREATE TABLE IF NOT EXISTS deposits (
3+
-- id is the auto-incrementing primary key for a static address.
4+
id INTEGER PRIMARY KEY,
5+
6+
tx_hash BYTEA NOT NULL,
7+
8+
tx_index INT NOT NULL,
9+
10+
amount BIGINT NOT NULL,
11+
12+
confirmation_height BIGINT NOT NULL,
13+
14+
time_out_sweep_pk_script BYTEA NOT NULL,
15+
16+
spent BOOLEAN NOT NULL
17+
);

loopdb/sqlc/models.go

Lines changed: 10 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/static_addresses.sql

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,24 @@ INSERT INTO static_addresses (
3434

3535
-- name: DeleteStaticAddress :exec
3636
DELETE FROM static_addresses
37-
WHERE address_id=$1;
37+
WHERE address_id=$1;
38+
39+
-- name: AllDeposits :many
40+
SELECT * FROM deposits;
41+
42+
-- name: CreateDeposit :exec
43+
INSERT INTO deposits (
44+
tx_hash,
45+
tx_index,
46+
amount,
47+
confirmation_height,
48+
time_out_sweep_pk_script,
49+
spent
50+
) VALUES (
51+
$1,
52+
$2,
53+
$3,
54+
$4,
55+
$5,
56+
$6
57+
);

loopdb/sqlc/static_addresses.sql.go

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

staticaddr/sql_store.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/btcsuite/btcd/btcec/v2"
88
"github.com/btcsuite/btcd/btcutil"
9+
"github.com/btcsuite/btcd/chaincfg/chainhash"
10+
"github.com/btcsuite/btcd/wire"
911
"github.com/jackc/pgx/v4"
1012
"github.com/lightninglabs/loop/loopdb"
1113
"github.com/lightninglabs/loop/loopdb/sqlc"
@@ -128,6 +130,61 @@ func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) (
128130
return result, nil
129131
}
130132

133+
// CreateDeposit creates a static address record in the database.
134+
func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
135+
createArgs := sqlc.CreateDepositParams{
136+
TxHash: deposit.Hash[:],
137+
TxIndex: int32(deposit.Index),
138+
Amount: int64(deposit.Value),
139+
ConfirmationHeight: deposit.ConfirmationHeight,
140+
TimeOutSweepPkScript: deposit.TimeOutSweepPkScript,
141+
Spent: deposit.Spent,
142+
}
143+
144+
return s.baseDB.Queries.CreateDeposit(ctx, createArgs)
145+
}
146+
147+
// AllDeposits retrieves all known deposits to our static address.
148+
func (s *SqlStore) AllDeposits(ctx context.Context) ([]Deposit, error) {
149+
deposits, err := s.baseDB.Queries.AllDeposits(ctx)
150+
if err != nil {
151+
return nil, err
152+
}
153+
154+
return s.toDeposits(deposits)
155+
}
156+
157+
func (s *SqlStore) toDeposits(rows []sqlc.Deposit) ([]Deposit, error) {
158+
var result []Deposit
159+
for _, row := range rows {
160+
deposit, err := s.toDeposit(row)
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
result = append(result, *deposit)
166+
}
167+
168+
return result, nil
169+
}
170+
171+
func (s *SqlStore) toDeposit(row sqlc.Deposit) (*Deposit, error) {
172+
hash, err := chainhash.NewHash(row.TxHash)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
return &Deposit{
178+
OutPoint: wire.OutPoint{
179+
Hash: *hash,
180+
Index: uint32(row.TxIndex),
181+
},
182+
Value: btcutil.Amount(row.Amount),
183+
ConfirmationHeight: row.ConfirmationHeight,
184+
Spent: row.Spent,
185+
}, nil
186+
}
187+
131188
// Close closes the database connection.
132189
func (s *SqlStore) Close() {
133190
s.baseDB.DB.Close()

0 commit comments

Comments
 (0)