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

Refactor / Move campaign percentage to separate field #387

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
3 changes: 1 addition & 2 deletions proto/sge/reward/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ message GenesisState {
[ (gogoproto.nullable) = false ];
repeated RewardByCampaign reward_by_campaign_list = 5
[ (gogoproto.nullable) = false ];
repeated Promoter promoter_list = 6
[ (gogoproto.nullable) = false ];
repeated Promoter promoter_list = 6 [ (gogoproto.nullable) = false ];
repeated PromoterByAddress promoter_by_address_list = 7
[ (gogoproto.nullable) = false ];
}
57 changes: 29 additions & 28 deletions proto/sge/reward/promoter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,46 @@ import "sge/reward/reward.proto";

option go_package = "github.com/sge-network/sge/x/reward/types";

// Promoter is type for defining the reward promoter properties and configuration.
// Promoter is type for defining the reward promoter properties and
// configuration.
message Promoter {
// creator is the address of promoter.
string creator = 1;
// uid is the unique identifier of a promoter.
string uid = 2 [
(gogoproto.customname) = "UID",
(gogoproto.jsontag) = "uid",
json_name = "uid"
];
// addresses is the list of account addresses of promoter.
repeated string addresses = 3;

// conf is the configurations of the current promoter for the reward grant.
PromoterConf conf = 4 [ (gogoproto.nullable) = false ];
// creator is the address of promoter.
string creator = 1;

// uid is the unique identifier of a promoter.
string uid = 2 [
(gogoproto.customname) = "UID",
(gogoproto.jsontag) = "uid",
json_name = "uid"
];

// addresses is the list of account addresses of promoter.
repeated string addresses = 3;

// conf is the configurations of the current promoter for the reward grant.
PromoterConf conf = 4 [ (gogoproto.nullable) = false ];
}

// PromoterConf is type for defining the promoter specific configurations.
message PromoterConf {
// category_cap is the maximium allowed cap for each category.
repeated CategoryCap category_cap = 1 [ (gogoproto.nullable) = false ];
// category_cap is the maximium allowed cap for each category.
repeated CategoryCap category_cap = 1 [ (gogoproto.nullable) = false ];
}

// CategoryCap is type to define category and its maximum cap.
message CategoryCap {
RewardCategory category = 1;
int32 cap_per_acc = 2;
RewardCategory category = 1;
int32 cap_per_acc = 2;
}

// PromoterByAddress is type for defining the promoter by address.
message PromoterByAddress {
// promoter_uid is the unique identifier of a certain promoter.
string promoter_uid = 1 [
(gogoproto.customname) = "PromoterUID",
(gogoproto.jsontag) = "promoter_uid",
json_name = "promoter_uid"
];
// address is the address of the promoter account.
string address = 2;
// promoter_uid is the unique identifier of a certain promoter.
string promoter_uid = 1 [
(gogoproto.customname) = "PromoterUID",
(gogoproto.jsontag) = "promoter_uid",
json_name = "promoter_uid"
];
// address is the address of the promoter account.
string address = 2;
}
19 changes: 11 additions & 8 deletions proto/sge/reward/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ service Query {
}

