-
Notifications
You must be signed in to change notification settings - Fork 19
/
liquidity.go
463 lines (404 loc) · 14.4 KB
/
liquidity.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// 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/>.
package types
import (
"errors"
"fmt"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/libs/stringer"
proto "code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
type LiquidityFeeMethod = proto.LiquidityFeeSettings_Method
const (
LiquidityFeeMethodUnspecified LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_UNSPECIFIED
LiquidityFeeMethodMarginalCost LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_MARGINAL_COST
LiquidityFeeMethodWeightedAverage LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_WEIGHTED_AVERAGE
LiquidityFeeMethodConstant LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_CONSTANT
)
type LiquidityProvisionStatus = proto.LiquidityProvision_Status
const (
// LiquidityProvisionUnspecified The default value.
LiquidityProvisionUnspecified LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_UNSPECIFIED
// LiquidityProvisionStatusActive The liquidity provision is active.
LiquidityProvisionStatusActive LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_ACTIVE
// LiquidityProvisionStatusStopped The liquidity provision was stopped by the network.
LiquidityProvisionStatusStopped LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_STOPPED
// LiquidityProvisionStatusCancelled The liquidity provision was cancelled by the liquidity provider.
LiquidityProvisionStatusCancelled LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_CANCELLED
// LiquidityProvisionStatusRejected The liquidity provision was invalid and got rejected.
LiquidityProvisionStatusRejected LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_REJECTED
// LiquidityProvisionStatusUndeployed The liquidity provision is valid and accepted by network, but orders aren't deployed.
LiquidityProvisionStatusUndeployed LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_UNDEPLOYED
// LiquidityProvisionStatusPending The liquidity provision is valid and accepted by network
// but have never been deployed. I when it's possible to deploy them for the first time
// margin check fails, then they will be cancelled without any penalties.
LiquidityProvisionStatusPending LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_PENDING
)
type LiquiditySLAParams struct {
PriceRange num.Decimal
CommitmentMinTimeFraction num.Decimal
PerformanceHysteresisEpochs uint64
SlaCompetitionFactor num.Decimal
}
func (l LiquiditySLAParams) IntoProto() *proto.LiquiditySLAParameters {
return &proto.LiquiditySLAParameters{
PriceRange: l.PriceRange.String(),
CommitmentMinTimeFraction: l.CommitmentMinTimeFraction.String(),
PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs,
SlaCompetitionFactor: l.SlaCompetitionFactor.String(),
}
}
func LiquiditySLAParamsFromProto(l *proto.LiquiditySLAParameters) *LiquiditySLAParams {
if l == nil {
return nil
}
return &LiquiditySLAParams{
PriceRange: num.MustDecimalFromString(l.PriceRange),
CommitmentMinTimeFraction: num.MustDecimalFromString(l.CommitmentMinTimeFraction),
PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs,
SlaCompetitionFactor: num.MustDecimalFromString(l.SlaCompetitionFactor),
}
}
func (l LiquiditySLAParams) String() string {
return fmt.Sprintf(
"priceRange(%s) commitmentMinTimeFraction(%s) performanceHysteresisEpochs(%v) slaCompetitionFactor(%s)",
l.PriceRange.String(),
l.CommitmentMinTimeFraction.String(),
l.PerformanceHysteresisEpochs,
l.SlaCompetitionFactor.String(),
)
}
func (l LiquiditySLAParams) DeepClone() *LiquiditySLAParams {
return &LiquiditySLAParams{
PriceRange: l.PriceRange,
CommitmentMinTimeFraction: l.CommitmentMinTimeFraction,
PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs,
SlaCompetitionFactor: l.SlaCompetitionFactor,
}
}
type TargetStakeParameters struct {
TimeWindow int64
ScalingFactor num.Decimal
}
func (t TargetStakeParameters) IntoProto() *proto.TargetStakeParameters {
sf, _ := t.ScalingFactor.Float64()
return &proto.TargetStakeParameters{
TimeWindow: t.TimeWindow,
ScalingFactor: sf,
}
}
func TargetStakeParametersFromProto(p *proto.TargetStakeParameters) *TargetStakeParameters {
return &TargetStakeParameters{
TimeWindow: p.TimeWindow,
ScalingFactor: num.DecimalFromFloat(p.ScalingFactor),
}
}
func (t TargetStakeParameters) String() string {
return fmt.Sprintf(
"timeWindows(%v) scalingFactor(%s)",
t.TimeWindow,
t.ScalingFactor.String(),
)
}
func (t TargetStakeParameters) DeepClone() *TargetStakeParameters {
return &TargetStakeParameters{
TimeWindow: t.TimeWindow,
ScalingFactor: t.ScalingFactor,
}
}
type LiquidityProvisionSubmission struct {
// Market identifier for the order, required field
MarketID string
// Specified as a unitless number that represents the amount of settlement asset of the market
CommitmentAmount *num.Uint
// Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per setting fees and rewarding liquidity providers
Fee num.Decimal
// A reference to be added to every order created out of this liquidityProvisionSubmission
Reference string
}
func (l LiquidityProvisionSubmission) IntoProto() *commandspb.LiquidityProvisionSubmission {
return &commandspb.LiquidityProvisionSubmission{
MarketId: l.MarketID,
CommitmentAmount: num.UintToString(l.CommitmentAmount),
Fee: l.Fee.String(),
Reference: l.Reference,
}
}
func LiquidityProvisionSubmissionFromProto(p *commandspb.LiquidityProvisionSubmission) (*LiquidityProvisionSubmission, error) {
fee, err := num.DecimalFromString(p.Fee)
if err != nil {
return nil, err
}
commitmentAmount := num.UintZero()
if len(p.CommitmentAmount) > 0 {
var overflowed bool
commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10)
if overflowed {
return nil, errors.New("invalid commitment amount")
}
}
l := LiquidityProvisionSubmission{
Fee: fee,
MarketID: p.MarketId,
CommitmentAmount: commitmentAmount,
Reference: p.Reference,
}
return &l, nil
}
func (l LiquidityProvisionSubmission) String() string {
return fmt.Sprintf(
"marketID(%s) reference(%s) commitmentAmount(%s) fee(%s)",
l.MarketID,
l.Reference,
stringer.PtrToString(l.CommitmentAmount),
l.Fee.String(),
)
}
type LiquidityProvision struct {
// Unique identifier
ID string
// Unique party identifier for the creator of the provision
Party string
// Timestamp for when the order was created at, in nanoseconds since the epoch
// - See [`VegaTimeResponse`](#api.VegaTimeResponse).`timestamp`
CreatedAt int64
// Timestamp for when the order was updated at, in nanoseconds since the epoch
// - See [`VegaTimeResponse`](#api.VegaTimeResponse).`timestamp`
UpdatedAt int64
// Market identifier for the order, required field
MarketID string
// Specified as a unitless number that represents the amount of settlement asset of the market
CommitmentAmount *num.Uint
// Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per seeting fees and rewarding liquidity providers
Fee num.Decimal
// Version of this liquidity provision
Version uint64
// Status of this liquidity provision
Status LiquidityProvisionStatus
// A reference shared between this liquidity provision and all it's orders
Reference string
}
func (l LiquidityProvision) String() string {
return fmt.Sprintf(
"ID(%s) marketID(%s) party(%s) status(%s) reference(%s) commitmentAmount(%s) fee(%s) version(%v) createdAt(%v) updatedAt(%v)",
l.ID,
l.MarketID,
l.Party,
l.Status.String(),
l.Reference,
stringer.PtrToString(l.CommitmentAmount),
l.Fee.String(),
l.Version,
l.CreatedAt,
l.UpdatedAt,
)
}
func (l LiquidityProvision) IntoProto() *proto.LiquidityProvision {
lp := &proto.LiquidityProvision{
Id: l.ID,
PartyId: l.Party,
CreatedAt: l.CreatedAt,
UpdatedAt: l.UpdatedAt,
MarketId: l.MarketID,
CommitmentAmount: num.UintToString(l.CommitmentAmount),
Fee: l.Fee.String(),
Version: l.Version,
Status: l.Status,
Reference: l.Reference,
}
return lp
}
func LiquidityProvisionFromProto(p *proto.LiquidityProvision) (*LiquidityProvision, error) {
fee, _ := num.DecimalFromString(p.Fee)
commitmentAmount := num.UintZero()
if len(p.CommitmentAmount) > 0 {
var overflowed bool
commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10)
if overflowed {
return nil, errors.New("invalid commitment amount")
}
}
l := LiquidityProvision{
CommitmentAmount: commitmentAmount,
CreatedAt: p.CreatedAt,
ID: p.Id,
MarketID: p.MarketId,
Party: p.PartyId,
Fee: fee,
Reference: p.Reference,
Status: p.Status,
UpdatedAt: p.UpdatedAt,
Version: p.Version,
}
return &l, nil
}
type LiquidityMonitoringParameters struct {
// Specifies parameters related to target stake calculation
TargetStakeParameters *TargetStakeParameters
}
func (l LiquidityMonitoringParameters) IntoProto() *proto.LiquidityMonitoringParameters {
var params *proto.TargetStakeParameters
if l.TargetStakeParameters != nil {
params = l.TargetStakeParameters.IntoProto()
}
return &proto.LiquidityMonitoringParameters{
TargetStakeParameters: params,
}
}
func (l LiquidityMonitoringParameters) DeepClone() *LiquidityMonitoringParameters {
var params *TargetStakeParameters
if l.TargetStakeParameters != nil {
params = l.TargetStakeParameters.DeepClone()
}
return &LiquidityMonitoringParameters{
TargetStakeParameters: params,
}
}
func (l LiquidityMonitoringParameters) String() string {
return fmt.Sprintf(
"auctionExtension(%v)",
stringer.PtrToString(l.TargetStakeParameters),
)
}
func LiquidityMonitoringParametersFromProto(p *proto.LiquidityMonitoringParameters) (*LiquidityMonitoringParameters, error) {
if p == nil {
return nil, nil
}
var params *TargetStakeParameters
if p.TargetStakeParameters != nil {
params = TargetStakeParametersFromProto(p.TargetStakeParameters)
}
return &LiquidityMonitoringParameters{
TargetStakeParameters: params,
}, nil
}
type LiquidityProvisionAmendment struct {
// Market identifier for the order, required field
MarketID string
// Specified as a unitless number that represents the amount of settlement asset of the market
CommitmentAmount *num.Uint
// Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per setting fees and rewarding liquidity providers
Fee num.Decimal
// A reference to be added to every order created out of this liquidityProvisionAmendment
Reference string
}
func LiquidityProvisionAmendmentFromProto(p *commandspb.LiquidityProvisionAmendment) (*LiquidityProvisionAmendment, error) {
fee, err := num.DecimalFromString(p.Fee)
if err != nil {
return nil, err
}
commitmentAmount := num.UintZero()
if len(p.CommitmentAmount) > 0 {
var overflowed bool
commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10)
if overflowed {
return nil, errors.New("invalid commitment amount")
}
}
return &LiquidityProvisionAmendment{
Fee: fee,
MarketID: p.MarketId,
CommitmentAmount: commitmentAmount,
Reference: p.Reference,
}, nil
}
func (a LiquidityProvisionAmendment) IntoProto() *commandspb.LiquidityProvisionAmendment {
return &commandspb.LiquidityProvisionAmendment{
MarketId: a.MarketID,
CommitmentAmount: num.UintToString(a.CommitmentAmount),
Fee: a.Fee.String(),
Reference: a.Reference,
}
}
func (a LiquidityProvisionAmendment) String() string {
return fmt.Sprintf(
"marketID(%s) reference(%s) commitmentAmount(%s) fee(%s)",
a.MarketID,
a.Reference,
stringer.PtrToString(a.CommitmentAmount),
a.Fee.String(),
)
}
func (a LiquidityProvisionAmendment) GetMarketID() string {
return a.MarketID
}
type LiquidityProvisionCancellation struct {
// Market identifier for the order, required field
MarketID string
}
func LiquidityProvisionCancellationFromProto(p *commandspb.LiquidityProvisionCancellation) (*LiquidityProvisionCancellation, error) {
l := LiquidityProvisionCancellation{
MarketID: p.MarketId,
}
return &l, nil
}
func (l LiquidityProvisionCancellation) IntoProto() *commandspb.LiquidityProvisionCancellation {
return &commandspb.LiquidityProvisionCancellation{
MarketId: l.MarketID,
}
}
func (l LiquidityProvisionCancellation) String() string {
return fmt.Sprintf("marketID(%s)", l.MarketID)
}
func (l LiquidityProvisionCancellation) GetMarketID() string {
return l.MarketID
}
type LiquidityFeeSettings struct {
Method LiquidityFeeMethod
FeeConstant num.Decimal
}
func (l *LiquidityFeeSettings) IntoProto() *proto.LiquidityFeeSettings {
if l == nil {
return nil
}
r := &proto.LiquidityFeeSettings{
Method: l.Method,
}
if l.Method == LiquidityFeeMethodConstant {
r.FeeConstant = ptr.From(l.FeeConstant.String())
}
return r
}
func LiquidityFeeSettingsFromProto(l *proto.LiquidityFeeSettings) *LiquidityFeeSettings {
if l == nil {
return nil
}
fc := num.DecimalZero()
if l.Method == LiquidityFeeMethodConstant {
fc, _ = num.DecimalFromString(*l.FeeConstant)
}
return &LiquidityFeeSettings{
Method: l.Method,
FeeConstant: fc,
}
}
func (l *LiquidityFeeSettings) DeepClone() *LiquidityFeeSettings {
if l == nil {
return nil
}
return &LiquidityFeeSettings{
Method: l.Method,
FeeConstant: l.FeeConstant,
}
}
func (l LiquidityFeeSettings) String() string {
return fmt.Sprintf(
"method(%s) feeConstant(%s)",
l.Method.String(),
l.FeeConstant.String(),
)
}