Skip to content

Commit

Permalink
Merge pull request #379 from scorpioborn/feature/max-amount-bet-bonus
Browse files Browse the repository at this point in the history
Feature /  Campaign constraints and bet bonus
  • Loading branch information
3eyedraga authored Apr 5, 2024
2 parents b7d106e + 6e6b60f commit 2b3ec59
Show file tree
Hide file tree
Showing 13 changed files with 663 additions and 125 deletions.
12 changes: 12 additions & 0 deletions proto/sge/reward/campaign.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ message Campaign {

// cap_count is the maximum allowed grant for a certain account.
uint64 cap_count = 14;

// constraints is the constrains of a campaign.
CampaignConstraints constraints = 15;
}

// Pool tracks funds assigned and spent to/for a campaign.
Expand All @@ -79,4 +82,13 @@ message Pool {
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"spent\""
];
}

// CampaignConstraints contains campaign constraints and criteria.
message CampaignConstraints {
string max_bet_amount = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"max_bet_amount\""
];
}
4 changes: 4 additions & 0 deletions proto/sge/reward/ticket.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sgenetwork.sge.reward;

import "gogoproto/gogo.proto";
import "sge/type/kyc.proto";
import "sge/reward/campaign.proto";
import "sge/reward/reward.proto";
import "sge/reward/promoter.proto";

Expand Down Expand Up @@ -41,6 +42,9 @@ message CreateCampaignPayload {

// cap_count is the maximum allowed grant for a certain account.
uint64 cap_count = 11;

// constraints is the constrains of a campaign.
CampaignConstraints constraints = 12;
}

// UpdateCampaignPayload is the type for campaign update payload.
Expand Down
5 changes: 5 additions & 0 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam
return nil, sdkerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

if !k.IsPromoter(ctx, payload.Promoter) {
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "promoter with the address: %s not found", payload.Promoter)
}

if msg.Creator != payload.Promoter {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, payload.Promoter, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
Expand Down Expand Up @@ -62,6 +66,7 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam
payload.Meta,
types.NewPool(msg.TotalFunds),
payload.CapCount,
payload.Constraints,
)

