Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
P-U-D-G-E committed Nov 8, 2023
1 parent 7ac0369 commit bd2e4df
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
10 changes: 6 additions & 4 deletions x/reward/client/cli/tx_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (

func CmdGrantReward() *cobra.Command {
cmd := &cobra.Command{
Use: "apply [campaign uid] [ticket]",
Use: "apply [uid] [campaign uid] [ticket]",
Short: "Apply a new reward",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
argCampaignUID := args[0]
argUID := args[0]
argCampaignUID := args[1]

// Get value arguments
argTicket := args[1]
argTicket := args[2]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -29,6 +30,7 @@ func CmdGrantReward() *cobra.Command {

msg := types.NewMsgGrantReward(
clientCtx.GetFromAddress().String(),
argUID,
argCampaignUID,
argTicket,
)
Expand Down
6 changes: 3 additions & 3 deletions x/reward/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

// DistributeRewards distributes the rewards according to the input distribution list.
func (k Keeper) DistributeRewards(ctx sdk.Context, receiver types.Receiver) error {
func (k Keeper) DistributeRewards(ctx sdk.Context, funderAddr string, receiver types.Receiver) error {
if receiver.SubAccountAmount.GT(sdk.ZeroInt()) {
if _, err := k.subaccountKeeper.TopUp(ctx, types.RewardPoolFunder{}.GetModuleAcc(), receiver.SubAccountAddr,
if _, err := k.subaccountKeeper.TopUp(ctx, funderAddr, receiver.MainAccountAddr,
[]subaccounttypes.LockedBalance{
{
UnlockTS: receiver.UnlockTS,
UnlockTS: uint64(ctx.BlockTime().Unix()) + receiver.UnlockPeriod,
Amount: receiver.SubAccountAmount,
},
}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam

campaign := types.NewCampaign(
msg.Creator, payload.Promoter, msg.Uid,
payload.StartTs, payload.EndTs,
payload.StartTs, payload.EndTs, payload.ClaimsPerCategory,
payload.RewardType,
payload.Category,
payload.RewardAmountType,
Expand Down
20 changes: 10 additions & 10 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to retrieve reward factory")
}

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

recevier, rewardCommon, err := rewardFactory.Calculate(goCtx, ctx,
types.RewardFactoryKeepers{
OVMKeeper: k.ovmKeeper,
Expand All @@ -49,11 +41,19 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "distribution calculation failed %s", err)
}

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

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

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

Expand All @@ -64,7 +64,7 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
rewardCommon.SourceUID,
"",
))
k.SetRewardByReceiver(ctx, types.NewRewardByType(msg.Uid, recevier.MainAccountAddr, campaign.RewardCategory))
k.SetRewardByReceiver(ctx, types.NewRewardByCategory(msg.Uid, recevier.MainAccountAddr, campaign.RewardCategory))
k.SetRewardByCampaign(ctx, types.NewRewardByCampaign(msg.Uid, campaign.UID))

msg.EmitEvent(&ctx, msg.CampaignUid, recevier)
Expand Down
4 changes: 2 additions & 2 deletions x/reward/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (k Keeper) GetRewardsByAddressAndCategory(
ctx sdk.Context,
address string,
category types.RewardCategory,
) (list []types.Reward, err error) {
) (list []types.RewardByCategory, err error) {
store := k.getRewardByReceiverAndCategoryStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, types.GetRewardsByCategoryPrefix(address, category))

Expand All @@ -73,7 +73,7 @@ func (k Keeper) GetRewardsByAddressAndCategory(
}()

for ; iterator.Valid(); iterator.Next() {
var val types.Reward
var val types.RewardByCategory
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
Expand Down
27 changes: 14 additions & 13 deletions x/reward/types/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func NewCampaign(
creator, promoter, uID string,
startTS, endTS uint64,
startTS, endTS, claimsPerCategory uint64,
rewardType RewardType,
rewardCategory RewardCategory,
rewardAmountType RewardAmountType,
Expand All @@ -17,18 +17,19 @@ func NewCampaign(
pool Pool,
) Campaign {
return Campaign{
Creator: creator,
Promoter: promoter,
UID: uID,
StartTS: startTS,
EndTS: endTS,
RewardCategory: rewardCategory,
RewardType: rewardType,
RewardAmountType: rewardAmountType,
RewardAmount: rewardAmount,
IsActive: isActive,
Meta: meta,
Pool: pool,
Creator: creator,
Promoter: promoter,
UID: uID,
StartTS: startTS,
EndTS: endTS,
RewardCategory: rewardCategory,
RewardType: rewardType,
RewardAmountType: rewardAmountType,
RewardAmount: rewardAmount,
IsActive: isActive,
Meta: meta,
Pool: pool,
ClaimsPerCategory: claimsPerCategory,
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/reward/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
ErrUnknownRewardType = sdkerrors.Register(ModuleName, 7107, "unknown reward type")
ErrInFundingCampaignPool = sdkerrors.Register(ModuleName, 7108, "error in funding the campaign pool")
ErrWithdrawFromCampaignPool = sdkerrors.Register(ModuleName, 7109, "error in withdrawing from the campaign pool")
ErrUnknownAccType = sdkerrors.Register(ModuleName, 7109, "unknown account type")
ErrCampaignEnded = sdkerrors.Register(ModuleName, 7110, "campaign validity period is ended")
ErrInsufficientPoolBalance = sdkerrors.Register(ModuleName, 7111, "insufficient campaign pool balance")
ErrInDistributionOfRewards = sdkerrors.Register(ModuleName, 7112, "reward distribution failed")
Expand All @@ -32,5 +31,6 @@ var (
ErrInvalidNoLossBetUID = sdkerrors.Register(ModuleName, 7120, "invalid no loss bet uid")
ErrWrongAmountForType = sdkerrors.Register(ModuleName, 7121, "wrong amount for account type")
ErrSubAccountCreationFailed = sdkerrors.Register(ModuleName, 7122, "suba ccount creation failed")
ErrWrongRewardAmountType = sdkerrors.Register(ModuleName, 7114, "wrong reward amount type")
ErrWrongRewardAmountType = sdkerrors.Register(ModuleName, 7123, "wrong reward amount type")
ErrUnknownAccType = sdkerrors.Register(ModuleName, 7124, "unknown account type")
)
2 changes: 2 additions & 0 deletions x/reward/types/messages_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ var _ sdk.Msg = &MsgGrantReward{}

func NewMsgGrantReward(
creator string,
uid string,
campaignUID string,
ticket string,
) *MsgGrantReward {
return &MsgGrantReward{
Creator: creator,
Uid: uid,
CampaignUid: campaignUID,
Ticket: ticket,
}
Expand Down
8 changes: 4 additions & 4 deletions x/reward/types/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Receiver struct {
SubAccountAddr string
SubAccountAmount sdkmath.Int
UnlockTS uint64
UnlockPeriod uint64
MainAccountAddr string
MainAccountAmount sdkmath.Int
}
Expand Down Expand Up @@ -41,7 +41,7 @@ func NewReward(
}
}

func NewRewardByType(uid, addr string, rewardCategory RewardCategory) RewardByCategory {
func NewRewardByCategory(uid, addr string, rewardCategory RewardCategory) RewardByCategory {
return RewardByCategory{
UID: uid,
RewardCategory: rewardCategory,
Expand All @@ -65,11 +65,11 @@ type IRewardFactory interface {
}

// NewReceiver creates reveiver object.
func NewReceiver(saAddr, maAddr string, saAmount, maAmount sdkmath.Int, unlockTS uint64) Receiver {
func NewReceiver(saAddr, maAddr string, saAmount, maAmount sdkmath.Int, unlockPeriod uint64) Receiver {
return Receiver{
SubAccountAddr: saAddr,
SubAccountAmount: saAmount,
UnlockTS: unlockTS,
UnlockPeriod: unlockPeriod,
MainAccountAddr: maAddr,
MainAccountAmount: maAmount,
}
Expand Down

0 comments on commit bd2e4df

Please sign in to comment.