-
Notifications
You must be signed in to change notification settings - Fork 19
/
liquidity.go
615 lines (542 loc) · 18.3 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// 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"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
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 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 set of liquidity sell orders to meet the liquidity provision obligation
Sells []*LiquidityOrder
// A set of liquidity buy orders to meet the liquidity provision obligation
Buys []*LiquidityOrder
// A reference to be added to every order created out of this liquidityProvisionSubmission
Reference string
}
func (l LiquidityProvisionSubmission) IntoProto() *commandspb.LiquidityProvisionSubmission {
lps := &commandspb.LiquidityProvisionSubmission{
MarketId: l.MarketID,
CommitmentAmount: num.UintToString(l.CommitmentAmount),
Fee: l.Fee.String(),
Sells: make([]*proto.LiquidityOrder, 0, len(l.Sells)),
Buys: make([]*proto.LiquidityOrder, 0, len(l.Buys)),
Reference: l.Reference,
}
for _, sell := range l.Sells {
lps.Sells = append(lps.Sells, sell.IntoProto())
}
for _, buy := range l.Buys {
lps.Buys = append(lps.Buys, buy.IntoProto())
}
return lps
}
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,
Sells: make([]*LiquidityOrder, 0, len(p.Sells)),
Buys: make([]*LiquidityOrder, 0, len(p.Buys)),
Reference: p.Reference,
}
for _, sell := range p.Sells {
order, err := LiquidityOrderFromProto(sell)
if err != nil {
return nil, err
}
l.Sells = append(l.Sells, order)
}
for _, buy := range p.Buys {
order, err := LiquidityOrderFromProto(buy)
if err != nil {
return nil, err
}
l.Buys = append(l.Buys, order)
}
return &l, nil
}
func (l LiquidityProvisionSubmission) String() string {
return fmt.Sprintf(
"marketID(%s) reference(%s) commitmentAmount(%s) fee(%s) sells(%s) buys(%s)",
l.MarketID,
l.Reference,
uintPointerToString(l.CommitmentAmount),
l.Fee.String(),
LiquidityOrders(l.Sells).String(),
LiquidityOrders(l.Buys).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
// A set of liquidity sell orders to meet the liquidity provision obligation
Sells []*LiquidityOrderReference
// A set of liquidity buy orders to meet the liquidity provision obligation
Buys []*LiquidityOrderReference
// Version of this liquidity provision order
Version uint64
// Status of this liquidity provision order
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) sells(%s) buys(%s) version(%v) createdAt(%v) updatedAt(%v)",
l.ID,
l.MarketID,
l.Party,
l.Status.String(),
l.Reference,
uintPointerToString(l.CommitmentAmount),
l.Fee.String(),
LiquidityOrderReferences(l.Sells).String(),
LiquidityOrderReferences(l.Buys).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,
Sells: make([]*proto.LiquidityOrderReference, 0, len(l.Sells)),
Buys: make([]*proto.LiquidityOrderReference, 0, len(l.Buys)),
}
for _, sell := range l.Sells {
lp.Sells = append(lp.Sells, sell.IntoProto())
}
for _, buy := range l.Buys {
lp.Buys = append(lp.Buys, buy.IntoProto())
}
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,
Sells: make([]*LiquidityOrderReference, 0, len(p.Sells)),
Buys: make([]*LiquidityOrderReference, 0, len(p.Buys)),
}
for _, sell := range p.Sells {
lor, err := LiquidityOrderReferenceFromProto(sell)
if err != nil {
return nil, err
}
l.Sells = append(l.Sells, lor)
}
for _, buy := range p.Buys {
lor, err := LiquidityOrderReferenceFromProto(buy)
if err != nil {
return nil, err
}
l.Buys = append(l.Buys, lor)
}
return &l, nil
}
type LiquidityOrderReferences []*LiquidityOrderReference
func (ls LiquidityOrderReferences) 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 LiquidityOrderReference struct {
// Unique identifier of the pegged order generated by the core to fulfil this liquidity order
OrderID string
// The liquidity order from the original submission
LiquidityOrder *LiquidityOrder
}
func (l LiquidityOrderReference) String() string {
return fmt.Sprintf(
"orderID(%s) liquidityOrder(%s)",
l.OrderID,
reflectPointerToString(l.LiquidityOrder),
)
}
func (l LiquidityOrderReference) IntoProto() *proto.LiquidityOrderReference {
var order *proto.LiquidityOrder
if l.LiquidityOrder != nil {
order = l.LiquidityOrder.IntoProto()
}
return &proto.LiquidityOrderReference{
OrderId: l.OrderID,
LiquidityOrder: order,
}
}
func LiquidityOrderReferenceFromProto(p *proto.LiquidityOrderReference) (*LiquidityOrderReference, error) {
lo, err := LiquidityOrderFromProto(p.LiquidityOrder)
if err != nil {
return nil, err
}
return &LiquidityOrderReference{
OrderID: p.OrderId,
LiquidityOrder: lo,
}, nil
}
type LiquidityOrders []*LiquidityOrder
func (ls LiquidityOrders) 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 LiquidityOrder struct {
// The pegged reference point for the order
Reference PeggedReference
// The relative proportion of the commitment to be allocated at a price level
Proportion uint32
// The offset/amount of units away for the order
Offset *num.Uint
}
func (l LiquidityOrder) String() string {
return fmt.Sprintf(
"reference(%s) proportion(%v) offset(%s)",
l.Reference.String(),
l.Proportion,
uintPointerToString(l.Offset),
)
}
func (l LiquidityOrder) DeepClone() *LiquidityOrder {
return &LiquidityOrder{
Reference: l.Reference,
Proportion: l.Proportion,
Offset: l.Offset,
}
}
func (l LiquidityOrder) IntoProto() *proto.LiquidityOrder {
return &proto.LiquidityOrder{
Reference: l.Reference,
Proportion: l.Proportion,
Offset: l.Offset.String(),
}
}
func LiquidityOrderFromProto(p *proto.LiquidityOrder) (*LiquidityOrder, error) {
offset, overflow := num.UintFromString(p.Offset, 10)
if overflow {
return nil, errors.New("invalid offset")
}
return &LiquidityOrder{
Offset: offset,
Proportion: p.Proportion,
Reference: p.Reference,
}, nil
}
type LiquidityMonitoringParameters struct {
// Specifies parameters related to target stake calculation
TargetStakeParameters *TargetStakeParameters
// Specifies the triggering ratio for entering liquidity auction
TriggeringRatio num.Decimal
// Specifies by how many seconds an auction should be extended if leaving the auction were to trigger a liquidity auction
AuctionExtension int64
}
func (l LiquidityMonitoringParameters) IntoProto() *proto.LiquidityMonitoringParameters {
var params *proto.TargetStakeParameters
if l.TargetStakeParameters != nil {
params = l.TargetStakeParameters.IntoProto()
}
return &proto.LiquidityMonitoringParameters{
TargetStakeParameters: params,
TriggeringRatio: l.TriggeringRatio.String(),
AuctionExtension: l.AuctionExtension,
}
}
func (l LiquidityMonitoringParameters) DeepClone() *LiquidityMonitoringParameters {
var params *TargetStakeParameters
if l.TargetStakeParameters != nil {
params = l.TargetStakeParameters.DeepClone()
}
return &LiquidityMonitoringParameters{
TriggeringRatio: l.TriggeringRatio,
AuctionExtension: l.AuctionExtension,
TargetStakeParameters: params,
}
}
func (l LiquidityMonitoringParameters) String() string {
return fmt.Sprintf(
"auctionExtension(%v) trigerringRatio(%s) targetStake(%s)",
l.AuctionExtension,
l.TriggeringRatio.String(),
reflectPointerToString(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)
}
tr, err := num.DecimalFromString(p.TriggeringRatio)
if err != nil {
return nil, fmt.Errorf("error getting trigerring ratio value from proto: %s", err)
}
return &LiquidityMonitoringParameters{
TargetStakeParameters: params,
AuctionExtension: p.AuctionExtension,
TriggeringRatio: tr,
}, 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 set of liquidity sell orders to meet the liquidity provision obligation
Sells []*LiquidityOrder
// A set of liquidity buy orders to meet the liquidity provision obligation
Buys []*LiquidityOrder
// 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")
}
}
l := LiquidityProvisionAmendment{
Fee: fee,
MarketID: p.MarketId,
CommitmentAmount: commitmentAmount,
Sells: make([]*LiquidityOrder, 0, len(p.Sells)),
Buys: make([]*LiquidityOrder, 0, len(p.Buys)),
Reference: p.Reference,
}
for _, sell := range p.Sells {
offset := num.UintZero()
if len(p.CommitmentAmount) > 0 {
var overflowed bool
offset, overflowed = num.UintFromString(sell.Offset, 10)
if overflowed {
return nil, errors.New("invalid sell side offset")
}
}
order := &LiquidityOrder{
Reference: sell.Reference,
Proportion: sell.Proportion,
Offset: offset,
}
l.Sells = append(l.Sells, order)
}
for _, buy := range p.Buys {
offset := num.UintZero()
if len(p.CommitmentAmount) > 0 {
var overflowed bool
offset, overflowed = num.UintFromString(buy.Offset, 10)
if overflowed {
return nil, errors.New("invalid buy side offset")
}
}
order := &LiquidityOrder{
Reference: buy.Reference,
Proportion: buy.Proportion,
Offset: offset,
}
l.Buys = append(l.Buys, order)
}
return &l, nil
}
func (a LiquidityProvisionAmendment) IntoProto() *commandspb.LiquidityProvisionAmendment {
lps := &commandspb.LiquidityProvisionAmendment{
MarketId: a.MarketID,
CommitmentAmount: num.UintToString(a.CommitmentAmount),
Fee: a.Fee.String(),
Sells: make([]*proto.LiquidityOrder, 0, len(a.Sells)),
Buys: make([]*proto.LiquidityOrder, 0, len(a.Buys)),
Reference: a.Reference,
}
for _, sell := range a.Sells {
order := &proto.LiquidityOrder{
Reference: sell.Reference,
Proportion: sell.Proportion,
Offset: sell.Offset.String(),
}
lps.Sells = append(lps.Sells, order)
}
for _, buy := range a.Buys {
order := &proto.LiquidityOrder{
Reference: buy.Reference,
Proportion: buy.Proportion,
Offset: buy.Offset.String(),
}
lps.Buys = append(lps.Buys, order)
}
return lps
}
func (a LiquidityProvisionAmendment) String() string {
return fmt.Sprintf(
"marketID(%s) reference(%s) commitmentAmount(%s) fee(%s) sells(%v) buys(%v)",
a.MarketID,
a.Reference,
uintPointerToString(a.CommitmentAmount),
a.Fee.String(),
LiquidityOrders(a.Sells).String(),
LiquidityOrders(a.Buys).String(),
)
}
func (a LiquidityProvisionAmendment) GetMarketID() string {
return a.MarketID
}
func (a LiquidityProvisionAmendment) ContainsOrders() bool {
return len(a.Sells) > 0 || len(a.Buys) > 0
}
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
}