rewardFactory, err := campaign.GetRewardsFactory()
Expand Down
63 changes: 49 additions & 14 deletions x/reward/keeper/msg_server_campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,29 @@ import (
// Prevent strconv unused error
var _ = strconv.IntSize

func setTestPromoter(k *keeper.Keeper, ctx sdk.Context, promoterAddr string) {
k.SetPromoter(ctx, types.Promoter{
Creator: promoterAddr,
UID: promoterUID,
Addresses: []string{
promoterAddr,
},
})
k.SetPromoterByAddress(ctx, types.PromoterByAddress{
PromoterUID: promoterUID,
Address: promoterAddr,
})
}

func TestCampaignMsgServerCreate(t *testing.T) {
k, ctx := setupKeeper(t)
srv := keeper.NewMsgServerImpl(*k)
ctx = ctx.WithBlockTime(time.Now())
wctx := sdk.WrapSDKContext(ctx)
creator := simapp.TestParamUsers["user1"].Address.String()

setTestPromoter(k, ctx, creator)

for i := 0; i < 5; i++ {
ticketClaim := jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
Expand All @@ -42,9 +59,11 @@ func TestCampaignMsgServerCreate(t *testing.T) {
SubaccountAmount: sdkmath.NewInt(100),
UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
"is_active": true,
"meta": "sample campaign",
"claims_per_category": 1,
"is_active": true,
"meta": "sample campaign",
"constraints": &types.CampaignConstraints{
MaxBetAmount: sdkmath.NewInt(300),
},
}
ticket, err := simapp.CreateJwtTicket(ticketClaim)
require.Nil(t, err)
Expand Down Expand Up @@ -74,6 +93,8 @@ func TestCampaignMsgServerCreateWithAuthoriation(t *testing.T) {
creator := simapp.TestParamUsers["user2"]
promoter := simapp.TestParamUsers["user1"]

setTestPromoter(k, ctx, promoter.Address.String())

grantAmount := sdkmath.NewInt(1000000)

expTime := time.Now().Add(5 * time.Minute)
Expand Down Expand Up @@ -108,9 +129,11 @@ func TestCampaignMsgServerCreateWithAuthoriation(t *testing.T) {
SubaccountAmount: sdkmath.NewInt(100),
UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
"is_active": true,
"meta": "sample campaign",
"claims_per_category": 1,
"is_active": true,
"meta": "sample campaign",
"constraints": &types.CampaignConstraints{
MaxBetAmount: sdkmath.NewInt(300),
},
}
ticket, err := simapp.CreateJwtTicket(ticketClaim)
require.Nil(t, err)
Expand Down Expand Up @@ -143,10 +166,14 @@ func TestCampaignMsgServerUnAuthorizedCreate(t *testing.T) {
ctx = ctx.WithBlockTime(time.Now())
wctx := sdk.WrapSDKContext(ctx)
creator := simapp.TestParamUsers["user1"].Address.String()
promoter := sample.AccAddress()

setTestPromoter(k, ctx, promoter)

ticketClaim := jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"promoter": sample.AccAddress(),
"promoter": promoter,
}
ticket, err := simapp.CreateJwtTicket(ticketClaim)
require.Nil(t, err)
Expand Down Expand Up @@ -199,6 +226,8 @@ func TestCampaignMsgServerUpdate(t *testing.T) {

creator := simapp.TestParamUsers["user1"].Address.String()

setTestPromoter(k, ctx, creator)

ticketClaim := jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
Expand All @@ -212,9 +241,11 @@ func TestCampaignMsgServerUpdate(t *testing.T) {
SubaccountAmount: sdkmath.NewInt(100),
UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
"is_active": true,
"meta": "sample campaign",
"claims_per_category": 1,
"is_active": true,
"meta": "sample campaign",
"constraints": &types.CampaignConstraints{
MaxBetAmount: sdkmath.NewInt(300),
},
}
ticket, err := simapp.CreateJwtTicket(ticketClaim)
require.Nil(t, err)
Expand Down Expand Up @@ -256,7 +287,7 @@ func TestCampaignMsgServerUpdate(t *testing.T) {
}
}

func TestCampaignMsgServerUpdateWithAuthoriation(t *testing.T) {
func TestCampaignMsgServerUpdateWithAuthorization(t *testing.T) {
tApp, k, ctx := setupKeeperAndApp(t)
srv := keeper.NewMsgServerImpl(*k)
ctx = ctx.WithBlockTime(time.Now())
Expand All @@ -267,6 +298,8 @@ func TestCampaignMsgServerUpdateWithAuthoriation(t *testing.T) {
creator := simapp.TestParamUsers["user2"]
promoter := simapp.TestParamUsers["user1"]

setTestPromoter(k, ctx, promoter.Address.String())

grantAmount := sdkmath.NewInt(1000000)

expTime := time.Now().Add(5 * time.Minute)
Expand Down Expand Up @@ -319,9 +352,11 @@ func TestCampaignMsgServerUpdateWithAuthoriation(t *testing.T) {
SubaccountAmount: sdkmath.NewInt(100),
UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
"is_active": true,
"meta": "sample campaign",
"claims_per_category": 1,
"is_active": true,
"meta": "sample campaign",
"constraints": &types.CampaignConstraints{
MaxBetAmount: sdkmath.NewInt(300),
},
}
ticket, err := simapp.CreateJwtTicket(ticketClaim)
require.Nil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions x/reward/keeper/msg_server_promoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (k msgServer) CreatePromoter(goCtx context.Context, msg *types.MsgCreatePro
UID: payload.UID,
Conf: payload.Conf,
})
k.SetPromoterByAddress(ctx, types.PromoterByAddress{
PromoterUID: payload.UID,
Address: msg.Creator,
})

msg.EmitEvent(&ctx, payload.UID, payload.Conf)

Expand Down
Loading

0 comments on commit 2b3ec59

Please sign in to comment.