-
Notifications
You must be signed in to change notification settings - Fork 19
/
pricemonitoring.go
241 lines (213 loc) · 6.4 KB
/
pricemonitoring.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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.
package types
import (
"errors"
"fmt"
"strings"
"code.vegaprotocol.io/vega/libs/num"
proto "code.vegaprotocol.io/vega/protos/vega"
)
type PriceMonitoringSettings struct {
Parameters *PriceMonitoringParameters
}
type PriceMonitoringParameters struct {
Triggers []*PriceMonitoringTrigger
}
type PriceMonitoringBoundsList []*PriceMonitoringBounds
func (ls PriceMonitoringBoundsList) String() string {
if ls == nil {
return "[]"
}
strs := make([]string, 0, len(ls))
for _, l := range ls {
strs = append(strs, l.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
type PriceMonitoringBounds struct {
MinValidPrice *num.Uint
MaxValidPrice *num.Uint
Trigger *PriceMonitoringTrigger
ReferencePrice num.Decimal
}
func (p PriceMonitoringBounds) String() string {
return fmt.Sprintf(
"minValidPrice(%s) maxValidPrice(%s) trigger(%s) referencePrice(%s)",
uintPointerToString(p.MinValidPrice),
uintPointerToString(p.MaxValidPrice),
reflectPointerToString(p.Trigger),
p.ReferencePrice.String(),
)
}
func (p PriceMonitoringSettings) IntoProto() *proto.PriceMonitoringSettings {
var parameters *proto.PriceMonitoringParameters
if p.Parameters != nil {
parameters = p.Parameters.IntoProto()
}
return &proto.PriceMonitoringSettings{
Parameters: parameters,
}
}
func (p PriceMonitoringSettings) DeepClone() *PriceMonitoringSettings {
return &PriceMonitoringSettings{
Parameters: p.Parameters.DeepClone(),
}
}
func (p PriceMonitoringSettings) String() string {
return fmt.Sprintf("parameters(%s)", reflectPointerToString(p.Parameters))
}
func PriceMonitoringSettingsFromProto(pr *proto.PriceMonitoringSettings) *PriceMonitoringSettings {
if pr == nil {
return nil
}
p := PriceMonitoringSettings{}
if pr.Parameters != nil {
p.Parameters = PriceMonitoringParametersFromProto(pr.Parameters)
}
return &p
}
func PriceMonitoringParametersFromProto(p *proto.PriceMonitoringParameters) *PriceMonitoringParameters {
triggers := make([]*PriceMonitoringTrigger, 0, len(p.Triggers))
for _, t := range p.Triggers {
triggers = append(triggers, PriceMonitoringTriggerFromProto(t))
}
return &PriceMonitoringParameters{
Triggers: triggers,
}
}
func (p PriceMonitoringParameters) IntoProto() *proto.PriceMonitoringParameters {
triggers := make([]*proto.PriceMonitoringTrigger, 0, len(p.Triggers))
for _, t := range p.Triggers {
triggers = append(triggers, t.IntoProto())
}
return &proto.PriceMonitoringParameters{
Triggers: triggers,
}
}
func (p PriceMonitoringParameters) DeepClone() *PriceMonitoringParameters {
cpy := PriceMonitoringParameters{
Triggers: make([]*PriceMonitoringTrigger, 0, len(p.Triggers)),
}
for _, t := range p.Triggers {
cpy.Triggers = append(cpy.Triggers, t.DeepClone())
}
return &cpy
}
func (p *PriceMonitoringParameters) Reset() {
*p = PriceMonitoringParameters{}
}
func (p PriceMonitoringParameters) String() string {
return fmt.Sprintf(
"triggers(%v)",
PriceMonitoringTriggers(p.Triggers).String(),
)
}
func (p PriceMonitoringBounds) IntoProto() *proto.PriceMonitoringBounds {
var trigger *proto.PriceMonitoringTrigger
if p.Trigger != nil {
trigger = p.Trigger.IntoProto()
}
return &proto.PriceMonitoringBounds{
MinValidPrice: num.UintToString(p.MinValidPrice),
MaxValidPrice: num.UintToString(p.MaxValidPrice),
Trigger: trigger,
ReferencePrice: p.ReferencePrice.BigInt().String(),
}
}
func PriceMonitoringBoundsFromProto(pr *proto.PriceMonitoringBounds) (*PriceMonitoringBounds, error) {
minValid, overflowed := num.UintFromString(pr.MinValidPrice, 10)
if overflowed {
return nil, errors.New("invalid min valid price")
}
maxValid, overflowed := num.UintFromString(pr.MaxValidPrice, 10)
if overflowed {
return nil, errors.New("invalid max valid price")
}
refPrice, err := num.DecimalFromString(pr.ReferencePrice)
if err != nil {
return nil, fmt.Errorf("invalid reference price: %w", err)
}
p := PriceMonitoringBounds{
MinValidPrice: minValid,
MaxValidPrice: maxValid,
ReferencePrice: refPrice,
}
if pr.Trigger != nil {
p.Trigger = PriceMonitoringTriggerFromProto(pr.Trigger)
}
return &p, nil
}
func (p PriceMonitoringBounds) DeepClone() *PriceMonitoringBounds {
cpy := p
if p.MinValidPrice != nil {
cpy.MinValidPrice = p.MinValidPrice.Clone()
}
if p.MaxValidPrice != nil {
cpy.MaxValidPrice = p.MaxValidPrice.Clone()
}
return &cpy
}
func PriceMonitoringTriggerFromProto(p *proto.PriceMonitoringTrigger) *PriceMonitoringTrigger {
probability, err := num.DecimalFromString(p.Probability)
if err != nil {
probability = num.DecimalZero()
}
return &PriceMonitoringTrigger{
Horizon: p.Horizon,
HorizonDec: num.DecimalFromInt64(p.Horizon),
Probability: probability,
AuctionExtension: p.AuctionExtension,
}
}
type PriceMonitoringTriggers []*PriceMonitoringTrigger
func (ts PriceMonitoringTriggers) String() string {
if ts == nil {
return "[]"
}
strs := make([]string, 0, len(ts))
for _, t := range ts {
strs = append(strs, t.String())
}
return "[" + strings.Join(strs, ", ") + "]"
}
type PriceMonitoringTrigger struct {
Horizon int64
HorizonDec num.Decimal
Probability num.Decimal
AuctionExtension int64
}
func (p PriceMonitoringTrigger) String() string {
return fmt.Sprintf(
"horizonDec(%s) horizon(%v) probability(%s), auctionExtension(%v)",
p.HorizonDec.String(),
p.Horizon,
p.Probability.String(),
p.AuctionExtension,
)
}
// IntoProto return proto version of the PriceMonitoringTrigger.
func (p PriceMonitoringTrigger) IntoProto() *proto.PriceMonitoringTrigger {
return &proto.PriceMonitoringTrigger{
Horizon: p.Horizon,
Probability: p.Probability.String(),
AuctionExtension: p.AuctionExtension,
}
}
func (p PriceMonitoringTrigger) DeepClone() *PriceMonitoringTrigger {
return &PriceMonitoringTrigger{
Horizon: p.Horizon,
HorizonDec: p.HorizonDec,
Probability: p.Probability,
AuctionExtension: p.AuctionExtension,
}
}