/
distribute.go
397 lines (354 loc) · 15 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
package keeper
import (
"fmt"
"time"
db "github.com/tendermint/tm-db"
"github.com/osmosis-labs/osmosis/v10/x/incentives/types"
lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) getDistributedCoinsFromGauges(gauges []types.Gauge) sdk.Coins {
coins := sdk.Coins{}
for _, gauge := range gauges {
coins = coins.Add(gauge.DistributedCoins...)
}
return coins
}
func (k Keeper) getToDistributeCoinsFromGauges(gauges []types.Gauge) sdk.Coins {
// TODO: Consider optimizing this in the future to only require one iteration over all gauges.
coins := k.getCoinsFromGauges(gauges)
distributed := k.getDistributedCoinsFromGauges(gauges)
return coins.Sub(distributed)
}
func (k Keeper) getToDistributeCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getToDistributeCoinsFromGauges(k.getGaugesFromIterator(ctx, iterator))
}
func (k Keeper) getDistributedCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getDistributedCoinsFromGauges(k.getGaugesFromIterator(ctx, iterator))
}
// moveUpcomingGaugeToActiveGauge is a utility to begin distribution for a specific gauge.
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 is a utility to finish distribution for a specific gauge.
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
}
// GetLocksToDistribution get locks that are associated to a condition.
func (k Keeper) GetLocksToDistribution(ctx sdk.Context, distrTo lockuptypes.QueryCondition) []lockuptypes.PeriodLock {
switch distrTo.LockQueryType {
case lockuptypes.ByDuration:
return k.lk.GetLocksLongerThanDurationDenom(ctx, distrTo.Denom, distrTo.Duration)
case lockuptypes.ByTime:
return k.lk.GetLocksPastTimeDenom(ctx, distrTo.Denom, distrTo.Timestamp)
default:
}
return []lockuptypes.PeriodLock{}
}
// getLocksToDistributionWithMaxDuration get locks that are associated to a condition
// and if its by duration, then use the min Duration.
func (k Keeper) getLocksToDistributionWithMaxDuration(ctx sdk.Context, distrTo lockuptypes.QueryCondition, minDuration time.Duration) []lockuptypes.PeriodLock {
switch distrTo.LockQueryType {
case lockuptypes.ByDuration:
// TODO: FIX ME!!!!
// Confusingly, synthetic lockups aren't indexed by denom as expected.
// Thus you have to do this as a hack
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!?!? Should have been blocked in ValidateBasic")
default:
}
return []lockuptypes.PeriodLock{}
}
// FilteredLocksDistributionEst estimate distribution amount coins from gauge for fitting conditions
// Expectation: gauge is a valid gauge
// filteredLocks are all locks that are valid for 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, error) {
TotalAmtLocked := k.lk.GetPeriodLocksAccumulation(ctx, gauge.DistributeTo)
if TotalAmtLocked.IsZero() {
return types.Gauge{}, nil, nil
}
remainCoins := gauge.Coins.Sub(gauge.DistributedCoins)
// Remaining epochs 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 for what rate it will get refilled at.
remainEpochs := uint64(1)
if !gauge.IsPerpetual {
remainEpochs = gauge.NumEpochsPaidOver - gauge.FilledEpochs
}
// TODO: Should this return err
if remainEpochs == 0 {
return gauge, sdk.Coins{}, 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, 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
}
func newDistributionInfo() distributionInfo {
return distributionInfo{
nextID: 0,
lockOwnerAddrToID: make(map[string]int),
idToBech32Addr: []string{},
idToDecodedAddr: []sdk.AccAddress{},
idToDistrCoins: []sdk.Coins{},
}
}
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
}
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 computed. 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 computed. 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)
remainEpochs := uint64(1)
if !gauge.IsPerpetual { // set remain epochs when it's not perpetual gauge
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
}
func (k Keeper) updateGaugePostDistribute(ctx sdk.Context, gauge types.Gauge, newlyDistributedCoins sdk.Coins) error {
// increase filled epochs after distribution
gauge.FilledEpochs += 1
gauge.DistributedCoins = gauge.DistributedCoins.Add(newlyDistributedCoins...)
if err := k.setGauge(ctx, &gauge); err != nil {
return err
}
return nil
}
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{}
}
// TODO: FIXME!!!
// 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 coins from gauge according to its conditions.
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
}
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
// TODO: Wat? we increment filled epochs earlier, this looks wrong and like
// were not paying out the last epoch of rewards...
if !gauge.IsPerpetual && gauge.NumEpochsPaidOver <= gauge.FilledEpochs+1 {
if err := k.moveActiveGaugeToFinishedGauge(ctx, gauge); err != nil {
panic(err)
}
}
}
}
// GetModuleToDistributeCoins returns sum of to distribute coins 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.UpcomingGaugesIteratorAfterTime(ctx, ctx.BlockTime()))
return activeGaugesDistr.Add(upcomingGaugesDistr...)
}
// GetModuleDistributedCoins returns sum of distributed coins so far.
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...)
}