Skip to content

Commit

Permalink
Merge branch 'release/v1.6.1' into release/v1.7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Apr 23, 2024
2 parents 162db98 + 46252d2 commit a109ef6
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 134 deletions.
4 changes: 2 additions & 2 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: cosmos-proto
commit: 1935555c206d4afb9e94615dfd0fad31
digest: shake256:c74d91a3ac7ae07d579e90eee33abf9b29664047ac8816500cf22c081fec0d72d62c89ce0bebafc1f6fec7aa5315be72606717740ca95007248425102c365377
commit: 04467658e59e44bbb22fe568206e1f70
digest: shake256:73a640bd60e0c523b0f8237ff34eab67c45a38b64bbbde1d80224819d272dbf316ac183526bd245f994af6608b025f5130483d0133c5edd385531326b5990466
- remote: buf.build
owner: cosmos
repository: cosmos-sdk
Expand Down
18 changes: 16 additions & 2 deletions proto/sgenetwork/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 @@ -13,24 +13,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(sdkmath.ZeroInt()) {
if receiver.RewardAmount.SubaccountAmount.GT(sdkmath.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(sdkmath.ZeroInt()) {
if receiver.RewardAmount.MainAccountAmount.GT(sdkmath.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
52 changes: 37 additions & 15 deletions x/reward/types/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import (
)

type Receiver struct {
SubaccountAddr string
SubaccountAmount sdkmath.Int
UnlockPeriod uint64
MainAccountAddr string
MainAccountAmount sdkmath.Int
SubaccountAddr string
MainAccountAddr string
RewardAmount RewardAmount
}

// RewardFactoryKeepers holds the keeper objects usable by reward types methods.
Expand Down Expand Up @@ -59,18 +57,30 @@ func NewRewardFactoryData(receiver Receiver, common RewardPayloadCommon) RewardF
}
}

func newRewardAmount(mainAccountAmount, subaccountAmount sdkmath.Int,
mainAccountPercentage, subaccountPercentage sdk.Dec,
unlockPeriod uint64,
) RewardAmount {
return RewardAmount{
MainAccountAmount: mainAccountAmount,
SubaccountAmount: subaccountAmount,
MainAccountPercentage: mainAccountPercentage,
SubaccountPercentage: subaccountPercentage,
UnlockPeriod: unlockPeriod,
}
}

func NewReward(
uid, creator, receiver string,
uid, creator string, receiver Receiver,
campaignUID string,
rewardAmount *RewardAmount,
sourceUID, meta string,
) Reward {
return Reward{
UID: uid,
Creator: creator,
Receiver: receiver,
Receiver: receiver.MainAccountAddr,
CampaignUID: campaignUID,
RewardAmount: rewardAmount,
RewardAmount: &receiver.RewardAmount,
SourceUID: sourceUID,
Meta: sanitize.XSS(meta),
}
Expand Down Expand Up @@ -100,13 +110,21 @@ type IRewardFactory interface {
}

// NewReceiver creates receiver object.
func NewReceiver(saAddr, maAddr string, saAmount, maAmount sdkmath.Int, unlockPeriod uint64) Receiver {
func NewReceiver(maAddr, saAddr string,
maAmount, saAmount sdkmath.Int,
maPercentage, saPercentage sdk.Dec,
unlockPeriod uint64,
) Receiver {
return Receiver{
SubaccountAddr: saAddr,
SubaccountAmount: saAmount,
UnlockPeriod: unlockPeriod,
MainAccountAddr: maAddr,
MainAccountAmount: maAmount,
SubaccountAddr: saAddr,
MainAccountAddr: maAddr,
RewardAmount: newRewardAmount(
maAmount,
saAmount,
maPercentage,
saPercentage,
unlockPeriod,
),
}
}

Expand All @@ -117,3 +135,7 @@ func (ds Receiver) String() string {
}
return string(out)
}

func (ds Receiver) TotalAmount() sdkmath.Int {
return ds.RewardAmount.MainAccountAmount.Add(ds.RewardAmount.SubaccountAmount)
}
Loading

0 comments on commit a109ef6

Please sign in to comment.