-
Notifications
You must be signed in to change notification settings - Fork 19
/
paid_liquidity_fee_stats.go
136 lines (112 loc) · 4.04 KB
/
paid_liquidity_fee_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
// 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"
"fmt"
"strings"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/georgysavva/scany/pgxscan"
)
type PaidLiquidityFeesStats struct {
*ConnectionSource
}
func NewPaidLiquidityFeesStats(src *ConnectionSource) *PaidLiquidityFeesStats {
return &PaidLiquidityFeesStats{
ConnectionSource: src,
}
}
func (rfs *PaidLiquidityFeesStats) Add(ctx context.Context, stats *entities.PaidLiquidityFeesStats) error {
defer metrics.StartSQLQuery("PaidLiquidityFeesStats", "Add")()
// It's possible that a market closes in the same block as an end of epoch event.
// In this case, the market close event will cause a paid liquidity fee stats event to be sent
// as well as the epoch end event. In this case we just want to ignore the second event.
_, err := rfs.Connection.Exec(
ctx,
`INSERT INTO paid_liquidity_fees(
market_id,
asset_id,
epoch_seq,
total_fees_paid,
fees_paid_per_party,
vega_time
) values ($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING`,
stats.MarketID,
stats.AssetID,
stats.EpochSeq,
stats.TotalFeesPaid,
stats.FeesPerParty,
stats.VegaTime,
)
return err
}
func (lfs *PaidLiquidityFeesStats) List(
ctx context.Context,
marketID *entities.MarketID,
assetID *entities.AssetID,
epochSeq *uint64,
partyIDs []string,
pagination entities.CursorPagination,
) ([]entities.PaidLiquidityFeesStats, entities.PageInfo, error) {
defer metrics.StartSQLQuery("PaidLiquidityFeesStats", "List")()
var (
args []interface{}
pageInfo entities.PageInfo
)
query := `SELECT t.market_id, t.asset_id, t.epoch_seq, t.total_fees_paid, array_to_json(array_agg(j)) as fees_per_party
FROM paid_liquidity_fees t, jsonb_array_elements(t.fees_paid_per_party) j`
whereClauses := []string{}
if (marketID == nil || assetID == nil) && epochSeq == nil {
whereClauses = append(whereClauses, "epoch_seq = (SELECT MAX(epoch_seq) FROM paid_liquidity_fees)")
}
if epochSeq != nil {
whereClauses = append(whereClauses, fmt.Sprintf("epoch_seq = %s", nextBindVar(&args, *epochSeq)))
}
if marketID != nil {
whereClauses = append(whereClauses, fmt.Sprintf("market_id = %s", nextBindVar(&args, marketID)))
}
if assetID != nil {
whereClauses = append(whereClauses, fmt.Sprintf("asset_id = %s", nextBindVar(&args, assetID)))
}
if len(partyIDs) > 0 {
parties := strings.Builder{}
for i, party := range partyIDs {
if i > 0 {
parties.WriteString(",")
}
parties.WriteString(nextBindVar(&args, party))
}
whereClauses = append(whereClauses, fmt.Sprintf("j->>'party' IN (%s)", parties.String()))
}
var whereStr string
if len(whereClauses) > 0 {
whereStr = " WHERE " + strings.Join(whereClauses, " AND ")
}
groupByStr := "GROUP BY market_id, asset_id, epoch_seq, vega_time"
query = fmt.Sprintf("%s %s %s", query, whereStr, groupByStr)
stats := []entities.PaidLiquidityFeesStats{}
query, args, err := PaginateQuery[entities.PaidLiquidityFeesStatsCursor](
query, args, paidLiquidityFeesStatsCursorOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
if err := pgxscan.Select(ctx, lfs.Connection, &stats, query, args...); err != nil {
return nil, pageInfo, err
}
stats, pageInfo = entities.PageEntities[*v2.PaidLiquidityFeesEdge](stats, pagination)
return stats, pageInfo, nil
}