-
Notifications
You must be signed in to change notification settings - Fork 0
/
clob_pair.go
100 lines (86 loc) · 2.84 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
package clob
import (
clobtypes "github.com/furyanprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/furyanprotocol/v4-chain/protocol/x/subaccounts/types"
)
// MustPerpetualId is a wrapper around ClobPair.GetPerpetualId() which panics if an error is returned.
func MustPerpetualId(clobPair clobtypes.ClobPair) uint32 {
perpetualId, err := clobPair.GetPerpetualId()
if err != nil {
panic(err)
}
return perpetualId
}
type ClobModifierOption func(cp *clobtypes.ClobPair)
func WithId(id uint32) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.Id = id
}
}
func WithStepBaseQuantums(bq satypes.BaseQuantums) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.StepBaseQuantums = bq.ToUint64()
}
}
func WithStatus(status clobtypes.ClobPair_Status) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.Status = status
}
}
func WithSubticksPerTick(subticks uint32) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.SubticksPerTick = subticks
}
}
func WithQuantumConversionExponent(exponent int32) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.QuantumConversionExponent = exponent
}
}
func WithPerpetualMetadata(metadata *clobtypes.ClobPair_PerpetualClobMetadata) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.Metadata = metadata
}
}
func WithPerpetualId(perpetualId uint32) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.Metadata = &clobtypes.ClobPair_PerpetualClobMetadata{
PerpetualClobMetadata: &clobtypes.PerpetualClobMetadata{
PerpetualId: perpetualId,
},
}
}
}
func WithSpotMetadata(metadata *clobtypes.ClobPair_SpotClobMetadata) ClobModifierOption {
return func(cp *clobtypes.ClobPair) {
cp.Metadata = metadata
}
}
// GenerateClobPair returns a `ClobPair` object set to default values.
// Passing in `ClobModifierOption` methods alters the value of the `ClobPair` returned.
// It will start with the default, valid `ClobPair` value defined within the method
// and make the requested modifications before returning the object.
//
// Example usage:
// `GenerateClobPair(WithQuantumConversionExponent(25))`
// This will start with the default `ClobPair` object defined within the method and
// return the newly-created object after overriding the values of
// `QuantumConversionExponent` to 25.
func GenerateClobPair(optionalModifications ...ClobModifierOption) *clobtypes.ClobPair {
clobPair := &clobtypes.ClobPair{
Metadata: &clobtypes.ClobPair_PerpetualClobMetadata{
PerpetualClobMetadata: &clobtypes.PerpetualClobMetadata{
PerpetualId: 0,
},
},
Id: 0,
StepBaseQuantums: 5,
SubticksPerTick: 10,
QuantumConversionExponent: -8,
Status: clobtypes.ClobPair_STATUS_ACTIVE,
}
for _, opt := range optionalModifications {
opt(clobPair)
}
return clobPair
}