-
Notifications
You must be signed in to change notification settings - Fork 19
/
equity_shares.go
259 lines (230 loc) · 7.24 KB
/
equity_shares.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
// 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 execution
import (
"fmt"
"code.vegaprotocol.io/vega/libs/num"
)
// lp holds LiquidityProvider stake and avg values.
type lp struct {
stake num.Decimal
share num.Decimal
avg num.Decimal
vStake num.Decimal
}
// EquityShares module controls the Equity sharing algorithm described on the spec:
// https://github.com/vegaprotocol/product/blob/02af55e048a92a204e9ee7b7ae6b4475a198c7ff/specs/0042-setting-fees-and-rewarding-lps.md#calculating-liquidity-provider-equity-like-share
type EquityShares struct {
// mvp is the MarketValueProxy
mvp num.Decimal
pMvp num.Decimal // @TODO add to snapshot
r num.Decimal
// lps is a map of party id to lp (LiquidityProviders)
lps map[string]*lp
openingAuctionEnded bool
stateChanged bool
}
func NewEquityShares(mvp num.Decimal) *EquityShares {
return &EquityShares{
mvp: mvp,
pMvp: num.DecimalZero(),
r: num.DecimalZero(),
lps: map[string]*lp{},
stateChanged: true,
}
}
// OpeningAuctionEnded signal to the EquityShare that the
// opening auction has ended.
func (es *EquityShares) OpeningAuctionEnded() {
// we should never call this twice
if es.openingAuctionEnded {
panic("market already left opening auction")
}
es.openingAuctionEnded = true
es.stateChanged = true
es.setOpeningAuctionAVG()
}
// we just the average entry valuation to the same value
// for every LP during opening auction.
func (es *EquityShares) setOpeningAuctionAVG() {
// now set average entry valuation for all of them.
for _, v := range es.lps {
v.avg = es.mvp
}
}
func (es *EquityShares) UpdateVirtualStake() {
// this isn't used if we have to set vStake to physical stake
growth := es.r
// if A(n) == 0 or A(n-1) == 0, vStake = physical stake
setPhysical := (es.mvp.IsZero() || es.pMvp.IsZero())
if !setPhysical {
growth = num.NewDecimalFromFloat(1.0).Add(growth)
}
for _, v := range es.lps {
// default to physical stake
vStake := v.stake
if !setPhysical {
// unless A(n) != 0 || A(n-1) != 0
// then set vStake = max(physical stake, ((r+1)*vStqake))
vStake = num.MaxD(v.stake, growth.Mul(v.vStake))
}
// if virtual stake doesn't change, then stateChanged shouldn't be toggled
es.stateChanged = (es.stateChanged || !vStake.Equals(v.vStake))
v.vStake = vStake
}
}
func (es *EquityShares) WithMVP(mvp num.Decimal) *EquityShares {
// growth always defaults to 0
es.r = num.DecimalZero()
if !es.mvp.IsZero() && !mvp.IsZero() {
// Spec notation: r = (A(n) - A(n-1))/A(n-1)
growth := mvp.Sub(es.mvp).Div(es.mvp)
// toggle state changed if growth rate has changed
es.stateChanged = (es.stateChanged || !growth.Equals(es.r))
es.r = growth
}
// only flip state changed if growth rate and/or mvp has changed
// previous mvp can still change, so we need to check that, too
es.stateChanged = (es.stateChanged || !es.mvp.Equals(mvp) || !es.mvp.Equals(es.pMvp))
// pMvp would otherwise be A(n-2) -> update to A(n-1)
es.pMvp = es.mvp
es.mvp = mvp
return es
}
// SetPartyStake sets LP values for a given party.
func (es *EquityShares) SetPartyStake(id string, newStakeU *num.Uint) {
if !es.openingAuctionEnded {
defer es.setOpeningAuctionAVG()
}
v, found := es.lps[id]
if newStakeU == nil || newStakeU.IsZero() {
delete(es.lps, id)
es.stateChanged = (es.stateChanged || found)
return
}
defer func() {
es.stateChanged = true
}()
newStake := num.DecimalFromUint(newStakeU)
// first time we set the newStake and mvp as avg.
if !found {
avg := num.DecimalZero()
if es.openingAuctionEnded {
avg = es.mvp
}
es.lps[id] = &lp{
stake: newStake,
avg: avg,
vStake: newStake,
}
return
}
delta := newStake.Sub(v.stake)
if newStake.LessThanOrEqual(v.stake) {
// vStake * (newStake/oldStake)
v.vStake = v.vStake.Mul(newStake.Div(v.stake))
v.stake = newStake
return
}
// delta will allways be > 0 at this point
if es.openingAuctionEnded {
eq := es.mustEquity(id)
// v.avg = ((eq * v.avg) + (delta * es.mvp)) / (eq + v.stake)
v.avg = (eq.Mul(v.avg).Add(delta.Mul(es.mvp))).Div(eq.Add(v.stake))
}
v.vStake = v.vStake.Add(delta) // increase
v.stake = newStake
}
// AvgEntryValuation returns the Average Entry Valuation for a given party.
func (es *EquityShares) AvgEntryValuation(id string) num.Decimal {
if v, ok := es.lps[id]; ok {
es.stateChanged = true
return v.avg
}
return num.DecimalZero()
}
func (es *EquityShares) mustEquity(party string) num.Decimal {
eq, err := es.equity(party)
if err != nil {
panic(err)
}
return eq
}
// equity returns the following:
// (LP i stake)(n) x market_value_proxy(n) / (LP i avg_entry_valuation)(n)
// given a party id (i).
//
// Returns an error if the party has no stake.
func (es *EquityShares) equity(id string) (num.Decimal, error) {
if v, ok := es.lps[id]; ok {
return (v.vStake.Mul(es.mvp)).Div(v.avg), nil
}
return num.DecimalZero(), fmt.Errorf("party %s has no stake", id)
}
// AllShares returns the ratio of equity for each party on the market.
func (es *EquityShares) AllShares() map[string]num.Decimal {
return es.SharesExcept(map[string]struct{}{})
}
// SharesFromParty returns the equity-like shares of a given party on the market.
func (es *EquityShares) SharesFromParty(party string) num.Decimal {
totalEquity := num.DecimalZero()
partyELS := num.DecimalZero()
for id := range es.lps {
eq, err := es.equity(id)
if err != nil {
// since equity(id) returns an error when the party does not exist
// getting an error here means we are doing something wrong cause
// it should never happen unless `.equity()` behavior changes.
panic(err)
}
if id == party {
partyELS = eq
}
totalEquity = totalEquity.Add(eq)
}
if partyELS.Equal(num.DecimalZero()) || totalEquity.Equal(num.DecimalZero()) {
return num.DecimalZero()
}
return partyELS.Div(totalEquity)
}
// SharesExcept returns the ratio of equity for each party on the market, except
// the ones listed in parameter.
func (es *EquityShares) SharesExcept(except map[string]struct{}) map[string]num.Decimal {
// Calculate the equity for each party and the totalEquity (the sum of all
// the equities)
var totalEquity num.Decimal
shares := map[string]num.Decimal{}
for id := range es.lps {
// If the party is not one of the deployed parties, we just skip.
if _, ok := except[id]; ok {
continue
}
eq, err := es.equity(id)
if err != nil {
// since equity(id) returns an error when the party does not exist
// getting an error here means we are doing something wrong cause
// it should never happen unless `.equity()` behavior changes.
panic(err)
}
shares[id] = eq
totalEquity = totalEquity.Add(eq)
}
for id, eq := range shares {
eqshare := eq.Div(totalEquity)
shares[id] = eqshare
es.lps[id].share = eqshare
}
if len(shares) > 0 {
es.stateChanged = true
}
return shares
}