-
Notifications
You must be signed in to change notification settings - Fork 22
/
order.go
117 lines (98 loc) · 4.63 KB
/
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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package service
import (
"context"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/utils"
"code.vegaprotocol.io/vega/logging"
)
//nolint:interfacebloat
type orderStore interface {
Flush(ctx context.Context) ([]entities.Order, error)
Add(o entities.Order) error
GetAll(ctx context.Context) ([]entities.Order, error)
GetOrder(ctx context.Context, orderID string, version *int32) (entities.Order, error)
GetByMarket(ctx context.Context, marketID string, p entities.OffsetPagination) ([]entities.Order, error)
GetByMarketAndID(ctx context.Context, marketIDstr string, orderIDs []string) ([]entities.Order, error)
GetByParty(ctx context.Context, partyID string, p entities.OffsetPagination) ([]entities.Order, error)
GetByReference(ctx context.Context, reference string, p entities.OffsetPagination) ([]entities.Order, error)
GetAllVersionsByOrderID(ctx context.Context, id string, p entities.OffsetPagination) ([]entities.Order, error)
GetLiveOrders(ctx context.Context) ([]entities.Order, error)
ListOrderVersions(ctx context.Context, orderIDStr string, p entities.CursorPagination) ([]entities.Order, entities.PageInfo, error)
ListOrders(ctx context.Context, party *string, market *string, reference *string, liveOnly bool, p entities.CursorPagination,
dateRange entities.DateRange, filter entities.OrderFilter) ([]entities.Order, entities.PageInfo, error)
}
type Order struct {
store orderStore
observer utils.Observer[entities.Order]
}
func NewOrder(store orderStore, log *logging.Logger) *Order {
return &Order{
store: store,
observer: utils.NewObserver[entities.Order]("order", log, 0, 0),
}
}
func (o *Order) ObserveOrders(ctx context.Context, retries int, market *string, party *string, includeLiquidity bool) (<-chan []entities.Order, uint64) {
ch, ref := o.observer.Observe(ctx,
retries,
func(o entities.Order) bool {
marketOk := market == nil || o.MarketID.String() == *market
partyOk := party == nil || o.PartyID.String() == *party
liqOrder := (o.LpID != nil && includeLiquidity) || !includeLiquidity
return marketOk && partyOk && liqOrder
})
return ch, ref
}
func (o *Order) Flush(ctx context.Context) error {
flushed, err := o.store.Flush(ctx)
if err != nil {
return err
}
o.observer.Notify(flushed)
return nil
}
func (o *Order) Add(order entities.Order) error {
return o.store.Add(order)
}
func (o *Order) GetAll(ctx context.Context) ([]entities.Order, error) {
return o.store.GetAll(ctx)
}
func (o *Order) GetOrder(ctx context.Context, orderID string, version *int32) (entities.Order, error) {
return o.store.GetOrder(ctx, orderID, version)
}
func (o *Order) GetByMarketAndID(ctx context.Context, marketIDstr string, orderIDs []string) ([]entities.Order, error) {
return o.store.GetByMarketAndID(ctx, marketIDstr, orderIDs)
}
func (o *Order) GetByMarket(ctx context.Context, marketID string, p entities.OffsetPagination) ([]entities.Order, error) {
return o.store.GetByMarket(ctx, marketID, p)
}
func (o *Order) GetByParty(ctx context.Context, partyID string, p entities.OffsetPagination) ([]entities.Order, error) {
return o.store.GetByParty(ctx, partyID, p)
}
func (o *Order) GetByReference(ctx context.Context, reference string, p entities.OffsetPagination) ([]entities.Order, error) {
return o.store.GetByReference(ctx, reference, p)
}
func (o *Order) GetAllVersionsByOrderID(ctx context.Context, id string, p entities.OffsetPagination) ([]entities.Order, error) {
return o.store.GetAllVersionsByOrderID(ctx, id, p)
}
func (o *Order) GetLiveOrders(ctx context.Context) ([]entities.Order, error) {
return o.store.GetLiveOrders(ctx)
}
func (o *Order) ListOrders(ctx context.Context, party *string, market *string, reference *string, liveOnly bool,
p entities.CursorPagination, dateRange entities.DateRange, filter entities.OrderFilter,
) ([]entities.Order, entities.PageInfo, error) {
return o.store.ListOrders(ctx, party, market, reference, liveOnly, p, dateRange, filter)
}
func (o *Order) ListOrderVersions(ctx context.Context, orderID string, p entities.CursorPagination) ([]entities.Order, entities.PageInfo, error) {
return o.store.ListOrderVersions(ctx, orderID, p)
}