-
Notifications
You must be signed in to change notification settings - Fork 19
/
orders.go
161 lines (142 loc) · 4.39 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
158
159
160
161
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//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"
"code.vegaprotocol.io/vega/libs/stringer"
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(),
stringer.UintPointerToString(o.Price),
stringer.Int64PointerToString(o.ExpiresAt),
stringer.UintPointerToString(o.PeggedOffset),
)
}
func (o OrderAmendment) GetOrderID() string {
return o.OrderID
}
func (o OrderAmendment) GetMarketID() string {
return o.MarketID
}