/
distribute.go
398 lines (355 loc) · 15.7 KB
/
distribute.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package keeper
import (
"fmt"
"time"
db "github.com/tendermint/tm-db"
"github.com/osmosis-labs/osmosis/v14/x/incentives/types"
lockuptypes "github.com/osmosis-labs/osmosis/v14/x/lockup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// getDistributedCoinsFromGauges returns coins that have been distributed already from the provided gauges
func (k Keeper) getDistributedCoinsFromGauges(gauges []types.Gauge) sdk.Coins {
coins := sdk.Coins{}
for _, gauge := range gauges {
coins = coins.Add(gauge.DistributedCoins...)
}
return coins
}
// getToDistributeCoinsFromGauges returns coins that have not been distributed yet from the provided gauges
func (k Keeper) getToDistributeCoinsFromGauges(gauges []types.Gauge) sdk.Coins {
coins := sdk.Coins{}
distributed := sdk.Coins{}
for _, gauge := range gauges {
coins = coins.Add(gauge.Coins...)
distributed = distributed.Add(gauge.DistributedCoins...)
}
return coins.Sub(distributed)
}
// getToDistributeCoinsFromIterator utilizes iterator to return a list of gauges.
// From these gauges, coins that have not yet been distributed are returned
func (k Keeper) getToDistributeCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getToDistributeCoinsFromGauges(k.getGaugesFromIterator(ctx, iterator))
}
// getDistributedCoinsFromIterator utilizes iterator to return a list of gauges.
// From these gauges, coins that have already been distributed are returned
func (k Keeper) getDistributedCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getDistributedCoinsFromGauges(k.getGaugesFromIterator(ctx, iterator))
}
// moveUpcomingGaugeToActiveGauge moves a gauge that has reached it's start time from an upcoming to an active status.
func (k Keeper) moveUpcomingGaugeToActiveGauge(ctx sdk.Context, gauge types.Gauge) error {
// validation for current time and distribution start time
if ctx.BlockTime().Before(gauge.StartTime) {
return fmt.Errorf("gauge is not able to start distribution yet: %s >= %s", ctx.BlockTime().String(), gauge.StartTime.String())
}
timeKey := getTimeKey(gauge.StartTime)
if err := k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixUpcomingGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id); err != nil {
return err
}
return nil
}
// moveActiveGaugeToFinishedGauge moves a gauge that has completed its distribution from an active to a finished status.
func (k Keeper) moveActiveGaugeToFinishedGauge(ctx sdk.Context, gauge types.Gauge) error {
timeKey := getTimeKey(gauge.StartTime)
if err := k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixFinishedGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.deleteGaugeIDForDenom(ctx, gauge.Id, gauge.DistributeTo.Denom); err != nil {
return err
}
k.hooks.AfterFinishDistribution(ctx, gauge.Id)
return nil
}
// getLocksToDistributionWithMaxDuration returns locks that match the provided lockuptypes QueryCondition,
// are greater than the provided minDuration, AND have yet to be distributed to.
func (k Keeper) getLocksToDistributionWithMaxDuration(ctx sdk.Context, distrTo lockuptypes.QueryCondition, minDuration time.Duration) []lockuptypes.PeriodLock {
switch distrTo.LockQueryType {
case lockuptypes.ByDuration:
denom := lockuptypes.NativeDenom(distrTo.Denom)
if distrTo.Duration > minDuration {
return k.lk.GetLocksLongerThanDurationDenom(ctx, denom, minDuration)
}
return k.lk.GetLocksLongerThanDurationDenom(ctx, distrTo.Denom, distrTo.Duration)
case lockuptypes.ByTime:
panic("Gauge by time is present, however is no longer supported. This should have been blocked in ValidateBasic")
default:
}
return []lockuptypes.PeriodLock{}
}
// FilteredLocksDistributionEst estimates distribution amount of coins from gauge.
// It also applies an update for the gauge, handling the sending of the rewards.
// (Note this update is in-memory, it does not change state.)
func (k Keeper) FilteredLocksDistributionEst(ctx sdk.Context, gauge types.Gauge, filteredLocks []lockuptypes.PeriodLock) (types.Gauge, sdk.Coins, bool, error) {
TotalAmtLocked := k.lk.GetPeriodLocksAccumulation(ctx, gauge.DistributeTo)
if TotalAmtLocked.IsZero() {
return types.Gauge{}, nil, false, nil
}
if TotalAmtLocked.IsNegative() {
return types.Gauge{}, nil, true, nil
}
remainCoins := gauge.Coins.Sub(gauge.DistributedCoins)
// remainEpochs is the number of remaining epochs that the gauge will pay out its rewards.
// for a perpetual gauge, it will pay out everything in the next epoch, and we don't make
// an assumption of the rate at which it will get refilled at.
remainEpochs := uint64(1)
if !gauge.IsPerpetual {
remainEpochs = gauge.NumEpochsPaidOver - gauge.FilledEpochs
}
if remainEpochs == 0 {
return gauge, sdk.Coins{}, false, nil
}
remainCoinsPerEpoch := sdk.Coins{}
for _, coin := range remainCoins {
// distribution amount per epoch = gauge_size / (remain_epochs)
amt := coin.Amount.QuoRaw(int64(remainEpochs))
remainCoinsPerEpoch = remainCoinsPerEpoch.Add(sdk.NewCoin(coin.Denom, amt))
}
// now we compute the filtered coins
filteredDistrCoins := sdk.Coins{}
if len(filteredLocks) == 0 {
// if were doing no filtering, we want to calculate the total amount to distributed in
// the next epoch.
// distribution in next epoch = gauge_size / (remain_epochs)
filteredDistrCoins = remainCoinsPerEpoch
}
for _, lock := range filteredLocks {
denomLockAmt := lock.Coins.AmountOf(gauge.DistributeTo.Denom)
for _, coin := range remainCoinsPerEpoch {
// distribution amount = gauge_size * denom_lock_amount / (total_denom_lock_amount * remain_epochs)
// distribution amount = gauge_size_per_epoch * denom_lock_amount / total_denom_lock_amount
amt := coin.Amount.Mul(denomLockAmt).Quo(TotalAmtLocked)
filteredDistrCoins = filteredDistrCoins.Add(sdk.NewCoin(coin.Denom, amt))
}
}
// increase filled epochs after distribution
gauge.FilledEpochs += 1
gauge.DistributedCoins = gauge.DistributedCoins.Add(remainCoinsPerEpoch...)
return gauge, filteredDistrCoins, false, nil
}
// distributionInfo stores all of the information for pent up sends for rewards distributions.
// This enables us to lower the number of events and calls to back.
type distributionInfo struct {
nextID int
lockOwnerAddrToID map[string]int
idToBech32Addr []string
idToDecodedAddr []sdk.AccAddress
idToDistrCoins []sdk.Coins
}
// newDistributionInfo creates a new distributionInfo struct
func newDistributionInfo() distributionInfo {
return distributionInfo{
nextID: 0,
lockOwnerAddrToID: make(map[string]int),
idToBech32Addr: []string{},
idToDecodedAddr: []sdk.AccAddress{},
idToDistrCoins: []sdk.Coins{},
}
}
// addLockRewards adds the provided rewards to the lockID mapped to the provided owner address.
func (d *distributionInfo) addLockRewards(owner string, rewards sdk.Coins) error {
if id, ok := d.lockOwnerAddrToID[owner]; ok {
oldDistrCoins := d.idToDistrCoins[id]
d.idToDistrCoins[id] = rewards.Add(oldDistrCoins...)
} else {
id := d.nextID
d.nextID += 1
d.lockOwnerAddrToID[owner] = id
decodedOwnerAddr, err := sdk.AccAddressFromBech32(owner)
if err != nil {
return err
}
d.idToBech32Addr = append(d.idToBech32Addr, owner)
d.idToDecodedAddr = append(d.idToDecodedAddr, decodedOwnerAddr)
d.idToDistrCoins = append(d.idToDistrCoins, rewards)
}
return nil
}
// doDistributionSends utilizes provided distributionInfo to send coins from the module account to various recipients.
func (k Keeper) doDistributionSends(ctx sdk.Context, distrs *distributionInfo) error {
numIDs := len(distrs.idToDecodedAddr)
ctx.Logger().Debug(fmt.Sprintf("Beginning distribution to %d users", numIDs))
err := k.bk.SendCoinsFromModuleToManyAccounts(
ctx,
types.ModuleName,
distrs.idToDecodedAddr,
distrs.idToDistrCoins)
if err != nil {
return err
}
ctx.Logger().Debug("Finished sending, now creating liquidity add events")
for id := 0; id < numIDs; id++ {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtDistribution,
sdk.NewAttribute(types.AttributeReceiver, distrs.idToBech32Addr[id]),
sdk.NewAttribute(types.AttributeAmount, distrs.idToDistrCoins[id].String()),
),
})
}
ctx.Logger().Debug(fmt.Sprintf("Finished Distributing to %d users", numIDs))
return nil
}
// distributeSyntheticInternal runs the distribution logic for a synthetic rewards distribution gauge, and adds the sends to
// the distrInfo struct. It also updates the gauge for the distribution.
// locks is expected to be the correct set of lock recipients for this gauge.
func (k Keeper) distributeSyntheticInternal(
ctx sdk.Context, gauge types.Gauge, locks []lockuptypes.PeriodLock, distrInfo *distributionInfo,
) (sdk.Coins, error) {
qualifiedLocks := k.lk.GetLocksLongerThanDurationDenom(ctx, gauge.DistributeTo.Denom, gauge.DistributeTo.Duration)
// map from lockID to present index in resultant list
// to be state compatible with what we had before, we iterate over locks, to get qualified locks
// to be in the same order as what is present in locks.
// in a future release, we can just use qualified locks directly.
type lockIndexPair struct {
lock lockuptypes.PeriodLock
index int
}
qualifiedLocksMap := make(map[uint64]lockIndexPair, len(qualifiedLocks))
for _, lock := range qualifiedLocks {
qualifiedLocksMap[lock.ID] = lockIndexPair{lock, -1}
}
curIndex := 0
for _, lock := range locks {
if v, ok := qualifiedLocksMap[lock.ID]; ok {
qualifiedLocksMap[lock.ID] = lockIndexPair{v.lock, curIndex}
curIndex += 1
}
}
sortedAndTrimmedQualifiedLocks := make([]lockuptypes.PeriodLock, curIndex)
for _, v := range qualifiedLocksMap {
if v.index < 0 {
continue
}
sortedAndTrimmedQualifiedLocks[v.index] = v.lock
}
return k.distributeInternal(ctx, gauge, sortedAndTrimmedQualifiedLocks, distrInfo)
}
// distributeInternal runs the distribution logic for a gauge, and adds the sends to
// the distrInfo struct. It also updates the gauge for the distribution.
// Locks is expected to be the correct set of lock recipients for this gauge.
func (k Keeper) distributeInternal(
ctx sdk.Context, gauge types.Gauge, locks []lockuptypes.PeriodLock, distrInfo *distributionInfo,
) (sdk.Coins, error) {
totalDistrCoins := sdk.NewCoins()
denom := lockuptypes.NativeDenom(gauge.DistributeTo.Denom)
lockSum := lockuptypes.SumLocksByDenom(locks, denom)
if lockSum.IsZero() {
return nil, nil
}
remainCoins := gauge.Coins.Sub(gauge.DistributedCoins)
// if its a perpetual gauge, we set remaining epochs to 1.
// otherwise is is a non perpetual gauge and we determine how many epoch payouts are left
remainEpochs := uint64(1)
if !gauge.IsPerpetual {
remainEpochs = gauge.NumEpochsPaidOver - gauge.FilledEpochs
}
for _, lock := range locks {
distrCoins := sdk.Coins{}
for _, coin := range remainCoins {
// distribution amount = gauge_size * denom_lock_amount / (total_denom_lock_amount * remain_epochs)
denomLockAmt := lock.Coins.AmountOfNoDenomValidation(denom)
amt := coin.Amount.Mul(denomLockAmt).Quo(lockSum.Mul(sdk.NewInt(int64(remainEpochs))))
if amt.IsPositive() {
newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt}
distrCoins = distrCoins.Add(newlyDistributedCoin)
}
}
distrCoins = distrCoins.Sort()
if distrCoins.Empty() {
continue
}
// update the amount for that address
err := distrInfo.addLockRewards(lock.Owner, distrCoins)
if err != nil {
return nil, err
}
totalDistrCoins = totalDistrCoins.Add(distrCoins...)
}
err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins)
return totalDistrCoins, err
}
// updateGaugePostDistribute increments the gauge's filled epochs field.
// Also adds the coins that were just distributed to the gauge's distributed coins field.
func (k Keeper) updateGaugePostDistribute(ctx sdk.Context, gauge types.Gauge, newlyDistributedCoins sdk.Coins) error {
gauge.FilledEpochs += 1
gauge.DistributedCoins = gauge.DistributedCoins.Add(newlyDistributedCoins...)
if err := k.setGauge(ctx, &gauge); err != nil {
return err
}
return nil
}
// getDistributeToBaseLocks takes a gauge along with cached period locks by denom and returns locks that must be distributed to
func (k Keeper) getDistributeToBaseLocks(ctx sdk.Context, gauge types.Gauge, cache map[string][]lockuptypes.PeriodLock) []lockuptypes.PeriodLock {
// if gauge is empty, don't get the locks
if gauge.Coins.Empty() {
return []lockuptypes.PeriodLock{}
}
// Confusingly, there is no way to get all synthetic lockups. Thus we use a separate method `distributeSyntheticInternal` to separately get lockSum for synthetic lockups.
// All gauges have a precondition of being ByDuration.
distributeBaseDenom := lockuptypes.NativeDenom(gauge.DistributeTo.Denom)
if _, ok := cache[distributeBaseDenom]; !ok {
cache[distributeBaseDenom] = k.getLocksToDistributionWithMaxDuration(
ctx, gauge.DistributeTo, time.Millisecond)
}
// get this from memory instead of hitting iterators / underlying stores.
// due to many details of cacheKVStore, iteration will still cause expensive IAVL reads.
allLocks := cache[distributeBaseDenom]
return FilterLocksByMinDuration(allLocks, gauge.DistributeTo.Duration)
}
// Distribute distributes coins from an array of gauges to all eligible locks.
func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, error) {
distrInfo := newDistributionInfo()
locksByDenomCache := make(map[string][]lockuptypes.PeriodLock)
totalDistributedCoins := sdk.Coins{}
for _, gauge := range gauges {
filteredLocks := k.getDistributeToBaseLocks(ctx, gauge, locksByDenomCache)
// send based on synthetic lockup coins if it's distributing to synthetic lockups
var gaugeDistributedCoins sdk.Coins
var err error
if lockuptypes.IsSyntheticDenom(gauge.DistributeTo.Denom) {
gaugeDistributedCoins, err = k.distributeSyntheticInternal(ctx, gauge, filteredLocks, &distrInfo)
} else {
gaugeDistributedCoins, err = k.distributeInternal(ctx, gauge, filteredLocks, &distrInfo)
}
if err != nil {
return nil, err
}
totalDistributedCoins = totalDistributedCoins.Add(gaugeDistributedCoins...)
}
err := k.doDistributionSends(ctx, &distrInfo)
if err != nil {
return nil, err
}
k.hooks.AfterEpochDistribution(ctx)
k.checkFinishDistribution(ctx, gauges)
return totalDistributedCoins, nil
}
// checkFinishDistribution checks if all non perpetual gauges provided have completed their required distributions.
// If complete, move the gauge from an active to a finished status.
func (k Keeper) checkFinishDistribution(ctx sdk.Context, gauges []types.Gauge) {
for _, gauge := range gauges {
// filled epoch is increased in this step and we compare with +1
if !gauge.IsPerpetual && gauge.NumEpochsPaidOver <= gauge.FilledEpochs+1 {
if err := k.moveActiveGaugeToFinishedGauge(ctx, gauge); err != nil {
panic(err)
}
}
}
}
// GetModuleToDistributeCoins returns sum of coins yet to be distributed for all of the module.
func (k Keeper) GetModuleToDistributeCoins(ctx sdk.Context) sdk.Coins {
activeGaugesDistr := k.getToDistributeCoinsFromIterator(ctx, k.ActiveGaugesIterator(ctx))
upcomingGaugesDistr := k.getToDistributeCoinsFromIterator(ctx, k.UpcomingGaugesIterator(ctx))
return activeGaugesDistr.Add(upcomingGaugesDistr...)
}
// GetModuleDistributedCoins returns sum of coins that have been distributed so far for all of the module.
func (k Keeper) GetModuleDistributedCoins(ctx sdk.Context) sdk.Coins {
activeGaugesDistr := k.getDistributedCoinsFromIterator(ctx, k.ActiveGaugesIterator(ctx))
finishedGaugesDistr := k.getDistributedCoinsFromIterator(ctx, k.FinishedGaugesIterator(ctx))
return activeGaugesDistr.Add(finishedGaugesDistr...)
}