forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge.go
283 lines (249 loc) · 8.76 KB
/
gauge.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
package keeper
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/gogo/protobuf/proto"
db "github.com/tendermint/tm-db"
epochtypes "github.com/MonOsmosis/osmosis/v10/x/epochs/types"
"github.com/MonOsmosis/osmosis/v10/x/incentives/types"
lockuptypes "github.com/MonOsmosis/osmosis/v10/x/lockup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Iterate over everything in a gauges iterator, until it reaches the end. Return all gauges iterated over.
func (k Keeper) getGaugesFromIterator(ctx sdk.Context, iterator db.Iterator) []types.Gauge {
gauges := []types.Gauge{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
gaugeIDs := []uint64{}
err := json.Unmarshal(iterator.Value(), &gaugeIDs)
if err != nil {
panic(err)
}
for _, gaugeID := range gaugeIDs {
gauge, err := k.GetGaugeByID(ctx, gaugeID)
if err != nil {
panic(err)
}
gauges = append(gauges, *gauge)
}
}
return gauges
}
// Compute the total amount of coins in all the gauges.
func (k Keeper) getCoinsFromGauges(gauges []types.Gauge) sdk.Coins {
coins := sdk.Coins{}
for _, gauge := range gauges {
coins = coins.Add(gauge.Coins...)
}
return coins
}
func (k Keeper) getCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getCoinsFromGauges(k.getGaugesFromIterator(ctx, iterator))
}
// setGauge set the gauge inside store.
func (k Keeper) setGauge(ctx sdk.Context, gauge *types.Gauge) error {
store := ctx.KVStore(k.storeKey)
bz, err := proto.Marshal(gauge)
if err != nil {
return err
}
store.Set(gaugeStoreKey(gauge.Id), bz)
return nil
}
// CreateGaugeRefKeys adds gauge references as needed. Used to consolidate codepaths for InitGenesis and CreateGauge.
func (k Keeper) CreateGaugeRefKeys(ctx sdk.Context, gauge *types.Gauge, combinedKeys []byte, activeOrUpcomingGauge bool) error {
if err := k.addGaugeRefByKey(ctx, combinedKeys, gauge.Id); err != nil {
return err
}
if activeOrUpcomingGauge {
if err := k.addGaugeIDForDenom(ctx, gauge.Id, gauge.DistributeTo.Denom); err != nil {
return err
}
}
return nil
}
func (k Keeper) SetGaugeWithRefKey(ctx sdk.Context, gauge *types.Gauge) error {
err := k.setGauge(ctx, gauge)
if err != nil {
return err
}
curTime := ctx.BlockTime()
timeKey := getTimeKey(gauge.StartTime)
activeOrUpcomingGauge := gauge.IsActiveGauge(curTime) || gauge.IsUpcomingGauge(curTime)
if gauge.IsUpcomingGauge(curTime) {
combinedKeys := combineKeys(types.KeyPrefixUpcomingGauges, timeKey)
return k.CreateGaugeRefKeys(ctx, gauge, combinedKeys, activeOrUpcomingGauge)
} else if gauge.IsActiveGauge(curTime) {
combinedKeys := combineKeys(types.KeyPrefixActiveGauges, timeKey)
return k.CreateGaugeRefKeys(ctx, gauge, combinedKeys, activeOrUpcomingGauge)
} else {
combinedKeys := combineKeys(types.KeyPrefixFinishedGauges, timeKey)
return k.CreateGaugeRefKeys(ctx, gauge, combinedKeys, activeOrUpcomingGauge)
}
}
// CreateGauge create a gauge and send coins to the gauge.
func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddress, coins sdk.Coins, distrTo lockuptypes.QueryCondition, startTime time.Time, numEpochsPaidOver uint64) (uint64, error) {
// Ensure that this gauge's duration is one of the allowed durations on chain
durations := k.GetLockableDurations(ctx)
if distrTo.LockQueryType == lockuptypes.ByDuration {
durationOk := false
for _, duration := range durations {
if duration == distrTo.Duration {
durationOk = true
break
}
}
if !durationOk {
return 0, fmt.Errorf("invalid duration: %d", distrTo.Duration)
}
}
// Ensure that the denom this gauge pays out to exists on-chain
if !k.bk.HasSupply(ctx, distrTo.Denom) && !strings.Contains(distrTo.Denom, "osmovaloper") {
return 0, fmt.Errorf("denom does not exist: %s", distrTo.Denom)
}
gauge := types.Gauge{
Id: k.GetLastGaugeID(ctx) + 1,
IsPerpetual: isPerpetual,
DistributeTo: distrTo,
Coins: coins,
StartTime: startTime,
NumEpochsPaidOver: numEpochsPaidOver,
}
if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, gauge.Coins); err != nil {
return 0, err
}
err := k.setGauge(ctx, &gauge)
if err != nil {
return 0, err
}
k.SetLastGaugeID(ctx, gauge.Id)
// TODO: Do we need to be concerned with case where this should be ActiveGauges?
combinedKeys := combineKeys(types.KeyPrefixUpcomingGauges, getTimeKey(gauge.StartTime))
activeOrUpcomingGauge := true
err = k.CreateGaugeRefKeys(ctx, &gauge, combinedKeys, activeOrUpcomingGauge)
if err != nil {
return 0, err
}
k.hooks.AfterCreateGauge(ctx, gauge.Id)
return gauge.Id, nil
}
// AddToGauge add coins to gauge.
func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gaugeID uint64) error {
gauge, err := k.GetGaugeByID(ctx, gaugeID)
if err != nil {
return err
}
if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil {
return err
}
gauge.Coins = gauge.Coins.Add(coins...)
err = k.setGauge(ctx, gauge)
if err != nil {
return err
}
k.hooks.AfterAddToGauge(ctx, gauge.Id)
return nil
}
// GetGaugeByID Returns gauge from gauge ID.
func (k Keeper) GetGaugeByID(ctx sdk.Context, gaugeID uint64) (*types.Gauge, error) {
gauge := types.Gauge{}
store := ctx.KVStore(k.storeKey)
gaugeKey := gaugeStoreKey(gaugeID)
if !store.Has(gaugeKey) {
return nil, fmt.Errorf("gauge with ID %d does not exist", gaugeID)
}
bz := store.Get(gaugeKey)
if err := proto.Unmarshal(bz, &gauge); err != nil {
return nil, err
}
return &gauge, nil
}
// GetGaugeFromIDs returns gauges from gauge ids reference.
func (k Keeper) GetGaugeFromIDs(ctx sdk.Context, gaugeIDs []uint64) ([]types.Gauge, error) {
gauges := []types.Gauge{}
for _, gaugeID := range gaugeIDs {
gauge, err := k.GetGaugeByID(ctx, gaugeID)
if err != nil {
return []types.Gauge{}, err
}
gauges = append(gauges, *gauge)
}
return gauges, nil
}
// GetGauges returns gauges both upcoming and active.
func (k Keeper) GetGauges(ctx sdk.Context) []types.Gauge {
return k.getGaugesFromIterator(ctx, k.GaugesIterator(ctx))
}
func (k Keeper) GetNotFinishedGauges(ctx sdk.Context) []types.Gauge {
return append(k.GetActiveGauges(ctx), k.GetUpcomingGauges(ctx)...)
}
// GetActiveGauges returns active gauges.
func (k Keeper) GetActiveGauges(ctx sdk.Context) []types.Gauge {
return k.getGaugesFromIterator(ctx, k.ActiveGaugesIterator(ctx))
}
// GetUpcomingGauges returns scheduled gauges.
func (k Keeper) GetUpcomingGauges(ctx sdk.Context) []types.Gauge {
return k.getGaugesFromIterator(ctx, k.UpcomingGaugesIterator(ctx))
}
// GetFinishedGauges returns finished gauges.
func (k Keeper) GetFinishedGauges(ctx sdk.Context) []types.Gauge {
return k.getGaugesFromIterator(ctx, k.FinishedGaugesIterator(ctx))
}
// GetRewardsEst returns rewards estimation at a future specific time
// If locks are nil, it returns the rewards between now and the end epoch associated with address.
// If locks are not nil, it returns all the rewards for the given locks between now and end epoch.
func (k Keeper) GetRewardsEst(ctx sdk.Context, addr sdk.AccAddress, locks []lockuptypes.PeriodLock, endEpoch int64) sdk.Coins {
// If locks are nil, populate with all locks associated with the address
if len(locks) == 0 {
locks = k.lk.GetAccountPeriodLocks(ctx, addr)
}
// Get all gauges that reward to these locks
// First get all the denominations being locked up
denomSet := map[string]bool{}
for _, l := range locks {
for _, c := range l.Coins {
denomSet[c.Denom] = true
}
}
gauges := []types.Gauge{}
// initialize gauges to active and upcomings if not set
for s := range denomSet {
gaugeIDs := k.getAllGaugeIDsByDenom(ctx, s)
// Each gauge only rewards locks to one denom, so no duplicates
for _, id := range gaugeIDs {
gauge, err := k.GetGaugeByID(ctx, id)
// Shouldn't happen
if err != nil {
return sdk.Coins{}
}
gauges = append(gauges, *gauge)
}
}
// estimate rewards
estimatedRewards := sdk.Coins{}
epochInfo := k.GetEpochInfo(ctx)
// no need to change storage while doing estimation and we use cached context
cacheCtx, _ := ctx.CacheContext()
for _, gauge := range gauges {
distrBeginEpoch := epochInfo.CurrentEpoch
blockTime := ctx.BlockTime()
if gauge.StartTime.After(blockTime) {
distrBeginEpoch = epochInfo.CurrentEpoch + 1 + int64(gauge.StartTime.Sub(blockTime)/epochInfo.Duration)
}
for epoch := distrBeginEpoch; epoch <= endEpoch; epoch++ {
newGauge, distrCoins, err := k.FilteredLocksDistributionEst(cacheCtx, gauge, locks)
if err != nil {
continue
}
estimatedRewards = estimatedRewards.Add(distrCoins...)
gauge = newGauge
}
}
return estimatedRewards
}
func (k Keeper) GetEpochInfo(ctx sdk.Context) epochtypes.EpochInfo {
params := k.GetParams(ctx)
return k.ek.GetEpochInfo(ctx, params.DistrEpochIdentifier)
}