-
Notifications
You must be signed in to change notification settings - Fork 126
/
enums.go
191 lines (159 loc) · 4.22 KB
/
enums.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
package quotes
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
)
// DriverType is enum that represents
// all available quotes providers.
type DriverType struct {
slug string
}
func (d DriverType) String() string {
return d.slug
}
var (
DriverBinance = DriverType{"binance"}
DriverKraken = DriverType{"kraken"}
DriverOpendax = DriverType{"opendax"}
DriverBitfaker = DriverType{"bitfaker"}
DriverUniswapV3 = DriverType{"uniswap_v3"}
DriverSyncswap = DriverType{"syncswap"}
DriverQuickswap = DriverType{"quickswap"}
DriverSectaV2 = DriverType{"secta_v2"}
DriverSectaV3 = DriverType{"secta_v3"}
DriverInternal = DriverType{"internal"} // Internal trades
)
func ToDriverType(raw string) (DriverType, error) {
allDrivers := map[string]DriverType{
DriverBinance.String(): DriverBinance,
DriverKraken.String(): DriverKraken,
DriverOpendax.String(): DriverOpendax,
DriverBitfaker.String(): DriverBitfaker,
DriverUniswapV3.String(): DriverUniswapV3,
DriverSyncswap.String(): DriverSyncswap,
DriverQuickswap.String(): DriverQuickswap,
DriverSectaV2.String(): DriverSectaV2,
DriverSectaV3.String(): DriverSectaV3,
DriverInternal.String(): DriverInternal,
}
driver, ok := allDrivers[raw]
if !ok {
return DriverType{}, fmt.Errorf("invalid driver type: %v", raw)
}
return driver, nil
}
func (t DriverType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.slug)
}
func (t *DriverType) UnmarshalJSON(raw []byte) error {
var rawParsed string
if err := json.Unmarshal(raw, &rawParsed); err != nil {
return err
}
typ, err := ToDriverType(rawParsed)
if err != nil {
return err
}
*t = typ
return nil
}
func (t DriverType) MarshalYAML() (any, error) {
return t.slug, nil
}
func (t *DriverType) UnmarshalYAML(value *yaml.Node) error {
var raw string
if err := value.Decode(&raw); err != nil {
return err
}
typ, err := ToDriverType(raw)
if err != nil {
return err
}
*t = typ
return nil
}
func (t *DriverType) SetValue(s string) error {
typ, err := ToDriverType(s)
if err != nil {
return err
}
*t = typ
return nil
}
// TakerType is enum that represents
// the side of taker in a trade.
type TakerType struct {
slug string
}
func (t TakerType) String() string {
return t.slug
}
var (
// TakerTypeUnknown represents the trade,
// for which you can't determine its type,
// and therefore the taker side cannot be deduced.
TakerTypeUnknown = TakerType{""}
// TakerTypeBuy represents a "buy" trade.
// Its value is set to "sell" because the sell order it
// was matched with was present in the order book before,
// therefore the taker is the "sell" side.
TakerTypeBuy = TakerType{"sell"}
// TakerTypeSell represents a "sell" trade.
// It's value is set to "buy" because the buy order it
// was matched with was present in the order book before,
// therefore the taker is the "buy" side.
TakerTypeSell = TakerType{"buy"}
)
func ToTakerType(raw string) (TakerType, error) {
allTypes := map[string]TakerType{
TakerTypeUnknown.String(): TakerTypeUnknown,
TakerTypeBuy.String(): TakerTypeBuy,
TakerTypeSell.String(): TakerTypeSell,
}
typ, ok := allTypes[raw]
if !ok {
return TakerType{}, fmt.Errorf("invalid taker type: %v", raw)
}
return typ, nil
}
func (t TakerType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.slug)
}
func (t *TakerType) UnmarshalJSON(raw []byte) error {
var rawParsed string
if err := json.Unmarshal(raw, &rawParsed); err != nil {
return err
}
typ, err := ToTakerType(rawParsed)
if err != nil {
return err
}
*t = typ
return nil
}
func (t TakerType) MarshalYAML() (any, error) {
return t.slug, nil
}
func (t *TakerType) UnmarshalYAML(value *yaml.Node) error {
var raw string
if err := value.Decode(&raw); err != nil {
return err
}
typ, err := ToTakerType(raw)
if err != nil {
return err
}
*t = typ
return nil
}
// ExchangeType is enum that represents the type of exchange.
// It is not implemented with struct based enums pattern because
// it's not used as an input parameter for any function or method.
type ExchangeType string
var (
ExchangeTypeUnspecified ExchangeType = ""
ExchangeTypeCEX ExchangeType = "cex"
ExchangeTypeDEX ExchangeType = "dex"
ExchangeTypeHybrid ExchangeType = "hybrid"
)