Skip to content

Commit ae9e3d3

Browse files
committed
Add api to vtxo repo to get all vtxos with pubkey & Fixes
1 parent b1978dc commit ae9e3d3

File tree

7 files changed

+136
-29
lines changed

7 files changed

+136
-29
lines changed

server/internal/core/domain/round_repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type VtxoRepository interface {
3939
GetAllSweepableVtxos(ctx context.Context) ([]Vtxo, error)
4040
GetSpendableVtxosWithPubKey(ctx context.Context, pubkey string) ([]Vtxo, error)
4141
GetAll(ctx context.Context) ([]Vtxo, error)
42+
GetAllVtxosWithPubKey(ctx context.Context, pubkey string) ([]Vtxo, []Vtxo, error)
4243
UpdateExpireAt(ctx context.Context, vtxos []VtxoKey, expireAt int64) error
4344
Close()
4445
}

server/internal/infrastructure/db/badger/vtxo_repo.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,27 @@ func (r *vtxoRepository) UpdateExpireAt(ctx context.Context, vtxos []domain.Vtxo
181181
return err
182182
}
183183

184+
func (r *vtxoRepository) GetAllVtxosWithPubKey(
185+
ctx context.Context, pubkey string,
186+
) ([]domain.Vtxo, []domain.Vtxo, error) {
187+
query := badgerhold.Where("PubKey").Eq(pubkey)
188+
vtxos, err := r.findVtxos(ctx, query)
189+
if err != nil {
190+
return nil, nil, err
191+
}
192+
193+
spentVtxos := make([]domain.Vtxo, 0, len(vtxos))
194+
unspentVtxos := make([]domain.Vtxo, 0, len(vtxos))
195+
for _, vtxo := range vtxos {
196+
if vtxo.Spent || vtxo.Swept {
197+
spentVtxos = append(spentVtxos, vtxo)
198+
} else {
199+
unspentVtxos = append(unspentVtxos, vtxo)
200+
}
201+
}
202+
return unspentVtxos, spentVtxos, nil
203+
}
204+
184205
func (r *vtxoRepository) Close() {
185206
r.store.Close()
186207
}

server/internal/infrastructure/db/sqlite/sqlc/queries/db.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/internal/infrastructure/db/sqlite/sqlc/queries/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/internal/infrastructure/db/sqlite/sqlc/queries/query.sql.go

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

server/internal/infrastructure/db/sqlite/sqlc/query.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ SELECT
9999
FROM vtxo v2
100100
JOIN tx_request req2 ON req2.id = v2.request_id
101101
WHERE req2.round_id = r.id
102-
)
102+
) as tx_req_inputs_amount
103103
) AS total_forfeit_amount,
104104
(
105105
SELECT COALESCE(COUNT(v3.txid), 0)
@@ -115,7 +115,7 @@ SELECT
115115
JOIN tx_request req4 ON req4.id = rr.request_id
116116
WHERE req4.round_id = r.id
117117
AND (rr.onchain_address = '' OR rr.onchain_address IS NULL)
118-
)
118+
) AS tx_req_outputs_amount
119119
) AS total_batch_amount,
120120
(
121121
SELECT COUNT(*)
@@ -260,4 +260,7 @@ SELECT * FROM market_hour ORDER BY updated_at DESC LIMIT 1;
260260
-- name: SelectTreeTxsWithRoundTxid :many
261261
SELECT tx.* FROM round
262262
LEFT OUTER JOIN tx ON round.id=tx.round_id
263-
WHERE round.txid = ? AND tx.type = 'tree'
263+
WHERE round.txid = ? AND tx.type = 'tree';
264+
265+
-- name: SelectVtxosWithPubkey :many
266+
SELECT sqlc.embed(vtxo) FROM vtxo WHERE pubkey = ?;

server/internal/infrastructure/db/sqlite/vtxo_repo.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/ark-network/ark/server/internal/infrastructure/db/sqlite/sqlc/queries"
1010
)
1111

