From a295b793a958b77a4fbec98c368fc41fa959802c Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 7 May 2020 13:46:40 -0400 Subject: [PATCH] fix: emit relevent events for incentive --- x/incentive/alias.go | 34 +++++++++++++++++++++------------- x/incentive/keeper/payout.go | 13 ++++++++++--- x/incentive/keeper/rewards.go | 25 ++++++++++++++----------- x/incentive/types/events.go | 13 ++++++++++--- x/incentive/types/rewards.go | 33 +++++++++++++++++++++++++++++---- 5 files changed, 84 insertions(+), 34 deletions(-) diff --git a/x/incentive/alias.go b/x/incentive/alias.go index 877ddba3..e4774df6 100644 --- a/x/incentive/alias.go +++ b/x/incentive/alias.go @@ -1,9 +1,10 @@ +package incentive + // nolint // autogenerated code using github.com/rigelrozanski/multitool // aliases generated for the following subdirectories: // ALIASGEN: github.com/kava-labs/kava/x/incentive/keeper // ALIASGEN: github.com/kava-labs/kava/x/incentive/types -package incentive import ( "github.com/kava-labs/kava/x/incentive/keeper" @@ -11,18 +12,24 @@ import ( ) const ( - EventTypeClaim = types.EventTypeClaim - AttributeValueCategory = types.AttributeValueCategory - AttributeKeySender = types.AttributeKeySender - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - DefaultParamspace = types.DefaultParamspace - QuerierRoute = types.QuerierRoute - QueryGetClaims = types.QueryGetClaims - RestClaimOwner = types.RestClaimOwner - RestClaimDenom = types.RestClaimDenom - QueryGetParams = types.QueryGetParams + EventTypeClaim = types.EventTypeClaim + EventTypeRewardPeriod = types.EventTypeRewardPeriod + EventTypeClaimPeriod = types.EventTypeClaimPeriod + EventTypeClaimPeriodExpiry = types.EventTypeClaimPeriodExpiry + AttributeValueCategory = types.AttributeValueCategory + AttributeKeyClaimedBy = types.AttributeKeyClaimedBy + AttributeKeyClaimAmount = types.AttributeKeyClaimAmount + AttributeKeyRewardPeriod = types.AttributeKeyRewardPeriod + AttributeKeyClaimPeriod = types.AttributeKeyClaimPeriod + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute + QueryGetClaims = types.QueryGetClaims + RestClaimOwner = types.RestClaimOwner + RestClaimDenom = types.RestClaimDenom + QueryGetParams = types.QueryGetParams ) var ( @@ -46,6 +53,7 @@ var ( NewRewardPeriod = types.NewRewardPeriod NewClaimPeriod = types.NewClaimPeriod NewClaim = types.NewClaim + NewRewardPeriodFromReward = types.NewRewardPeriodFromReward // variable aliases ModuleCdc = types.ModuleCdc diff --git a/x/incentive/keeper/payout.go b/x/incentive/keeper/payout.go index eaeb7d62..b9a67c5b 100644 --- a/x/incentive/keeper/payout.go +++ b/x/incentive/keeper/payout.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "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) + ctx.EventManager().EmitEvent( sdk.NewEvent( 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 @@ -104,6 +104,13 @@ func (k Keeper) DeleteExpiredClaimsAndClaimPeriods(ctx sdk.Context) { return false }) k.DeleteClaimPeriod(ctx, cp.ID, cp.Denom) + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventTypeClaimPeriodExpiry, + sdk.NewAttribute(types.AttributeKeyClaimPeriod, cp.String()), + ), + ) + return false }) } diff --git a/x/incentive/keeper/rewards.go b/x/incentive/keeper/rewards.go index 9fd11d20..d8c2054b 100644 --- a/x/incentive/keeper/rewards.go +++ b/x/incentive/keeper/rewards.go @@ -20,18 +20,15 @@ func (k Keeper) HandleRewardPeriodExpiry(ctx sdk.Context, rp types.RewardPeriod) // CreateNewRewardPeriod creates a new reward period from the input reward func (k Keeper) CreateNewRewardPeriod(ctx sdk.Context, reward types.Reward) { - // 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) - 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, - } + rp := types.NewRewardPeriodFromReward(reward, ctx.BlockTime()) 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 @@ -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) { id := k.GetNextClaimPeriodID(ctx, denom) 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.SetNextClaimPeriodID(ctx, denom, id+1) } diff --git a/x/incentive/types/events.go b/x/incentive/types/events.go index 09d0cd47..e431617a 100644 --- a/x/incentive/types/events.go +++ b/x/incentive/types/events.go @@ -1,8 +1,15 @@ package types +// Events emitted by the incentive module const ( - EventTypeClaim = "claim_reward" + EventTypeClaim = "claim_reward" + EventTypeRewardPeriod = "new_reward_period" + EventTypeClaimPeriod = "new_claim_period" + EventTypeClaimPeriodExpiry = "claim_period_expiry" - AttributeValueCategory = ModuleName - AttributeKeySender = "sender" + AttributeValueCategory = ModuleName + AttributeKeyClaimedBy = "claimed_by" + AttributeKeyClaimAmount = "claim_amount" + AttributeKeyRewardPeriod = "reward_period" + AttributeKeyClaimPeriod = "claim_period" ) diff --git a/x/incentive/types/rewards.go b/x/incentive/types/rewards.go index 237b222a..76539c86 100644 --- a/x/incentive/types/rewards.go +++ b/x/incentive/types/rewards.go @@ -25,8 +25,8 @@ func (rp RewardPeriod) String() string { End: %s, Reward: %s, Claim End: %s, - Claim Time Lock: %s`, - rp.Denom, rp.Start, rp.End, rp.Reward, rp.ClaimEnd, rp.ClaimTimeLock) + Claim Time Lock: %s + `, rp.Denom, rp.Start, rp.End, rp.Reward, rp.ClaimEnd, rp.ClaimTimeLock) } // NewRewardPeriod returns a new RewardPeriod @@ -52,6 +52,16 @@ type ClaimPeriod struct { 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 func NewClaimPeriod(denom string, id uint64, end time.Time, timeLock time.Duration) ClaimPeriod { return ClaimPeriod{ @@ -89,9 +99,24 @@ func (c Claim) String() string { Owner: %s, Denom: %s, Reward: %s, - Claim Period ID: %d,`, - c.Owner, c.Denom, c.Reward, c.ClaimPeriodID) + Claim Period ID: %d, + `, c.Owner, c.Denom, c.Reward, c.ClaimPeriodID) } // Claims array of 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, + } +}