-
Notifications
You must be signed in to change notification settings - Fork 19
/
the_market_data_should_be.go
438 lines (413 loc) · 12.2 KB
/
the_market_data_should_be.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// 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 steps
import (
"fmt"
"strings"
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"github.com/cucumber/godog"
)
type MappedMD struct {
md types.MarketData
uintMap map[string]**num.Uint
u64Map map[string]*uint64
strMap map[string]*string
tMap map[string]*int64
i64Map map[string]*int64
tm *types.MarketTradingMode
tr *types.AuctionTrigger
et *types.AuctionTrigger
}
type ErrStack []error
func TheMarketDataShouldBe(engine Execution, mID string, data *godog.Table) error {
actual, err := engine.GetMarketData(mID)
if err != nil {
return err
}
// strictly parse first row to catch any columns that aren't expected
StrictParseTable(data, []string{}, []string{
"market",
"best bid price",
"best bid volume",
"best offer price",
"best offer volume",
"best static bid price",
"best static bid volume",
"best static offer price",
"best static offer volume",
"mid price",
"static mid price",
"mark price",
"last traded price",
"timestamp",
"open interest",
"indicative price",
"indicative volume",
"auction start",
"auction end",
"trading mode",
"auction trigger",
"extension trigger",
"target stake",
"supplied stake",
"horizon",
"ref price",
"min bound",
"max bound",
"market value proxy",
"average entry valuation",
"party",
"equity share",
})
// create a copy (deep copy), override the values we've gotten with those from the table so we can compare the objects
expect := mappedMD(actual)
// special fields first, these need to be compared manually
u64Set := expect.parseU64(data)
i64Set := expect.parseI64(data)
tSet := expect.parseTimes(data)
strSet := expect.parseStr(data)
uintSet := expect.parseUint(data)
expect.parseSpecial(data)
if pm := getPriceBounds(data); len(pm) > 0 {
expect.md.PriceMonitoringBounds = pm
}
// this might be a sparse check
if lp := getLPFeeShare(data); len(lp) > 0 {
expect.md.LiquidityProviderFeeShare = lp
}
cmp := mappedMD(actual)
parsed := mappedMD(expect.md)
errs := make([]error, 0, len(u64Set)+len(i64Set)+len(strSet)+2+len(uintSet))
if expect.tm != nil && *expect.tm != expect.md.MarketTradingMode {
errs = append(errs, fmt.Errorf("expected '%s' trading mode, instead got '%s'", *expect.tm, expect.md.MarketTradingMode))
}
if expect.tr != nil && *expect.tr != expect.md.Trigger {
errs = append(errs, fmt.Errorf("expected '%s' auction trigger, instead got '%s'", *expect.tr, expect.md.Trigger))
}
if expect.et != nil && *expect.et != expect.md.ExtensionTrigger {
errs = append(errs, fmt.Errorf("expected '%s' extension trigger, instead got '%s'", *expect.et, expect.md.ExtensionTrigger))
}
// compare uints as strings
for _, u := range uintSet {
e, g := parsed.uintMap[u], cmp.uintMap[u]
if (*e).String() != (*g).String() {
errs = append(errs, fmt.Errorf("expected '%s' for %s, instead got '%s'", (*e).String(), u, (*g).String()))
}
}
// compare uint64
for _, u := range u64Set {
e, g := parsed.u64Map[u], cmp.u64Map[u] // get pointers to both fields
if *e != *g {
errs = append(errs, fmt.Errorf("expected '%d' for %s, instead got '%d'", *e, u, *g))
}
}
// compare int64
for _, i := range i64Set {
e, g := parsed.i64Map[i], cmp.i64Map[i]
if *e != *g {
errs = append(errs, fmt.Errorf("expected '%d' for %s, instead got '%d'", *e, i, *g))
}
}
// compare times, which is basically identical to comparing i64
for _, i := range tSet {
e, g := parsed.tMap[i], cmp.tMap[i]
if *e != *g {
errs = append(errs, fmt.Errorf("expected '%d' for %s, instead got '%d'", *e, i, *g))
}
}
// compare strings
for _, s := range strSet {
e, g := parsed.strMap[s], cmp.strMap[s]
if *e != *g {
errs = append(errs, fmt.Errorf("expected '%s' for %s, instead got '%s'", *e, s, *g))
}
}
if err := cmpPriceBounds(expect, actual); len(err) > 0 {
errs = append(errs, err...)
}
if err := cmpLPFeeShare(expect, actual); len(err) > 0 {
errs = append(errs, err...)
}
// wrap all errors in a single error type for complete information
if len(errs) > 0 {
return ErrStack(errs)
}
// compare special fields (trading mode and auction trigger)
return nil
}
func cmpLPFeeShare(expect *MappedMD, got types.MarketData) []error {
errs := make([]error, 0, len(expect.md.LiquidityProviderFeeShare))
for _, lpfs := range expect.md.LiquidityProviderFeeShare {
match := false
var found *types.LiquidityProviderFeeShare
for _, g := range got.LiquidityProviderFeeShare {
if lpfs.Party == g.Party {
found = g
match = lpfs.AverageEntryValuation == g.AverageEntryValuation && lpfs.EquityLikeShare == g.EquityLikeShare
break
}
}
if !match {
if found == nil {
errs = append(errs, fmt.Errorf("no LP fee share found for party %s", lpfs.Party))
} else {
errs = append(errs, fmt.Errorf(
"expected LP fee share for party %s with avg valuation %s and equity like share %s, instead got avg. valuation %s and equity %s",
lpfs.Party,
lpfs.AverageEntryValuation,
lpfs.EquityLikeShare,
found.AverageEntryValuation,
found.EquityLikeShare,
))
}
}
}
if len(errs) == 0 {
return nil
}
return errs
}
func cmpPriceBounds(expect *MappedMD, got types.MarketData) []error {
errs := make([]error, 0, len(expect.md.PriceMonitoringBounds))
for _, pmb := range expect.md.PriceMonitoringBounds {
var bounds *types.PriceMonitoringBounds
match := false
for _, g := range got.PriceMonitoringBounds {
if g.Trigger.Horizon == pmb.Trigger.Horizon {
bounds = g
match = pmb.MaxValidPrice.EQ(g.MaxValidPrice) && pmb.MinValidPrice.EQ(g.MinValidPrice)
if !pmb.ReferencePrice.IsZero() {
match = match && pmb.ReferencePrice.Equal(g.ReferencePrice)
}
break
}
}
if !match {
if bounds == nil {
errs = append(errs, fmt.Errorf("no price bound for horizon %d found", pmb.Trigger.Horizon))
} else {
errs = append(errs, fmt.Errorf(
"expected price bounds %d-%d (ref price=%s) for horizon %d, instead got %d-%d (ref price=%s)",
pmb.MinValidPrice,
pmb.MaxValidPrice,
pmb.ReferencePrice.String(),
pmb.Trigger.Horizon,
bounds.MinValidPrice,
bounds.MaxValidPrice,
bounds.ReferencePrice.String(),
))
}
}
}
if len(errs) == 0 {
return nil
}
return errs
}
func getPriceBounds(data *godog.Table) (ret []*types.PriceMonitoringBounds) {
for _, row := range ParseTable(data) {
h, ok := row.I64B("horizon")
if !ok {
return nil
}
referencePrice := num.DecimalZero()
if row.HasColumn("ref price") {
referencePrice = row.Decimal("ref price")
}
expected := &types.PriceMonitoringBounds{
MinValidPrice: row.MustUint("min bound"),
MaxValidPrice: row.MustUint("max bound"),
ReferencePrice: referencePrice,
Trigger: &types.PriceMonitoringTrigger{
Horizon: h,
},
}
ret = append(ret, expected)
}
return ret
}
func getLPFeeShare(data *godog.Table) (ret []*types.LiquidityProviderFeeShare) {
for _, r := range ParseTable(data) {
avg, ok := r.StrB("average entry valuation")
if !ok {
return nil
}
ret = append(ret, &types.LiquidityProviderFeeShare{
Party: r.MustStr("party"),
EquityLikeShare: r.MustStr("equity share"),
AverageEntryValuation: avg,
})
}
return ret
}
func (m *MappedMD) parseSpecial(data *godog.Table) {
todo := map[string]struct{}{
"trading mode": {},
"auction trigger": {},
"extension trigger": {},
}
for _, r := range ParseTable(data) {
for k := range todo {
if _, ok := r.StrB(k); ok {
switch k {
case "trading mode":
tm := r.MustTradingMode(k)
m.tm = &tm
case "auction trigger":
at := r.MustAuctionTrigger(k)
m.tr = &at
case "extension trigger":
et := r.MustAuctionTrigger(k)
m.et = &et
}
delete(todo, k)
}
}
if len(todo) == 0 {
return
}
}
}
// parses the data, and returns a slice of keys for the values that were provided.
func (m *MappedMD) parseU64(data *godog.Table) []string {
set := make([]string, 0, len(m.u64Map))
for _, r := range ParseTable(data) {
for k, ptr := range m.u64Map {
if u, ok := r.U64B(k); ok {
*ptr = u
set = append(set, k)
// avoid reassignments in following iterations
delete(m.u64Map, k)
}
}
}
return set
}
func (m *MappedMD) parseTimes(data *godog.Table) []string {
// already set start based off of the value in the map
// does some trickery WRT auction end time, so we can check if the auction duration is N seconds
var (
end int64
start = *m.tMap["auction start"]
)
set := make([]string, 0, len(m.tMap))
for _, r := range ParseTable(data) {
for k, ptr := range m.tMap {
if i, ok := r.I64B(k); ok {
if k == "auction end" {
end = i
if end < start {
i = start + int64(time.Duration(end)*time.Second)
}
}
*ptr = i
set = append(set, k)
// again: avoid reassignments when parsing the next row
delete(m.i64Map, k)
}
}
}
return set
}
func (m *MappedMD) parseUint(data *godog.Table) []string {
set := make([]string, 0, len(m.uintMap))
for _, r := range ParseTable(data) {
for k, ptr := range m.uintMap {
if i, ok := r.StrB(k); ok {
n, _ := num.UintFromString(i, 10)
*ptr = n
set = append(set, k)
// again: avoid reassignments when parsing the next row
delete(m.uintMap, k)
}
}
}
return set
}
func (m *MappedMD) parseI64(data *godog.Table) []string {
set := make([]string, 0, len(m.i64Map))
for _, r := range ParseTable(data) {
for k, ptr := range m.i64Map {
if i, ok := r.I64B(k); ok {
*ptr = i
set = append(set, k)
// again: avoid reassignments when parsing the next row
delete(m.i64Map, k)
}
}
}
return set
}
func (m *MappedMD) parseStr(data *godog.Table) []string {
set := make([]string, 0, len(m.strMap))
for _, r := range ParseTable(data) {
for k, ptr := range m.strMap {
if i, ok := r.StrB(k); ok {
*ptr = i
set = append(set, k)
// again: avoid reassignments when parsing the next row
delete(m.strMap, k)
}
}
}
return set
}
func mappedMD(md types.MarketData) *MappedMD {
r := &MappedMD{
md: md,
}
// no need to clone here, the values are already cloned
r.uintMap = map[string]**num.Uint{
"mark price": &(r.md.MarkPrice),
"last traded price": &(r.md.LastTradedPrice),
"best bid price": &r.md.BestBidPrice,
"best offer price": &r.md.BestOfferPrice,
"best static bid price": &r.md.BestStaticBidPrice,
"best static offer price": &r.md.BestStaticOfferPrice,
"mid price": &r.md.MidPrice,
"static mid price": &r.md.StaticMidPrice,
"indicative price": &r.md.IndicativePrice,
}
r.u64Map = map[string]*uint64{
"best bid volume": &r.md.BestBidVolume,
"best offer volume": &r.md.BestOfferVolume,
"best static bid volume": &r.md.BestStaticBidVolume,
"best static offer volume": &r.md.BestStaticOfferVolume,
"open interest": &r.md.OpenInterest,
"indicative volume": &r.md.IndicativeVolume,
}
r.strMap = map[string]*string{
"target stake": &r.md.TargetStake,
"supplied stake": &r.md.SuppliedStake,
"market value proxy": &r.md.MarketValueProxy,
"market": &r.md.Market, // this is a bit pointless, but might as well add it
}
r.tMap = map[string]*int64{
"timestamp": &r.md.Timestamp,
"auction end": &r.md.AuctionEnd,
"auction start": &r.md.AuctionStart,
}
return r
}
// Error so we print out the wrong matches line by line.
func (e ErrStack) Error() string {
str := make([]string, 0, len(e))
for _, v := range e {
str = append(str, v.Error())
}
return strings.Join(str, "\n")
}