/
keeper.go
81 lines (69 loc) · 2.2 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/peggyjv/sommelier/v6/x/incentives/types"
"github.com/tendermint/tendermint/libs/log"
)
// Keeper of the incentives store
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
DistributionKeeper types.DistributionKeeper
BankKeeper types.BankKeeper
MintKeeper types.MintKeeper
}
func NewKeeper(
cdc codec.BinaryCodec,
storeKey sdk.StoreKey,
paramSpace paramtypes.Subspace,
distributionKeeper types.DistributionKeeper,
bankKeeper types.BankKeeper,
mintKeeper types.MintKeeper,
) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: storeKey,
cdc: cdc,
paramSpace: paramSpace,
DistributionKeeper: distributionKeeper,
BankKeeper: bankKeeper,
MintKeeper: mintKeeper,
}
}
////////////
// Params //
////////////
// GetParamSet returns the parameters
func (k Keeper) GetParamSet(ctx sdk.Context) types.Params {
var p types.Params
k.paramSpace.GetParamSet(ctx, &p)
return p
}
// setParams sets the parameters in the store
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
////////////
// APY //
////////////
func (k Keeper) GetAPY(ctx sdk.Context) sdk.Dec {
incentivesParams := k.GetParamSet(ctx)
// check if incentives are enabled
if uint64(ctx.BlockHeight()) >= incentivesParams.IncentivesCutoffHeight || incentivesParams.DistributionPerBlock.IsZero() {
return sdk.ZeroDec()
}
mintParams := k.MintKeeper.GetParams(ctx)
bondedRatio := k.MintKeeper.BondedRatio(ctx)
totalCoins := k.MintKeeper.StakingTokenSupply(ctx)
annualRewards := incentivesParams.DistributionPerBlock.Amount.Mul(sdk.NewInt(int64(mintParams.BlocksPerYear)))
return annualRewards.ToDec().Quo(totalCoins.ToDec()).Quo(bondedRatio)
}