-
Notifications
You must be signed in to change notification settings - Fork 19
/
amendments.go
173 lines (150 loc) · 5.14 KB
/
amendments.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
// 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 liquidity
import (
"context"
"errors"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/liquidity/supplied"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/protos/vega"
)
var ErrPartyHaveNoLiquidityProvision = errors.New("party have no liquidity provision")
func (e *Engine) CanAmend(
lps *types.LiquidityProvisionAmendment,
party string,
) error {
// does the party is an LP
_, ok := e.provisions.Get(party)
if !ok {
return ErrPartyHaveNoLiquidityProvision
}
// is the new amendment valid?
if err := e.ValidateLiquidityProvisionAmendment(lps); err != nil {
return err
}
// yes
return nil
}
func (e *Engine) AmendLiquidityProvision(
ctx context.Context,
lpa *types.LiquidityProvisionAmendment,
party string,
idGen IDGen,
) ([]*types.Order, error) {
if err := e.CanAmend(lpa, party); err != nil {
return nil, err
}
// LP exists, checked in the previous func
lp, _ := e.provisions.Get(party)
// first we get all orders from this party to be cancelled
// get the liquidity order to be cancelled
// NOTE: safe to iterate over the map straight away here as
// no operation is done on orders
cancels := e.orderBook.GetLiquidityOrders(party)
sort.Slice(cancels, func(i, j int) bool {
return cancels[i].ID < cancels[j].ID
})
// update the LP
lp.UpdatedAt = e.timeService.GetTimeNow().UnixNano()
lp.CommitmentAmount = lpa.CommitmentAmount.Clone()
lp.Fee = lpa.Fee
lp.Reference = lpa.Reference
// only if it's active, we don't want to loose a PENDING
// status here.
if lp.Status == types.LiquidityProvisionStatusActive {
lp.Status = types.LiquidityProvisionStatusUndeployed
}
// update version
lp.Version++
e.setShapesReferencesOnLiquidityProvision(lp, lpa.Buys, lpa.Sells, idGen)
e.broker.Send(events.NewLiquidityProvisionEvent(ctx, lp))
e.provisions.Set(party, lp)
return cancels, nil
}
// GetPotentialShapeOrders is used to create orders from
// shape when amending a liquidity provision this allows us to
// ensure enough funds can be taken from the margin account in orders
// to submit orders later on.
func (e *Engine) GetPotentialShapeOrders(
party string,
minLpPrice, maxLpPrice *num.Uint,
lps *types.LiquidityProvisionAmendment,
repriceFn RepriceOrder,
) ([]*types.Order, error) {
if err := e.ValidateLiquidityProvisionAmendment(lps); err != nil {
return nil, err
}
priceShape := func(loShape []*types.LiquidityOrder, side types.Side) ([]*supplied.LiquidityOrder, bool) {
shape := make([]*supplied.LiquidityOrder, 0, len(loShape))
for _, lorder := range loShape {
order := &supplied.LiquidityOrder{
Details: lorder,
}
price, err := repriceFn(side, lorder.Reference, lorder.Offset.Clone())
if err != nil {
return nil, false
}
order.Price = price
shape = append(shape, order)
}
return shape, true
}
buyShape, ok := priceShape(lps.Buys, types.SideBuy)
if !ok {
return nil, errors.New("unable to price buy shape")
}
sellShape, ok := priceShape(lps.Sells, types.SideSell)
if !ok {
return nil, errors.New("unable to price sell shape")
}
// Update this once we have updated the commitment value to use Uint TODO UINT
obligation, _ := num.UintFromDecimal(lps.CommitmentAmount.ToDecimal().Mul(e.stakeToObligationFactor).Round(0))
// Create a slice shaped copy of the orders
partyOrders := e.orderBook.GetOrdersPerParty(party)
orders := make([]*types.Order, 0, len(partyOrders))
for _, order := range partyOrders {
if !order.IsLiquidityOrder() && order.Status == vega.Order_STATUS_ACTIVE {
orders = append(orders, order)
}
}
// now calculate the implied volume for our shape
e.suppliedEngine.CalculateLiquidityImpliedVolumes(
obligation,
orders,
minLpPrice, maxLpPrice,
buyShape, sellShape,
)
// from this point we should have no error possible, let's just
// make the order shapes
toCreate := e.buildPotentialShapeOrders(party, buyShape, types.SideBuy)
toCreate = append(toCreate,
e.buildPotentialShapeOrders(party, sellShape, types.SideSell)...)
return toCreate, nil
}
func (e *Engine) buildPotentialShapeOrders(party string, supplied []*supplied.LiquidityOrder, side types.Side) []*types.Order {
orders := make([]*types.Order, 0, len(supplied))
for _, o := range supplied {
// only add order with non = volume to the list
if o.LiquidityImpliedVolume == 0 {
continue
}
// no need to make it a proper pegged order, set an actual ID etc here
// as we actually just return this order as a template for margin
// calculation
order := e.buildOrder(side, o.Price, party, e.marketID, o.LiquidityImpliedVolume, "", "")
orders = append(orders, order)
}
return orders
}