Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reward): emit events #752

Merged
merged 14 commits into from
May 11, 2022
27 changes: 27 additions & 0 deletions proto/reward/events.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";
package tendermint.spn.reward;

import "gogoproto/gogo.proto";
import "reward/reward_pool.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/tendermint/spn/x/reward/types";

message EventRewardPoolCreated {
uint64 launchID = 1;
string provider = 2;
}

message EventRewardPoolRemoved {
uint64 launchID = 1;
}

message EventRewardsDistributed {
uint64 launchID = 1;
string receiver = 2;
repeated cosmos.base.v1beta1.Coin rewards = 3 [
(gogoproto.nullable) = false,
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
17 changes: 12 additions & 5 deletions x/reward/keeper/msg_set_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func (k msgServer) SetRewards(goCtx context.Context, msg *types.MsgSetRewards) (
ctx := sdk.UnwrapSDKContext(goCtx)

// determine if the chain exists
chain, found := k.launchKeeper.GetChain(ctx, msg.LaunchID)
if !found {
chain, chainFound := k.launchKeeper.GetChain(ctx, msg.LaunchID)
if !chainFound {
return nil, sdkerrors.Wrapf(launchtypes.ErrChainNotFound, "%d", msg.LaunchID)
}
// check coordinator
Expand All @@ -42,8 +42,8 @@ func (k msgServer) SetRewards(goCtx context.Context, msg *types.MsgSetRewards) (
previousCoins sdk.Coins
previousLastRewardHeight int64
)
rewardPool, found := k.GetRewardPool(ctx, msg.LaunchID)
if !found {
rewardPool, poolFound := k.GetRewardPool(ctx, msg.LaunchID)
if !poolFound {
// create the reward pool and transfer tokens if not created yet
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, provider, types.ModuleName, msg.Coins); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, err.Error())
Expand All @@ -62,20 +62,27 @@ func (k msgServer) SetRewards(goCtx context.Context, msg *types.MsgSetRewards) (
rewardPool.RemainingCoins = sdk.NewCoins()
rewardPool.LastRewardHeight = 0
k.RemoveRewardPool(ctx, msg.LaunchID)
err = ctx.EventManager().EmitTypedEvent(&types.EventRewardPoolRemoved{LaunchID: msg.LaunchID})
} else {
rewardPool.InitialCoins = msg.Coins
rewardPool.RemainingCoins = msg.Coins
rewardPool.Provider = msg.Provider
rewardPool.LastRewardHeight = msg.LastRewardHeight
k.SetRewardPool(ctx, rewardPool)
if !poolFound {
err = ctx.EventManager().EmitTypedEvent(&types.EventRewardPoolCreated{
LaunchID: rewardPool.LaunchID,
Provider: rewardPool.Provider,
})
}
}

return &types.MsgSetRewardsResponse{
PreviousCoins: previousCoins,
PreviousLastRewardHeight: previousLastRewardHeight,
NewCoins: rewardPool.InitialCoins,
NewLastRewardHeight: rewardPool.LastRewardHeight,
}, nil
}, err
}

// SetBalance set balance to Coins on the module account
Expand Down
8 changes: 8 additions & 0 deletions x/reward/keeper/reward_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func (k Keeper) DistributeRewards(
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, account, rewards); err != nil {
return spnerrors.Criticalf("send rewards error: %s", err.Error())
}
if err := ctx.EventManager().EmitTypedEvent(&types.EventRewardsDistributed{
LaunchID: launchID,
Receiver: address,
Rewards: rewards,
}); err != nil {
return spnerrors.Criticalf("error emitting event: %s", err.Error())
}
}

// if the reward pool is closed or last reward height is reached
Expand All @@ -138,6 +145,7 @@ func (k Keeper) DistributeRewards(
rewardPool.Closed = true
rewardPool.RemainingCoins = rewardPool.RemainingCoins.Sub(rewardPool.RemainingCoins) // sub coins transferred
k.SetRewardPool(ctx, rewardPool)

return nil
}

Expand Down
Loading