fix: emit relevent events for incentive

This commit is contained in:
Kevin Davis 2020-05-07 13:46:40 -04:00
parent 0c49f1b089
commit a295b793a9
5 changed files with 84 additions and 34 deletions

View File

@ -1,9 +1,10 @@
package incentive
// nolint // nolint
// autogenerated code using github.com/rigelrozanski/multitool // autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories: // aliases generated for the following subdirectories:
// ALIASGEN: github.com/kava-labs/kava/x/incentive/keeper // ALIASGEN: github.com/kava-labs/kava/x/incentive/keeper
// ALIASGEN: github.com/kava-labs/kava/x/incentive/types // ALIASGEN: github.com/kava-labs/kava/x/incentive/types
package incentive
import ( import (
"github.com/kava-labs/kava/x/incentive/keeper" "github.com/kava-labs/kava/x/incentive/keeper"
@ -11,18 +12,24 @@ import (
) )
const ( const (
EventTypeClaim = types.EventTypeClaim EventTypeClaim = types.EventTypeClaim
AttributeValueCategory = types.AttributeValueCategory EventTypeRewardPeriod = types.EventTypeRewardPeriod
AttributeKeySender = types.AttributeKeySender EventTypeClaimPeriod = types.EventTypeClaimPeriod
ModuleName = types.ModuleName EventTypeClaimPeriodExpiry = types.EventTypeClaimPeriodExpiry
StoreKey = types.StoreKey AttributeValueCategory = types.AttributeValueCategory
RouterKey = types.RouterKey AttributeKeyClaimedBy = types.AttributeKeyClaimedBy
DefaultParamspace = types.DefaultParamspace AttributeKeyClaimAmount = types.AttributeKeyClaimAmount
QuerierRoute = types.QuerierRoute AttributeKeyRewardPeriod = types.AttributeKeyRewardPeriod
QueryGetClaims = types.QueryGetClaims AttributeKeyClaimPeriod = types.AttributeKeyClaimPeriod
RestClaimOwner = types.RestClaimOwner ModuleName = types.ModuleName
RestClaimDenom = types.RestClaimDenom StoreKey = types.StoreKey
QueryGetParams = types.QueryGetParams RouterKey = types.RouterKey
DefaultParamspace = types.DefaultParamspace
QuerierRoute = types.QuerierRoute
QueryGetClaims = types.QueryGetClaims
RestClaimOwner = types.RestClaimOwner
RestClaimDenom = types.RestClaimDenom
QueryGetParams = types.QueryGetParams
) )
var ( var (
@ -46,6 +53,7 @@ var (
NewRewardPeriod = types.NewRewardPeriod NewRewardPeriod = types.NewRewardPeriod
NewClaimPeriod = types.NewClaimPeriod NewClaimPeriod = types.NewClaimPeriod
NewClaim = types.NewClaim NewClaim = types.NewClaim
NewRewardPeriodFromReward = types.NewRewardPeriodFromReward
// variable aliases // variable aliases
ModuleCdc = types.ModuleCdc ModuleCdc = types.ModuleCdc

View File

@ -1,8 +1,6 @@
package keeper package keeper
import ( import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
@ -30,10 +28,12 @@ func (k Keeper) PayoutClaim(ctx sdk.Context, addr sdk.AccAddress, denom string,
} }
k.DeleteClaim(ctx, addr, denom, id) k.DeleteClaim(ctx, addr, denom, id)
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeClaim, types.EventTypeClaim,
sdk.NewAttribute(types.AttributeKeySender, fmt.Sprintf("%s", addr)), sdk.NewAttribute(types.AttributeKeyClaimedBy, addr.String()),
sdk.NewAttribute(types.AttributeKeyClaimAmount, claim.Reward.String()),
), ),
) )
return nil return nil
@ -104,6 +104,13 @@ func (k Keeper) DeleteExpiredClaimsAndClaimPeriods(ctx sdk.Context) {
return false return false
}) })
k.DeleteClaimPeriod(ctx, cp.ID, cp.Denom) k.DeleteClaimPeriod(ctx, cp.ID, cp.Denom)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeClaimPeriodExpiry,
sdk.NewAttribute(types.AttributeKeyClaimPeriod, cp.String()),
),
)
return false return false
}) })
} }

View File

