Skip to content

Commit

Permalink
feat: reward messages event emission
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Oct 6, 2023
1 parent 5bd66c2 commit 33edf83
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion x/reward/keeper/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createNCampaign(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Camp
items[i].StartTS = uint64(time.Now().Unix())
items[i].EndTS = uint64(time.Now().Add(5 * time.Minute).Unix())
items[i].RewardType = types.RewardType_REWARD_TYPE_REFERRAL
items[i].RewardDefs = []types.Definition{{
items[i].RewardDefs = types.Definitions{{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
RecAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_SUB,
Amount: sdkmath.NewInt(100),
Expand Down
4 changes: 4 additions & 0 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam

k.SetCampaign(ctx, campaign)

msg.EmitEvent(&ctx, msg.Uid)

return &types.MsgCreateCampaignResponse{}, nil
}

Expand Down Expand Up @@ -104,5 +106,7 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam

k.SetCampaign(ctx, valFound)

msg.EmitEvent(&ctx, msg.Uid)

return &types.MsgUpdateCampaignResponse{}, nil
}
2 changes: 2 additions & 0 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward)

k.UpdateCampaignPool(ctx, campaign, distribution)

msg.EmitEvent(&ctx, msg.CampaignUid, distribution)

return &types.MsgApplyRewardResponse{}, nil
}
4 changes: 2 additions & 2 deletions x/reward/types/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func NewCampaign(
creator, funderAddres, uID string,
startTS, endTS uint64,
rewardType RewardType,
rewardDefs []Definition,
rewardDefs Definitions,
pool Pool,
) Campaign {
return Campaign{
Expand Down Expand Up @@ -48,7 +48,7 @@ func (c *Campaign) CheckExpiration(blockTime uint64) error {
}

// CheckPoolBalance checks if a pool balance of a capaign has enough fund to pay the rewards.
func (c *Campaign) CheckPoolBalance(distributions []Distribution) error {
func (c *Campaign) CheckPoolBalance(distributions Distributions) error {
totalAmount := sdkmath.ZeroInt()
for _, d := range distributions {
totalAmount.Add(d.Allocation.Amount)
Expand Down
8 changes: 8 additions & 0 deletions x/reward/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

const (
attributeValueCategory = ModuleName
attributeKeyUID = "uid"
attributeKeyCampaignUID = "campaign_uid"
attributeKeyDistributions = "distributions"
)
18 changes: 18 additions & 0 deletions x/reward/types/messages_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func (msg *MsgCreateCampaign) ValidateBasic() error {
return nil
}

// EmitEvent emits the event for the message success.
func (msg *MsgCreateCampaign) EmitEvent(ctx *sdk.Context, uid string) {
emitter := utils.NewEventEmitter(ctx, attributeValueCategory)
emitter.AddMsg(TypeMsgCreateCampaign, msg.Creator,
sdk.NewAttribute(attributeKeyUID, uid),
)
emitter.Emit()
}

var _ sdk.Msg = &MsgUpdateCampaign{}

func NewMsgUpdateCampaign(
Expand Down Expand Up @@ -115,3 +124,12 @@ func (msg *MsgUpdateCampaign) ValidateBasic() error {

return nil
}

// EmitEvent emits the event for the message success.
func (msg *MsgUpdateCampaign) EmitEvent(ctx *sdk.Context, uid string) {
emitter := utils.NewEventEmitter(ctx, attributeValueCategory)
emitter.AddMsg(TypeMsgUpdateCampaign, msg.Creator,
sdk.NewAttribute(attributeKeyUID, uid),
)
emitter.Emit()
}
10 changes: 10 additions & 0 deletions x/reward/types/messages_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ func (msg *MsgApplyReward) ValidateBasic() error {

return nil
}

// EmitEvent emits the event for the message success.
func (msg *MsgApplyReward) EmitEvent(ctx *sdk.Context, campaignUID string, distributions Distributions) {
emitter := utils.NewEventEmitter(ctx, attributeValueCategory)
emitter.AddMsg(TypeMsgApplyReward, msg.Creator,
sdk.NewAttribute(attributeKeyCampaignUID, campaignUID),
sdk.NewAttribute(attributeKeyDistributions, distributions.String()),
)
emitter.Emit()
}
16 changes: 14 additions & 2 deletions x/reward/types/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
sdkerrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
yaml "gopkg.in/yaml.v2"
)

type Definitions []Definition
type (
Definitions []Definition
Distributions []Distribution
)

// RewardFactoryKeepers holds the keeper objectes usable by reward types methods.
type RewardFactoryKeepers struct {
Expand All @@ -20,7 +24,7 @@ type RewardFactoryKeepers struct {
type IRewardFactory interface {
VaidateDefinitions(campaign Campaign) error
CalculateDistributions(goCtx context.Context, ctx sdk.Context, keepers RewardFactoryKeepers,
definitions Definitions, ticket string) ([]Distribution, error)
definitions Definitions, ticket string) (Distributions, error)
}

// NewDistribution created new distribution object.
Expand Down Expand Up @@ -51,3 +55,11 @@ func (d *Definition) ValidateBasic(blockTime uint64) error {
}
return nil
}

func (ds Distributions) String() string {
out, err := yaml.Marshal(ds)
if err != nil {
panic(err)
}
return string(out)
}
4 changes: 2 additions & 2 deletions x/reward/types/reward_affiliate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (afr AffiliationReward) VaidateDefinitions(campaign Campaign) error {
// CalculateDistributions parses ticket payload and returns the distribution list of affiliation reward.
func (afr AffiliationReward) CalculateDistributions(goCtx context.Context, ctx sdk.Context, keepers RewardFactoryKeepers,
definitions Definitions, ticket string,
) ([]Distribution, error) {
) (Distributions, error) {
var payload ApplyAffiliationRewardPayload
if err := keepers.OVMKeeper.VerifyTicketUnmarshal(goCtx, ticket, &payload); err != nil {
return nil, sdkerrors.Wrapf(ErrInTicketVerification, "%s", err)
Expand All @@ -42,7 +42,7 @@ func (afr AffiliationReward) CalculateDistributions(goCtx context.Context, ctx s
return nil, sdkerrors.Wrapf(ErrAccReceiverTypeNotFound, "%s", payload.Receiver.RecType)
}

return []Distribution{
return Distributions{
NewDistribution(
payload.Receiver.Addr,
NewAllocation(
Expand Down
4 changes: 2 additions & 2 deletions x/reward/types/reward_noloss.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (afr NoLossBetsReward) VaidateDefinitions(campaign Campaign) error {
// CalculateDistributions parses ticket payload and returns the distribution list of no loss bets reward.
func (afr NoLossBetsReward) CalculateDistributions(goCtx context.Context, ctx sdk.Context, keepers RewardFactoryKeepers,
definitions Definitions, ticket string,
) ([]Distribution, error) {
) (Distributions, error) {
var payload ApplyNoLossBetsRewardPayload
if err := keepers.OVMKeeper.VerifyTicketUnmarshal(goCtx, ticket, &payload); err != nil {
return nil, sdkerrors.Wrapf(ErrInTicketVerification, "%s", err)
Expand All @@ -59,7 +59,7 @@ func (afr NoLossBetsReward) CalculateDistributions(goCtx context.Context, ctx sd
}
}

return []Distribution{
return Distributions{
NewDistribution(
payload.Receiver.Addr,
NewAllocation(
Expand Down
4 changes: 2 additions & 2 deletions x/reward/types/reward_referral.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func (rfr ReferralReward) VaidateDefinitions(campaign Campaign) error {
// CalculateDistributions parses ticket payload and returns the distribution list of referral reward.
func (rfr ReferralReward) CalculateDistributions(goCtx context.Context, ctx sdk.Context, keepers RewardFactoryKeepers,
definitions Definitions, ticket string,
) ([]Distribution, error) {
) (Distributions, error) {
var payload ApplyRerferralRewardPayload
if err := keepers.OVMKeeper.VerifyTicketUnmarshal(goCtx, ticket, &payload); err != nil {
return nil, sdkerrors.Wrapf(ErrInTicketVerification, "%s", err)
}

distributions := []Distribution{}
distributions := Distributions{}
for _, d := range definitions {
found := false
for _, r := range payload.Receivers {
Expand Down
4 changes: 2 additions & 2 deletions x/reward/types/reward_signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (sur SignUpReward) VaidateDefinitions(campaign Campaign) error {
// CalculateDistributions parses ticket payload and returns the distribution list of signup reward.
func (sur SignUpReward) CalculateDistributions(goCtx context.Context, ctx sdk.Context, keepers RewardFactoryKeepers,
definitions Definitions, ticket string,
) ([]Distribution, error) {
) (Distributions, error) {
var payload ApplySignupRewardPayload
if err := keepers.OVMKeeper.VerifyTicketUnmarshal(goCtx, ticket, &payload); err != nil {
return nil, sdkerrors.Wrapf(ErrInTicketVerification, "%s", err)
Expand All @@ -42,7 +42,7 @@ func (sur SignUpReward) CalculateDistributions(goCtx context.Context, ctx sdk.Co
return nil, sdkerrors.Wrapf(ErrAccReceiverTypeNotFound, "%s", payload.Receiver.RecType)
}

return []Distribution{
return Distributions{
NewDistribution(
payload.Receiver.Addr,
NewAllocation(
Expand Down

0 comments on commit 33edf83

Please sign in to comment.