-
Notifications
You must be signed in to change notification settings - Fork 91
/
internal_operation.go
164 lines (150 loc) · 4.82 KB
/
internal_operation.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
package types
import (
"fmt"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
// NewShortTermOrderPlacementInternalOperation returns a new internal operation for placing
// a Short-Term order.
// This function will panic if it's called with a non Short-Term order.
func NewShortTermOrderPlacementInternalOperation(order Order) InternalOperation {
order.OrderId.MustBeShortTermOrder()
return InternalOperation{
Operation: &InternalOperation_ShortTermOrderPlacement{
ShortTermOrderPlacement: NewMsgPlaceOrder(order),
},
}
}
// NewPreexistingStatefulOrderPlacementInternalOperation returns a new internal operation for placing
// a stateful order.
// This function will panic if it's called with a non stateful order.
func NewPreexistingStatefulOrderPlacementInternalOperation(order Order) InternalOperation {
order.OrderId.MustBeStatefulOrder()
return InternalOperation{
Operation: &InternalOperation_PreexistingStatefulOrder{
PreexistingStatefulOrder: &order.OrderId,
},
}
}
// NewMatchOrdersInternalOperation returns a new operation for matching maker orders against a
// taker order.
// This function panics if there are zero maker fills.
func NewMatchOrdersInternalOperation(
takerOrder Order,
makerFills []MakerFill,
) InternalOperation {
if len(makerFills) == 0 {
panic(
fmt.Sprintf(
"NewMatchOrdersInternalOperation: cannot create a match orders "+
"internal operation with no maker fills: %+v",
takerOrder,
),
)
}
return InternalOperation{
Operation: &InternalOperation_Match{
Match: &ClobMatch{
Match: &ClobMatch_MatchOrders{
MatchOrders: &MatchOrders{
TakerOrderId: takerOrder.OrderId,
Fills: makerFills,
},
},
},
},
}
}
// NewMatchPerpetualLiquidationInternalOperation returns a new operation for matching maker orders
// against a perpetual liquidation order.
// This function panics if this is called with a non-liquidation order or there are zero maker fills.
func NewMatchPerpetualLiquidationInternalOperation(
takerLiquidationOrder MatchableOrder,
makerFills []MakerFill,
) InternalOperation {
if !takerLiquidationOrder.IsLiquidation() {
panic(
fmt.Sprintf(
"NewMatchPerpetualLiquidationInternalOperation: called with a non-liquidation order: %+v",
takerLiquidationOrder,
),
)
}
if len(makerFills) == 0 {
panic(
fmt.Sprintf(
"NewMatchPerpetualLiquidationInternalOperation: cannot create a match perpetual "+
"liquidation internal operation with no maker fills: %+v",
takerLiquidationOrder,
),
)
}
return InternalOperation{
Operation: &InternalOperation_Match{
Match: &ClobMatch{
Match: &ClobMatch_MatchPerpetualLiquidation{
MatchPerpetualLiquidation: &MatchPerpetualLiquidation{
Liquidated: takerLiquidationOrder.GetSubaccountId(),
ClobPairId: takerLiquidationOrder.GetClobPairId().ToUint32(),
PerpetualId: takerLiquidationOrder.MustGetLiquidatedPerpetualId(),
TotalSize: takerLiquidationOrder.GetBaseQuantums().ToUint64(),
IsBuy: takerLiquidationOrder.IsBuy(),
Fills: makerFills,
},
},
},
},
}
}
// NewMatchPerpetualDeleveragingInternalOperation returns a new operation for deleveraging liquidated subaccount's
// position against one or more offsetting subaccounts.
// This function panics if there are zero maker fills.
func NewMatchPerpetualDeleveragingInternalOperation(
liquidatedSubaccountId satypes.SubaccountId,
perpetualId uint32,
fills []MatchPerpetualDeleveraging_Fill,
) InternalOperation {
if len(fills) == 0 {
panic(
fmt.Sprintf(
"NewMatchPerpetualDeleveragingInternalOperation: cannot create a match perpetual "+
"deleveraging internal operation with no fills: subaccount (%+v), perpetual (%+v)",
liquidatedSubaccountId,
perpetualId,
),
)
}
return InternalOperation{
Operation: &InternalOperation_Match{
Match: &ClobMatch{
Match: &ClobMatch_MatchPerpetualDeleveraging{
MatchPerpetualDeleveraging: &MatchPerpetualDeleveraging{
Liquidated: liquidatedSubaccountId,
PerpetualId: perpetualId,
Fills: fills,
},
},
},
},
}
}
// NewOrderRemovalInternalOperation returns a new operation for removing an order.
// This function panics if it's called with an order removal containing an OrderId
// for a non stateful order or the removal reason is unspecified.
func NewOrderRemovalInternalOperation(
orderId OrderId,
removalReason OrderRemoval_RemovalReason,
) InternalOperation {
orderId.MustBeStatefulOrder()
if removalReason == OrderRemoval_REMOVAL_REASON_UNSPECIFIED {
panic("NewOrderRemovalInternalOperation: removal reason unspecified")
}
orderRemoval := OrderRemoval{
OrderId: orderId,
RemovalReason: removalReason,
}
return InternalOperation{
Operation: &InternalOperation_OrderRemoval{
OrderRemoval: &orderRemoval,
},
}
}