-
Notifications
You must be signed in to change notification settings - Fork 19
/
types.go
184 lines (151 loc) · 4.97 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
// 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 (
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
)
type ToCancel struct {
Party string
OrderIDs []string
}
func (c *ToCancel) Merge(oth *ToCancel) *ToCancel {
if c.Party != oth.Party {
panic("could not merge ToCancel from different parties")
}
return &ToCancel{
Party: c.Party,
OrderIDs: append(c.OrderIDs, oth.OrderIDs...),
}
}
func (c *ToCancel) Add(id string) {
c.OrderIDs = append(c.OrderIDs, id)
}
func (c *ToCancel) Empty() bool {
return len(c.OrderIDs) <= 0
}
// 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
}
type lpsByFee Provisions
func (l lpsByFee) Len() int { return len(l) }
func (l lpsByFee) Less(i, j int) bool { return l[i].Fee.LessThan(l[j].Fee) }
func (l lpsByFee) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// sortByFee sorts in-place and returns the LiquidityProvisions for convenience.
func (l Provisions) sortByFee() Provisions {
byFee := lpsByFee(l)
sort.Sort(byFee)
return Provisions(byFee)
}
type SnapshotablePendingProvisions struct {
m map[string]struct{}
}
func newSnapshotablePendingProvisions() *SnapshotablePendingProvisions {
return &SnapshotablePendingProvisions{
m: map[string]struct{}{},
}
}
func (s *SnapshotablePendingProvisions) Add(key string) {
s.m[key] = struct{}{}
}
func (s *SnapshotablePendingProvisions) Delete(key string) {
delete(s.m, key)
}
func (s *SnapshotablePendingProvisions) Exists(key string) bool {
_, ok := s.m[key]
return ok
}
// Provisions is a map of 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.Slice(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)
}
// 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
}