-
Notifications
You must be signed in to change notification settings - Fork 91
/
clob_pair.go
106 lines (91 loc) · 3.23 KB
/
clob_pair.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
package types
import (
errorsmod "cosmossdk.io/errors"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
// SupportedClobPairStatusTransitions has keys corresponding to currently-supported
// ClobPair_Status types with values equal to the set of ClobPair_Status types that
// may be transitioned to from this state. Note the keys of this map may be
// a subset of the types defined in the proto for ClobPair_Status.
var SupportedClobPairStatusTransitions = map[ClobPair_Status]map[ClobPair_Status]struct{}{
ClobPair_STATUS_ACTIVE: {},
ClobPair_STATUS_INITIALIZING: {
ClobPair_STATUS_ACTIVE: struct{}{},
},
}
// IsSupportedClobPairStatus returns true if the provided ClobPair_Status is in the list
// of currently supported ClobPair_Status types. Else, returns false.
func IsSupportedClobPairStatus(clobPairStatus ClobPair_Status) bool {
_, exists := SupportedClobPairStatusTransitions[clobPairStatus]
return exists
}
// IsSupportedClobPairStatusTransition returns true if it is considered valid to transition from
// the first provided ClobPair_Status to the second provided ClobPair_Status. Else, returns false.
func IsSupportedClobPairStatusTransition(from ClobPair_Status, to ClobPair_Status) bool {
_, exists := SupportedClobPairStatusTransitions[from][to]
return exists
}
func (c *ClobPair) GetClobPairSubticksPerTick() SubticksPerTick {
return SubticksPerTick(c.SubticksPerTick)
}
func (c *ClobPair) GetClobPairMinOrderBaseQuantums() satypes.BaseQuantums {
return satypes.BaseQuantums(c.StepBaseQuantums)
}
// GetPerpetualId returns the `PerpetualId` for the provided `clobPair`.
func (c *ClobPair) GetPerpetualId() (uint32, error) {
perpetualClobMetadata := c.GetPerpetualClobMetadata()
if perpetualClobMetadata == nil {
return 0, ErrAssetOrdersNotImplemented
}
return perpetualClobMetadata.PerpetualId, nil
}
// MustGetPerpetualId returns the `PerpetualId` for the provided `clobPair`.
// Will panic if `GetPerpetualId` returns an error.
func (c *ClobPair) MustGetPerpetualId() uint32 {
id, err := c.GetPerpetualId()
if err != nil {
panic(err)
}
return id
}
// GetId returns the `ClobPairId` for the provided `clobPair`.
func (c *ClobPair) GetClobPairId() ClobPairId {
return ClobPairId(c.Id)
}
// Stateless validation on ClobPair.
func (c *ClobPair) Validate() error {
switch c.Metadata.(type) {
// TODO(DEC-1535): update this when additional clob pair types are supported.
case *ClobPair_SpotClobMetadata:
return errorsmod.Wrapf(
ErrInvalidClobPairParameter,
"CLOB pair (%+v) is not a perpetual CLOB.",
c,
)
}
if !IsSupportedClobPairStatus(c.Status) {
return errorsmod.Wrapf(
ErrInvalidClobPairParameter,
"CLOB pair (%+v) has unsupported status %+v",
c,
c.Status,
)
}
if c.StepBaseQuantums <= 0 {
return errorsmod.Wrapf(
ErrInvalidClobPairParameter,
"invalid ClobPair parameter: StepBaseQuantums must be > 0. Got %v",
c.StepBaseQuantums,
)
}
// Since a subtick will be calculated as (1 tick/SubticksPerTick), the denominator cannot be 0
// and negative numbers do not make sense.
if c.SubticksPerTick <= 0 {
return errorsmod.Wrapf(
ErrInvalidClobPairParameter,
"invalid ClobPair parameter: SubticksPerTick must be > 0. Got %v",
c.SubticksPerTick,
)
}
return nil
}