@ -20,18 +20,15 @@ func (k Keeper) HandleRewardPeriodExpiry(ctx sdk.Context, rp types.RewardPeriod)
// CreateNewRewardPeriod creates a new reward period from the input reward // CreateNewRewardPeriod creates a new reward period from the input reward
func (k Keeper) CreateNewRewardPeriod(ctx sdk.Context, reward types.Reward) { func (k Keeper) CreateNewRewardPeriod(ctx sdk.Context, reward types.Reward) {
// reward periods store the amount of rewards paid PER SECOND rp := types.NewRewardPeriodFromReward(reward, ctx.BlockTime())
rewardsPerSecond := sdk.NewDecFromInt(reward.AvailableRewards.Amount).Quo(sdk.NewDecFromInt(sdk.NewInt(int64(reward.Duration.Seconds())))).TruncateInt()
rewardCoinPerSecond := sdk.NewCoin(reward.AvailableRewards.Denom, rewardsPerSecond)
rp := types.RewardPeriod{
Denom: reward.Denom,
Start: ctx.BlockTime(),
End: ctx.BlockTime().Add(reward.Duration),
Reward: rewardCoinPerSecond,
ClaimEnd: ctx.BlockTime().Add(reward.Duration).Add(reward.ClaimDuration),
ClaimTimeLock: reward.TimeLock,
}
k.SetRewardPeriod(ctx, rp) k.SetRewardPeriod(ctx, rp)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRewardPeriod,
sdk.NewAttribute(types.AttributeKeyRewardPeriod, rp.String()),
),
)
} }
// CreateAndDeleteRewardPeriods creates reward periods for active rewards that don't already have a reward period and deletes reward periods for inactive rewards that currently have a reward period // CreateAndDeleteRewardPeriods creates reward periods for active rewards that don't already have a reward period and deletes reward periods for inactive rewards that currently have a reward period
@ -95,6 +92,12 @@ func (k Keeper) ApplyRewardsToCdps(ctx sdk.Context) {
func (k Keeper) CreateUniqueClaimPeriod(ctx sdk.Context, denom string, end time.Time, timeLock time.Duration) { func (k Keeper) CreateUniqueClaimPeriod(ctx sdk.Context, denom string, end time.Time, timeLock time.Duration) {
id := k.GetNextClaimPeriodID(ctx, denom) id := k.GetNextClaimPeriodID(ctx, denom)
claimPeriod := types.NewClaimPeriod(denom, id, end, timeLock) claimPeriod := types.NewClaimPeriod(denom, id, end, timeLock)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeClaimPeriod,
sdk.NewAttribute(types.AttributeKeyClaimPeriod, claimPeriod.String()),
),
)
k.SetClaimPeriod(ctx, claimPeriod) k.SetClaimPeriod(ctx, claimPeriod)
k.SetNextClaimPeriodID(ctx, denom, id+1) k.SetNextClaimPeriodID(ctx, denom, id+1)
} }

View File

@ -1,8 +1,15 @@
package types package types
// Events emitted by the incentive module
const ( const (
EventTypeClaim = "claim_reward" EventTypeClaim = "claim_reward"
EventTypeRewardPeriod = "new_reward_period"
EventTypeClaimPeriod = "new_claim_period"
EventTypeClaimPeriodExpiry = "claim_period_expiry"
AttributeValueCategory = ModuleName AttributeValueCategory = ModuleName
AttributeKeySender = "sender" AttributeKeyClaimedBy = "claimed_by"
AttributeKeyClaimAmount = "claim_amount"
AttributeKeyRewardPeriod = "reward_period"
AttributeKeyClaimPeriod = "claim_period"
) )

View File

@ -25,8 +25,8 @@ func (rp RewardPeriod) String() string {
End: %s, End: %s,
Reward: %s, Reward: %s,
Claim End: %s, Claim End: %s,
Claim Time Lock: %s`, Claim Time Lock: %s
rp.Denom, rp.Start, rp.End, rp.Reward, rp.ClaimEnd, rp.ClaimTimeLock) `, rp.Denom, rp.Start, rp.End, rp.Reward, rp.ClaimEnd, rp.ClaimTimeLock)
} }
// NewRewardPeriod returns a new RewardPeriod // NewRewardPeriod returns a new RewardPeriod
@ -52,6 +52,16 @@ type ClaimPeriod struct {
TimeLock time.Duration `json:"time_lock" yaml:"time_lock"` TimeLock time.Duration `json:"time_lock" yaml:"time_lock"`
} }
// String implements fmt.Stringer
func (cp ClaimPeriod) String() string {
return fmt.Sprintf(`Claim Period:
Denom: %s,
ID: %d,
End: %s,
Claim Time Lock: %s
`, cp.Denom, cp.ID, cp.End, cp.TimeLock)
}
// NewClaimPeriod returns a new ClaimPeriod // NewClaimPeriod returns a new ClaimPeriod
func NewClaimPeriod(denom string, id uint64, end time.Time, timeLock time.Duration) ClaimPeriod { func NewClaimPeriod(denom string, id uint64, end time.Time, timeLock time.Duration) ClaimPeriod {
return ClaimPeriod{ return ClaimPeriod{
@ -89,9 +99,24 @@ func (c Claim) String() string {
Owner: %s, Owner: %s,
Denom: %s, Denom: %s,
Reward: %s, Reward: %s,
Claim Period ID: %d,`, Claim Period ID: %d,
c.Owner, c.Denom, c.Reward, c.ClaimPeriodID) `, c.Owner, c.Denom, c.Reward, c.ClaimPeriodID)
} }
// Claims array of Claim // Claims array of Claim
type Claims []Claim type Claims []Claim
// NewRewardPeriodFromReward returns a new reward period from the input reward and block time
func NewRewardPeriodFromReward(reward Reward, blockTime time.Time) RewardPeriod {
// note: reward periods store the amount of rewards paid PER SECOND
rewardsPerSecond := sdk.NewDecFromInt(reward.AvailableRewards.Amount).Quo(sdk.NewDecFromInt(sdk.NewInt(int64(reward.Duration.Seconds())))).TruncateInt()
rewardCoinPerSecond := sdk.NewCoin(reward.AvailableRewards.Denom, rewardsPerSecond)
return RewardPeriod{
Denom: reward.Denom,
Start: blockTime,
End: blockTime.Add(reward.Duration),
Reward: rewardCoinPerSecond,
ClaimEnd: blockTime.Add(reward.Duration).Add(reward.ClaimDuration),
ClaimTimeLock: reward.TimeLock,
}
}