-
Notifications
You must be signed in to change notification settings - Fork 91
/
liquidation_order.go
125 lines (108 loc) · 4.59 KB
/
liquidation_order.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
package types
import (
"crypto/sha256"
"math/big"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
var _ MatchableOrder = &LiquidationOrder{}
// LiquidationOrder is used to represent an IOC liquidation order.
type LiquidationOrder struct {
// Information about this liquidation order.
perpetualLiquidationInfo PerpetualLiquidationInfo
// CLOB pair ID of the CLOB pair the liquidation order will be matched against.
clobPairId ClobPairId
// True if this is a buy order liquidating a short position, false if vice versa.
isBuy bool
// The number of base quantums for this liquidation order.
quantums satypes.BaseQuantums
// The subticks this order will be submitted at.
subticks Subticks
}
// NewLiquidationOrder creates and returns a new liquidation order.
// This function will panic if the caller attempts to create a liquidation order with a non-perpetual
// CLOB pair.
func NewLiquidationOrder(
subaccountId satypes.SubaccountId,
clobPair ClobPair,
isBuy bool,
quantums satypes.BaseQuantums,
subticks Subticks,
) *LiquidationOrder {
// If this is not a perpetual CLOB, panic.
perpetualClobMetadata := clobPair.GetPerpetualClobMetadata()
if perpetualClobMetadata == nil {
panic("NewLiquidationOrder: Attempting to create liquidation order with a non-perpetual CLOB pair")
}
return &LiquidationOrder{
perpetualLiquidationInfo: PerpetualLiquidationInfo{
SubaccountId: subaccountId,
PerpetualId: perpetualClobMetadata.PerpetualId,
},
clobPairId: clobPair.GetClobPairId(),
isBuy: isBuy,
quantums: quantums,
subticks: subticks,
}
}
// IsBuy returns true if this is a buy order, false if not.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) IsBuy() bool {
return lo.isBuy
}
// GetOrderHash returns the SHA256 hash of the `PerpetualLiquidationInfo` field.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) GetOrderHash() OrderHash {
perpetualLiquidationInfoBytes, err := lo.perpetualLiquidationInfo.Marshal()
if err != nil {
panic(err)
}
return sha256.Sum256(perpetualLiquidationInfoBytes)
}
// GetBaseQuantums returns the quantums of this liquidation order.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) GetBaseQuantums() satypes.BaseQuantums {
return satypes.BaseQuantums(lo.quantums)
}
// GetClobPairId returns the CLOB pair ID of this liquidation order.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) GetClobPairId() ClobPairId {
return lo.clobPairId
}
// GetOrderSubticks returns the subticks of this liquidation order.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) GetOrderSubticks() Subticks {
return Subticks(lo.subticks)
}
// GetSubaccountId returns the subaccount ID that is being liquidated.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) GetSubaccountId() satypes.SubaccountId {
return lo.perpetualLiquidationInfo.SubaccountId
}
// IsLiquidation always returns true since this order is a liquidation order.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) IsLiquidation() bool {
return true
}
// MustGetOrder always panics since there is no underlying `Order` type for a liquidation.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) MustGetOrder() Order {
panic("MustGetOrder: No underlying order on a LiquidationOrder type.")
}
// MustGetLiquidatedPerpetualId returns the perpetual ID that this perpetual order is liquidating.
// This function is necessary for the `LiquidationOrder` type to implement the `MatchableOrder` interface.
func (lo *LiquidationOrder) MustGetLiquidatedPerpetualId() uint32 {
return lo.perpetualLiquidationInfo.PerpetualId
}
// IsReduceOnly returns whether this is a reduce-only order. This always returns false
// for liquidation orders.
func (o *LiquidationOrder) IsReduceOnly() bool {
return false
}
// GetDeltaQuantums returns the delta quantums of this liquidation order.
func (o *LiquidationOrder) GetDeltaQuantums() *big.Int {
deltaQuantums := o.GetBaseQuantums().ToBigInt()
if !o.IsBuy() {
deltaQuantums.Neg(deltaQuantums)
}
return deltaQuantums
}