/
types.go
294 lines (240 loc) · 7.19 KB
/
types.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
// 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 liquidity
import (
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
)
// Provisions provides convenience functions to a slice of *vega/proto.LiquidityProvision.
type Provisions []*types.LiquidityProvision
// feeForTarget returns the right fee given a group of sorted (by ascending fee) LiquidityProvisions.
// To find the right fee we need to find smallest index k such that:
// [target stake] < sum from i=1 to k of [MM-stake-i]. In other words we want in this
// ordered list to find the liquidity providers that supply the liquidity
// that's required. If no such k exists we set k=N.
func (l Provisions) feeForTarget(t *num.Uint) num.Decimal {
if len(l) == 0 {
return num.DecimalZero()
}
n := num.UintZero()
for _, i := range l {
n.AddSum(i.CommitmentAmount)
if n.GTE(t) {
return i.Fee
}
}
// return the last one
return l[len(l)-1].Fee
}
// feeForWeightedAverage calculates the fee based on the weight average of the LP's commitment and their nominated fee factor.
func (l Provisions) feeForWeightedAverage() num.Decimal {
if len(l) == 0 {
return num.DecimalZero()
}
sum := num.DecimalZero()
totalComittment := num.DecimalZero()
for _, i := range l {
sum = sum.Add(i.CommitmentAmount.ToDecimal().Mul(i.Fee))
totalComittment = totalComittment.Add(i.CommitmentAmount.ToDecimal())
}
if totalComittment.IsZero() {
return sum
}
return sum.Div(totalComittment)
}
// sortByFee sorts in-place and returns the LiquidityProvisions for convenience.
func (l Provisions) sortByFee() Provisions {
sort.Slice(l, func(i, j int) bool { return l[i].Fee.LessThan(l[j].Fee) })
return l
}
// sortByCommitment sorts in-place and returns the LiquidityProvisions for convenience.
func (l Provisions) sortByCommitment() Provisions {
sort.Slice(l, func(i, j int) bool { return l[i].CommitmentAmount.LT(l[j].CommitmentAmount) })
return l
}
func (pp Provisions) Get(key string) (*types.LiquidityProvision, int) {
for idx, pp := range pp {
if pp.Party == key {
return pp, idx
}
}
return nil, -1
}
func (pp *Provisions) Set(lp *types.LiquidityProvision) {
_, idx := pp.Get(lp.Party)
if idx > -1 {
p := *pp
p[idx] = lp
return
}
*pp = append(*pp, lp)
}
// ProvisionsPerParty maps parties to *types.LiquidityProvision.
type ProvisionsPerParty map[string]*types.LiquidityProvision
type SnapshotableProvisionsPerParty struct {
ProvisionsPerParty
}
func newSnapshotableProvisionsPerParty() *SnapshotableProvisionsPerParty {
return &SnapshotableProvisionsPerParty{
ProvisionsPerParty: map[string]*types.LiquidityProvision{},
}
}
func (s *SnapshotableProvisionsPerParty) Delete(key string) {
delete(s.ProvisionsPerParty, key)
}
func (s *SnapshotableProvisionsPerParty) Get(key string) (*types.LiquidityProvision, bool) {
p, ok := s.ProvisionsPerParty[key]
return p, ok
}
func (s *SnapshotableProvisionsPerParty) Set(key string, p *types.LiquidityProvision) {
s.ProvisionsPerParty[key] = p
}
// Slice returns the parties as a slice.
func (l ProvisionsPerParty) Slice() Provisions {
slice := make(Provisions, 0, len(l))
for _, p := range l {
slice = append(slice, p)
}
// sorting by partyId to ensure any processing in a deterministic manner later on
sort.SliceStable(slice, func(i, j int) bool { return slice[i].Party < slice[j].Party })
return slice
}
func (l ProvisionsPerParty) FeeForTarget(v *num.Uint) num.Decimal {
return l.Slice().sortByFee().feeForTarget(v)
}
func (l ProvisionsPerParty) FeeForWeightedAverage() num.Decimal {
return l.Slice().sortByCommitment().feeForWeightedAverage()
}
// TotalStake returns the sum of all CommitmentAmount, which corresponds to the
// total stake of a market.
func (l ProvisionsPerParty) TotalStake() *num.Uint {
n := num.UintZero()
for _, p := range l {
n.AddSum(p.CommitmentAmount)
}
return n
}
// Orders provides convenience functions to a slice of *veaga/proto.Orders.
type Orders []*types.Order
type PartyOrders struct {
Party string
Orders []*types.Order
}
// ByParty returns the orders grouped by it's PartyID.
func (ords Orders) ByParty() []PartyOrders {
// first extract all orders, per party
parties := map[string][]*types.Order{}
for _, order := range ords {
parties[order.Party] = append(parties[order.Party], order)
}
// now, move stuff from the map, into the PartyOrders type, and sort it
partyOrders := make([]PartyOrders, 0, len(parties))
for k, v := range parties {
partyOrders = append(partyOrders, PartyOrders{k, v})
}
// now sort them to guaranty deterministic
sort.Slice(partyOrders, func(i, j int) bool {
return partyOrders[i].Party < partyOrders[j].Party
})
return partyOrders
}
type SnapshotablePendingProvisions struct {
PendingProvisions Provisions
}
func newSnapshotablePendingProvisions() *SnapshotablePendingProvisions {
return &SnapshotablePendingProvisions{
PendingProvisions: Provisions{},
}
}
func (s SnapshotablePendingProvisions) Slice() Provisions {
return s.PendingProvisions
}
func (s *SnapshotablePendingProvisions) Delete(key string) {
_, id := s.PendingProvisions.Get(key)
if id == -1 {
return
}
s.PendingProvisions = append(s.PendingProvisions[:id], s.PendingProvisions[id+1:]...)
}
func (s *SnapshotablePendingProvisions) Get(key string) (*types.LiquidityProvision, bool) {
lp, idx := s.PendingProvisions.Get(key)
return lp, idx > -1
}
func (s *SnapshotablePendingProvisions) Set(lp *types.LiquidityProvision) {
s.PendingProvisions.Set(lp)
}
func (s *SnapshotablePendingProvisions) Len() int {
return len(s.PendingProvisions)
}
type sliceRing[T any] struct {
s []T
pos int
}
func restoreSliceRing[T any](s []T, size uint64, position int) *sliceRing[T] {
sr := &sliceRing[T]{
s: s,
pos: position,
}
sr.ModifySize(size)
return sr
}
func NewSliceRing[T any](size uint64) *sliceRing[T] {
return &sliceRing[T]{
s: make([]T, size),
pos: 0,
}
}
func (r *sliceRing[T]) Add(val T) {
if len(r.s) == 0 {
return
}
r.s[r.pos] = val
if r.pos == cap(r.s)-1 {
r.pos = 0
return
}
r.pos++
}
func (r *sliceRing[T]) ModifySize(newSize uint64) {
currentCap := cap(r.s)
currentCapUint := uint64(currentCap)
if currentCapUint == newSize {
return
}
newS := make([]T, newSize)
// decrease
if newSize < currentCapUint {
newS = r.s[currentCapUint-newSize:]
r.s = newS
r.pos = 0
return
}
// increase
for i := 0; i < currentCap; i++ {
newS[i] = r.s[i]
}
r.s = newS
r.pos = currentCap
}
func (r sliceRing[T]) Slice() []T {
return r.s
}
func (r sliceRing[T]) Len() int {
return len(r.s)
}
func (r sliceRing[T]) Position() int {
return r.pos
}