-
Notifications
You must be signed in to change notification settings - Fork 19
/
ledgerentry_filter.go
178 lines (146 loc) · 5.23 KB
/
ledgerentry_filter.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
package sqlstore
import (
"errors"
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/protos/vega"
)
var (
ErrLedgerEntryFilterForParty = errors.New("filtering ledger entries should be limited to a single party")
ErrLedgerEntryExportForParty = errors.New("exporting ledger entries should be limited to a single party")
)
// Return an SQL query string and corresponding bind arguments to return
// ledger entries rows resulting from different filter options.
func filterLedgerEntriesQuery(filter *entities.LedgerEntryFilter) ([3]string, []interface{}, error) {
err := handlePartiesFiltering(filter)
if err != nil {
return [3]string{}, nil, err
}
var args []interface{}
filterQueries := [3]string{}
// FromAccount filter
fromAccountDBQuery, nargs, err := accountFilterToDBQuery(filter.FromAccountFilter, &args, "account_from_")
if err != nil {
return [3]string{}, nil, fmt.Errorf("error parsing fromAccount filter values: %w", err)
}
args = *nargs
// ToAccount filter
toAccountDBQuery, nargs, err := accountFilterToDBQuery(filter.ToAccountFilter, &args, "account_to_")
if err != nil {
return [3]string{}, nil, fmt.Errorf("error parsing fromAccount filter values: %w", err)
}
args = *nargs
// TransferTypeFilters
accountTransferTypeDBQuery := transferTypeFilterToDBQuery(filter.TransferTypes, &args)
filterQueries[0] = fromAccountDBQuery
filterQueries[1] = toAccountDBQuery
filterQueries[2] = accountTransferTypeDBQuery
return filterQueries, args, nil
}
// accountFilterToDBQuery creates a DB query section string from the given account filter values.
func accountFilterToDBQuery(af entities.AccountFilter, args *[]interface{}, prefix string) (string, *[]interface{}, error) {
var (
singleAccountFilter string
err error
)
// Asset filtering
if af.AssetID.String() != "" {
assetIDAsBytes, err := af.AssetID.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid asset id: %w", err)
}
singleAccountFilter = fmt.Sprintf("%sasset_id=%s", singleAccountFilter, nextBindVar(args, assetIDAsBytes))
}
// Party filtering
if len(af.PartyIDs) == 1 {
partyIDAsBytes, err := af.PartyIDs[0].Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid party id: %w", err)
}
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %sparty_id=%s`, singleAccountFilter, prefix, nextBindVar(args, partyIDAsBytes))
} else {
singleAccountFilter = fmt.Sprintf(`%sparty_id=%s`, prefix, nextBindVar(args, partyIDAsBytes))
}
}
// Market filtering
if len(af.MarketIDs) > 0 {
marketIds := make([][]byte, len(af.MarketIDs))
for i, market := range af.MarketIDs {
marketIds[i], err = market.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid market id: %w", err)
}
}
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %smarket_id=ANY(%s)`, singleAccountFilter, prefix, nextBindVar(args, marketIds))
} else {
singleAccountFilter = fmt.Sprintf(`%smarket_id=ANY(%s)`, prefix, nextBindVar(args, marketIds))
}
}
// Account types filtering
if len(af.AccountTypes) > 0 {
acTypes := getUniqueAccountTypes(af.AccountTypes)
if singleAccountFilter != "" {
singleAccountFilter = fmt.Sprintf(`%s AND %saccount_type=ANY(%s)`, singleAccountFilter, prefix, nextBindVar(args, acTypes))
} else {
singleAccountFilter = fmt.Sprintf(`%saccount_type=ANY(%s)`, prefix, nextBindVar(args, acTypes))
}
}
return singleAccountFilter, args, nil
}
func getUniqueAccountTypes(accountTypes []vega.AccountType) []vega.AccountType {
accountTypesList := []vega.AccountType{}
accountTypesMap := map[vega.AccountType]struct{}{}
for _, at := range accountTypes {
_, ok := accountTypesMap[at]
if ok {
continue
}
accountTypesMap[at] = struct{}{}
accountTypesList = append(accountTypesList, at)
}
return accountTypesList
}
func transferTypeFilterToDBQuery(transferTypeFilter []entities.LedgerMovementType, args *[]interface{}) string {
transferTypeFilterString := ""
if len(transferTypeFilter) > 0 {
transferTypesMap := map[entities.LedgerMovementType]struct{}{}
for _, transferType := range transferTypeFilter {
_, ok := transferTypesMap[transferType]
if ok {
continue
}
transferTypesMap[transferType] = struct{}{}
}
for v := range transferTypesMap {
_, ok := vega.TransferType_name[int32(v)]
if !ok {
continue
}
if transferTypeFilterString == "" {
transferTypeFilterString = fmt.Sprintf(`%stransfer_type=%s`, transferTypeFilterString, nextBindVar(args, v))
} else {
transferTypeFilterString = fmt.Sprintf(`%s OR transfer_type=%s`, transferTypeFilterString, nextBindVar(args, v))
}
}
}
return transferTypeFilterString
}
func handlePartiesFiltering(filter *entities.LedgerEntryFilter) error {
var partyIDFrom entities.PartyID
var partyIDTo entities.PartyID
if len(filter.FromAccountFilter.PartyIDs) > 1 || len(filter.ToAccountFilter.PartyIDs) > 1 {
return ErrLedgerEntryFilterForParty
}
if len(filter.FromAccountFilter.PartyIDs) > 0 {
partyIDFrom = filter.FromAccountFilter.PartyIDs[0]
}
if len(filter.ToAccountFilter.PartyIDs) > 0 {
partyIDTo = filter.ToAccountFilter.PartyIDs[0]
}
if partyIDFrom == "" && partyIDTo == "" {
return ErrLedgerEntryFilterForParty
}
return nil
}