This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
/
tx_repository.go
209 lines (183 loc) · 6.02 KB
/
tx_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package bux
import (
"context"
"errors"
"github.com/libsv/go-bt"
"github.com/mrz1836/go-datastore"
)
// getTransactionByID will get the model from a given transaction ID
func getTransactionByID(ctx context.Context, xPubID, txID string, opts ...ModelOps) (*Transaction, error) {
// Construct an empty tx
tx := emptyTx(opts...)
tx.ID = txID
tx.XPubID = xPubID
// Get the record
if err := Get(ctx, tx, nil, false, defaultDatabaseReadTimeout, false); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
return tx, nil
}
// getTransactions will get all the transactions with the given conditions
func getTransactions(ctx context.Context, metadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams, opts ...ModelOps,
) ([]*Transaction, error) {
modelItems := make([]*Transaction, 0)
if err := getModelsByConditions(ctx, ModelTransaction, &modelItems, metadata, conditions, queryParams, opts...); err != nil {
return nil, err
}
return modelItems, nil
}
// getTransactionsAggregate will get a count of all transactions per aggregate column with the given conditions
func getTransactionsAggregate(ctx context.Context, metadata *Metadata, conditions *map[string]interface{},
aggregateColumn string, opts ...ModelOps,
) (map[string]interface{}, error) {
modelItems := make([]*Transaction, 0)
results, err := getModelsAggregateByConditions(
ctx, ModelTransaction, &modelItems, metadata, conditions, aggregateColumn, opts...,
)
if err != nil {
return nil, err
}
return results, nil
}
// getTransactionsCount will get a count of all the transactions with the given conditions
func getTransactionsCount(ctx context.Context, metadata *Metadata, conditions *map[string]interface{},
opts ...ModelOps,
) (int64, error) {
return getModelCountByConditions(ctx, ModelTransaction, Transaction{}, metadata, conditions, opts...)
}
// getTransactionsCountByXpubID will get the count of all the models for a given xpub ID
func getTransactionsCountByXpubID(ctx context.Context, xPubID string, metadata *Metadata,
conditions *map[string]interface{}, opts ...ModelOps,
) (int64, error) {
dbConditions := processDBConditions(xPubID, conditions, metadata)
return getTransactionsCountInternal(ctx, dbConditions, opts...)
}
// getTransactionsByXpubID will get all the models for a given xpub ID
func getTransactionsByXpubID(ctx context.Context, xPubID string,
metadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams, opts ...ModelOps,
) ([]*Transaction, error) {
dbConditions := processDBConditions(xPubID, conditions, metadata)
return getTransactionsInternal(ctx, dbConditions, xPubID, queryParams, opts...)
}
func processDBConditions(xPubID string, conditions *map[string]interface{},
metadata *Metadata,
) map[string]interface{} {
dbConditions := map[string]interface{}{
"$or": []map[string]interface{}{{
"xpub_in_ids": xPubID,
}, {
"xpub_out_ids": xPubID,
}},
}
// check for direction query
if conditions != nil && (*conditions)["direction"] != nil {
direction := (*conditions)["direction"].(string)
if direction == string(TransactionDirectionIn) {
dbConditions["xpub_output_value"] = map[string]interface{}{
xPubID: map[string]interface{}{
"$gt": 0,
},
}
} else if direction == string(TransactionDirectionOut) {
dbConditions["xpub_output_value"] = map[string]interface{}{
xPubID: map[string]interface{}{
"$lt": 0,
},
}
} else if direction == string(TransactionDirectionReconcile) {
dbConditions["xpub_output_value"] = map[string]interface{}{
xPubID: 0,
}
}
delete(*conditions, "direction")
}
if metadata != nil && len(*metadata) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
for key, value := range *metadata {
condition := map[string]interface{}{
"$or": []map[string]interface{}{{
metadataField: Metadata{
key: value,
},
}, {
xPubMetadataField: map[string]interface{}{
xPubID: Metadata{
key: value,
},
},
}},
}
and = append(and, condition)
}
dbConditions["$and"] = and
}
if conditions != nil && len(*conditions) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
and = append(and, *conditions)
dbConditions["$and"] = and
}
return dbConditions
}
// getTransactionsInternal get all transactions for the given conditions
// NOTE: this function should only be used internally, it allows to query the whole transaction table
func getTransactionsInternal(ctx context.Context, conditions map[string]interface{}, xPubID string,
queryParams *datastore.QueryParams, opts ...ModelOps,
) ([]*Transaction, error) {
var models []Transaction
if err := getModels(
ctx,
NewBaseModel(ModelTransaction, opts...).Client().Datastore(),
&models,
conditions,
queryParams,
defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
// Loop and enrich
transactions := make([]*Transaction, 0)
for index := range models {
models[index].enrich(ModelTransaction, opts...)
models[index].XPubID = xPubID
tx := &models[index]
transactions = append(transactions, tx)
}
return transactions, nil
}
// getTransactionsCountInternal get a count of all transactions for the given conditions
func getTransactionsCountInternal(ctx context.Context, conditions map[string]interface{},
opts ...ModelOps,
) (int64, error) {
count, err := getModelCount(
ctx,
NewBaseModel(ModelNameEmpty, opts...).Client().Datastore(),
Transaction{},
conditions,
defaultDatabaseReadTimeout,
)
if err != nil {
return 0, err
}
return count, nil
}
func getTransactionByHex(ctx context.Context, hex string, opts ...ModelOps) (*Transaction, error) {
btTx, err := bt.NewTxFromString(hex)
if err != nil {
return nil, err
}
return getTransactionByID(ctx, "", btTx.GetTxID(), opts...)
}