-
Notifications
You must be signed in to change notification settings - Fork 22
/
vesting.go
352 lines (297 loc) · 8.58 KB
/
vesting.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
// Copyright (c) 2023 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 vesting
import (
"context"
"sort"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
proto "code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/vesting Collateral,ActivityStreakVestingMultiplier,Broker,Assets
type Collateral interface {
TransferVestedRewards(
ctx context.Context, transfers []*types.Transfer,
) ([]*types.LedgerMovement, error)
GetVestingRecovery() map[string]map[string]*num.Uint
GetAllVestingQuantumBalance(party string) *num.Uint
}
type ActivityStreakVestingMultiplier interface {
GetRewardsVestingMultiplier(party string) num.Decimal
}
type Broker interface {
Send(events events.Event)
}
type Assets interface {
Get(assetID string) (*assets.Asset, error)
}
type PartyRewards struct {
// the amounts per assets still being locked in the
// account and not available to be released
// this is a map of:
// asset -> (remainingEpochLock -> Amount)
Locked map[string]map[uint64]*num.Uint
// the current part of the vesting account
// per asset available for vesting
Vesting map[string]*num.Uint
}
type Engine struct {
log *logging.Logger
c Collateral
asvm ActivityStreakVestingMultiplier
broker Broker
assets Assets
minTransfer num.Decimal
baseRate num.Decimal
benefitTiers []*types.VestingBenefitTier
state map[string]*PartyRewards
}
func New(
log *logging.Logger,
c Collateral,
asvm ActivityStreakVestingMultiplier,
broker Broker,
assets Assets,
) *Engine {
log = log.Named(namedLogger)
return &Engine{
log: log,
c: c,
asvm: asvm,
broker: broker,
assets: assets,
state: map[string]*PartyRewards{},
}
}
func (e *Engine) OnCheckpointLoaded() {
vestingBalances := e.c.GetVestingRecovery()
for party, assetBalances := range vestingBalances {
for asset, balance := range assetBalances {
e.increaseVestingBalance(party, asset, balance.Clone())
}
}
}
func (e *Engine) OnBenefitTiersUpdate(
_ context.Context, v interface{},
) error {
tiers, err := types.VestingBenefitTiersFromUntypedProto(v)
if err != nil {
return err
}
e.benefitTiers = tiers.Clone().Tiers
sort.Slice(e.benefitTiers, func(i, j int) bool {
return e.benefitTiers[i].MinimumQuantumBalance.LT(e.benefitTiers[j].MinimumQuantumBalance)
})
return nil
}
func (e *Engine) OnRewardVestingBaseRateUpdate(
_ context.Context, baseRate num.Decimal,
) error {
e.baseRate = baseRate
return nil
}
func (e *Engine) OnRewardVestingMinimumTransferUpdate(
_ context.Context, minimumTransfer num.Decimal,
) error {
e.minTransfer = minimumTransfer
return nil
}
func (e *Engine) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if epoch.Action == proto.EpochAction_EPOCH_ACTION_END {
e.broadcastRewardBonusMultipliers(ctx, epoch.Seq)
e.moveLocked()
e.distributeVested(ctx)
e.clearup()
}
}
func (e *Engine) OnEpochRestore(ctx context.Context, epoch types.Epoch) {}
func (e *Engine) AddReward(
party, asset string,
amount *num.Uint,
lockedForEpochs uint64,
) {
// no locktime, just increase the amount in vesting
if lockedForEpochs == 0 {
e.increaseVestingBalance(
party, asset, amount,
)
return
}
e.increaseLockedForAsset(
party, asset, amount, lockedForEpochs,
)
}
func (e *Engine) GetRewardBonusMultiplier(party string) num.Decimal {
quantumBalance := e.c.GetAllVestingQuantumBalance(party)
multiplier := num.DecimalOne()
for _, b := range e.benefitTiers {
if quantumBalance.LT(b.MinimumQuantumBalance) {
break
}
multiplier = b.RewardMultiplier
}
return multiplier
}
func (e *Engine) getPartyRewards(party string) *PartyRewards {
partyRewards, ok := e.state[party]
if !ok {
e.state[party] = &PartyRewards{
Locked: map[string]map[uint64]*num.Uint{},
Vesting: map[string]*num.Uint{},
}
partyRewards = e.state[party]
}
return partyRewards
}
func (e *Engine) increaseLockedForAsset(
party, asset string,
amount *num.Uint,
lockedForEpochs uint64,
) {
partyRewards := e.getPartyRewards(party)
locked, ok := partyRewards.Locked[asset]
if !ok {
locked = map[uint64]*num.Uint{}
}
amountLockedForEpochs, ok := locked[lockedForEpochs]
if !ok {
amountLockedForEpochs = num.UintZero()
}
amountLockedForEpochs.Add(amountLockedForEpochs, amount)
locked[lockedForEpochs] = amountLockedForEpochs
partyRewards.Locked[asset] = locked
}
func (e *Engine) increaseVestingBalance(
party, asset string,
amount *num.Uint,
) {
partyRewards := e.getPartyRewards(party)
vesting, ok := partyRewards.Vesting[asset]
if !ok {
vesting = num.UintZero()
}
vesting.Add(vesting, amount)
partyRewards.Vesting[asset] = vesting
}
// checkLocked will move around locked funds.
// if the lock for epoch reach 0, the full amount
// is added to the vesting amount for the asset.
func (e *Engine) moveLocked() {
for party, partyReward := range e.state {
for asset, assetLocks := range partyReward.Locked {
newLocked := map[uint64]*num.Uint{}
for epochLeft, amount := range assetLocks {
if epochLeft == 0 {
e.increaseVestingBalance(party, asset, amount)
continue
}
epochLeft--
// just add the new map
newLocked[epochLeft] = amount
}
// clear up if no rewards left
if len(newLocked) <= 0 {
delete(partyReward.Locked, asset)
continue
}
partyReward.Locked[asset] = newLocked
}
}
}
func (e *Engine) distributeVested(ctx context.Context) {
transfers := []*types.Transfer{}
parties := maps.Keys(e.state)
sort.Strings(parties)
for _, party := range parties {
rewards := e.state[party]
assets := maps.Keys(rewards.Vesting)
sort.Strings(assets)
for _, asset := range assets {
balance := rewards.Vesting[asset]
transfer := e.makeTransfer(
party, asset, balance.Clone(),
)
// we are clearing the account,
// we can delete it.
if transfer.MinAmount.EQ(balance) {
delete(rewards.Vesting, asset)
} else {
rewards.Vesting[asset] = balance.Sub(balance, transfer.MinAmount)
}
transfers = append(transfers, transfer)
}
}
// nothing to be done
if len(transfers) <= 0 {
return
}
responses, err := e.c.TransferVestedRewards(ctx, transfers)
if err != nil {
e.log.Panic("could not transfer funds", logging.Error(err))
}
e.broker.Send(events.NewLedgerMovements(ctx, responses))
}
func (e *Engine) makeTransfer(
party, assetID string,
balance *num.Uint,
) *types.Transfer {
asset, _ := e.assets.Get(assetID)
quantum := asset.Type().Details.Quantum
minTransferAmount, _ := num.UintFromDecimal(quantum.Mul(e.minTransfer))
transfer := &types.Transfer{
Owner: party,
Amount: &types.FinancialAmount{
Asset: assetID,
},
Type: types.TransferTypeRewardsVested,
}
expectTransfer, _ := num.UintFromDecimal(
balance.ToDecimal().Mul(e.baseRate).Mul(e.asvm.GetRewardsVestingMultiplier(party)),
)
// now we see which is the largest between the minimumTransfer
// and the expected transfer
expectTransfer = num.Max(expectTransfer, minTransferAmount)
// and now we prevent any transfer to exceed the current balance
expectTransfer = num.Min(expectTransfer, balance)
transfer.Amount.Amount = expectTransfer.Clone()
transfer.MinAmount = expectTransfer
return transfer
}
// just remove party entries once they are not needed anymore.
func (e *Engine) clearup() {
for party, v := range e.state {
if len(v.Locked) <= 0 && len(v.Vesting) <= 0 {
delete(e.state, party)
}
}
}
func (e *Engine) broadcastRewardBonusMultipliers(ctx context.Context, seq uint64) {
evt := &eventspb.VestingStatsUpdated{
AtEpoch: seq,
Stats: make([]*eventspb.PartyVestingStats, 0, len(e.state)),
}
parties := maps.Keys(e.state)
slices.Sort(parties)
for _, party := range parties {
evt.Stats = append(evt.Stats, &eventspb.PartyVestingStats{
PartyId: party,
RewardBonusMultiplier: e.GetRewardBonusMultiplier(party).String(),
})
}
e.broker.Send(events.NewVestingStatsUpdatedEvent(ctx, evt))
}