-
Notifications
You must be signed in to change notification settings - Fork 19
/
orders.go
157 lines (138 loc) · 4.13 KB
/
orders.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA 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.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package types
import (
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/num"
proto "code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
type PriceLevel struct {
Price *num.Uint
NumberOfOrders uint64
Volume uint64
}
func (p PriceLevel) IntoProto() *proto.PriceLevel {
return &proto.PriceLevel{
Price: num.UintToString(p.Price),
NumberOfOrders: p.NumberOfOrders,
Volume: p.Volume,
}
}
type PriceLevels []*PriceLevel
func (p PriceLevels) IntoProto() []*proto.PriceLevel {
out := make([]*proto.PriceLevel, 0, len(p))
for _, v := range p {
out = append(out, v.IntoProto())
}
return out
}
type OrderAmendment struct {
OrderID string
MarketID string
Price *num.Uint
SizeDelta int64
ExpiresAt *int64 // timestamp
TimeInForce OrderTimeInForce
PeggedOffset *num.Uint
PeggedReference PeggedReference
}
func NewOrderAmendmentFromProto(p *commandspb.OrderAmendment) (*OrderAmendment, error) {
var (
price, peggedOffset *num.Uint
exp *int64
)
if p.Price != nil && len(*p.Price) > 0 {
var overflowed bool
price, overflowed = num.UintFromString(*p.Price, 10)
if overflowed {
return nil, errors.New("invalid amount")
}
}
if p.ExpiresAt != nil {
exp = toPtr(*p.ExpiresAt)
}
if p.PeggedOffset != "" {
var overflowed bool
peggedOffset, overflowed = num.UintFromString(p.PeggedOffset, 10)
if overflowed {
return nil, errors.New("invalid offset")
}
}
return &OrderAmendment{
OrderID: p.OrderId,
MarketID: p.MarketId,
Price: price,
SizeDelta: p.SizeDelta,
ExpiresAt: exp,
TimeInForce: p.TimeInForce,
PeggedOffset: peggedOffset,
PeggedReference: p.PeggedReference,
}, nil
}
func (o OrderAmendment) IntoProto() *commandspb.OrderAmendment {
r := &commandspb.OrderAmendment{
OrderId: o.OrderID,
MarketId: o.MarketID,
SizeDelta: o.SizeDelta,
TimeInForce: o.TimeInForce,
PeggedReference: o.PeggedReference,
}
if o.Price != nil {
r.Price = toPtr(num.UintToString(o.Price))
}
if o.ExpiresAt != nil {
r.ExpiresAt = toPtr(*o.ExpiresAt)
}
if o.PeggedOffset != nil {
r.PeggedOffset = o.PeggedOffset.String()
}
return r
}
// Validate santiy-checks the order amendment as-is, the market will further validate the amendment
// based on the order it's actually trying to amend.
func (o OrderAmendment) Validate() error {
// check TIME_IN_FORCE and expiry
if o.TimeInForce == OrderTimeInForceGTT && o.ExpiresAt == nil {
return OrderErrorCannotAmendToGTTWithoutExpiryAt
}
if o.TimeInForce == OrderTimeInForceGTC && o.ExpiresAt != nil {
// this is cool, but we need to ensure and expiry is not set
return OrderErrorCannotHaveGTCAndExpiryAt
}
if o.TimeInForce == OrderTimeInForceFOK || o.TimeInForce == OrderTimeInForceIOC {
// IOC and FOK are not acceptable for amend order
return OrderErrorCannotAmendToFOKOrIOC
}
return nil
}
func (o OrderAmendment) String() string {
return fmt.Sprintf(
"orderID(%s) marketID(%s) sizeDelta(%v) timeInForce(%s) peggedReference(%s) price(%s) expiresAt(%v) peggedOffset(%s)",
o.OrderID,
o.MarketID,
o.SizeDelta,
o.TimeInForce.String(),
o.PeggedReference.String(),
uintPointerToString(o.Price),
int64PointerToString(o.ExpiresAt),
uintPointerToString(o.PeggedOffset),
)
}
func (o OrderAmendment) GetOrderID() string {
return o.OrderID
}
func (o OrderAmendment) GetMarketID() string {
return o.MarketID
}