9
9
"github.com/ark-network/ark/server/internal/infrastructure/db/sqlite/sqlc/queries"
10
10
)
11
11
12
- type vxtoRepository struct {
12
+ type vtxoRepository struct {
13
13
db * sql.DB
14
14
querier * queries.Queries
15
15
}
@@ -23,17 +23,17 @@ func NewVtxoRepository(config ...interface{}) (domain.VtxoRepository, error) {
23
23
return nil , fmt .Errorf ("cannot open vtxo repository: invalid config" )
24
24
}
25
25
26
- return & vxtoRepository {
26
+ return & vtxoRepository {
27
27
db : db ,
28
28
querier : queries .New (db ),
29
29
}, nil
30
30
}
31
31
32
- func (v * vxtoRepository ) Close () {
32
+ func (v * vtxoRepository ) Close () {
33
33
_ = v .db .Close ()
34
34
}
35
35
36
- func (v * vxtoRepository ) AddVtxos (ctx context.Context , vtxos []domain.Vtxo ) error {
36
+ func (v * vtxoRepository ) AddVtxos (ctx context.Context , vtxos []domain.Vtxo ) error {
37
37
txBody := func (querierWithTx * queries.Queries ) error {
38
38
for i := range vtxos {
39
39
vtxo := vtxos [i ]
@@ -64,7 +64,7 @@ func (v *vxtoRepository) AddVtxos(ctx context.Context, vtxos []domain.Vtxo) erro
64
64
return execTx (ctx , v .db , txBody )
65
65
}
66
66
67
- func (v * vxtoRepository ) GetAllSweepableVtxos (ctx context.Context ) ([]domain.Vtxo , error ) {
67
+ func (v * vtxoRepository ) GetAllSweepableVtxos (ctx context.Context ) ([]domain.Vtxo , error ) {
68
68
res , err := v .querier .SelectSweepableVtxos (ctx )
69
69
if err != nil {
70
70
return nil , err
@@ -77,7 +77,7 @@ func (v *vxtoRepository) GetAllSweepableVtxos(ctx context.Context) ([]domain.Vtx
77
77
return readRows (rows )
78
78
}
79
79
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 ) {
81
81
withPubkey := len (pubkey ) > 0
82
82
83
83
var rows []queries.Vtxo
@@ -120,7 +120,7 @@ func (v *vxtoRepository) GetAllNonRedeemedVtxos(ctx context.Context, pubkey stri
120
120
return unspentVtxos , spentVtxos , nil
121
121
}
122
122
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 ) {
124
124
vtxos := make ([]domain.Vtxo , 0 , len (outpoints ))
125
125
for _ , o := range outpoints {
126
126
res , err := v .querier .SelectVtxoByOutpoint (
@@ -149,7 +149,7 @@ func (v *vxtoRepository) GetVtxos(ctx context.Context, outpoints []domain.VtxoKe
149
149
return vtxos , nil
150
150
}
151
151
152
- func (v * vxtoRepository ) GetAll (ctx context.Context ) ([]domain.Vtxo , error ) {
152
+ func (v * vtxoRepository ) GetAll (ctx context.Context ) ([]domain.Vtxo , error ) {
153
153
res , err := v .querier .SelectAllVtxos (ctx )
154
154
if err != nil {
155
155
return nil , err
@@ -162,7 +162,7 @@ func (v *vxtoRepository) GetAll(ctx context.Context) ([]domain.Vtxo, error) {
162
162
return readRows (rows )
163
163
}
164
164
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 ) {
166
166
res , err := v .querier .SelectVtxosByRoundTxid (ctx , txid )
167
167
if err != nil {
168
168
return nil , err
@@ -175,7 +175,7 @@ func (v *vxtoRepository) GetVtxosForRound(ctx context.Context, txid string) ([]d
175
175
return readRows (rows )
176
176
}
177
177
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 ) {
179
179
rows , err := v .querier .GetSpendableVtxosWithPubKey (ctx , pubkey )
180
180
if err != nil {
181
181
return nil , err
@@ -204,7 +204,7 @@ func (v *vxtoRepository) GetSpendableVtxosWithPubKey(ctx context.Context, pubkey
204
204
return vtxos , nil
205
205
}
206
206
207
- func (v * vxtoRepository ) RedeemVtxos (ctx context.Context , vtxos []domain.VtxoKey ) error {
207
+ func (v * vtxoRepository ) RedeemVtxos (ctx context.Context , vtxos []domain.VtxoKey ) error {
208
208
txBody := func (querierWithTx * queries.Queries ) error {
209
209
for _ , vtxo := range vtxos {
210
210
if err := querierWithTx .MarkVtxoAsRedeemed (
@@ -224,7 +224,7 @@ func (v *vxtoRepository) RedeemVtxos(ctx context.Context, vtxos []domain.VtxoKey
224
224
return execTx (ctx , v .db , txBody )
225
225
}
226
226
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 {
228
228
txBody := func (querierWithTx * queries.Queries ) error {
229
229
for _ , vtxo := range vtxos {
230
230
if err := querierWithTx .MarkVtxoAsSpent (
@@ -245,7 +245,7 @@ func (v *vxtoRepository) SpendVtxos(ctx context.Context, vtxos []domain.VtxoKey,
245
245
return execTx (ctx , v .db , txBody )
246
246
}
247
247
248
- func (v * vxtoRepository ) SweepVtxos (ctx context.Context , vtxos []domain.VtxoKey ) error {
248
+ func (v * vtxoRepository ) SweepVtxos (ctx context.Context , vtxos []domain.VtxoKey ) error {
249
249
txBody := func (querierWithTx * queries.Queries ) error {
250
250
for _ , vtxo := range vtxos {
251
251
if err := querierWithTx .MarkVtxoAsSwept (
@@ -265,7 +265,7 @@ func (v *vxtoRepository) SweepVtxos(ctx context.Context, vtxos []domain.VtxoKey)
265
265
return execTx (ctx , v .db , txBody )
266
266
}
267
267
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 {
269
269
txBody := func (querierWithTx * queries.Queries ) error {
270
270
for _ , vtxo := range vtxos {
271
271
if err := querierWithTx .UpdateVtxoExpireAt (
@@ -286,6 +286,37 @@ func (v *vxtoRepository) UpdateExpireAt(ctx context.Context, vtxos []domain.Vtxo
286
286
return execTx (ctx , v .db , txBody )
287
287
}
288
288
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
+
289
320
func rowToVtxo (row queries.Vtxo ) domain.Vtxo {
290
321
return domain.Vtxo {
291
322
VtxoKey : domain.VtxoKey {
0 commit comments