-
Notifications
You must be signed in to change notification settings - Fork 19
/
trades.go
271 lines (230 loc) · 8.21 KB
/
trades.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
// 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"
"github.com/jackc/pgx/v4"
)
const tradesFilterDateColumn = "synthetic_time"
type Trades struct {
*ConnectionSource
trades []*entities.Trade
}
var tradesOrdering = TableOrdering{
ColumnOrdering{Name: "synthetic_time", Sorting: ASC},
}
func NewTrades(connectionSource *ConnectionSource) *Trades {
t := &Trades{
ConnectionSource: connectionSource,
}
return t
}
func (ts *Trades) Flush(ctx context.Context) ([]*entities.Trade, error) {
rows := make([][]interface{}, 0, len(ts.trades))
for _, t := range ts.trades {
rows = append(rows, []interface{}{
t.SyntheticTime,
t.TxHash,
t.VegaTime,
t.SeqNum,
t.ID,
t.MarketID,
t.Price,
t.Size,
t.Buyer,
t.Seller,
t.Aggressor,
t.BuyOrder,
t.SellOrder,
t.Type,
t.BuyerMakerFee,
t.BuyerInfrastructureFee,
t.BuyerLiquidityFee,
t.SellerMakerFee,
t.SellerInfrastructureFee,
t.SellerLiquidityFee,
t.BuyerAuctionBatch,
t.SellerAuctionBatch,
t.BuyerMakerFeeReferralDiscount,
t.BuyerInfrastructureFeeReferralDiscount,
t.BuyerLiquidityFeeReferralDiscount,
t.BuyerMakerFeeVolumeDiscount,
t.BuyerInfrastructureFeeVolumeDiscount,
t.BuyerLiquidityFeeVolumeDiscount,
t.SellerMakerFeeReferralDiscount,
t.SellerInfrastructureFeeReferralDiscount,
t.SellerLiquidityFeeReferralDiscount,
t.SellerMakerFeeVolumeDiscount,
t.SellerInfrastructureFeeVolumeDiscount,
t.SellerLiquidityFeeVolumeDiscount,
})
}
defer metrics.StartSQLQuery("Trades", "Flush")()
if rows != nil {
copyCount, err := ts.Connection.CopyFrom(
ctx,
pgx.Identifier{"trades"},
[]string{
"synthetic_time", "tx_hash", "vega_time", "seq_num", "id", "market_id", "price", "size", "buyer", "seller",
"aggressor", "buy_order", "sell_order", "type", "buyer_maker_fee", "buyer_infrastructure_fee",
"buyer_liquidity_fee", "seller_maker_fee", "seller_infrastructure_fee", "seller_liquidity_fee",
"buyer_auction_batch", "seller_auction_batch", "buyer_maker_fee_referral_discount", "buyer_infrastructure_fee_referral_discount",
"buyer_liquidity_fee_referral_discount", "buyer_maker_fee_volume_discount", "buyer_infrastructure_fee_volume_discount", "buyer_liquidity_fee_volume_discount",
"seller_maker_fee_referral_discount", "seller_infrastructure_fee_referral_discount", "seller_liquidity_fee_referral_discount",
"seller_maker_fee_volume_discount", "seller_infrastructure_fee_volume_discount", "seller_liquidity_fee_volume_discount",
},
pgx.CopyFromRows(rows),
)
if err != nil {
return nil, fmt.Errorf("failed to copy trades into database:%w", err)
}
if copyCount != int64(len(rows)) {
return nil, fmt.Errorf("copied %d trade rows into the database, expected to copy %d", copyCount, len(rows))
}
}
flushed := ts.trades
ts.trades = nil
return flushed, nil
}
func (ts *Trades) Add(t *entities.Trade) error {
ts.trades = append(ts.trades, t)
return nil
}
func (ts *Trades) List(ctx context.Context,
marketIDs []entities.MarketID,
partyIDs []entities.PartyID,
orderIDs []entities.OrderID,
pagination entities.CursorPagination,
dateRange entities.DateRange,
) ([]entities.Trade, entities.PageInfo, error) {
args := []interface{}{}
conditions := []string{}
if len(marketIDs) > 0 {
markets := make([][]byte, 0)
for _, m := range marketIDs {
bs, err := m.Bytes()
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("received invalid market ID: %w", err)
}
markets = append(markets, bs)
}
conditions = append(conditions, fmt.Sprintf("market_id = ANY(%s::bytea[])", nextBindVar(&args, markets)))
}
if len(partyIDs) > 0 {
parties := make([][]byte, 0)
for _, p := range partyIDs {
bs, err := p.Bytes()
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("received invalid party ID: %w", err)
}
parties = append(parties, bs)
}
bindVar := nextBindVar(&args, parties)
conditions = append(conditions, fmt.Sprintf("(buyer = ANY(%s::bytea[]) or seller = ANY(%s::bytea[]))", bindVar, bindVar))
}
if len(orderIDs) > 0 {
orders := make([][]byte, 0)
for _, o := range orderIDs {
bs, err := o.Bytes()
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("received invalid order ID: %w", err)
}
orders = append(orders, bs)
}
bindVar := nextBindVar(&args, orders)
conditions = append(conditions, fmt.Sprintf("(buy_order = ANY(%s::bytea[]) or sell_order = ANY(%s::bytea[]))", bindVar, bindVar))
}
query := `SELECT * from trades`
first := true
if len(conditions) > 0 {
query = fmt.Sprintf("%s WHERE %s", query, strings.Join(conditions, " AND "))
first = false
}
query, args = filterDateRange(query, tradesFilterDateColumn, dateRange, first, args...)
trades, pageInfo, err := ts.queryTradesWithCursorPagination(ctx, query, args, pagination)
if err != nil {
return nil, pageInfo, fmt.Errorf("failed to get trade by market:%w", err)
}
return trades, pageInfo, nil
}
func (ts *Trades) GetLastTradeByMarket(ctx context.Context, market string) ([]entities.Trade, error) {
query := `SELECT * from trades WHERE market_id=$1`
args := []interface{}{entities.MarketID(market)}
defer metrics.StartSQLQuery("Trades", "GetByMarket")()
trades, err := ts.queryTrades(ctx, query, args)
if err != nil {
return nil, fmt.Errorf("failed to get trade by market:%w", err)
}
return trades, nil
}
func (ts *Trades) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Trade, error) {
defer metrics.StartSQLQuery("Trades", "GetByTxHash")()
query := `SELECT * from trades WHERE tx_hash=$1`
var trades []entities.Trade
err := pgxscan.Select(ctx, ts.Connection, &trades, query, txHash)
if err != nil {
return nil, fmt.Errorf("querying trades: %w", err)
}
return trades, nil
}
func (ts *Trades) queryTrades(ctx context.Context, query string, args []interface{}) ([]entities.Trade, error) {
query, args = queryTradesLast(query, []string{"synthetic_time"}, args...)
var trades []entities.Trade
err := pgxscan.Select(ctx, ts.Connection, &trades, query, args...)
if err != nil {
return nil, fmt.Errorf("querying trades: %w", err)
}
return trades, nil
}
func queryTradesLast(query string, orderColumns []string, args ...interface{}) (string, []interface{}) {
ordering := "DESC"
sbOrderBy := strings.Builder{}
if len(orderColumns) > 0 {
sbOrderBy.WriteString("ORDER BY")
sep := ""
for _, column := range orderColumns {
sbOrderBy.WriteString(fmt.Sprintf("%s %s %s", sep, column, ordering))
sep = ","
}
}
var paging string
paging = fmt.Sprintf("%sOFFSET %s ", paging, nextBindVar(&args, 0))
paging = fmt.Sprintf("%sLIMIT %s ", paging, nextBindVar(&args, 1))
query = fmt.Sprintf("%s %s %s", query, sbOrderBy.String(), paging)
return query, args
}
func (ts *Trades) queryTradesWithCursorPagination(ctx context.Context, query string, args []interface{}, pagination entities.CursorPagination) ([]entities.Trade, entities.PageInfo, error) {
var (
err error
pageInfo entities.PageInfo
)
query, args, err = PaginateQuery[entities.TradeCursor](query, args, tradesOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
var trades []entities.Trade
err = pgxscan.Select(ctx, ts.Connection, &trades, query, args...)
if err != nil {
return trades, pageInfo, fmt.Errorf("querying trades: %w", err)
}
trades, pageInfo = entities.PageEntities[*v2.TradeEdge](trades, pagination)
return trades, pageInfo, nil
}