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/campaigntx #286

Merged
merged 3 commits into from
Nov 7, 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
18 changes: 8 additions & 10 deletions proto/sge/reward/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ service Query {
option (google.api.http).get = "/sge-network/sge/reward/rewards/{address}";
}

// Queries list of all Reward items by user address and reward category
// Queries list of all Reward items by user address and reward type
// endpoint.
rpc RewardsByAddressAndCategory(QueryRewardsByAddressAndCategoryRequest)
returns (QueryRewardsByAddressAndCategoryResponse) {
Expand All @@ -57,7 +57,7 @@ service Query {
rpc RewardsByCampaign(QueryRewardsByCampaignRequest)
returns (QueryRewardsByCampaignResponse) {
option (google.api.http).get =
"/sge-network/sge/reward/rewards/campaign/{campaign}";
"/sge-network/sge/reward/rewards/campaign/{campaign_uid}";
}
}
// QueryParamsRequest is request type for the Query/Params RPC method.
Expand All @@ -71,9 +71,7 @@ message QueryParamsResponse {
}

// QueryCampaignRequest is request body of the query campaign endpoint.
message QueryCampaignRequest {
string uid = 1;
}
message QueryCampaignRequest { string uid = 1; }

// QueryCampaignRequest is response body of the query campaign endpoint.
message QueryCampaignResponse {
Expand Down Expand Up @@ -126,15 +124,15 @@ message QueryRewardsByAddressResponse {
}

// QueryRewardsByAddressAndCategoryRequest is request body for the query all
// rewards by address and category endpoint.
// rewards by address and type endpoint.
message QueryRewardsByAddressAndCategoryRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string address = 2;
string category = 3;
RewardCategory category = 3;
}

// QueryRewardsByAddressAndCategoryResponse is response body of the query all
// rewards by address and category endpoint.
// QueryRewardsByAddressAndTypeResponse is response body of the query all
// rewards by address and type endpoint.
message QueryRewardsByAddressAndCategoryResponse {
repeated Reward rewards = 1 [ (gogoproto.nullable) = false ];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
Expand All @@ -144,7 +142,7 @@ message QueryRewardsByAddressAndCategoryResponse {
// campaign endpoint.
message QueryRewardsByCampaignRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string campaign = 2;
string campaign_uid = 2;
}

// QueryRewardsByCampaignResponse is response body of the query all rewards by
Expand Down
43 changes: 4 additions & 39 deletions proto/sge/reward/reward.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,26 @@ message Reward {
json_name = "campaign_uid"
];

// reward_type is the type of the reward.
RewardType reward_type = 5 [
(gogoproto.customname) = "RewardType",
(gogoproto.jsontag) = "reward_type",
json_name = "reward_type"
];

RewardCategory reward_category = 6 [
(gogoproto.customname) = "RewardCategory",
(gogoproto.jsontag) = "reward_category",
json_name = "reward_category"
];

// reward_amount is the amount of the reward.
RewardAmount reward_amount = 7 [
(gogoproto.customname) = "RewardAmount",
(gogoproto.jsontag) = "reward_amount",
json_name = "reward_amount"
];

// source is the source of the reward.
// It is used to identify the source of the reward.
// For example, the source of a referral signup is Type - referral.
string source = 8 [
(gogoproto.customname) = "Source",
(gogoproto.jsontag) = "source",
json_name = "source"
];

// source_code is the source code of the reward.
// It is used to identify the source of the reward.
// For example, the source code of a referral signup is referral code of
// referer.
string source_code = 9 [
(gogoproto.customname) = "SourceCode",
(gogoproto.jsontag) = "source_code",
json_name = "source_code"
];

// source_uid is the address of the source.
// It is used to identify the source of the reward.
// For example, the source uid of a referral signup reward is the address of
// the referer.
string source_uid = 10 [
string source_uid = 8 [
(gogoproto.customname) = "SourceUID",
(gogoproto.jsontag) = "source_uid",
json_name = "source_uid"
];

// created_at is the time when the reward is created.
uint64 created_at = 11 [
(gogoproto.customname) = "CreatedAt",
(gogoproto.jsontag) = "created_at",
json_name = "created_at"
];
// meta is the metadata of the campaign.
// It is a stringified base64 encoded json.
string meta = 12;
}

message RewardAmount {
Expand Down
42 changes: 20 additions & 22 deletions proto/sge/reward/ticket.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,32 @@ message UpdateCampaignPayload {
bool is_active = 3;
}

message RewardPayloadCommon {
// receiver is the address of the account that receives the reward.
string receiver = 1;
// WithdrawFundsPayload is the type for campaign withdraw funds payload.
message WithdrawFundsPayload {
// promoter is the address of campaign promoter.
// Funds would be transferred to this account.
string promoter = 1;

// source is the source of the reward.
// It is used to identify the source of the reward.
// For example, the source of a referral signup is Type - referral.
string source = 2 [
(gogoproto.customname) = "Source",
(gogoproto.jsontag) = "source",
json_name = "source"
// amount is the funds that needs to be withdrawn.
string amount = 8 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"amount\""
];

// source_code is the source code of the reward.
// It is used to identify the source of the reward.
// For example, the source code of a referral signup is referral code of
// referer.
string source_code = 3 [
(gogoproto.customname) = "SourceCode",
(gogoproto.jsontag) = "source_code",
json_name = "source_code"
];
// is_active is the flag to check if the campaign is active or not.
bool is_active = 9;
}

message RewardPayloadCommon {
// receiver is the address of the account that receives the reward.
string receiver = 1;

// source_uid is the address of the source.
// It is used to identify the source of the reward.
// For example, the source uid of a referral signup reward is the address of
// the referer.
string source_uid = 4 [
string source_uid = 2 [
(gogoproto.customname) = "SourceUID",
(gogoproto.jsontag) = "source_uid",
json_name = "source_uid"
Expand All @@ -96,5 +94,5 @@ message RewardPayloadCommon {
// GrantSignupRewardPayload is the type for signup reward grant payload.
message GrantSignupRewardPayload {
// common is the common properties of a reward
RewardPayloadCommon common = 1 [ (gogoproto.nullable) = false ];
}
RewardPayloadCommon common = 2 [ (gogoproto.nullable) = false ];
}
20 changes: 19 additions & 1 deletion proto/sge/reward/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ service Msg {
rpc CreateCampaign(MsgCreateCampaign) returns (MsgCreateCampaignResponse);
// UpdateCampaign is a method to update campaign
rpc UpdateCampaign(MsgUpdateCampaign) returns (MsgUpdateCampaignResponse);
// GrantSignupReward is method to allocate signup rewards
// WithdrawCampaignFunds is method to withdraw funds from the campaign
rpc WithdrawFunds(MsgWithdrawFunds) returns (MsgWithdrawFundsResponse);
// GrantReward is method to allocate rewards
rpc GrantReward(MsgGrantReward) returns (MsgGrantRewardResponse);


}

// MsgCreateCampaign is msg to create a reward campaign
Expand Down Expand Up @@ -62,3 +66,17 @@ message MsgGrantReward {

// MsgGrantSignupRewardResponse execute signup reward message response type.
message MsgGrantRewardResponse {}

// MsgWithdrawFunds is withdraw funds message type.
message MsgWithdrawFunds {
// creator is the address of creator account.
string creator = 1;
// uid is the unique identifier of the reward campaign.
string uid = 2;
// ticket is the payload data.
string ticket = 3;
}

// MsgWithdrawFundsResponse withdraw funds message response type.
message MsgWithdrawFundsResponse {}

17 changes: 11 additions & 6 deletions x/reward/client/cli/query_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"

"github.com/spf13/cast"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -129,8 +130,8 @@ func CmdGetRewardsByCampaign() *cobra.Command {
}

params := &types.QueryRewardsByCampaignRequest{
Campaign: argCampaignId,
Pagination: pageReq,
CampaignUid: argCampaignId,
Pagination: pageReq,
}

res, err := queryClient.RewardsByCampaign(context.Background(), params)
Expand Down Expand Up @@ -158,20 +159,24 @@ func CmdGetRewardByUserAndCategory() *cobra.Command {
queryClient := types.NewQueryClient(clientCtx)

argUser := args[0]
argCategory := args[1]
argRewType := args[1]
argRewTypeint32, err := cast.ToInt32E(argRewType)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

params := &types.QueryRewardsByAddressAndCategoryRequest{
params := &types.QueryRewardsByAddressAndTypeRequest{
Address: argUser,
Category: argCategory,
RewardType: types.RewardType(argRewTypeint32),
Pagination: pageReq,
}

res, err := queryClient.RewardsByAddressAndCategory(context.Background(), params)
res, err := queryClient.RewardsByAddressAndType(context.Background(), params)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/reward/keeper/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (k Keeper) GetAllCampaign(ctx sdk.Context) (list []types.Campaign) {
return
}

func (k Keeper) UpdateCampaignPool(ctx sdk.Context, campaign types.Campaign, allocation types.Allocation) {
totalAmount := allocation.MainAcc.Amount.Add(allocation.SubAcc.Amount)
func (k Keeper) UpdateCampaignPool(ctx sdk.Context, campaign types.Campaign, receiver types.Receiver) {
totalAmount := receiver.Amount.Add(receiver.Amount)
campaign.Pool.Spent = campaign.Pool.Spent.Add(totalAmount)

k.SetCampaign(ctx, campaign)
Expand Down
9 changes: 4 additions & 5 deletions x/reward/keeper/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ func createNCampaign(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Camp
items[i].Promoter = sample.AccAddress()
items[i].StartTS = uint64(time.Now().Unix())
items[i].EndTS = uint64(time.Now().Add(5 * time.Minute).Unix())
items[i].RewardCategory = types.RewardCategory_REWARD_CATEGORY_SIGNUP
items[i].RewardType = types.RewardType_REWARD_TYPE_REFERRAL
items[i].RewardDefs = types.Definitions{{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
RecAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_SUB,
Amount: sdkmath.NewInt(100),
}}
items[i].RewardAmountType = types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED
items[i].IsActive = true
items[i].Meta = "campaign " + items[i].UID
items[i].Pool = types.Pool{Spent: sdk.ZeroInt(), Total: sdkmath.NewInt(100)}

keeper.SetCampaign(ctx, items[i])
Expand Down
32 changes: 16 additions & 16 deletions x/reward/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import (
)

// DistributeRewards distributes the rewards according to the input distribution list.
func (k Keeper) DistributeRewards(ctx sdk.Context, funderAddr string, allocation types.Allocation) error {
if allocation.MainAcc.Amount.GT(sdkmath.ZeroInt()) {
if err := k.modFunder.Refund(
types.RewardPoolFunder{}, ctx,
sdk.MustAccAddressFromBech32(allocation.MainAcc.Addr),
allocation.MainAcc.Amount,
); err != nil {
return err
}
}

if allocation.SubAcc.Amount.GT(sdkmath.ZeroInt()) {
if _, err := k.subaccountKeeper.TopUp(ctx, funderAddr, allocation.SubAcc.Addr,
func (k Keeper) DistributeRewards(ctx sdk.Context, funderAddr string, isSubAccount bool, receiver types.Receiver) error {
if isSubAccount {
if _, err := k.subaccountKeeper.TopUp(ctx, funderAddr, receiver.Addr,
[]subaccounttypes.LockedBalance{
{
UnlockTS: allocation.SubAcc.UnlockTS,
Amount: allocation.SubAcc.Amount,
UnlockTS: receiver.UnlockTS,
Amount: receiver.Amount,
},
}); err != nil {
return sdkerrors.Wrapf(types.ErrSubAccRewardTopUp, "subaccount address %s, %s", allocation.SubAcc.Addr, err)
return sdkerrors.Wrapf(types.ErrSubAccRewardTopUp, "subaccount address %s, %s", receiver.Addr, err)
}
} else {
if receiver.Amount.GT(sdkmath.ZeroInt()) {
if err := k.modFunder.Refund(
types.RewardPoolFunder{}, ctx,
sdk.MustAccAddressFromBech32(receiver.Addr),
receiver.Amount,
); err != nil {
return err
}
}
}

Expand Down
Loading
Loading