0g-chain/x/incentive/keeper/store/accrual_time.go
Derrick Lee cf009647e6
Use different accumulator for earn (#1395)
* Add accumulators

* Move accumulator back to keeper package

* Add earn specific accumulators

* Move store methods to sub-package

* Move earn accumulator

* Rename accumulator files

* Add store doc comment

* Add earn accumulator tests, panic if accumulator not used with earn claim type

* Update earn accumulator tests to use new methods

* Add staking test for earn accumulator

* Add test for accumulator proportional rewards

* Remove old copy of GetProportionalRewardsPerSecond

* Add test for basic accumulator

* Fix AddIncentiveMultiRewardPeriod replacement

* Deduplicate base earn reward accumulator

* Check errors in tests

* Validate RewardPeriods in Params.Validate()

* Use adapter to fetch earn total shares
2022-11-29 14:23:33 -08:00

96 lines
2.8 KiB
Go

package store
import (
"time"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/incentive/types"
)
// GetRewardAccrualTime fetches the last time rewards were accrued for the
// specified ClaimType and sourceID.
func (k IncentiveStore) GetRewardAccrualTime(
ctx sdk.Context,
claimType types.ClaimType,
sourceID string,
) (time.Time, bool) {
store := prefix.NewStore(ctx.KVStore(k.key), types.GetPreviousRewardAccrualTimeKeyPrefix(claimType))
b := store.Get(types.GetKeyFromSourceID(sourceID))
if b == nil {
return time.Time{}, false
}
var accrualTime types.AccrualTime
k.cdc.MustUnmarshal(b, &accrualTime)
return accrualTime.PreviousAccumulationTime, true
}
// SetRewardAccrualTime stores the last time rewards were accrued for the
// specified ClaimType and sourceID.
func (k IncentiveStore) SetRewardAccrualTime(
ctx sdk.Context,
claimType types.ClaimType,
sourceID string,
blockTime time.Time,
) {
store := prefix.NewStore(ctx.KVStore(k.key), types.GetPreviousRewardAccrualTimeKeyPrefix(claimType))
at := types.NewAccrualTime(claimType, sourceID, blockTime)
bz := k.cdc.MustMarshal(&at)
store.Set(types.GetKeyFromSourceID(sourceID), bz)
}
// IterateRewardAccrualTimesByClaimType iterates over all reward accrual times of a given
// claimType and performs a callback function.
func (k IncentiveStore) IterateRewardAccrualTimesByClaimType(
ctx sdk.Context,
claimType types.ClaimType,
cb func(string, time.Time) (stop bool),
) {
store := prefix.NewStore(ctx.KVStore(k.key), types.GetPreviousRewardAccrualTimeKeyPrefix(claimType))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var accrualTime types.AccrualTime
k.cdc.MustUnmarshal(iterator.Value(), &accrualTime)
if cb(accrualTime.CollateralType, accrualTime.PreviousAccumulationTime) {
break
}
}
}
// IterateRewardAccrualTimes iterates over all reward accrual times of any
// claimType and performs a callback function.
func (k IncentiveStore) IterateRewardAccrualTimes(
ctx sdk.Context,
cb func(types.AccrualTime) (stop bool),
) {
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousRewardAccrualTimeKeyPrefix)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var accrualTime types.AccrualTime
k.cdc.MustUnmarshal(iterator.Value(), &accrualTime)
if cb(accrualTime) {
break
}
}
}
// GetAllRewardAccrualTimes returns all reward accrual times of any claimType.
func (k IncentiveStore) GetAllRewardAccrualTimes(ctx sdk.Context) types.AccrualTimes {
var ats types.AccrualTimes
k.IterateRewardAccrualTimes(
ctx,
func(accrualTime types.AccrualTime) bool {
ats = append(ats, accrualTime)
return false
},
)
return ats
}