Skip to content

Commit

Permalink
Merge pull request #324 from sge-network/feature/signup-referral
Browse files Browse the repository at this point in the history
Feature / Referral Signup and Referral (#321)
  • Loading branch information
3eyedraga authored Dec 18, 2023
2 parents 895f769 + f2ba9ca commit e9bf56c
Show file tree
Hide file tree
Showing 11 changed files with 747 additions and 97 deletions.
13 changes: 12 additions & 1 deletion proto/sge/reward/ticket.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,16 @@ message RewardPayloadCommon {
// GrantSignupRewardPayload is the type for signup reward grant payload.
message GrantSignupRewardPayload {
// common is the common properties of a reward
RewardPayloadCommon common = 2 [ (gogoproto.nullable) = false ];
RewardPayloadCommon common = 1 [ (gogoproto.nullable) = false ];
}

// GrantSignupReferrerRewardPayload is the type for signup referrer reward grant
// payload.
message GrantSignupReferrerRewardPayload {
// common is the common properties of a reward
RewardPayloadCommon common = 1 [ (gogoproto.nullable) = false ];

// referee is the address of the account that used this referrer address as
// source_uid
string referee = 2;
}
22 changes: 12 additions & 10 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,45 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to retrieve reward factory")
}

receiver, rewardCommon, err := rewardFactory.Calculate(goCtx, ctx,
factData, err := rewardFactory.Calculate(goCtx, ctx,
types.RewardFactoryKeepers{
OVMKeeper: k.ovmKeeper,
BetKeeper: k.betKeeper,
SubAccountKeeper: k.subaccountKeeper,
RewardKeeper: k.Keeper,
AccountKeeper: k.accountKeeper,
}, campaign, msg.Ticket, msg.Creator)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "distribution calculation failed %s", err)
}

rewards, err := k.GetRewardsByAddressAndCategory(ctx, receiver.MainAccountAddr, campaign.RewardCategory)
rewards, err := k.GetRewardsByAddressAndCategory(ctx, factData.Receiver.MainAccountAddr, campaign.RewardCategory)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to retrieve rewards for user.")
}
if len(rewards) >= cast.ToInt(campaign.ClaimsPerCategory) {
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "maximum rewards claimed for the given category.")
}

if err := campaign.CheckPoolBalance(receiver.SubAccountAmount.Add(receiver.MainAccountAmount)); err != nil {
if err := campaign.CheckPoolBalance(factData.Receiver.SubAccountAmount.Add(factData.Receiver.MainAccountAmount)); err != nil {
return nil, types.ErrInsufficientPoolBalance
}

if err := k.DistributeRewards(ctx, receiver); err != nil {
if err := k.DistributeRewards(ctx, factData.Receiver); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInDistributionOfRewards, "%s", err)
}

k.UpdateCampaignPool(ctx, campaign, receiver)
k.UpdateCampaignPool(ctx, campaign, factData.Receiver)
k.SetReward(ctx, types.NewReward(
msg.Uid, msg.Creator, receiver.MainAccountAddr,
msg.Uid, msg.Creator, factData.Receiver.MainAccountAddr,
msg.CampaignUid, campaign.RewardAmount,
rewardCommon.SourceUID,
rewardCommon.Meta,
factData.Common.SourceUID,
factData.Common.Meta,
))
k.SetRewardByReceiver(ctx, types.NewRewardByCategory(msg.Uid, receiver.MainAccountAddr, campaign.RewardCategory))
k.SetRewardByReceiver(ctx, types.NewRewardByCategory(msg.Uid, factData.Receiver.MainAccountAddr, campaign.RewardCategory))
k.SetRewardByCampaign(ctx, types.NewRewardByCampaign(msg.Uid, campaign.UID))

msg.EmitEvent(&ctx, msg.CampaignUid, msg.Uid, campaign.Promoter, *campaign.RewardAmount, receiver)
msg.EmitEvent(&ctx, msg.CampaignUid, msg.Uid, campaign.Promoter, *campaign.RewardAmount, factData.Receiver)

return &types.MsgGrantRewardResponse{}, nil
}
Loading

0 comments on commit e9bf56c

Please sign in to comment.