mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
c63ecf908a
* Add 'InterestFactor' to CDP type (#734) * update cdp type to include interest factor * fix build * Add cdp accumulator methods (#735) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * Add sync cdp interest method (#737) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: round time difference properly * update cdp genesis state and migrations (#738) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * update cdp genesis state and migrations * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: simplify add/remove/update collateral index * update genesis state to include total principal amounts * update migration * Delete kava-4-cdp-state-block-500000.json * Add cdp liquidations by external keeper (#750) * feat: split liquidations between external keepers and automated begin blocker * address review comments * USDX incentive accumulators (#752) * feat: split liquidations between external keepers and automated begin blocker * wip: refactor usdx minting incentives to use accumulators/hooks * wip: refactor usdx minting claim object * feat: use accumulators/hooks for usdx minting rewards * fix: get tests passing * fix: don't create claim objects unless that cdp type is eligable for rewards * add begin blocker * update client * cleanup comments/tests * update querier * address review comments * fix: check for division by zero * address review comments * run hook before interest is synced * Remove savings rate (#764) * remove savings rate * remove savings rate from debt param * update migrations * address review comments * Add usdx incentives calculation test (#765) * add usdx incentive calculation test * update reward calculation * add allowable error to test criteria * Update x/incentive/keeper/rewards_test.go Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * fix: remove old fields from test genesis state Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
139 lines
4.7 KiB
Go
139 lines
4.7 KiB
Go
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"
|
|
|
|
"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,
|
|
}
|
|
}
|
|
|
|
// 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) (types.USDXMintingClaim, bool) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
|
bz := store.Get(addr)
|
|
if bz == nil {
|
|
return types.USDXMintingClaim{}, false
|
|
}
|
|
var c types.USDXMintingClaim
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &c)
|
|
return c, true
|
|
}
|
|
|
|
// SetClaim sets the claim in the store corresponding to the input address, collateral type, and id
|
|
func (k Keeper) SetClaim(ctx sdk.Context, c types.USDXMintingClaim) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
|
bz := k.cdc.MustMarshalBinaryBare(c)
|
|
store.Set(c.Owner, bz)
|
|
|
|
}
|
|
|
|
// 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) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimKeyPrefix)
|
|
store.Delete(owner)
|
|
}
|
|
|
|
// 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.USDXMintingClaim) (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.USDXMintingClaim
|
|
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.USDXMintingClaims {
|
|
cs := types.USDXMintingClaims{}
|
|
k.IterateClaims(ctx, func(c types.USDXMintingClaim) (stop bool) {
|
|
cs = append(cs, c)
|
|
return false
|
|
})
|
|
return cs
|
|
}
|
|
|
|
// 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 {
|
|
return time.Time{}, false
|
|
}
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &blockTime)
|
|
return blockTime, true
|
|
}
|
|
|
|
// 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))
|
|
}
|