2020-09-21 21:08:43 +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-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// Keeper keeper for the hard module
|
2020-09-21 21:08:43 +00:00
|
|
|
type Keeper struct {
|
2020-10-30 09:59:47 +00:00
|
|
|
key sdk.StoreKey
|
|
|
|
cdc *codec.Codec
|
|
|
|
paramSubspace subspace.Subspace
|
|
|
|
accountKeeper types.AccountKeeper
|
|
|
|
supplyKeeper types.SupplyKeeper
|
|
|
|
stakingKeeper types.StakingKeeper
|
|
|
|
pricefeedKeeper types.PricefeedKeeper
|
2020-12-15 17:38:14 +00:00
|
|
|
auctionKeeper types.AuctionKeeper
|
2021-01-21 13:52:09 +00:00
|
|
|
hooks types.HARDHooks
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a new keeper
|
2020-10-30 09:59:47 +00:00
|
|
|
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramstore subspace.Subspace,
|
|
|
|
ak types.AccountKeeper, sk types.SupplyKeeper, stk types.StakingKeeper,
|
2020-12-15 17:38:14 +00:00
|
|
|
pfk types.PricefeedKeeper, auk types.AuctionKeeper) Keeper {
|
2020-09-21 21:08:43 +00:00
|
|
|
if !paramstore.HasKeyTable() {
|
|
|
|
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
2020-10-30 09:59:47 +00:00
|
|
|
key: key,
|
|
|
|
cdc: cdc,
|
|
|
|
paramSubspace: paramstore,
|
|
|
|
accountKeeper: ak,
|
|
|
|
supplyKeeper: sk,
|
|
|
|
stakingKeeper: stk,
|
|
|
|
pricefeedKeeper: pfk,
|
2020-12-15 17:38:14 +00:00
|
|
|
auctionKeeper: auk,
|
2021-01-21 13:52:09 +00:00
|
|
|
hooks: nil,
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
// SetHooks sets the cdp keeper hooks
|
|
|
|
func (k *Keeper) SetHooks(hooks types.HARDHooks) *Keeper {
|
|
|
|
if k.hooks != nil {
|
|
|
|
panic("cannot set validator hooks twice")
|
|
|
|
}
|
|
|
|
k.hooks = hooks
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
// GetDeposit returns a deposit from the store for a particular depositor address, deposit denom
|
2020-12-18 16:05:21 +00:00
|
|
|
func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress) (types.Deposit, bool) {
|
2020-09-21 21:08:43 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
2020-12-18 16:05:21 +00:00
|
|
|
bz := store.Get(depositor.Bytes())
|
2020-09-21 21:08:43 +00:00
|
|
|
if bz == nil {
|
|
|
|
return types.Deposit{}, false
|
|
|
|
}
|
|
|
|
var deposit types.Deposit
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &deposit)
|
|
|
|
return deposit, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeposit sets the input deposit in the store, prefixed by the deposit type, deposit denom, and depositor address, in that order
|
|
|
|
func (k Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(deposit)
|
2020-12-18 16:05:21 +00:00
|
|
|
store.Set(deposit.Depositor.Bytes(), bz)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDeposit deletes a deposit from the store
|
|
|
|
func (k Keeper) DeleteDeposit(ctx sdk.Context, deposit types.Deposit) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
2020-12-18 16:05:21 +00:00
|
|
|
store.Delete(deposit.Depositor.Bytes())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateDeposits iterates over all deposit objects in the store and performs a callback function
|
|
|
|
func (k Keeper) IterateDeposits(ctx sdk.Context, cb func(deposit types.Deposit) (stop bool)) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
var deposit types.Deposit
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &deposit)
|
|
|
|
if cb(deposit) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 09:59:47 +00:00
|
|
|
// GetDepositsByUser gets all deposits for an individual user
|
|
|
|
func (k Keeper) GetDepositsByUser(ctx sdk.Context, user sdk.AccAddress) []types.Deposit {
|
|
|
|
var deposits []types.Deposit
|
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
|
|
|
if deposit.Depositor.Equals(user) {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return deposits
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
// BondDenom returns the bond denom from the staking keeper
|
|
|
|
func (k Keeper) BondDenom(ctx sdk.Context) string {
|
|
|
|
return k.stakingKeeper.BondDenom(ctx)
|
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
// GetBorrow returns a Borrow from the store for a particular borrower address and borrow denom
|
|
|
|
func (k Keeper) GetBorrow(ctx sdk.Context, borrower sdk.AccAddress) (types.Borrow, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowsKeyPrefix)
|
|
|
|
bz := store.Get(borrower)
|
|
|
|
if bz == nil {
|
|
|
|
return types.Borrow{}, false
|
|
|
|
}
|
|
|
|
var borrow types.Borrow
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &borrow)
|
|
|
|
return borrow, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBorrow sets the input borrow in the store, prefixed by the borrower address and borrow denom
|
|
|
|
func (k Keeper) SetBorrow(ctx sdk.Context, borrow types.Borrow) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowsKeyPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(borrow)
|
|
|
|
store.Set(borrow.Borrower, bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBorrow deletes a borrow from the store
|
|
|
|
func (k Keeper) DeleteBorrow(ctx sdk.Context, borrow types.Borrow) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowsKeyPrefix)
|
|
|
|
store.Delete(borrow.Borrower)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateBorrows iterates over all borrow objects in the store and performs a callback function
|
|
|
|
func (k Keeper) IterateBorrows(ctx sdk.Context, cb func(borrow types.Borrow) (stop bool)) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowsKeyPrefix)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
var borrow types.Borrow
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &borrow)
|
|
|
|
if cb(borrow) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 15:50:54 +00:00
|
|
|
|
|
|
|
// SetBorrowedCoins sets the total amount of coins currently borrowed in the store
|
|
|
|
func (k Keeper) SetBorrowedCoins(ctx sdk.Context, borrowedCoins sdk.Coins) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowedCoinsPrefix)
|
2020-12-07 21:51:35 +00:00
|
|
|
if borrowedCoins.Empty() {
|
|
|
|
store.Set([]byte{}, []byte{})
|
|
|
|
} else {
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(borrowedCoins)
|
|
|
|
store.Set([]byte{}, bz)
|
|
|
|
}
|
2020-11-12 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBorrowedCoins returns an sdk.Coins object from the store representing all currently borrowed coins
|
|
|
|
func (k Keeper) GetBorrowedCoins(ctx sdk.Context) (sdk.Coins, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowedCoinsPrefix)
|
|
|
|
bz := store.Get([]byte{})
|
|
|
|
if bz == nil {
|
|
|
|
return sdk.Coins{}, false
|
|
|
|
}
|
|
|
|
var borrowedCoins sdk.Coins
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &borrowedCoins)
|
|
|
|
return borrowedCoins, true
|
|
|
|
}
|
2020-11-13 15:51:52 +00:00
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// SetSuppliedCoins sets the total amount of coins currently supplied in the store
|
|
|
|
func (k Keeper) SetSuppliedCoins(ctx sdk.Context, suppliedCoins sdk.Coins) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.SuppliedCoinsPrefix)
|
|
|
|
if suppliedCoins.Empty() {
|
|
|
|
store.Set([]byte{}, []byte{})
|
|
|
|
} else {
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(suppliedCoins)
|
|
|
|
store.Set([]byte{}, bz)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSuppliedCoins returns an sdk.Coins object from the store representing all currently supplied coins
|
|
|
|
func (k Keeper) GetSuppliedCoins(ctx sdk.Context) (sdk.Coins, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.SuppliedCoinsPrefix)
|
|
|
|
bz := store.Get([]byte{})
|
|
|
|
if bz == nil {
|
|
|
|
return sdk.Coins{}, false
|
|
|
|
}
|
|
|
|
var suppliedCoins sdk.Coins
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &suppliedCoins)
|
|
|
|
return suppliedCoins, true
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
// GetMoneyMarket returns a money market from the store for a denom
|
|
|
|
func (k Keeper) GetMoneyMarket(ctx sdk.Context, denom string) (types.MoneyMarket, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.MoneyMarketsPrefix)
|
2020-11-13 15:51:52 +00:00
|
|
|
bz := store.Get([]byte(denom))
|
|
|
|
if bz == nil {
|
2020-12-03 21:50:35 +00:00
|
|
|
return types.MoneyMarket{}, false
|
2020-11-13 15:51:52 +00:00
|
|
|
}
|
2020-12-03 21:50:35 +00:00
|
|
|
var moneyMarket types.MoneyMarket
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &moneyMarket)
|
|
|
|
return moneyMarket, true
|
2020-11-13 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
// SetMoneyMarket sets a money market in the store for a denom
|
|
|
|
func (k Keeper) SetMoneyMarket(ctx sdk.Context, denom string, moneyMarket types.MoneyMarket) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.MoneyMarketsPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(moneyMarket)
|
2020-11-13 15:51:52 +00:00
|
|
|
store.Set([]byte(denom), bz)
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
// DeleteMoneyMarket deletes a money market from the store
|
|
|
|
func (k Keeper) DeleteMoneyMarket(ctx sdk.Context, denom string) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.MoneyMarketsPrefix)
|
2020-11-13 15:51:52 +00:00
|
|
|
store.Delete([]byte(denom))
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
// IterateMoneyMarkets iterates over all money markets objects in the store and performs a callback function
|
|
|
|
// that returns both the money market and the key (denom) it's stored under
|
|
|
|
func (k Keeper) IterateMoneyMarkets(ctx sdk.Context, cb func(denom string, moneyMarket types.MoneyMarket) (stop bool)) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.MoneyMarketsPrefix)
|
2020-11-13 15:51:52 +00:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, []byte{})
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2020-12-03 21:50:35 +00:00
|
|
|
var moneyMarket types.MoneyMarket
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &moneyMarket)
|
|
|
|
if cb(string(iterator.Key()), moneyMarket) {
|
2020-11-13 15:51:52 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-03 21:50:35 +00:00
|
|
|
|
2021-02-10 17:53:53 +00:00
|
|
|
// GetAllMoneyMarkets returns all money markets from the store
|
|
|
|
func (k Keeper) GetAllMoneyMarkets(ctx sdk.Context) (moneyMarkets types.MoneyMarkets) {
|
|
|
|
k.IterateMoneyMarkets(ctx, func(denom string, moneyMarket types.MoneyMarket) bool {
|
|
|
|
moneyMarkets = append(moneyMarkets, moneyMarket)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
// GetPreviousAccrualTime returns the last time an individual market accrued interest
|
|
|
|
func (k Keeper) GetPreviousAccrualTime(ctx sdk.Context, denom string) (time.Time, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousAccrualTimePrefix)
|
|
|
|
bz := store.Get([]byte(denom))
|
|
|
|
if bz == nil {
|
|
|
|
return time.Time{}, false
|
|
|
|
}
|
|
|
|
var previousAccrualTime time.Time
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &previousAccrualTime)
|
|
|
|
return previousAccrualTime, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPreviousAccrualTime sets the most recent accrual time for a particular market
|
|
|
|
func (k Keeper) SetPreviousAccrualTime(ctx sdk.Context, denom string, previousAccrualTime time.Time) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousAccrualTimePrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(previousAccrualTime)
|
|
|
|
store.Set([]byte(denom), bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalReserves returns the total reserves for an individual market
|
2021-01-23 05:17:40 +00:00
|
|
|
func (k Keeper) GetTotalReserves(ctx sdk.Context) (sdk.Coins, bool) {
|
2020-12-03 21:50:35 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.TotalReservesPrefix)
|
2021-01-23 05:17:40 +00:00
|
|
|
bz := store.Get([]byte{})
|
2020-12-03 21:50:35 +00:00
|
|
|
if bz == nil {
|
2021-01-23 05:17:40 +00:00
|
|
|
return sdk.Coins{}, false
|
2020-12-03 21:50:35 +00:00
|
|
|
}
|
2021-01-23 05:17:40 +00:00
|
|
|
var totalReserves sdk.Coins
|
2020-12-03 21:50:35 +00:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &totalReserves)
|
|
|
|
return totalReserves, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTotalReserves sets the total reserves for an individual market
|
2021-01-23 05:17:40 +00:00
|
|
|
func (k Keeper) SetTotalReserves(ctx sdk.Context, coins sdk.Coins) {
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.TotalReservesPrefix)
|
2021-01-23 05:17:40 +00:00
|
|
|
if coins.Empty() {
|
|
|
|
store.Set([]byte{}, []byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(coins)
|
|
|
|
store.Set([]byte{}, bz)
|
2020-12-03 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// GetBorrowInterestFactor returns the current borrow interest factor for an individual market
|
|
|
|
func (k Keeper) GetBorrowInterestFactor(ctx sdk.Context, denom string) (sdk.Dec, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowInterestFactorPrefix)
|
|
|
|
bz := store.Get([]byte(denom))
|
|
|
|
if bz == nil {
|
|
|
|
return sdk.ZeroDec(), false
|
|
|
|
}
|
|
|
|
var borrowInterestFactor sdk.Dec
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &borrowInterestFactor)
|
|
|
|
return borrowInterestFactor, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBorrowInterestFactor sets the current borrow interest factor for an individual market
|
|
|
|
func (k Keeper) SetBorrowInterestFactor(ctx sdk.Context, denom string, borrowInterestFactor sdk.Dec) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowInterestFactorPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(borrowInterestFactor)
|
|
|
|
store.Set([]byte(denom), bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSupplyInterestFactor returns the current supply interest factor for an individual market
|
|
|
|
func (k Keeper) GetSupplyInterestFactor(ctx sdk.Context, denom string) (sdk.Dec, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.SupplyInterestFactorPrefix)
|
2020-12-03 21:50:35 +00:00
|
|
|
bz := store.Get([]byte(denom))
|
|
|
|
if bz == nil {
|
|
|
|
return sdk.ZeroDec(), false
|
|
|
|
}
|
2021-01-07 10:23:05 +00:00
|
|
|
var supplyInterestFactor sdk.Dec
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &supplyInterestFactor)
|
|
|
|
return supplyInterestFactor, true
|
2020-12-03 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// SetSupplyInterestFactor sets the current supply interest factor for an individual market
|
|
|
|
func (k Keeper) SetSupplyInterestFactor(ctx sdk.Context, denom string, supplyInterestFactor sdk.Dec) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.SupplyInterestFactorPrefix)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(supplyInterestFactor)
|
2020-12-03 21:50:35 +00:00
|
|
|
store.Set([]byte(denom), bz)
|
|
|
|
}
|