-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_matching.go
152 lines (131 loc) · 3.83 KB
/
order_matching.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
package grpc
import (
"context"
"github.com/cockroachdb/apd"
"github.com/rs/zerolog/log"
pb "github.com/karta0898098/mome/pb/order"
"github.com/karta0898098/mome/pkg/order"
)
// check OrderMatchingHandler is implement for pb.OrderMatchingServiceServer
var _ pb.OrderMatchingServiceServer = &OrderMatchingHandler{}
// OrderMatchingHandler is handler convert gRPC request to service
type OrderMatchingHandler struct {
provider order.Provider
}
// NewOrderMatchingHandler new OrderMatchingHandler method
func NewOrderMatchingHandler(provider order.Provider) *OrderMatchingHandler {
return &OrderMatchingHandler{provider: provider}
}
// SubmitOrder is implement for pb.OrderMatchingServiceServer
func (h *OrderMatchingHandler) SubmitOrder(ctx context.Context, req *pb.SubmitOrderRequest) (*pb.SubmitOrderReply, error) {
logger := log.Ctx(ctx)
logger.Debug().Interface("req", req).Msg("debug...")
price := apd.New(0, 0)
if req.Price != nil {
price = apd.New(req.Price.Coefficient, req.Price.Exponent)
}
stopPrice := apd.New(0, 0)
if req.StopPrice != nil {
stopPrice = apd.New(req.StopPrice.Coefficient, req.StopPrice.Exponent)
}
var params order.Condition
switch req.Params {
case pb.OrderParams_ORDER_PARAMS_STOP:
params |= order.ConditionStop
case pb.OrderParams_ORDER_PARAMS_AON:
params |= order.ConditionAON
case pb.OrderParams_ORDER_PARAMS_IOC:
params |= order.ConditionIOC
case pb.OrderParams_ORDER_PARAMS_FOK:
params |= order.ConditionFOK
case pb.OrderParams_ORDER_PARAMS_GTC:
params |= order.ConditionGTC
case pb.OrderParams_ORDER_PARAMS_GFD:
params |= order.ConditionGFD
case pb.OrderParams_ORDER_PARAMS_GTD:
params |= order.ConditionGTD
}
o, err := order.NewOrder(
req.Symbol,
req.CustomerID,
order.Kind(req.Kind),
params,
req.Quantity,
price,
stopPrice,
order.Side(req.Side),
)
if err != nil {
return nil, err
}
err = h.provider.SubmitOrder(ctx, o)
if err != nil {
return nil, err
}
return &pb.SubmitOrderReply{
OrderID: o.ID,
CreatedAtMilli: o.CreatedAt.UnixMilli(),
}, nil
}
// ListAllAsks is implement for pb.OrderMatchingServiceServer
func (h *OrderMatchingHandler) ListAllAsks(ctx context.Context, req *pb.ListAllAsksRequest) (*pb.ListAllAskReply, error) {
orders, err := h.provider.ListAllAsks(ctx, req.Symbol)
if err != nil {
return nil, err
}
reply := &pb.ListAllAskReply{
Orders: nil,
}
for _, o := range orders {
o := o
params := pb.OrderParams_value["ORDER_PARAMS_"+o.Params.String()]
reply.Orders = append(reply.Orders, &pb.Order{
ID: o.ID,
Kind: pb.OrderKind(o.Kind),
Price: &pb.Price{
Coefficient: o.Price.Coeff.Int64(),
Exponent: o.Price.Exponent,
},
StopPrice: &pb.Price{
Coefficient: o.StopPrice.Coeff.Int64(),
Exponent: o.StopPrice.Exponent,
},
CreatedAtMilli: o.CreatedAt.UnixMilli(),
Quantity: o.Qty,
FilledQuantity: o.FilledQty,
Params: pb.OrderParams(params),
})
}
return reply, nil
}
// ListAllBids is implement for pb.OrderMatchingServiceServer
func (h *OrderMatchingHandler) ListAllBids(ctx context.Context, req *pb.ListAllBidsRequest) (*pb.ListAllBidsReply, error) {
orders, err := h.provider.ListAllBids(ctx, req.Symbol)
if err != nil {
return nil, err
}
reply := &pb.ListAllBidsReply{
Orders: nil,
}
for _, o := range orders {
o := o
params := pb.OrderParams_value["ORDER_PARAMS_"+o.Params.String()]
reply.Orders = append(reply.Orders, &pb.Order{
ID: o.ID,
Kind: pb.OrderKind(o.Kind),
Price: &pb.Price{
Coefficient: o.Price.Coeff.Int64(),
Exponent: o.Price.Exponent,
},
StopPrice: &pb.Price{
Coefficient: o.StopPrice.Coeff.Int64(),
Exponent: o.StopPrice.Exponent,
},
CreatedAtMilli: o.CreatedAt.UnixMilli(),
Quantity: o.Qty,
FilledQuantity: o.FilledQty,
Params: pb.OrderParams(params),
})
}
return reply, nil
}