2020-04-24 15:20:34 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/params/subspace"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper keeper for the incentive module
|
|
|
|
type Keeper struct {
|
|
|
|
accountKeeper types.AccountKeeper
|
|
|
|
cdc *codec.Codec
|
|
|
|
cdpKeeper types.CdpKeeper
|
|
|
|
key sdk.StoreKey
|
|
|
|
paramSubspace subspace.Subspace
|
|
|
|
supplyKeeper types.SupplyKeeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a new keeper
|
|
|
|
func NewKeeper(
|
|
|
|
cdc *codec.Codec, key sdk.StoreKey, paramstore subspace.Subspace, sk types.SupplyKeeper,
|
|
|
|
cdpk types.CdpKeeper, ak types.AccountKeeper,
|
|
|
|
) Keeper {
|
|
|
|
|
|
|
|
return Keeper{
|
|
|
|
accountKeeper: ak,
|
|
|
|
cdc: cdc,
|
|
|
|
cdpKeeper: cdpk,
|
|
|
|
key: key,
|
|
|
|
paramSubspace: paramstore.WithKeyTable(types.ParamKeyTable()),
|
|
|
|
supplyKeeper: sk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
// GetClaim returns the claim in the store corresponding the the input address collateral type and id and a boolean for if the claim was found
|
2021-01-18 19:12:37 +00:00
|
|
|
func (k Keeper) GetClaim(ctx sdk.Context, addr sdk.AccAddress) (types.USDXMintingClaim, bool) {
|
2020-04-24 15:20:34 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
2021-01-18 19:12:37 +00:00
|
|
|
bz := store.Get(addr)
|
2020-04-24 15:20:34 +00:00
|
|
|
if bz == nil {
|
2021-01-18 19:12:37 +00:00
|
|
|
return types.USDXMintingClaim{}, false
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
2021-01-18 19:12:37 +00:00
|
|
|
var c types.USDXMintingClaim
|
2020-04-24 15:20:34 +00:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &c)
|
|
|
|
return c, true
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
// SetClaim sets the claim in the store corresponding to the input address, collateral type, and id
|
2021-01-18 19:12:37 +00:00
|
|
|
func (k Keeper) SetClaim(ctx sdk.Context, c types.USDXMintingClaim) {
|
2020-04-24 15:20:34 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(c)
|
2021-01-18 19:12:37 +00:00
|
|
|
store.Set(c.Owner, bz)
|
2020-04-24 15:20:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
// DeleteClaim deletes the claim in the store corresponding to the input address, collateral type, and id
|
2021-01-18 19:12:37 +00:00
|
|
|
func (k Keeper) DeleteClaim(ctx sdk.Context, owner sdk.AccAddress) {
|
2020-04-24 15:20:34 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
2021-01-18 19:12:37 +00:00
|
|
|
store.Delete(owner)
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateClaims iterates over all claim objects in the store and preforms a callback function
|
2021-01-18 19:12:37 +00:00
|
|
|
func (k Keeper) IterateClaims(ctx sdk.Context, cb func(c types.USDXMintingClaim) (stop bool)) {
|
2020-04-24 15:20:34 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2021-01-18 19:12:37 +00:00
|
|
|
var c types.USDXMintingClaim
|
2020-04-24 15:20:34 +00:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &c)
|
|
|
|
if cb(c) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllClaims returns all Claim objects in the store
|
2021-01-18 19:12:37 +00:00
|
|
|
func (k Keeper) GetAllClaims(ctx sdk.Context) types.USDXMintingClaims {
|
|
|
|
cs := types.USDXMintingClaims{}
|
|
|
|
k.IterateClaims(ctx, func(c types.USDXMintingClaim) (stop bool) {
|
2020-04-24 15:20:34 +00:00
|
|
|
cs = append(cs, c)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:12:37 +00:00
|
|
|
// GetPreviousAccrualTime returns the last time a collateral type accrued rewards
|
|
|
|
func (k Keeper) GetPreviousAccrualTime(ctx sdk.Context, ctype string) (blockTime time.Time, found bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BlockTimeKey)
|
|
|
|
bz := store.Get([]byte(ctype))
|
|
|
|
if bz == nil {
|
2020-04-24 15:20:34 +00:00
|
|
|
return time.Time{}, false
|
|
|
|
}
|
2021-01-18 19:12:37 +00:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &blockTime)
|
2020-04-24 15:20:34 +00:00
|
|
|
return blockTime, true
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:12:37 +00:00
|
|
|
// SetPreviousAccrualTime sets the last time a collateral type accrued rewards
|
|
|
|
func (k Keeper) SetPreviousAccrualTime(ctx sdk.Context, ctype string, blockTime time.Time) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BlockTimeKey)
|
|
|
|
store.Set([]byte(ctype), k.cdc.MustMarshalBinaryBare(blockTime))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAccrualTimes iterates over all previous accrual times and preforms a callback function
|
|
|
|
func (k Keeper) IterateAccrualTimes(ctx sdk.Context, cb func(string, time.Time) (stop bool)) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BlockTimeKey)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
var accrualTime time.Time
|
|
|
|
var collateralType string
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &collateralType)
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &accrualTime)
|
|
|
|
if cb(collateralType, accrualTime) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRewardFactor returns the current reward factor for an individual collateral type
|
|
|
|
func (k Keeper) GetRewardFactor(ctx sdk.Context, ctype string) (factor sdk.Dec, found bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.RewardFactorKey)
|
|
|
|
bz := store.Get([]byte(ctype))
|
|
|
|
if bz == nil {
|
|
|
|
return sdk.ZeroDec(), false
|
|
|
|
}
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &factor)
|
|
|
|
return factor, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRewardFactor sets the current reward factor for an individual collateral type
|
|
|
|
func (k Keeper) SetRewardFactor(ctx sdk.Context, ctype string, factor sdk.Dec) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.RewardFactorKey)
|
|
|
|
store.Set([]byte(ctype), k.cdc.MustMarshalBinaryBare(factor))
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|