-
Notifications
You must be signed in to change notification settings - Fork 19
/
fees_stats.go
363 lines (308 loc) · 11.3 KB
/
fees_stats.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sqlstore
import (
"context"
"errors"
"fmt"
"strings"
"time"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
"code.vegaprotocol.io/vega/libs/num"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/georgysavva/scany/pgxscan"
"golang.org/x/exp/maps"
)
var feesStatsByPartyColumn = []string{
"market_id",
"asset_id",
"party_id",
"epoch_seq",
"total_rewards_received",
"referees_discount_applied",
"volume_discount_applied",
"total_maker_fees_received",
"vega_time",
}
type FeesStats struct {
*ConnectionSource
}
func NewFeesStats(src *ConnectionSource) *FeesStats {
return &FeesStats{
ConnectionSource: src,
}
}
func (rfs *FeesStats) AddFeesStats(ctx context.Context, stats *entities.FeesStats) error {
defer metrics.StartSQLQuery("FeesStats", "AddFeesStats")()
ctx, err := rfs.WithTransaction(ctx)
if err != nil {
return fmt.Errorf("could not initialise transaction: %w", err)
}
if _, err := rfs.Connection.Exec(
ctx,
`INSERT INTO fees_stats(
market_id,
asset_id,
epoch_seq,
total_rewards_received,
referrer_rewards_generated,
referees_discount_applied,
volume_discount_applied,
total_maker_fees_received,
maker_fees_generated,
vega_time
) values ($1,$2,$3,$4,$5,$6,$7,$8, $9, $10)`,
stats.MarketID,
stats.AssetID,
stats.EpochSeq,
stats.TotalRewardsReceived,
stats.ReferrerRewardsGenerated,
stats.RefereesDiscountApplied,
stats.VolumeDiscountApplied,
stats.TotalMakerFeesReceived,
stats.MakerFeesGenerated,
stats.VegaTime,
); err != nil {
_ = rfs.Rollback(ctx)
return fmt.Errorf("could not execute insertion in `fees_stats`: %w", err)
}
partiesStats := computePartiesStats(stats)
batcher := NewListBatcher[*feesStatsForPartyRow]("fees_stats_by_party", feesStatsByPartyColumn)
for _, s := range partiesStats {
batcher.Add(s)
}
if _, err := batcher.Flush(ctx, rfs.Connection); err != nil {
_ = rfs.Rollback(ctx)
return fmt.Errorf("could not flush the batch insertion in `fees_stats_by_party`: %w", err)
}
if err := rfs.Commit(ctx); err != nil {
return fmt.Errorf("an error occurred during transactions commit: %w", err)
}
return nil
}
func (rfs *FeesStats) StatsForParty(ctx context.Context, partyID entities.PartyID, assetID *entities.AssetID, fromEpoch, toEpoch *uint64) ([]entities.FeesStatsForParty, error) {
defer metrics.StartSQLQuery("FeesStats", "StatsForParty")()
var args []interface{}
where := []string{
fmt.Sprintf("party_id = %s", nextBindVar(&args, partyID)),
}
if assetID != nil {
where = append(where, fmt.Sprintf("asset_id = %s", nextBindVar(&args, *assetID)))
}
if fromEpoch == nil && toEpoch == nil {
where = append(where, "epoch_seq = (SELECT MAX(epoch_seq) FROM fees_stats)")
}
if fromEpoch != nil {
where = append(where, fmt.Sprintf("epoch_seq >= %s", nextBindVar(&args, *fromEpoch)))
}
if toEpoch != nil {
where = append(where, fmt.Sprintf("epoch_seq <= %s", nextBindVar(&args, *toEpoch)))
}
query := fmt.Sprintf(`select
asset_id,
sum(total_maker_fees_received) as total_maker_fees_received,
sum(referees_discount_applied) as referees_discount_applied,
sum(total_rewards_received) as total_rewards_received,
sum(volume_discount_applied) as volume_discount_applied
from fees_stats_by_party where %s group by party_id, asset_id order by asset_id`,
strings.Join(where, " and "),
)
var rows []feesStatsForPartyRow
if err := pgxscan.Select(ctx, rfs.Connection, &rows, query, args...); err != nil {
return nil, err
}
stats := make([]entities.FeesStatsForParty, 0, len(rows))
for _, row := range rows {
stats = append(stats, entities.FeesStatsForParty{
AssetID: row.AssetID,
TotalRewardsReceived: row.TotalRewardsReceived.String(),
RefereesDiscountApplied: row.RefereesDiscountApplied.String(),
VolumeDiscountApplied: row.VolumeDiscountApplied.String(),
TotalMakerFeesReceived: row.TotalMakerFeesReceived.String(),
})
}
return stats, nil
}
func (rfs *FeesStats) GetFeesStats(ctx context.Context, marketID *entities.MarketID, assetID *entities.AssetID, epochSeq *uint64, partyID *string) (*entities.FeesStats, error) {
defer metrics.StartSQLQuery("FeesStats", "GetFeesStats")()
var (
stats []entities.FeesStats
err error
args []interface{}
)
if marketID != nil && assetID != nil {
return nil, errors.New("only a marketID or assetID should be provided")
}
query := `SELECT * FROM fees_stats`
where := make([]string, 0)
if epochSeq != nil {
where = append(where, fmt.Sprintf("epoch_seq = %s", nextBindVar(&args, *epochSeq)))
}
if assetID != nil {
where = append(where, fmt.Sprintf("asset_id = %s", nextBindVar(&args, *assetID)))
}
if marketID != nil {
where = append(where, fmt.Sprintf("market_id = %s", nextBindVar(&args, *marketID)))
}
if epochSeq == nil { // we want the most recent stat so order and limit the query
where = append(where, "epoch_seq = (SELECT MAX(epoch_seq) FROM fees_stats)")
}
if partyFilter := getPartyFilter(partyID); partyFilter != "" {
where = append(where, partyFilter)
}
if len(where) > 0 {
query = fmt.Sprintf("%s where %s", query, strings.Join(where, " and "))
}
query = fmt.Sprintf("%s order by market_id, asset_id, epoch_seq desc", query)
if err = pgxscan.Select(ctx, rfs.Connection, &stats, query, args...); err != nil {
return nil, err
}
if len(stats) == 0 {
return nil, errors.New("no fees stats found")
}
// The query returns the full JSON object and doesn't filter for the party,
// it only matches on the records where the json object contains the party.
if partyID != nil {
stats[0].TotalRewardsReceived = filterPartyAmounts(stats[0].TotalRewardsReceived, *partyID)
stats[0].ReferrerRewardsGenerated = filterReferrerRewardsGenerated(stats[0].ReferrerRewardsGenerated, *partyID)
stats[0].TotalMakerFeesReceived = filterPartyAmounts(stats[0].TotalMakerFeesReceived, *partyID)
stats[0].MakerFeesGenerated = filterMakerFeesGenerated(stats[0].MakerFeesGenerated, *partyID)
stats[0].RefereesDiscountApplied = filterPartyAmounts(stats[0].RefereesDiscountApplied, *partyID)
stats[0].VolumeDiscountApplied = filterPartyAmounts(stats[0].VolumeDiscountApplied, *partyID)
}
return &stats[0], err
}
func filterPartyAmounts(totalRewardsReceived []*eventspb.PartyAmount, party string) []*eventspb.PartyAmount {
filteredEntries := make([]*eventspb.PartyAmount, 0)
for _, reward := range totalRewardsReceived {
if strings.EqualFold(reward.Party, party) {
filteredEntries = append(filteredEntries, reward)
}
}
return filteredEntries
}
func filterReferrerRewardsGenerated(rewardsGenerated []*eventspb.ReferrerRewardsGenerated, partyID string) []*eventspb.ReferrerRewardsGenerated {
filteredEntries := make([]*eventspb.ReferrerRewardsGenerated, 0)
for _, reward := range rewardsGenerated {
if strings.EqualFold(reward.Referrer, partyID) {
filteredEntries = append(filteredEntries, reward)
}
}
return filteredEntries
}
func filterMakerFeesGenerated(makerFeesGenerated []*eventspb.MakerFeesGenerated, partyID string) []*eventspb.MakerFeesGenerated {
filteredEntries := make([]*eventspb.MakerFeesGenerated, 0)
for _, reward := range makerFeesGenerated {
if strings.EqualFold(reward.Taker, partyID) {
filteredEntries = append(filteredEntries, reward)
}
}
return filteredEntries
}
func getPartyFilter(partyID *string) string {
builder := strings.Builder{}
if partyID == nil {
return ""
}
builder.WriteString("(")
builder.WriteString(fmt.Sprintf(
`total_rewards_received @> '[{"party_id":"%s"}]'`, *partyID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`referrer_rewards_generated @> '[{"referrer":"%s"}]'`, *partyID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`referees_discount_applied @> '[{"party_id":"%s"}]'`, *partyID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`volume_discount_applied @> '[{"party_id":"%s"}]'`, *partyID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`total_maker_fees_received @> '[{"party_id":"%s"}]'`, *partyID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`maker_fees_generated @> '[{"taker":"%s"}]'`, *partyID,
))
builder.WriteString(")")
return builder.String()
}
func computePartiesStats(stats *entities.FeesStats) []*feesStatsForPartyRow {
partiesStats := map[string]*feesStatsForPartyRow{}
for _, t := range stats.TotalMakerFeesReceived {
partyStats := ensurePartyStats(stats, partiesStats, t)
partyStats.TotalMakerFeesReceived = partyStats.TotalMakerFeesReceived.Add(num.MustDecimalFromString(t.Amount))
}
for _, t := range stats.TotalRewardsReceived {
partyStats := ensurePartyStats(stats, partiesStats, t)
partyStats.TotalRewardsReceived = partyStats.TotalRewardsReceived.Add(num.MustDecimalFromString(t.Amount))
}
for _, t := range stats.VolumeDiscountApplied {
partyStats := ensurePartyStats(stats, partiesStats, t)
partyStats.VolumeDiscountApplied = partyStats.VolumeDiscountApplied.Add(num.MustDecimalFromString(t.Amount))
}
for _, t := range stats.RefereesDiscountApplied {
partyStats := ensurePartyStats(stats, partiesStats, t)
partyStats.RefereesDiscountApplied = partyStats.RefereesDiscountApplied.Add(num.MustDecimalFromString(t.Amount))
}
return maps.Values(partiesStats)
}
func ensurePartyStats(stats *entities.FeesStats, partiesStats map[string]*feesStatsForPartyRow, t *eventspb.PartyAmount) *feesStatsForPartyRow {
partyStats, ok := partiesStats[t.Party]
if !ok {
partyStats = &feesStatsForPartyRow{
MarketID: stats.MarketID,
AssetID: stats.AssetID,
PartyID: entities.PartyID(t.Party),
EpochSeq: stats.EpochSeq,
TotalRewardsReceived: num.DecimalZero(),
RefereesDiscountApplied: num.DecimalZero(),
VolumeDiscountApplied: num.DecimalZero(),
TotalMakerFeesReceived: num.DecimalZero(),
VegaTime: stats.VegaTime,
}
partiesStats[t.Party] = partyStats
}
return partyStats
}
type feesStatsForPartyRow struct {
MarketID entities.MarketID
AssetID entities.AssetID
PartyID entities.PartyID
EpochSeq uint64
TotalRewardsReceived num.Decimal
RefereesDiscountApplied num.Decimal
VolumeDiscountApplied num.Decimal
TotalMakerFeesReceived num.Decimal
VegaTime time.Time
}
func (f feesStatsForPartyRow) ToRow() []interface{} {
return []any{
f.MarketID,
f.AssetID,
f.PartyID,
f.EpochSeq,
f.TotalRewardsReceived,
f.RefereesDiscountApplied,
f.VolumeDiscountApplied,
f.TotalMakerFeesReceived,
f.VegaTime,
}
}