// PromoterByAddress queries a certain promoter.
rpc PromoterByAddress(QueryPromoterByAddressRequest) returns (QueryPromoterByAddressResponse) {
option (google.api.http).get = "/sge-network/sge/reward/promoter-by-address/{addr}";
rpc PromoterByAddress(QueryPromoterByAddressRequest)
returns (QueryPromoterByAddressResponse) {
option (google.api.http).get =
"/sge-network/sge/reward/promoter-by-address/{addr}";
}

// Queries list of all Promoter items.
Expand Down Expand Up @@ -53,7 +55,8 @@ service Query {
// Queries list of all Reward items by user address.
rpc RewardsByAddress(QueryRewardsByAddressRequest)
returns (QueryRewardsByAddressResponse) {
option (google.api.http).get = "/sge-network/sge/reward/rewards/{promoter_uid}/{address}";
option (google.api.http).get =
"/sge-network/sge/reward/rewards/{promoter_uid}/{address}";
}

// Queries list of all Reward items by user address and reward type
Expand Down Expand Up @@ -81,12 +84,12 @@ message QueryParamsResponse {
Params params = 1 [ (gogoproto.nullable) = false ];
}

// QueryPromoterByAddressRequest is request type for the Query/GetPromoterByAddress RPC method.
message QueryPromoterByAddressRequest {
string addr = 1;
}
// QueryPromoterByAddressRequest is request type for the
// Query/GetPromoterByAddress RPC method.
message QueryPromoterByAddressRequest { string addr = 1; }

// QueryPromoterByAddressResponse is response type for the Query/GetPromoterByAddress RPC method.
// QueryPromoterByAddressResponse is response type for the
// Query/GetPromoterByAddress RPC method.
message QueryPromoterByAddressResponse {
// promoter holds the queries promoter.
Promoter promoter = 1 [ (gogoproto.nullable) = false ];
Expand Down
18 changes: 16 additions & 2 deletions proto/sge/reward/reward.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ message Reward {

// RewardAmount
message RewardAmount {
// main_account_reward amount transferred to main account address
// main_account_amount transferred to main account address
string main_account_amount = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"main_account_amount\""
];

// sub_account reward amount transferred to subaccount address
// subaccount_amount transferred to subaccount address
string subaccount_amount = 2 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
Expand All @@ -73,6 +73,20 @@ message RewardAmount {
(gogoproto.jsontag) = "unlock_period",
json_name = "unlock_period"
];

// main_account_percentage transferred to main account address
string main_account_percentage = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"main_account_percentage\""
];

// subaccount_percentage amount transferred to subaccount address
string subaccount_percentage = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"subaccount_percentage\""
];
}

// RewardByCategory
Expand Down
5 changes: 3 additions & 2 deletions x/reward/client/cli/tx_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

func CmdGrantReward() *cobra.Command {
cmd := &cobra.Command{
Use: "apply [uid] [campaign uid] [ticket]",
Short: "Apply a new reward",
Use: "grant [uid] [campaign uid] [ticket]",
Short: "Grant a new reward for the campaign",
Long: "Grant a new reward for the campaign with the provided uid",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
Expand Down
5 changes: 2 additions & 3 deletions x/reward/keeper/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ func (k Keeper) GetAllCampaign(ctx sdk.Context) (list []types.Campaign) {
return
}

// UpdateCampaignPool updates campaign pool according to the total receiving amount of receiver
func (k Keeper) UpdateCampaignPool(ctx sdk.Context, campaign types.Campaign, receiver types.Receiver) {
totalAmount := receiver.SubaccountAmount.Add(receiver.MainAccountAmount) // Fixme: Check if the logic is correct
campaign.Pool.Spend(totalAmount)

campaign.Pool.Spend(receiver.TotalAmount())
k.SetCampaign(ctx, campaign)
}
10 changes: 5 additions & 5 deletions x/reward/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import (
// DistributeRewards distributes the rewards according to the input distribution list.
func (k Keeper) DistributeRewards(ctx sdk.Context, receiver types.Receiver) (uint64, error) {
unlockTS := uint64(0)
if receiver.SubaccountAmount.GT(sdk.ZeroInt()) {
if receiver.RewardAmount.SubaccountAmount.GT(sdk.ZeroInt()) {
moduleAccAddr := types.RewardPoolFunder{}.GetModuleAcc()
unlockTS = cast.ToUint64(ctx.BlockTime().Unix()) + receiver.UnlockPeriod
unlockTS = cast.ToUint64(ctx.BlockTime().Unix()) + receiver.RewardAmount.UnlockPeriod
if _, err := k.subaccountKeeper.TopUp(ctx, k.accountKeeper.GetModuleAddress(moduleAccAddr).String(), receiver.MainAccountAddr,
[]subaccounttypes.LockedBalance{
{
UnlockTS: unlockTS,
Amount: receiver.SubaccountAmount,
Amount: receiver.RewardAmount.SubaccountAmount,
},
}); err != nil {
return unlockTS, sdkerrors.Wrapf(types.ErrSubaccountRewardTopUp, "subaccount address %s, %s", receiver.SubaccountAddr, err)
}
}
if receiver.MainAccountAmount.GT(sdk.ZeroInt()) {
if receiver.RewardAmount.MainAccountAmount.GT(sdk.ZeroInt()) {
if err := k.modFunder.Refund(
types.RewardPoolFunder{}, ctx,
sdk.MustAccAddressFromBech32(receiver.MainAccountAddr),
receiver.MainAccountAmount,
receiver.RewardAmount.MainAccountAmount,
); err != nil {
return unlockTS, err
}
Expand Down
13 changes: 13 additions & 0 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward amount %s is more than total funds %s", totalRewardAmount, msg.TotalFunds)
}

totalRewardPercentage := sdk.ZeroDec()
if !payload.RewardAmount.MainAccountPercentage.IsNil() {
totalRewardPercentage = totalRewardPercentage.Add(payload.RewardAmount.MainAccountPercentage)
}
if !payload.RewardAmount.SubaccountPercentage.IsNil() {
totalRewardPercentage = totalRewardPercentage.Add(payload.RewardAmount.SubaccountPercentage)
}

// check the sum of percentages, it should not be more than or equal to 1 (100%)
if totalRewardPercentage.GTE(sdk.OneDec()) {
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward percentage is equal or more than 1.0(100 percent), the provided value is %s", totalRewardPercentage)
}

campaign := types.NewCampaign(
msg.Creator, payload.Promoter, msg.Uid,
payload.StartTs, payload.EndTs,
Expand Down
9 changes: 5 additions & 4 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
}
}

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

Expand All @@ -94,17 +94,18 @@ func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward)
}

k.UpdateCampaignPool(ctx, campaign, factData.Receiver)

k.SetReward(ctx, types.NewReward(
msg.Uid, msg.Creator, factData.Receiver.MainAccountAddr,
msg.CampaignUid, campaign.RewardAmount,
msg.Uid, msg.Creator, factData.Receiver,
msg.CampaignUid,
factData.Common.SourceUID,
factData.Common.Meta,
))

k.SetRewardOfReceiverByPromoterAndCategory(ctx, promoter.UID, 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, factData.Receiver, unlockTS)
msg.EmitEvent(&ctx, msg.CampaignUid, msg.Uid, campaign.Promoter, factData.Receiver, unlockTS)

return &types.MsgGrantRewardResponse{}, nil
}
4 changes: 2 additions & 2 deletions x/reward/keeper/msg_server_reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ func TestMsgApplyBetBonus(t *testing.T) {
betBonusCampClaims["reward_type"] = types.RewardType_REWARD_TYPE_BET_DISCOUNT
betBonusCampClaims["reward_amount_type"] = types.RewardAmountType_REWARD_AMOUNT_TYPE_PERCENTAGE
betBonusCampClaims["reward_amount"] = types.RewardAmount{
MainAccountAmount: sdkmath.NewInt(10),
UnlockPeriod: 0,
MainAccountPercentage: sdk.NewDecWithPrec(10, 2),
UnlockPeriod: 0,
}
betBonusCampClaims["constraints"] = types.CampaignConstraints{
MaxBetAmount: sdkmath.NewInt(300),
Expand Down
10 changes: 5 additions & 5 deletions x/reward/types/messages_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func (msg *MsgGrantReward) ValidateBasic() error {

// EmitEvent emits the event for the message success.
func (msg *MsgGrantReward) EmitEvent(ctx *sdk.Context, campaignUID string,
rewardUID, promoterAddr string, rewardAmount RewardAmount, receiver Receiver, subAccUnlockTS uint64,
rewardUID, promoterAddr string, receiver Receiver, subAccUnlockTS uint64,
) {
mainAmount := sdkmath.ZeroInt()
if !rewardAmount.MainAccountAmount.IsNil() {
mainAmount = rewardAmount.MainAccountAmount
if !receiver.RewardAmount.MainAccountAmount.IsNil() {
mainAmount = receiver.RewardAmount.MainAccountAmount
}
subAmount := sdkmath.ZeroInt()
if !rewardAmount.SubaccountAmount.IsNil() {
subAmount = rewardAmount.SubaccountAmount
if !receiver.RewardAmount.SubaccountAmount.IsNil() {
subAmount = receiver.RewardAmount.SubaccountAmount
}

emitter := utils.NewEventEmitter(ctx, attributeValueCategory)
Expand Down
3 changes: 2 additions & 1 deletion x/reward/types/promoter.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions x/reward/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading