-
Notifications
You must be signed in to change notification settings - Fork 19
/
market.go
425 lines (360 loc) · 12.9 KB
/
market.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package entities
import (
"encoding/json"
"errors"
"fmt"
"math"
"time"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/libs/num"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"code.vegaprotocol.io/vega/protos/vega"
"github.com/shopspring/decimal"
)
type _Market struct{}
type MarketID = ID[_Market]
type Market struct {
ID MarketID
TxHash TxHash
VegaTime time.Time
InstrumentID string
TradableInstrument TradableInstrument
DecimalPlaces int
Fees Fees
OpeningAuction AuctionDuration
PriceMonitoringSettings PriceMonitoringSettings
LiquidityMonitoringParameters LiquidityMonitoringParameters
TradingMode MarketTradingMode
State MarketState
MarketTimestamps MarketTimestamps
PositionDecimalPlaces int
LpPriceRange string
LinearSlippageFactor *decimal.Decimal
QuadraticSlippageFactor *decimal.Decimal
ParentMarketID MarketID
InsurancePoolFraction *decimal.Decimal
// Not saved in the market table, but used when retrieving data from the database.
// This will be populated when a market has a successor
SuccessorMarketID MarketID
}
type MarketCursor struct {
VegaTime time.Time `json:"vegaTime"`
ID MarketID `json:"id"`
}
func (mc MarketCursor) String() string {
bs, err := json.Marshal(mc)
if err != nil {
panic(fmt.Errorf("could not marshal market cursor: %w", err))
}
return string(bs)
}
func (mc *MarketCursor) Parse(cursorString string) error {
if cursorString == "" {
return nil
}
return json.Unmarshal([]byte(cursorString), mc)
}
func NewMarketFromProto(market *vega.Market, txHash TxHash, vegaTime time.Time) (*Market, error) {
var err error
var liquidityMonitoringParameters LiquidityMonitoringParameters
var marketTimestamps MarketTimestamps
var priceMonitoringSettings PriceMonitoringSettings
var openingAuction AuctionDuration
var fees Fees
if fees, err = feesFromProto(market.Fees); err != nil {
return nil, err
}
if market.OpeningAuction != nil {
openingAuction.Duration = market.OpeningAuction.Duration
openingAuction.Volume = market.OpeningAuction.Volume
}
if priceMonitoringSettings, err = priceMonitoringSettingsFromProto(market.PriceMonitoringSettings); err != nil {
return nil, err
}
if liquidityMonitoringParameters, err = liquidityMonitoringParametersFromProto(market.LiquidityMonitoringParameters); err != nil {
return nil, err
}
if marketTimestamps, err = marketTimestampsFromProto(market.MarketTimestamps); err != nil {
return nil, err
}
if market.DecimalPlaces > math.MaxInt {
return nil, fmt.Errorf("%d is not a valid number for decimal places", market.DecimalPlaces)
}
if market.PositionDecimalPlaces > math.MaxInt {
return nil, fmt.Errorf("%d is not a valid number for position decimal places", market.PositionDecimalPlaces)
}
lppr, err := num.DecimalFromString(market.LpPriceRange)
if err != nil || lppr.IsNegative() || lppr.IsZero() || lppr.GreaterThan(num.DecimalFromInt64(100)) {
return nil, fmt.Errorf("%v is not a valid number for LP price range", market.LpPriceRange)
}
dps := int(market.DecimalPlaces)
positionDps := int(market.PositionDecimalPlaces)
linearSlippageFactor := (*num.Decimal)(nil)
if market.LinearSlippageFactor != "" {
factor, err := num.DecimalFromString(market.LinearSlippageFactor)
if err != nil {
return nil, fmt.Errorf("'%v' is not a valid number for linear slippage factor", market.LinearSlippageFactor)
}
linearSlippageFactor = &factor
}
quadraticSlippageFactor := (*num.Decimal)(nil)
if market.QuadraticSlippageFactor != "" {
factor, err := num.DecimalFromString(market.QuadraticSlippageFactor)
if err != nil {
return nil, fmt.Errorf("'%v' is not a valid number for quadratic slippage factor", market.QuadraticSlippageFactor)
}
quadraticSlippageFactor = &factor
}
parentMarketID := MarketID("")
if market.ParentMarketId != nil && *market.ParentMarketId != "" {
parent := MarketID(*market.ParentMarketId)
parentMarketID = parent
}
var insurancePoolFraction *num.Decimal
if market.InsurancePoolFraction != nil && *market.InsurancePoolFraction != "" {
insurance, err := num.DecimalFromString(*market.InsurancePoolFraction)
if err != nil {
return nil, fmt.Errorf("'%v' is not a valid number for insurance pool fraction", market.InsurancePoolFraction)
}
insurancePoolFraction = &insurance
}
return &Market{
ID: MarketID(market.Id),
TxHash: txHash,
VegaTime: vegaTime,
InstrumentID: market.TradableInstrument.Instrument.Id,
TradableInstrument: TradableInstrument{market.TradableInstrument},
DecimalPlaces: dps,
Fees: fees,
OpeningAuction: openingAuction,
PriceMonitoringSettings: priceMonitoringSettings,
LiquidityMonitoringParameters: liquidityMonitoringParameters,
TradingMode: MarketTradingMode(market.TradingMode),
State: MarketState(market.State),
MarketTimestamps: marketTimestamps,
PositionDecimalPlaces: positionDps,
LpPriceRange: market.LpPriceRange,
LinearSlippageFactor: linearSlippageFactor,
QuadraticSlippageFactor: quadraticSlippageFactor,
ParentMarketID: parentMarketID,
InsurancePoolFraction: insurancePoolFraction,
}, nil
}
func (m Market) ToProto() *vega.Market {
linearSlippageFactor := ""
if m.LinearSlippageFactor != nil {
linearSlippageFactor = m.LinearSlippageFactor.String()
}
quadraticSlippageFactor := ""
if m.QuadraticSlippageFactor != nil {
quadraticSlippageFactor = m.QuadraticSlippageFactor.String()
}
var parentMarketID, insurancePoolFraction *string
if m.ParentMarketID != "" {
parentMarketID = ptr.From(m.ParentMarketID.String())
}
if m.InsurancePoolFraction != nil {
insurancePoolFraction = ptr.From(m.InsurancePoolFraction.String())
}
var successorMarketID *string
if m.SuccessorMarketID != "" {
successorMarketID = ptr.From(m.SuccessorMarketID.String())
}
return &vega.Market{
Id: m.ID.String(),
TradableInstrument: m.TradableInstrument.ToProto(),
DecimalPlaces: uint64(m.DecimalPlaces),
Fees: m.Fees.ToProto(),
OpeningAuction: &vega.AuctionDuration{
Duration: m.OpeningAuction.Duration,
Volume: m.OpeningAuction.Volume,
},
PriceMonitoringSettings: m.PriceMonitoringSettings.ToProto(),
LiquidityMonitoringParameters: m.LiquidityMonitoringParameters.ToProto(),
TradingMode: vega.Market_TradingMode(m.TradingMode),
State: vega.Market_State(m.State),
MarketTimestamps: m.MarketTimestamps.ToProto(),
PositionDecimalPlaces: int64(m.PositionDecimalPlaces),
LpPriceRange: m.LpPriceRange,
LinearSlippageFactor: linearSlippageFactor,
QuadraticSlippageFactor: quadraticSlippageFactor,
ParentMarketId: parentMarketID,
InsurancePoolFraction: insurancePoolFraction,
SuccessorMarketId: successorMarketID,
}
}
func (m Market) Cursor() *Cursor {
mc := MarketCursor{
VegaTime: m.VegaTime,
ID: m.ID,
}
return NewCursor(mc.String())
}
func (m Market) ToProtoEdge(_ ...any) (*v2.MarketEdge, error) {
return &v2.MarketEdge{
Node: m.ToProto(),
Cursor: m.Cursor().Encode(),
}, nil
}
type MarketTimestamps struct {
Proposed int64 `json:"proposed,omitempty"`
Pending int64 `json:"pending,omitempty"`
Open int64 `json:"open,omitempty"`
Close int64 `json:"close,omitempty"`
}
func (mt MarketTimestamps) ToProto() *vega.MarketTimestamps {
return &vega.MarketTimestamps{
Proposed: mt.Proposed,
Pending: mt.Pending,
Open: mt.Open,
Close: mt.Close,
}
}
func marketTimestampsFromProto(ts *vega.MarketTimestamps) (MarketTimestamps, error) {
if ts == nil {
return MarketTimestamps{}, errors.New("market timestamps cannot be nil")
}
return MarketTimestamps{
Proposed: ts.Proposed,
Pending: ts.Pending,
Open: ts.Open,
Close: ts.Close,
}, nil
}
type TargetStakeParameters struct {
TimeWindow int64 `json:"timeWindow,omitempty"`
ScalingFactors float64 `json:"scalingFactor,omitempty"`
}
func (tsp TargetStakeParameters) ToProto() *vega.TargetStakeParameters {
return &vega.TargetStakeParameters{
TimeWindow: tsp.TimeWindow,
ScalingFactor: tsp.ScalingFactors,
}
}
type LiquidityMonitoringParameters struct {
TargetStakeParameters *TargetStakeParameters `json:"targetStakeParameters,omitempty"`
TriggeringRatio string `json:"triggeringRatio,omitempty"`
AuctionExtension int64 `json:"auctionExtension,omitempty"`
}
func (lmp LiquidityMonitoringParameters) ToProto() *vega.LiquidityMonitoringParameters {
if lmp.TargetStakeParameters == nil {
return nil
}
return &vega.LiquidityMonitoringParameters{
TargetStakeParameters: lmp.TargetStakeParameters.ToProto(),
TriggeringRatio: lmp.TriggeringRatio,
AuctionExtension: lmp.AuctionExtension,
}
}
func liquidityMonitoringParametersFromProto(lmp *vega.LiquidityMonitoringParameters) (LiquidityMonitoringParameters, error) {
if lmp == nil {
return LiquidityMonitoringParameters{}, errors.New("liquidity monitoring parameters cannot be Nil")
}
var tsp *TargetStakeParameters
if lmp.TargetStakeParameters != nil {
tsp = &TargetStakeParameters{
TimeWindow: lmp.TargetStakeParameters.TimeWindow,
ScalingFactors: lmp.TargetStakeParameters.ScalingFactor,
}
}
return LiquidityMonitoringParameters{
TargetStakeParameters: tsp,
TriggeringRatio: lmp.TriggeringRatio,
AuctionExtension: lmp.AuctionExtension,
}, nil
}
type PriceMonitoringParameters struct {
Triggers []*PriceMonitoringTrigger `json:"triggers,omitempty"`
}
func priceMonitoringParametersFromProto(pmp *vega.PriceMonitoringParameters) PriceMonitoringParameters {
if len(pmp.Triggers) == 0 {
return PriceMonitoringParameters{}
}
triggers := make([]*PriceMonitoringTrigger, 0, len(pmp.Triggers))
for _, trigger := range pmp.Triggers {
probability, _ := decimal.NewFromString(trigger.Probability)
triggers = append(triggers, &PriceMonitoringTrigger{
Horizon: uint64(trigger.Horizon),
Probability: probability,
AuctionExtension: uint64(trigger.AuctionExtension),
})
}
return PriceMonitoringParameters{
Triggers: triggers,
}
}
type PriceMonitoringSettings struct {
Parameters *PriceMonitoringParameters `json:"priceMonitoringParameters,omitempty"`
}
func (s PriceMonitoringSettings) ToProto() *vega.PriceMonitoringSettings {
if s.Parameters == nil {
return nil
}
triggers := make([]*vega.PriceMonitoringTrigger, 0, len(s.Parameters.Triggers))
if len(s.Parameters.Triggers) > 0 {
for _, trigger := range s.Parameters.Triggers {
triggers = append(triggers, trigger.ToProto())
}
}
return &vega.PriceMonitoringSettings{
Parameters: &vega.PriceMonitoringParameters{
Triggers: triggers,
},
}
}
func priceMonitoringSettingsFromProto(pms *vega.PriceMonitoringSettings) (PriceMonitoringSettings, error) {
if pms == nil {
return PriceMonitoringSettings{}, errors.New("price monitoring settings cannot be nil")
}
parameters := priceMonitoringParametersFromProto(pms.Parameters)
return PriceMonitoringSettings{
Parameters: ¶meters,
}, nil
}
type AuctionDuration struct {
Duration int64 `json:"duration,omitempty"`
Volume uint64 `json:"volume,omitempty"`
}
type FeeFactors struct {
MakerFee string `json:"makerFee,omitempty"`
InfrastructureFee string `json:"infrastructureFee,omitempty"`
LiquidityFee string `json:"liquidityFee,omitempty"`
}
type Fees struct {
Factors *FeeFactors `json:"factors,omitempty"`
}
func (f Fees) ToProto() *vega.Fees {
if f.Factors == nil {
return nil
}
return &vega.Fees{
Factors: &vega.FeeFactors{
MakerFee: f.Factors.MakerFee,
InfrastructureFee: f.Factors.InfrastructureFee,
LiquidityFee: f.Factors.LiquidityFee,
},
}
}
func feesFromProto(fees *vega.Fees) (Fees, error) {
if fees == nil {
return Fees{}, errors.New("fees cannot be Nil")
}
return Fees{
Factors: &FeeFactors{
MakerFee: fees.Factors.MakerFee,
InfrastructureFee: fees.Factors.InfrastructureFee,
LiquidityFee: fees.Factors.LiquidityFee,
},
}, nil
}