12-
type vxtoRepository struct {
12+
type vtxoRepository struct {
1313
db *sql.DB
1414
querier *queries.Queries
1515
}
@@ -23,17 +23,17 @@ func NewVtxoRepository(config ...interface{}) (domain.VtxoRepository, error) {
2323
return nil, fmt.Errorf("cannot open vtxo repository: invalid config")
2424
}
2525

26-
return &vxtoRepository{
26+
return &vtxoRepository{
2727
db: db,
2828
querier: queries.New(db),
2929
}, nil
3030
}
3131

32-
func (v *vxtoRepository) Close() {
32+
func (v *vtxoRepository) Close() {
3333
_ = v.db.Close()
3434
}
3535

36-
func (v *vxtoRepository) AddVtxos(ctx context.Context, vtxos []domain.Vtxo) error {
36+
func (v *vtxoRepository) AddVtxos(ctx context.Context, vtxos []domain.Vtxo) error {
3737
txBody := func(querierWithTx *queries.Queries) error {
3838
for i := range vtxos {
3939
vtxo := vtxos[i]
@@ -64,7 +64,7 @@ func (v *vxtoRepository) AddVtxos(ctx context.Context, vtxos []domain.Vtxo) erro
6464
return execTx(ctx, v.db, txBody)
6565
}
6666

67-
func (v *vxtoRepository) GetAllSweepableVtxos(ctx context.Context) ([]domain.Vtxo, error) {
67+
func (v *vtxoRepository) GetAllSweepableVtxos(ctx context.Context) ([]domain.Vtxo, error) {
6868
res, err := v.querier.SelectSweepableVtxos(ctx)
6969
if err != nil {
7070
return nil, err
@@ -77,7 +77,7 @@ func (v *vxtoRepository) GetAllSweepableVtxos(ctx context.Context) ([]domain.Vtx
7777
return readRows(rows)
7878
}
7979

80-
func (v *vxtoRepository) GetAllNonRedeemedVtxos(ctx context.Context, pubkey string) ([]domain.Vtxo, []domain.Vtxo, error) {
80+
func (v *vtxoRepository) GetAllNonRedeemedVtxos(ctx context.Context, pubkey string) ([]domain.Vtxo, []domain.Vtxo, error) {
8181
withPubkey := len(pubkey) > 0
8282

8383
var rows []queries.Vtxo
@@ -120,7 +120,7 @@ func (v *vxtoRepository) GetAllNonRedeemedVtxos(ctx context.Context, pubkey stri
120120
return unspentVtxos, spentVtxos, nil
121121
}
122122

123-
func (v *vxtoRepository) GetVtxos(ctx context.Context, outpoints []domain.VtxoKey) ([]domain.Vtxo, error) {
123+
func (v *vtxoRepository) GetVtxos(ctx context.Context, outpoints []domain.VtxoKey) ([]domain.Vtxo, error) {
124124
vtxos := make([]domain.Vtxo, 0, len(outpoints))
125125
for _, o := range outpoints {
126126
res, err := v.querier.SelectVtxoByOutpoint(
@@ -149,7 +149,7 @@ func (v *vxtoRepository) GetVtxos(ctx context.Context, outpoints []domain.VtxoKe
149149
return vtxos, nil
150150
}
151151

152-
func (v *vxtoRepository) GetAll(ctx context.Context) ([]domain.Vtxo, error) {
152+
func (v *vtxoRepository) GetAll(ctx context.Context) ([]domain.Vtxo, error) {
153153
res, err := v.querier.SelectAllVtxos(ctx)
154154
if err != nil {
155155
return nil, err
@@ -162,7 +162,7 @@ func (v *vxtoRepository) GetAll(ctx context.Context) ([]domain.Vtxo, error) {
162162
return readRows(rows)
163163
}
164164

165-
func (v *vxtoRepository) GetVtxosForRound(ctx context.Context, txid string) ([]domain.Vtxo, error) {
165+
func (v *vtxoRepository) GetVtxosForRound(ctx context.Context, txid string) ([]domain.Vtxo, error) {
166166
res, err := v.querier.SelectVtxosByRoundTxid(ctx, txid)
167167
if err != nil {
168168
return nil, err
@@ -175,7 +175,7 @@ func (v *vxtoRepository) GetVtxosForRound(ctx context.Context, txid string) ([]d
175175
return readRows(rows)
176176
}
177177

178-
func (v *vxtoRepository) GetSpendableVtxosWithPubKey(ctx context.Context, pubkey string) ([]domain.Vtxo, error) {
178+
func (v *vtxoRepository) GetSpendableVtxosWithPubKey(ctx context.Context, pubkey string) ([]domain.Vtxo, error) {
179179
rows, err := v.querier.GetSpendableVtxosWithPubKey(ctx, pubkey)
180180
if err != nil {
181181
return nil, err
@@ -204,7 +204,7 @@ func (v *vxtoRepository) GetSpendableVtxosWithPubKey(ctx context.Context, pubkey
204204
return vtxos, nil
205205
}
206206

207-
func (v *vxtoRepository) RedeemVtxos(ctx context.Context, vtxos []domain.VtxoKey) error {
207+
func (v *vtxoRepository) RedeemVtxos(ctx context.Context, vtxos []domain.VtxoKey) error {
208208
txBody := func(querierWithTx *queries.Queries) error {
209209
for _, vtxo := range vtxos {
210210
if err := querierWithTx.MarkVtxoAsRedeemed(
@@ -224,7 +224,7 @@ func (v *vxtoRepository) RedeemVtxos(ctx context.Context, vtxos []domain.VtxoKey
224224
return execTx(ctx, v.db, txBody)
225225
}
226226

227-
func (v *vxtoRepository) SpendVtxos(ctx context.Context, vtxos []domain.VtxoKey, txid string) error {
227+
func (v *vtxoRepository) SpendVtxos(ctx context.Context, vtxos []domain.VtxoKey, txid string) error {
228228
txBody := func(querierWithTx *queries.Queries) error {
229229
for _, vtxo := range vtxos {
230230
if err := querierWithTx.MarkVtxoAsSpent(
@@ -245,7 +245,7 @@ func (v *vxtoRepository) SpendVtxos(ctx context.Context, vtxos []domain.VtxoKey,
245245
return execTx(ctx, v.db, txBody)
246246
}
247247

248-
func (v *vxtoRepository) SweepVtxos(ctx context.Context, vtxos []domain.VtxoKey) error {
248+
func (v *vtxoRepository) SweepVtxos(ctx context.Context, vtxos []domain.VtxoKey) error {
249249
txBody := func(querierWithTx *queries.Queries) error {
250250
for _, vtxo := range vtxos {
251251
if err := querierWithTx.MarkVtxoAsSwept(
@@ -265,7 +265,7 @@ func (v *vxtoRepository) SweepVtxos(ctx context.Context, vtxos []domain.VtxoKey)
265265
return execTx(ctx, v.db, txBody)
266266
}
267267

268-
func (v *vxtoRepository) UpdateExpireAt(ctx context.Context, vtxos []domain.VtxoKey, expireAt int64) error {
268+
func (v *vtxoRepository) UpdateExpireAt(ctx context.Context, vtxos []domain.VtxoKey, expireAt int64) error {
269269
txBody := func(querierWithTx *queries.Queries) error {
270270
for _, vtxo := range vtxos {
271271
if err := querierWithTx.UpdateVtxoExpireAt(
@@ -286,6 +286,37 @@ func (v *vxtoRepository) UpdateExpireAt(ctx context.Context, vtxos []domain.Vtxo
286286
return execTx(ctx, v.db, txBody)
287287
}
288288

289+
func (v *vtxoRepository) GetAllVtxosWithPubKey(
290+
ctx context.Context, pubkey string,
291+
) ([]domain.Vtxo, []domain.Vtxo, error) {
292+
res, err := v.querier.SelectVtxosWithPubkey(ctx, pubkey)
293+
if err != nil {
294+
return nil, nil, err
295+
}
296+
rows := make([]queries.Vtxo, 0, len(res))
297+
for _, row := range res {
298+
rows = append(rows, row.Vtxo)
299+
}
300+
301+
vtxos, err := readRows(rows)
302+
if err != nil {
303+
return nil, nil, err
304+
}
305+
306+
unspentVtxos := make([]domain.Vtxo, 0)
307+
spentVtxos := make([]domain.Vtxo, 0)
308+
309+
for _, vtxo := range vtxos {
310+
if vtxo.Spent || vtxo.Swept {
311+
spentVtxos = append(spentVtxos, vtxo)
312+
} else {
313+
unspentVtxos = append(unspentVtxos, vtxo)
314+
}
315+
}
316+
317+
return unspentVtxos, spentVtxos, nil
318+
}
319+
289320
func rowToVtxo(row queries.Vtxo) domain.Vtxo {
290321
return domain.Vtxo{
291322
VtxoKey: domain.VtxoKey{

0 commit comments

Comments
 (0)