-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
160 lines (130 loc) · 5 KB
/
keeper.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
package keeper
import (
"fmt"
"time"
"github.com/tendermint/tendermint/libs/log"
"github.com/merlinslair/merlin/osmoutils"
gammtypes "github.com/merlinslair/merlin/x/gamm/types"
incentivestypes "github.com/merlinslair/merlin/x/incentives/types"
lockuptypes "github.com/merlinslair/merlin/x/lockup/types"
"github.com/merlinslair/merlin/x/pool-incentives/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
type Keeper struct {
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
incentivesKeeper types.IncentivesKeeper
distrKeeper types.DistrKeeper
poolmanagerKeeper types.PoolManagerKeeper
}
func NewKeeper(storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, incentivesKeeper types.IncentivesKeeper, distrKeeper types.DistrKeeper, poolmanagerKeeper types.PoolManagerKeeper) Keeper {
// ensure pool-incentives module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: storeKey,
paramSpace: paramSpace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
incentivesKeeper: incentivesKeeper,
distrKeeper: distrKeeper,
poolmanagerKeeper: poolmanagerKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
func (k Keeper) CreatePoolGauges(ctx sdk.Context, poolId uint64) error {
// Create the same number of gaugeges as there are LockableDurations
for _, lockableDuration := range k.GetLockableDurations(ctx) {
gaugeId, err := k.incentivesKeeper.CreateGauge(
ctx,
true,
k.accountKeeper.GetModuleAddress(types.ModuleName),
sdk.Coins{},
lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: gammtypes.GetPoolShareDenom(poolId),
Duration: lockableDuration,
Timestamp: time.Time{},
},
// QUESTION: Should we set the startTime as the epoch start time that the modules share or the current block time?
ctx.BlockTime(),
1,
)
if err != nil {
return err
}
k.SetPoolGaugeId(ctx, poolId, lockableDuration, gaugeId)
}
return nil
}
func (k Keeper) SetPoolGaugeId(ctx sdk.Context, poolId uint64, lockableDuration time.Duration, gaugeId uint64) {
key := types.GetPoolGaugeIdStoreKey(poolId, lockableDuration)
store := ctx.KVStore(k.storeKey)
store.Set(key, sdk.Uint64ToBigEndian(gaugeId))
key = types.GetPoolIdFromGaugeIdStoreKey(gaugeId, lockableDuration)
store.Set(key, sdk.Uint64ToBigEndian(poolId))
}
func (k Keeper) GetPoolGaugeId(ctx sdk.Context, poolId uint64, lockableDuration time.Duration) (uint64, error) {
key := types.GetPoolGaugeIdStoreKey(poolId, lockableDuration)
store := ctx.KVStore(k.storeKey)
bz := store.Get(key)
if len(bz) == 0 {
return 0, sdkerrors.Wrapf(types.ErrNoGaugeIdExist, "gauge id for pool (%d) with duration (%s) not exist", poolId, lockableDuration.String())
}
return sdk.BigEndianToUint64(bz), nil
}
func (k Keeper) GetPoolIdFromGaugeId(ctx sdk.Context, gaugeId uint64, lockableDuration time.Duration) (uint64, error) {
key := types.GetPoolIdFromGaugeIdStoreKey(gaugeId, lockableDuration)
store := ctx.KVStore(k.storeKey)
bz := store.Get(key)
if len(bz) == 0 {
return 0, sdkerrors.Wrapf(types.ErrNoGaugeIdExist, "pool for gauge id (%d) with duration (%s) not exist", gaugeId, lockableDuration.String())
}
return sdk.BigEndianToUint64(bz), nil
}
func (k Keeper) SetLockableDurations(ctx sdk.Context, lockableDurations []time.Duration) {
store := ctx.KVStore(k.storeKey)
info := types.LockableDurationsInfo{LockableDurations: lockableDurations}
osmoutils.MustSet(store, types.LockableDurationsKey, &info)
}
func (k Keeper) GetLockableDurations(ctx sdk.Context) []time.Duration {
store := ctx.KVStore(k.storeKey)
info := types.LockableDurationsInfo{}
osmoutils.MustGet(store, types.LockableDurationsKey, &info)
return info.LockableDurations
}
func (k Keeper) GetAllGauges(ctx sdk.Context) []incentivestypes.Gauge {
gauges := k.incentivesKeeper.GetGauges(ctx)
return gauges
}
func (k Keeper) IsPoolIncentivized(ctx sdk.Context, poolId uint64) bool {
lockableDurations := k.GetLockableDurations(ctx)
distrInfo := k.GetDistrInfo(ctx)
candidateGaugeIds := []uint64{}
for _, lockableDuration := range lockableDurations {
gaugeId, err := k.GetPoolGaugeId(ctx, poolId, lockableDuration)
if err == nil {
candidateGaugeIds = append(candidateGaugeIds, gaugeId)
}
}
for _, record := range distrInfo.Records {
for _, gaugeId := range candidateGaugeIds {
if record.GaugeId == gaugeId {
return true
}
}
}
return false
}