2020-08-17 17:09:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-08-21 22:56:20 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-17 17:09:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-08-21 22:56:20 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2020-08-17 17:09:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2020-08-17 17:09:02 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/issuance/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper keeper for the issuance module
|
|
|
|
type Keeper struct {
|
|
|
|
key sdk.StoreKey
|
2022-01-08 00:39:27 +00:00
|
|
|
cdc codec.Codec
|
|
|
|
paramSubspace paramtypes.Subspace
|
2020-08-17 17:09:02 +00:00
|
|
|
accountKeeper types.AccountKeeper
|
2022-01-08 00:39:27 +00:00
|
|
|
bankKeeper types.BankKeeper
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper returns a new keeper
|
2022-01-08 00:39:27 +00:00
|
|
|
func NewKeeper(cdc codec.Codec, key sdk.StoreKey, paramstore paramtypes.Subspace, ak types.AccountKeeper, bk types.BankKeeper) Keeper {
|
2020-08-17 17:09:02 +00:00
|
|
|
if !paramstore.HasKeyTable() {
|
|
|
|
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
|
|
|
key: key,
|
|
|
|
cdc: cdc,
|
|
|
|
paramSubspace: paramstore,
|
|
|
|
accountKeeper: ak,
|
2022-01-08 00:39:27 +00:00
|
|
|
bankKeeper: bk,
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 22:56:20 +00:00
|
|
|
|
|
|
|
// GetAssetSupply gets an asset's current supply from the store.
|
|
|
|
func (k Keeper) GetAssetSupply(ctx sdk.Context, denom string) (types.AssetSupply, bool) {
|
|
|
|
var assetSupply types.AssetSupply
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
|
|
|
bz := store.Get([]byte(denom))
|
|
|
|
if bz == nil {
|
|
|
|
return types.AssetSupply{}, false
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
k.cdc.MustUnmarshal(bz, &assetSupply)
|
2020-08-21 22:56:20 +00:00
|
|
|
return assetSupply, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAssetSupply updates an asset's supply
|
|
|
|
func (k Keeper) SetAssetSupply(ctx sdk.Context, supply types.AssetSupply, denom string) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
2022-01-08 00:39:27 +00:00
|
|
|
store.Set([]byte(denom), k.cdc.MustMarshal(&supply))
|
2020-08-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAssetSupplies provides an iterator over all stored AssetSupplies.
|
|
|
|
func (k Keeper) IterateAssetSupplies(ctx sdk.Context, cb func(supply types.AssetSupply) (stop bool)) {
|
|
|
|
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
|
|
|
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
var supply types.AssetSupply
|
2022-01-08 00:39:27 +00:00
|
|
|
k.cdc.MustUnmarshal(iterator.Value(), &supply)
|
2020-08-21 22:56:20 +00:00
|
|
|
if cb(supply) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllAssetSupplies returns all asset supplies from the store
|
2022-01-08 00:39:27 +00:00
|
|
|
func (k Keeper) GetAllAssetSupplies(ctx sdk.Context) (supplies []types.AssetSupply) {
|
2020-08-21 22:56:20 +00:00
|
|
|
k.IterateAssetSupplies(ctx, func(supply types.AssetSupply) bool {
|
|
|
|
supplies = append(supplies, supply)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2022-01-08 00:39:27 +00:00
|
|
|
b := store.Get(types.PreviousBlockTimeKey)
|
2020-08-21 22:56:20 +00:00
|
|
|
if b == nil {
|
|
|
|
return time.Time{}, false
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
if err := blockTime.UnmarshalBinary(b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-21 22:56:20 +00:00
|
|
|
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)
|
2022-01-08 00:39:27 +00:00
|
|
|
b, err := blockTime.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
store.Set(types.PreviousBlockTimeKey, b)
|
2020-08-21 22:56:20 +00:00
|
|
|
}
|