-
Notifications
You must be signed in to change notification settings - Fork 85
/
order_fill.go
68 lines (64 loc) · 2.67 KB
/
order_fill.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
package events
import (
"fmt"
v1 "github.com/dydxprotocol/v4-chain/protocol/indexer/protocol/v1"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
// NewOrderFillEvent creates a new OrderFillEvent proto message given the maker and taker orders along
// with the fill and fee amounts. Note: This function does no validation of the input maker/taker orders
// or the fill amount and assumes all such validation has been done before constructing the event.
func NewOrderFillEvent(
makerOrder clobtypes.Order,
takerOrder clobtypes.Order,
fillAmount satypes.BaseQuantums,
makerFee int64,
takerFee int64,
totalFilledMaker satypes.BaseQuantums,
totalFilledTaker satypes.BaseQuantums,
) *OrderFillEventV1 {
indexerTakerOrder := v1.OrderToIndexerOrder(takerOrder)
return &OrderFillEventV1{
MakerOrder: v1.OrderToIndexerOrder(makerOrder),
TakerOrder: &OrderFillEventV1_Order{
Order: &indexerTakerOrder,
},
FillAmount: fillAmount.ToUint64(),
MakerFee: makerFee,
TakerFee: takerFee,
TotalFilledMaker: totalFilledMaker.ToUint64(),
TotalFilledTaker: totalFilledTaker.ToUint64(),
}
}
// NewLiquidationOrderFillEvent creates a new OrderFillEvent proto message given the maker and liquidation
// taker orders along with the fill and fee amounts. Panics if the taker order is not a liquidation order.
// The taker fee here refers to the special liquidation fee, not the standard taker fee.
func NewLiquidationOrderFillEvent(
makerOrder clobtypes.Order,
liquidationTakerOrder clobtypes.MatchableOrder,
fillAmount satypes.BaseQuantums,
makerFee int64,
takerFee int64,
totalFilledMaker satypes.BaseQuantums,
) *OrderFillEventV1 {
if !liquidationTakerOrder.IsLiquidation() {
panic(fmt.Sprintf("liquidationTakerOrder is not a liquidation order: %v", liquidationTakerOrder))
}
liquidationOrder := LiquidationOrderV1{
Liquidated: v1.SubaccountIdToIndexerSubaccountId(liquidationTakerOrder.GetSubaccountId()),
ClobPairId: liquidationTakerOrder.GetClobPairId().ToUint32(),
PerpetualId: liquidationTakerOrder.MustGetLiquidatedPerpetualId(),
TotalSize: uint64(liquidationTakerOrder.GetBaseQuantums()),
IsBuy: liquidationTakerOrder.IsBuy(),
Subticks: uint64(liquidationTakerOrder.GetOrderSubticks()),
}
return &OrderFillEventV1{
MakerOrder: v1.OrderToIndexerOrder(makerOrder),
TakerOrder: &OrderFillEventV1_LiquidationOrder{LiquidationOrder: &liquidationOrder},
FillAmount: fillAmount.ToUint64(),
MakerFee: makerFee,
TakerFee: takerFee,
TotalFilledMaker: totalFilledMaker.ToUint64(),
TotalFilledTaker: fillAmount.ToUint64(),
}
}