forked from InjectiveLabs/sdk-go
/
exchange.go
239 lines (199 loc) · 5.46 KB
/
exchange.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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)
type MatchedMarketDirection struct {
MarketId common.Hash
BuysExists bool
SellsExists bool
}
type TriggeredOrdersInMarket struct {
Market *DerivativeMarket
MarkPrice sdk.Dec
MarketOrders []*DerivativeMarketOrder
LimitOrders []*DerivativeLimitOrder
HasLimitBuyOrders bool
HasLimitSellOrders bool
}
func (e ExecutionType) IsMarket() bool {
return e == ExecutionType_Market
}
func (e ExecutionType) IsMaker() bool {
return !e.IsTaker()
}
func (e ExecutionType) IsTaker() bool {
return e == ExecutionType_Market || e == ExecutionType_LimitMatchNewOrder
}
func (s MarketStatus) SupportsOrderCancellations() bool {
switch s {
case MarketStatus_Active, MarketStatus_Demolished, MarketStatus_Expired:
return true
case MarketStatus_Paused:
return false
default:
return false
}
}
type TradingRewardAccountPoints struct {
Account sdk.AccAddress
Points sdk.Dec
}
func (p *PointsMultiplier) GetMultiplier(e ExecutionType) sdk.Dec {
if e.IsMaker() {
return p.MakerPointsMultiplier
}
return p.TakerPointsMultiplier
}
type IOrder interface {
GetPrice() sdk.Dec
GetQuantity() sdk.Dec
GetFillable() sdk.Dec
IsBuy() bool
GetSubaccountID() common.Hash
}
// IDerivativeOrder proto interface for wrapping all different variations of representations of derivative orders. Methods can be added as needed (make sure to add to every implementor)
type IDerivativeOrder interface {
IOrder
GetMargin() sdk.Dec
IsReduceOnly() bool
IsVanilla() bool
}
type IMutableDerivativeOrder interface {
IDerivativeOrder
SetPrice(sdk.Dec)
SetQuantity(sdk.Dec)
SetMargin(sdk.Dec)
}
// DerivativeOrder - IMutableDerivativeOrder implementation
func (o *DerivativeOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *DerivativeOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *DerivativeOrder) GetFillable() sdk.Dec {
return o.GetQuantity()
}
func (o *DerivativeOrder) GetMargin() sdk.Dec {
return o.Margin
}
func (o *DerivativeOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (o *DerivativeOrder) SetPrice(price sdk.Dec) {
o.OrderInfo.Price = price
}
func (o *DerivativeOrder) SetQuantity(quantity sdk.Dec) {
o.OrderInfo.Quantity = quantity
}
func (o *DerivativeOrder) SetMargin(margin sdk.Dec) {
o.Margin = margin
}
// DerivativeLimitOrder - IMutableDerivativeOrder implementation
func (o *DerivativeLimitOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *DerivativeLimitOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *DerivativeLimitOrder) GetFillable() sdk.Dec {
return o.Fillable
}
func (o *DerivativeLimitOrder) GetMargin() sdk.Dec {
return o.Margin
}
func (o *DerivativeLimitOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (o *DerivativeLimitOrder) SetPrice(price sdk.Dec) {
o.OrderInfo.Price = price
}
func (o *DerivativeLimitOrder) SetQuantity(quantity sdk.Dec) {
o.OrderInfo.Quantity = quantity
o.Fillable = quantity
}
func (o *DerivativeLimitOrder) SetMargin(margin sdk.Dec) {
o.Margin = margin
}
// DerivativeMarketOrder - IMutableDerivativeOrder implementation
func (o *DerivativeMarketOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *DerivativeMarketOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *DerivativeMarketOrder) GetFillable() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *DerivativeMarketOrder) GetMargin() sdk.Dec {
return o.Margin
}
func (o *DerivativeMarketOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (o *DerivativeMarketOrder) SetPrice(price sdk.Dec) {
o.OrderInfo.Price = price
}
func (o *DerivativeMarketOrder) SetQuantity(quantity sdk.Dec) {
o.OrderInfo.Quantity = quantity
}
func (o *DerivativeMarketOrder) SetMargin(margin sdk.Dec) {
o.Margin = margin
}
func (o *DerivativeMarketOrder) DebugString() string {
return fmt.Sprintf("(q:%v, p:%v, m:%v, isLong: %v)", o.Quantity(), o.Price(), o.Margin, o.IsBuy())
}
// spot orders
func (o *SpotOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *SpotLimitOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *SpotMarketOrder) GetPrice() sdk.Dec {
return o.OrderInfo.Price
}
func (o *SpotOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *SpotLimitOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *SpotMarketOrder) GetQuantity() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *SpotMarketOrder) IsBuy() bool {
return o.OrderType.IsBuy()
}
func (o *SpotOrder) GetFillable() sdk.Dec {
return o.OrderInfo.Quantity
}
func (o *SpotMarketOrder) GetFillable() sdk.Dec {
// no fillable for market order, but quantity works same in this case
return o.OrderInfo.Quantity
}
func (o *SpotLimitOrder) GetFillable() sdk.Dec {
return o.Fillable
}
func (o *SpotOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (o *SpotMarketOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (o *SpotLimitOrder) GetSubaccountID() common.Hash {
return o.OrderInfo.SubaccountID()
}
func (al AtomicMarketOrderAccessLevel) IsValid() bool {
switch al {
case AtomicMarketOrderAccessLevel_Nobody,
AtomicMarketOrderAccessLevel_SmartContractsOnly,
AtomicMarketOrderAccessLevel_BeginBlockerSmartContractsOnly,
AtomicMarketOrderAccessLevel_Everyone:
return true
default:
return false
}
}