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
// GetRewardPeriod returns the reward period from the store for the input collateral type and a boolean for if it was found
func ( k Keeper ) GetRewardPeriod ( ctx sdk . Context , collateralType string ) ( types . RewardPeriod , bool ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . RewardPeriodKeyPrefix )
2020-08-21 19:42:46 +00:00
bz := store . Get ( [ ] byte ( collateralType ) )
2020-04-24 15:20:34 +00:00
if bz == nil {
return types . RewardPeriod { } , false
}
var rp types . RewardPeriod
k . cdc . MustUnmarshalBinaryBare ( bz , & rp )
return rp , true
}
// SetRewardPeriod sets the reward period in the store for the input deno,
func ( k Keeper ) SetRewardPeriod ( ctx sdk . Context , rp types . RewardPeriod ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . RewardPeriodKeyPrefix )
bz := k . cdc . MustMarshalBinaryBare ( rp )
2020-08-21 19:42:46 +00:00
store . Set ( [ ] byte ( rp . CollateralType ) , bz )
2020-04-24 15:20:34 +00:00
}
2020-08-21 19:42:46 +00:00
// DeleteRewardPeriod deletes the reward period in the store for the input collateral type,
func ( k Keeper ) DeleteRewardPeriod ( ctx sdk . Context , collateralType string ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . RewardPeriodKeyPrefix )
2020-08-21 19:42:46 +00:00
store . Delete ( [ ] byte ( collateralType ) )
2020-04-24 15:20:34 +00:00
}
// IterateRewardPeriods iterates over all reward period objects in the store and preforms a callback function
func ( k Keeper ) IterateRewardPeriods ( ctx sdk . Context , cb func ( rp types . RewardPeriod ) ( stop bool ) ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . RewardPeriodKeyPrefix )
iterator := sdk . KVStorePrefixIterator ( store , [ ] byte { } )
defer iterator . Close ( )
for ; iterator . Valid ( ) ; iterator . Next ( ) {
var rp types . RewardPeriod
k . cdc . MustUnmarshalBinaryBare ( iterator . Value ( ) , & rp )
if cb ( rp ) {
break
}
}
}
// GetAllRewardPeriods returns all reward periods in the store
func ( k Keeper ) GetAllRewardPeriods ( ctx sdk . Context ) types . RewardPeriods {
rps := types . RewardPeriods { }
k . IterateRewardPeriods ( ctx , func ( rp types . RewardPeriod ) ( stop bool ) {
rps = append ( rps , rp )
return false
} )
return rps
}
2020-08-21 19:42:46 +00:00
// GetNextClaimPeriodID returns the highest claim period id in the store for the input collateral type
func ( k Keeper ) GetNextClaimPeriodID ( ctx sdk . Context , collateralType string ) uint64 {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . NextClaimPeriodIDPrefix )
2020-08-21 19:42:46 +00:00
bz := store . Get ( [ ] byte ( collateralType ) )
2020-07-29 17:06:14 +00:00
if bz == nil {
2020-08-21 19:42:46 +00:00
k . SetNextClaimPeriodID ( ctx , collateralType , 1 )
2020-07-29 17:06:14 +00:00
return uint64 ( 1 )
}
2020-04-24 15:20:34 +00:00
return types . BytesToUint64 ( bz )
}
2020-08-21 19:42:46 +00:00
// SetNextClaimPeriodID sets the highest claim period id in the store for the input collateral type
func ( k Keeper ) SetNextClaimPeriodID ( ctx sdk . Context , collateralType string , id uint64 ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . NextClaimPeriodIDPrefix )
2020-08-21 19:42:46 +00:00
store . Set ( [ ] byte ( collateralType ) , sdk . Uint64ToBigEndian ( id ) )
2020-04-24 15:20:34 +00:00
}
2020-08-21 19:42:46 +00:00
// IterateClaimPeriodIDKeysAndValues iterates over the claim period id (value) and collateral type (key) of each claim period id in the store and performs a callback function
func ( k Keeper ) IterateClaimPeriodIDKeysAndValues ( ctx sdk . Context , cb func ( collateralType string , id uint64 ) ( stop bool ) ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . NextClaimPeriodIDPrefix )
iterator := sdk . KVStorePrefixIterator ( store , [ ] byte { } )
defer iterator . Close ( )
for ; iterator . Valid ( ) ; iterator . Next ( ) {
id := types . BytesToUint64 ( iterator . Value ( ) )
2020-08-21 19:42:46 +00:00
collateralType := string ( iterator . Key ( ) )
if cb ( collateralType , id ) {
2020-04-24 15:20:34 +00:00
break
}
}
}
2020-08-21 19:42:46 +00:00
// GetAllClaimPeriodIDPairs returns all collateralType:nextClaimPeriodID pairs in the store
2020-04-24 15:20:34 +00:00
func ( k Keeper ) GetAllClaimPeriodIDPairs ( ctx sdk . Context ) types . GenesisClaimPeriodIDs {
ids := types . GenesisClaimPeriodIDs { }
2020-08-21 19:42:46 +00:00
k . IterateClaimPeriodIDKeysAndValues ( ctx , func ( collateralType string , id uint64 ) ( stop bool ) {
2020-04-24 15:20:34 +00:00
genID := types . GenesisClaimPeriodID {
2020-08-21 19:42:46 +00:00
CollateralType : collateralType ,
ID : id ,
2020-04-24 15:20:34 +00:00
}
ids = append ( ids , genID )
return false
} )
return ids
}
2020-08-21 19:42:46 +00:00
// GetClaimPeriod returns claim period in the store for the input ID and collateral type and a boolean for if it was found
func ( k Keeper ) GetClaimPeriod ( ctx sdk . Context , id uint64 , collateralType string ) ( types . ClaimPeriod , bool ) {
2020-04-24 15:20:34 +00:00
var cp types . ClaimPeriod
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimPeriodKeyPrefix )
2020-08-21 19:42:46 +00:00
bz := store . Get ( types . GetClaimPeriodPrefix ( collateralType , id ) )
2020-04-24 15:20:34 +00:00
if bz == nil {
return types . ClaimPeriod { } , false
}
k . cdc . MustUnmarshalBinaryBare ( bz , & cp )
return cp , true
}
2020-08-21 19:42:46 +00:00
// SetClaimPeriod sets the claim period in the store for the input ID and collateral type
2020-04-24 15:20:34 +00:00
func ( k Keeper ) SetClaimPeriod ( ctx sdk . Context , cp types . ClaimPeriod ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimPeriodKeyPrefix )
bz := k . cdc . MustMarshalBinaryBare ( cp )
2020-08-21 19:42:46 +00:00
store . Set ( types . GetClaimPeriodPrefix ( cp . CollateralType , cp . ID ) , bz )
2020-04-24 15:20:34 +00:00
}
2020-08-21 19:42:46 +00:00
// DeleteClaimPeriod deletes the claim period in the store for the input ID and collateral type
func ( k Keeper ) DeleteClaimPeriod ( ctx sdk . Context , id uint64 , collateralType string ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimPeriodKeyPrefix )
2020-08-21 19:42:46 +00:00
store . Delete ( types . GetClaimPeriodPrefix ( collateralType , id ) )
2020-04-24 15:20:34 +00:00
}
// IterateClaimPeriods iterates over all claim period objects in the store and preforms a callback function
func ( k Keeper ) IterateClaimPeriods ( ctx sdk . Context , cb func ( cp types . ClaimPeriod ) ( stop bool ) ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimPeriodKeyPrefix )
iterator := sdk . KVStorePrefixIterator ( store , [ ] byte { } )
defer iterator . Close ( )
for ; iterator . Valid ( ) ; iterator . Next ( ) {
var cp types . ClaimPeriod
k . cdc . MustUnmarshalBinaryBare ( iterator . Value ( ) , & cp )
if cb ( cp ) {
break
}
}
}
// GetAllClaimPeriods returns all ClaimPeriod objects in the store
func ( k Keeper ) GetAllClaimPeriods ( ctx sdk . Context ) types . ClaimPeriods {
cps := types . ClaimPeriods { }
k . IterateClaimPeriods ( ctx , func ( cp types . ClaimPeriod ) ( stop bool ) {
cps = append ( cps , cp )
return false
} )
return cps
}
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
func ( k Keeper ) GetClaim ( ctx sdk . Context , addr sdk . AccAddress , collateralType string , id uint64 ) ( types . Claim , bool ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimKeyPrefix )
2020-08-21 19:42:46 +00:00
bz := store . Get ( types . GetClaimPrefix ( addr , collateralType , id ) )
2020-04-24 15:20:34 +00:00
if bz == nil {
return types . Claim { } , false
}
var c types . Claim
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
2020-04-24 15:20:34 +00:00
func ( k Keeper ) SetClaim ( ctx sdk . Context , c types . Claim ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimKeyPrefix )
bz := k . cdc . MustMarshalBinaryBare ( c )
2020-08-21 19:42:46 +00:00
store . Set ( types . GetClaimPrefix ( c . Owner , c . CollateralType , c . ClaimPeriodID ) , 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
func ( k Keeper ) DeleteClaim ( ctx sdk . Context , owner sdk . AccAddress , collateralType string , id uint64 ) {
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimKeyPrefix )
2020-08-21 19:42:46 +00:00
store . Delete ( types . GetClaimPrefix ( owner , collateralType , id ) )
2020-04-24 15:20:34 +00:00
}
// IterateClaims iterates over all claim objects in the store and preforms a callback function
func ( k Keeper ) IterateClaims ( ctx sdk . Context , cb func ( c types . Claim ) ( stop bool ) ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . ClaimKeyPrefix )
iterator := sdk . KVStorePrefixIterator ( store , [ ] byte { } )
defer iterator . Close ( )
for ; iterator . Valid ( ) ; iterator . Next ( ) {
var c types . Claim
k . cdc . MustUnmarshalBinaryBare ( iterator . Value ( ) , & c )
if cb ( c ) {
break
}
}
}
// GetAllClaims returns all Claim objects in the store
func ( k Keeper ) GetAllClaims ( ctx sdk . Context ) types . Claims {
cs := types . Claims { }
k . IterateClaims ( ctx , func ( c types . Claim ) ( stop bool ) {
cs = append ( cs , c )
return false
} )
return cs
}
// GetPreviousBlockTime get the blocktime for the previous block
func ( k Keeper ) GetPreviousBlockTime ( ctx sdk . Context ) ( blockTime time . Time , found bool ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . PreviousBlockTimeKey )
b := store . Get ( [ ] byte { } )
if b == nil {
return time . Time { } , false
}
k . cdc . MustUnmarshalBinaryBare ( b , & blockTime )
return blockTime , true
}
// SetPreviousBlockTime set the time of the previous block
func ( k Keeper ) SetPreviousBlockTime ( ctx sdk . Context , blockTime time . Time ) {
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . PreviousBlockTimeKey )
store . Set ( [ ] byte { } , k . cdc . MustMarshalBinaryBare ( blockTime ) )
}