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

Feature / Referral Signup and Referral (#321) #324

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading