-
Notifications
You must be signed in to change notification settings - Fork 19
/
market.go
1323 lines (1169 loc) · 39.6 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/>.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package types
import (
"errors"
"fmt"
"strings"
"time"
"code.vegaprotocol.io/vega/core/datasource"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/libs/stringer"
"code.vegaprotocol.io/vega/protos/vega"
vegapb "code.vegaprotocol.io/vega/protos/vega"
"google.golang.org/protobuf/proto"
)
type MarketStats struct {
PartiesOpenNotionalVolume map[string]*num.Uint
PartiesTotalTradeVolume map[string]*num.Uint
}
type (
LiquidityProviderFeeShare = vegapb.LiquidityProviderFeeShare
LiquidityProviderSLA = vegapb.LiquidityProviderSLA
)
type LiquidityProviderFeeShares []*LiquidityProviderFeeShare
func (ls LiquidityProviderFeeShares) String() string {
if ls == nil {
return "[]"
}
strs := make([]string, 0, len(ls))
for _, l := range ls {
strs = append(strs, l.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
type LiquidityProviderSLAs []*LiquidityProviderSLA
func (ls LiquidityProviderSLAs) String() string {
if ls == nil {
return "[]"
}
strs := make([]string, 0, len(ls))
for _, l := range ls {
strs = append(strs, l.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
var (
ErrNilTradableInstrument = errors.New("nil tradable instrument")
ErrNilInstrument = errors.New("nil instrument")
ErrNilProduct = errors.New("nil product")
ErrUnknownAsset = errors.New("unknown asset")
)
type MarketTimestamps struct {
Proposed int64
Pending int64
Open int64
Close int64
}
func MarketTimestampsFromProto(p *vegapb.MarketTimestamps) *MarketTimestamps {
var ts MarketTimestamps
if p != nil {
ts = MarketTimestamps{
Proposed: p.Proposed,
Pending: p.Pending,
Open: p.Open,
Close: p.Close,
}
}
return &ts
}
func (m MarketTimestamps) IntoProto() *vegapb.MarketTimestamps {
return &vegapb.MarketTimestamps{
Proposed: m.Proposed,
Pending: m.Pending,
Open: m.Open,
Close: m.Close,
}
}
func (m MarketTimestamps) DeepClone() *MarketTimestamps {
return &MarketTimestamps{
Proposed: m.Proposed,
Pending: m.Pending,
Open: m.Open,
Close: m.Close,
}
}
func (m MarketTimestamps) String() string {
return fmt.Sprintf(
"proposed(%v) open(%v) pending(%v) close(%v)",
m.Proposed,
m.Open,
m.Pending,
m.Close,
)
}
type MarketTradingMode = vegapb.Market_TradingMode
const (
// Default value, this is invalid.
MarketTradingModeUnspecified MarketTradingMode = vegapb.Market_TRADING_MODE_UNSPECIFIED
// Normal trading.
MarketTradingModeContinuous MarketTradingMode = vegapb.Market_TRADING_MODE_CONTINUOUS
// Auction trading (FBA).
MarketTradingModeBatchAuction MarketTradingMode = vegapb.Market_TRADING_MODE_BATCH_AUCTION
// Opening auction.
MarketTradingModeOpeningAuction MarketTradingMode = vegapb.Market_TRADING_MODE_OPENING_AUCTION
// Auction triggered by monitoring.
MarketTradingModeMonitoringAuction MarketTradingMode = vegapb.Market_TRADING_MODE_MONITORING_AUCTION
// No trading allowed.
MarketTradingModeNoTrading MarketTradingMode = vegapb.Market_TRADING_MODE_NO_TRADING
// Special auction mode for market suspended via governance.
MarketTradingModeSuspendedViaGovernance MarketTradingMode = vegapb.Market_TRADING_MODE_SUSPENDED_VIA_GOVERNANCE
)
type MarketState = vegapb.Market_State
const (
// Default value, invalid.
MarketStateUnspecified MarketState = vegapb.Market_STATE_UNSPECIFIED
// The Governance proposal valid and accepted.
MarketStateProposed MarketState = vegapb.Market_STATE_PROPOSED
// Outcome of governance votes is to reject the market.
MarketStateRejected MarketState = vegapb.Market_STATE_REJECTED
// Governance vote passes/wins.
MarketStatePending MarketState = vegapb.Market_STATE_PENDING
// Market triggers cancellation condition or governance
// votes to close before market becomes Active.
MarketStateCancelled MarketState = vegapb.Market_STATE_CANCELLED
// Enactment date reached and usual auction exit checks pass.
MarketStateActive MarketState = vegapb.Market_STATE_ACTIVE
// Price monitoring or liquidity monitoring trigger.
MarketStateSuspended MarketState = vegapb.Market_STATE_SUSPENDED
// Governance vote (to close).
MarketStateClosed MarketState = vegapb.Market_STATE_CLOSED
// Defined by the product (i.e. from a product parameter,
// specified in market definition, giving close date/time).
MarketStateTradingTerminated MarketState = vegapb.Market_STATE_TRADING_TERMINATED
// Settlement triggered and completed as defined by product.
MarketStateSettled MarketState = vegapb.Market_STATE_SETTLED
// Market has been suspended via a governance proposal.
MarketStateSuspendedViaGovernance MarketState = vegapb.Market_STATE_SUSPENDED_VIA_GOVERNANCE
)
type AuctionTrigger = vegapb.AuctionTrigger
const (
// Default value for AuctionTrigger, no auction triggered.
AuctionTriggerUnspecified AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_UNSPECIFIED
// Batch auction.
AuctionTriggerBatch AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_BATCH
// Opening auction.
AuctionTriggerOpening AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_OPENING
// Price monitoring trigger.
AuctionTriggerPrice AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_PRICE
// Liquidity monitoring due to unmet target trigger.
AuctionTriggerLiquidityTargetNotMet AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_LIQUIDITY_TARGET_NOT_MET
// Governance triggered auction.
AuctionTriggerGovernanceSuspension AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_GOVERNANCE_SUSPENSION
// AuctionTriggerUnableToDeployLPOrders legacy liquidity provision supports.
AuctionTriggerUnableToDeployLPOrders AuctionTrigger = vegapb.AuctionTrigger_AUCTION_TRIGGER_UNABLE_TO_DEPLOY_LP_ORDERS
)
type InstrumentMetadata struct {
Tags []string
}
func InstrumentMetadataFromProto(m *vegapb.InstrumentMetadata) *InstrumentMetadata {
return &InstrumentMetadata{
Tags: append([]string{}, m.Tags...),
}
}
func (i InstrumentMetadata) IntoProto() *vegapb.InstrumentMetadata {
tags := make([]string, 0, len(i.Tags))
return &vegapb.InstrumentMetadata{
Tags: append(tags, i.Tags...),
}
}
func (i InstrumentMetadata) String() string {
return fmt.Sprintf(
"tags(%v)",
Tags(i.Tags).String(),
)
}
func (i InstrumentMetadata) DeepClone() *InstrumentMetadata {
ret := &InstrumentMetadata{
Tags: make([]string, len(i.Tags)),
}
copy(ret.Tags, i.Tags)
return ret
}
type AuctionDuration struct {
Duration int64
Volume uint64
}
func AuctionDurationFromProto(ad *vegapb.AuctionDuration) *AuctionDuration {
if ad == nil {
return nil
}
return &AuctionDuration{
Duration: ad.Duration,
Volume: ad.Volume,
}
}
func (a AuctionDuration) IntoProto() *vegapb.AuctionDuration {
return &vegapb.AuctionDuration{
Duration: a.Duration,
Volume: a.Volume,
}
}
func (a AuctionDuration) String() string {
return fmt.Sprintf(
"duration(%v) volume(%v)",
a.Duration,
a.Volume,
)
}
func (a AuctionDuration) DeepClone() *AuctionDuration {
return &AuctionDuration{
Duration: a.Duration,
Volume: a.Volume,
}
}
type rmType int
const (
SimpleRiskModelType rmType = iota
LogNormalRiskModelType
)
type TradableInstrument struct {
Instrument *Instrument
MarginCalculator *MarginCalculator
RiskModel isTRM
rmt rmType
}
type isTRM interface {
isTRM()
trmIntoProto() interface{}
rmType() rmType
String() string
Equal(isTRM) bool
}
func TradableInstrumentFromProto(ti *vegapb.TradableInstrument) *TradableInstrument {
if ti == nil {
return nil
}
rm := isTRMFromProto(ti.RiskModel)
return &TradableInstrument{
Instrument: InstrumentFromProto(ti.Instrument),
MarginCalculator: MarginCalculatorFromProto(ti.MarginCalculator),
RiskModel: rm,
rmt: rm.rmType(),
}
}
func (t TradableInstrument) IntoProto() *vegapb.TradableInstrument {
var (
i *vegapb.Instrument
m *vegapb.MarginCalculator
)
if t.Instrument != nil {
i = t.Instrument.IntoProto()
}
if t.MarginCalculator != nil {
m = t.MarginCalculator.IntoProto()
}
r := &vegapb.TradableInstrument{
Instrument: i,
MarginCalculator: m,
}
if t.RiskModel == nil {
return r
}
rmp := t.RiskModel.trmIntoProto()
switch rm := rmp.(type) {
case *vegapb.TradableInstrument_SimpleRiskModel:
r.RiskModel = rm
case *vegapb.TradableInstrument_LogNormalRiskModel:
r.RiskModel = rm
}
return r
}
func (t TradableInstrument) GetSimpleRiskModel() *SimpleRiskModel {
if t.rmt == SimpleRiskModelType {
srm, ok := t.RiskModel.(*TradableInstrumentSimpleRiskModel)
if !ok || srm == nil {
return nil
}
return srm.SimpleRiskModel
}
return nil
}
func (t TradableInstrument) GetLogNormalRiskModel() *LogNormalRiskModel {
if t.rmt == LogNormalRiskModelType {
lrm, ok := t.RiskModel.(*TradableInstrumentLogNormalRiskModel)
if !ok || lrm == nil {
return nil
}
return lrm.LogNormalRiskModel
}
return nil
}
func (t TradableInstrument) String() string {
return fmt.Sprintf(
"instrument(%s) marginCalculator(%s) riskModel(%s)",
stringer.PtrToString(t.Instrument),
stringer.PtrToString(t.MarginCalculator),
stringer.ObjToString(t.RiskModel),
)
}
func (t TradableInstrument) DeepClone() *TradableInstrument {
ti := &TradableInstrument{
Instrument: t.Instrument.DeepClone(),
RiskModel: t.RiskModel,
rmt: t.rmt,
}
if t.MarginCalculator != nil {
ti.MarginCalculator = t.MarginCalculator.DeepClone()
}
return ti
}
type InstrumentSpot struct {
Spot *Spot
}
func (InstrumentSpot) Type() ProductType {
return ProductTypeSpot
}
func (i InstrumentSpot) String() string {
return fmt.Sprintf(
"spot(%s)",
stringer.PtrToString(i.Spot),
)
}
type Spot struct {
Name string
BaseAsset string
QuoteAsset string
}
func SpotFromProto(s *vegapb.Spot) *Spot {
return &Spot{
Name: s.Name,
BaseAsset: s.BaseAsset,
QuoteAsset: s.QuoteAsset,
}
}
func (s Spot) IntoProto() *vegapb.Spot {
return &vegapb.Spot{
Name: s.Name,
BaseAsset: s.BaseAsset,
QuoteAsset: s.QuoteAsset,
}
}
func (s Spot) String() string {
return fmt.Sprintf(
"baseAsset(%s) quoteAsset(%s)",
s.BaseAsset,
s.QuoteAsset,
)
}
type InstrumentFuture struct {
Future *Future
}
func (InstrumentFuture) Type() ProductType {
return ProductTypeFuture
}
func (i InstrumentFuture) String() string {
return fmt.Sprintf(
"future(%s)",
stringer.PtrToString(i.Future),
)
}
type Future struct {
SettlementAsset string
QuoteName string
DataSourceSpecForSettlementData *datasource.Spec
DataSourceSpecForTradingTermination *datasource.Spec
DataSourceSpecBinding *datasource.SpecBindingForFuture
}
func FutureFromProto(f *vegapb.Future) *Future {
return &Future{
SettlementAsset: f.SettlementAsset,
QuoteName: f.QuoteName,
DataSourceSpecForSettlementData: datasource.SpecFromProto(f.DataSourceSpecForSettlementData),
DataSourceSpecForTradingTermination: datasource.SpecFromProto(f.DataSourceSpecForTradingTermination),
DataSourceSpecBinding: datasource.SpecBindingForFutureFromProto(f.DataSourceSpecBinding),
}
}
func (f Future) IntoProto() *vegapb.Future {
return &vegapb.Future{
SettlementAsset: f.SettlementAsset,
QuoteName: f.QuoteName,
DataSourceSpecForSettlementData: f.DataSourceSpecForSettlementData.IntoProto(),
DataSourceSpecForTradingTermination: f.DataSourceSpecForTradingTermination.IntoProto(),
DataSourceSpecBinding: f.DataSourceSpecBinding.IntoProto(),
}
}
func (f Future) String() string {
return fmt.Sprintf(
"quoteName(%s) settlementAsset(%s) dataSourceSpec(settlementData(%s) tradingTermination(%s) binding(%s))",
f.QuoteName,
f.SettlementAsset,
stringer.PtrToString(f.DataSourceSpecForSettlementData),
stringer.PtrToString(f.DataSourceSpecForTradingTermination),
stringer.PtrToString(f.DataSourceSpecBinding),
)
}
type InstrumentPerps struct {
Perps *Perps
}
func (InstrumentPerps) Type() ProductType {
return ProductTypePerps
}
func (i InstrumentPerps) String() string {
return fmt.Sprintf(
"perps(%s)",
stringer.PtrToString(i.Perps),
)
}
type Perps struct {
SettlementAsset string
QuoteName string
MarginFundingFactor num.Decimal
InterestRate num.Decimal
ClampLowerBound num.Decimal
ClampUpperBound num.Decimal
// funding payment modifiers
FundingRateScalingFactor *num.Decimal
FundingRateLowerBound *num.Decimal
FundingRateUpperBound *num.Decimal
DataSourceSpecForSettlementData *datasource.Spec
DataSourceSpecForSettlementSchedule *datasource.Spec
DataSourceSpecBinding *datasource.SpecBindingForPerps
InternalCompositePriceConfig *CompositePriceConfiguration
}
func PerpsFromProto(p *vegapb.Perpetual) *Perps {
var scalingFactor *num.Decimal
if p.FundingRateScalingFactor != nil {
scalingFactor = ptr.From(num.MustDecimalFromString(*p.FundingRateScalingFactor))
}
var upperBound *num.Decimal
if p.FundingRateUpperBound != nil {
upperBound = ptr.From(num.MustDecimalFromString(*p.FundingRateUpperBound))
}
var lowerBound *num.Decimal
if p.FundingRateLowerBound != nil {
lowerBound = ptr.From(num.MustDecimalFromString(*p.FundingRateLowerBound))
}
var internalCompositePriceConfig *CompositePriceConfiguration
if p.InternalCompositePriceConfig != nil {
internalCompositePriceConfig = CompositePriceConfigurationFromProto(p.InternalCompositePriceConfig)
}
return &Perps{
SettlementAsset: p.SettlementAsset,
QuoteName: p.QuoteName,
MarginFundingFactor: num.MustDecimalFromString(p.MarginFundingFactor),
InterestRate: num.MustDecimalFromString(p.InterestRate),
ClampLowerBound: num.MustDecimalFromString(p.ClampLowerBound),
ClampUpperBound: num.MustDecimalFromString(p.ClampUpperBound),
FundingRateScalingFactor: scalingFactor,
FundingRateUpperBound: upperBound,
FundingRateLowerBound: lowerBound,
DataSourceSpecForSettlementData: datasource.SpecFromProto(p.DataSourceSpecForSettlementData),
DataSourceSpecForSettlementSchedule: datasource.SpecFromProto(p.DataSourceSpecForSettlementSchedule),
DataSourceSpecBinding: datasource.SpecBindingForPerpsFromProto(p.DataSourceSpecBinding),
InternalCompositePriceConfig: internalCompositePriceConfig,
}
}
func (p Perps) IntoProto() *vegapb.Perpetual {
var scalingFactor *string
if p.FundingRateScalingFactor != nil {
scalingFactor = ptr.From(p.FundingRateScalingFactor.String())
}
var upperBound *string
if p.FundingRateUpperBound != nil {
upperBound = ptr.From(p.FundingRateUpperBound.String())
}
var lowerBound *string
if p.FundingRateLowerBound != nil {
lowerBound = ptr.From(p.FundingRateLowerBound.String())
}
var internalCompositePriceConfig *vega.CompositePriceConfiguration
if p.InternalCompositePriceConfig != nil {
internalCompositePriceConfig = p.InternalCompositePriceConfig.IntoProto()
}
return &vegapb.Perpetual{
SettlementAsset: p.SettlementAsset,
QuoteName: p.QuoteName,
MarginFundingFactor: p.MarginFundingFactor.String(),
InterestRate: p.InterestRate.String(),
ClampLowerBound: p.ClampLowerBound.String(),
ClampUpperBound: p.ClampUpperBound.String(),
FundingRateScalingFactor: scalingFactor,
FundingRateUpperBound: upperBound,
FundingRateLowerBound: lowerBound,
DataSourceSpecForSettlementData: p.DataSourceSpecForSettlementData.IntoProto(),
DataSourceSpecForSettlementSchedule: p.DataSourceSpecForSettlementSchedule.IntoProto(),
DataSourceSpecBinding: p.DataSourceSpecBinding.IntoProto(),
InternalCompositePriceConfig: internalCompositePriceConfig,
}
}
func (p Perps) String() string {
return fmt.Sprintf(
"quoteName(%s) settlementAsset(%s) marginFundingFactore(%s) interestRate(%s) clampLowerBound(%s) clampUpperBound(%s) settlementData(%s) tradingTermination(%s) binding(%s), internalCompositePriceConfig(%s)",
p.QuoteName,
p.SettlementAsset,
p.MarginFundingFactor.String(),
p.InterestRate.String(),
p.ClampLowerBound.String(),
p.ClampUpperBound.String(),
stringer.PtrToString(p.DataSourceSpecForSettlementData),
stringer.PtrToString(p.DataSourceSpecForSettlementSchedule),
stringer.PtrToString(p.DataSourceSpecBinding),
stringer.PtrToString(p.InternalCompositePriceConfig),
)
}
func iInstrumentFromProto(pi interface{}) iProto {
switch i := pi.(type) {
case vegapb.Instrument_Future:
return InstrumentFutureFromProto(&i)
case *vegapb.Instrument_Future:
return InstrumentFutureFromProto(i)
case vegapb.Instrument_Perpetual:
return InstrumentPerpsFromProto(&i)
case *vegapb.Instrument_Perpetual:
return InstrumentPerpsFromProto(i)
case vegapb.Instrument_Spot:
return InstrumentSpotFromProto(&i)
case *vegapb.Instrument_Spot:
return InstrumentSpotFromProto(i)
}
return nil
}
func InstrumentSpotFromProto(f *vegapb.Instrument_Spot) *InstrumentSpot {
return &InstrumentSpot{
Spot: SpotFromProto(f.Spot),
}
}
func (i InstrumentSpot) IntoProto() *vegapb.Instrument_Spot {
return &vegapb.Instrument_Spot{
Spot: i.Spot.IntoProto(),
}
}
func (i InstrumentSpot) getAssets() ([]string, error) {
if i.Spot == nil {
return []string{}, ErrUnknownAsset
}
return []string{i.Spot.BaseAsset, i.Spot.QuoteAsset}, nil
}
func (i InstrumentSpot) iIntoProto() interface{} {
return i.IntoProto()
}
func InstrumentFutureFromProto(f *vegapb.Instrument_Future) *InstrumentFuture {
return &InstrumentFuture{
Future: FutureFromProto(f.Future),
}
}
func (i InstrumentFuture) IntoProto() *vegapb.Instrument_Future {
return &vegapb.Instrument_Future{
Future: i.Future.IntoProto(),
}
}
func (i InstrumentFuture) getAssets() ([]string, error) {
if i.Future == nil {
return []string{}, ErrUnknownAsset
}
return []string{i.Future.SettlementAsset}, nil
}
func InstrumentPerpsFromProto(p *vegapb.Instrument_Perpetual) *InstrumentPerps {
return &InstrumentPerps{
Perps: PerpsFromProto(p.Perpetual),
}
}
func (i InstrumentPerps) IntoProto() *vegapb.Instrument_Perpetual {
return &vegapb.Instrument_Perpetual{
Perpetual: i.Perps.IntoProto(),
}
}
func (i InstrumentPerps) getAssets() ([]string, error) {
if i.Perps == nil {
return []string{}, ErrUnknownAsset
}
return []string{i.Perps.SettlementAsset}, nil
}
func (m *Market) GetAssets() ([]string, error) {
if m.TradableInstrument == nil {
return []string{}, ErrNilTradableInstrument
}
if m.TradableInstrument.Instrument == nil {
return []string{}, ErrNilInstrument
}
if m.TradableInstrument.Instrument.Product == nil {
return []string{}, ErrNilProduct
}
return m.TradableInstrument.Instrument.Product.getAssets()
}
func (m *Market) ProductType() ProductType {
return m.TradableInstrument.Instrument.Product.Type()
}
func (m *Market) GetFuture() *InstrumentFuture {
if m.ProductType() == ProductTypeFuture {
f, _ := m.TradableInstrument.Instrument.Product.(*InstrumentFuture)
return f
}
return nil
}
func (m *Market) GetPerps() *InstrumentPerps {
if m.ProductType() == ProductTypePerps {
p, _ := m.TradableInstrument.Instrument.Product.(*InstrumentPerps)
return p
}
return nil
}
func (m *Market) GetSpot() *InstrumentSpot {
if m.ProductType() == ProductTypeSpot {
s, _ := m.TradableInstrument.Instrument.Product.(*InstrumentSpot)
return s
}
return nil
}
func (i InstrumentFuture) iIntoProto() interface{} {
return i.IntoProto()
}
func (i InstrumentPerps) iIntoProto() interface{} {
return i.IntoProto()
}
type iProto interface {
iIntoProto() interface{}
getAssets() ([]string, error)
String() string
Type() ProductType
}
type Instrument struct {
ID string
Code string
Name string
Metadata *InstrumentMetadata
// Types that are valid to be assigned to Product:
// *InstrumentFuture
// *InstrumentSpot
// *InstrumentPerps
Product iProto
}
func InstrumentFromProto(i *vegapb.Instrument) *Instrument {
if i == nil {
return nil
}
return &Instrument{
ID: i.Id,
Code: i.Code,
Name: i.Name,
Metadata: InstrumentMetadataFromProto(i.Metadata),
Product: iInstrumentFromProto(i.Product),
}
}
func (i Instrument) GetSpot() *Spot {
switch p := i.Product.(type) {
case *InstrumentSpot:
return p.Spot
default:
return nil
}
}
func (i Instrument) GetFuture() *Future {
switch p := i.Product.(type) {
case *InstrumentFuture:
return p.Future
default:
return nil
}
}
func (i Instrument) GetPerps() *Perps {
switch p := i.Product.(type) {
case *InstrumentPerps:
return p.Perps
default:
return nil
}
}
func (i Instrument) IntoProto() *vegapb.Instrument {
p := i.Product.iIntoProto()
r := &vegapb.Instrument{
Id: i.ID,
Code: i.Code,
Name: i.Name,
Metadata: i.Metadata.IntoProto(),
}
switch pt := p.(type) {
case *vegapb.Instrument_Future:
r.Product = pt
case *vegapb.Instrument_Perpetual:
r.Product = pt
case *vegapb.Instrument_Spot:
r.Product = pt
}
return r
}
func (i Instrument) DeepClone() *Instrument {
cpy := &Instrument{
ID: i.ID,
Code: i.Code,
Name: i.Name,
Product: i.Product,
}
if i.Metadata != nil {
cpy.Metadata = i.Metadata.DeepClone()
}
return cpy
}
func (i Instrument) String() string {
return fmt.Sprintf(
"ID(%s) name(%s) code(%s) product(%s) metadata(%s)",
i.ID,
i.Name,
i.Code,
stringer.ObjToString(i.Product),
stringer.PtrToString(i.Metadata),
)
}
type iProductData interface {
IntoProto() *vegapb.ProductData
}
type ProductData struct {
Data iProductData
}
type PerpetualData struct {
FundingRate string
FundingPayment string
InternalTWAP string
ExternalTWAP string
SeqNum uint64
StartTime int64
InternalCompositePrice *num.Uint
NextInternalCompositePriceCalc int64
InternalCompositePriceType CompositePriceType
UnderlyingIndexPrice *num.Uint
InternalCompositePriceState *CompositePriceState
}
func (p PerpetualData) IntoProto() *vegapb.ProductData {
var internalCompositePriceState *vegapb.CompositePriceState
if p.InternalCompositePriceState != nil {
internalCompositePriceState = p.InternalCompositePriceState.IntoProto()
}
return &vegapb.ProductData{
Data: &vegapb.ProductData_PerpetualData{
PerpetualData: &vegapb.PerpetualData{
FundingRate: p.FundingRate,
FundingPayment: p.FundingPayment,
InternalTwap: p.InternalTWAP,
ExternalTwap: p.ExternalTWAP,
SeqNum: p.SeqNum,
StartTime: p.StartTime,
InternalCompositePrice: num.UintToString(p.InternalCompositePrice),
NextInternalCompositePriceCalc: p.NextInternalCompositePriceCalc,
InternalCompositePriceType: p.InternalCompositePriceType,
InternalCompositePriceState: internalCompositePriceState,
UnderlyingIndexPrice: num.UintToString(p.UnderlyingIndexPrice),
},
},
}
}
type MarketData struct {
MarkPrice *num.Uint
LastTradedPrice *num.Uint
BestBidPrice *num.Uint
BestBidVolume uint64
BestOfferPrice *num.Uint
BestOfferVolume uint64
BestStaticBidPrice *num.Uint
BestStaticBidVolume uint64
BestStaticOfferPrice *num.Uint
BestStaticOfferVolume uint64
MidPrice *num.Uint
StaticMidPrice *num.Uint
Market string
Timestamp int64
OpenInterest uint64
AuctionEnd int64
AuctionStart int64
IndicativePrice *num.Uint
IndicativeVolume uint64
MarketTradingMode MarketTradingMode
MarketState MarketState
Trigger AuctionTrigger
ExtensionTrigger AuctionTrigger
TargetStake string
SuppliedStake string
PriceMonitoringBounds []*PriceMonitoringBounds
MarketValueProxy string
LiquidityProviderFeeShare []*LiquidityProviderFeeShare
LiquidityProviderSLA []*LiquidityProviderSLA
NextMTM int64
MarketGrowth num.Decimal
ProductData *ProductData
NextNetClose int64
MarkPriceType CompositePriceType
MarkPriceState *CompositePriceState
}
func (m MarketData) DeepClone() *MarketData {
cpy := m
cpy.MarkPrice = m.MarkPrice.Clone()
cpy.LastTradedPrice = m.LastTradedPrice.Clone()
cpy.BestBidPrice = m.BestBidPrice.Clone()
cpy.BestOfferPrice = m.BestOfferPrice.Clone()
cpy.BestStaticBidPrice = m.BestStaticBidPrice.Clone()
cpy.BestStaticOfferPrice = m.BestStaticOfferPrice.Clone()
cpy.MidPrice = m.MidPrice.Clone()
cpy.StaticMidPrice = m.StaticMidPrice.Clone()
cpy.IndicativePrice = m.IndicativePrice.Clone()
cpy.PriceMonitoringBounds = make([]*PriceMonitoringBounds, 0, len(m.PriceMonitoringBounds))
for _, pmb := range m.PriceMonitoringBounds {
cpy.PriceMonitoringBounds = append(cpy.PriceMonitoringBounds, pmb.DeepClone())
}
lpfs := make([]*LiquidityProviderFeeShare, 0, len(m.LiquidityProviderFeeShare))
for _, fs := range m.LiquidityProviderFeeShare {
lpfs = append(lpfs, proto.Clone(fs).(*LiquidityProviderFeeShare))
}
cpy.LiquidityProviderFeeShare = lpfs
lpsla := make([]*LiquidityProviderSLA, 0, len(m.LiquidityProviderSLA))
for _, sla := range m.LiquidityProviderSLA {
lpsla = append(lpsla, proto.Clone(sla).(*LiquidityProviderSLA))
}
cpy.LiquidityProviderSLA = lpsla
cpy.MarkPriceState = m.MarkPriceState.DeepClone()
return &cpy
}
func (m MarketData) IntoProto() *vegapb.MarketData {
var markPriceState *vegapb.CompositePriceState
if m.MarkPriceState != nil {
markPriceState = m.MarkPriceState.IntoProto()
}
r := &vegapb.MarketData{
MarkPrice: num.UintToString(m.MarkPrice),
LastTradedPrice: num.UintToString(m.LastTradedPrice),
BestBidPrice: num.UintToString(m.BestBidPrice),
BestBidVolume: m.BestBidVolume,
BestOfferPrice: num.UintToString(m.BestOfferPrice),
BestOfferVolume: m.BestOfferVolume,
BestStaticBidPrice: num.UintToString(m.BestStaticBidPrice),
BestStaticBidVolume: m.BestStaticBidVolume,
BestStaticOfferPrice: num.UintToString(m.BestStaticOfferPrice),
BestStaticOfferVolume: m.BestStaticOfferVolume,
MidPrice: num.UintToString(m.MidPrice),
StaticMidPrice: num.UintToString(m.StaticMidPrice),
Market: m.Market,
Timestamp: m.Timestamp,
OpenInterest: m.OpenInterest,
AuctionEnd: m.AuctionEnd,
AuctionStart: m.AuctionStart,
IndicativePrice: num.UintToString(m.IndicativePrice),
IndicativeVolume: m.IndicativeVolume,
MarketTradingMode: m.MarketTradingMode,
MarketState: m.MarketState,
Trigger: m.Trigger,
ExtensionTrigger: m.ExtensionTrigger,
TargetStake: m.TargetStake,
SuppliedStake: m.SuppliedStake,
PriceMonitoringBounds: make([]*vegapb.PriceMonitoringBounds, 0, len(m.PriceMonitoringBounds)),
MarketValueProxy: m.MarketValueProxy,
LiquidityProviderFeeShare: make([]*vegapb.LiquidityProviderFeeShare, 0, len(m.LiquidityProviderFeeShare)),
LiquidityProviderSla: make([]*vegapb.LiquidityProviderSLA, 0, len(m.LiquidityProviderSLA)),
NextMarkToMarket: m.NextMTM,
MarketGrowth: m.MarketGrowth.String(),
NextNetworkCloseout: m.NextNetClose,
MarkPriceType: m.MarkPriceType,
MarkPriceState: markPriceState,
}
for _, pmb := range m.PriceMonitoringBounds {
r.PriceMonitoringBounds = append(r.PriceMonitoringBounds, pmb.IntoProto())
}
for _, lpfs := range m.LiquidityProviderFeeShare {
r.LiquidityProviderFeeShare = append(r.LiquidityProviderFeeShare, proto.Clone(lpfs).(*vegapb.LiquidityProviderFeeShare)) // call IntoProto if this type gets updated
}
for _, lpfs := range m.LiquidityProviderSLA {
r.LiquidityProviderSla = append(r.LiquidityProviderSla, proto.Clone(lpfs).(*vegapb.LiquidityProviderSLA)) // call IntoProto if this type gets updated
}
if m.ProductData != nil {
r.ProductData = m.ProductData.Data.IntoProto()
}