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

Audit/ Rewards handler and codec fixs #312

Merged
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
19 changes: 11 additions & 8 deletions x/reward/client/cli/tx_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ func CmdCreateCampaign() *cobra.Command {
cmd := &cobra.Command{
Use: "create-campaign [uid] [totalfunds] [ticket]",
Short: "Create a new campaign",
Long: "Creating a new campaign with certain amount of funds and the ticket",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
argUID := args[0]

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

argTotalFundsCosmosInt, ok := sdkmath.NewIntFromString(args[2])
argTotalFundsCosmosInt, ok := sdkmath.NewIntFromString(args[1])
if !ok {
return types.ErrInvalidFunds
}

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

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -55,19 +56,20 @@ func CmdUpdateCampaign() *cobra.Command {
cmd := &cobra.Command{
Use: "update-campaign [uid] [topupfunds] [ticket]",
Short: "Update a campaign",
Long: "Updating a new campaign with certain amount of funds and the ticket",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
argUID := args[0]

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

argTopupFundsCosmosInt, ok := sdkmath.NewIntFromString(args[2])
argTopupFundsCosmosInt, ok := sdkmath.NewIntFromString(args[1])
if !ok {
return types.ErrInvalidFunds
}

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

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -95,6 +97,7 @@ func CmdWithdrawFunds() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-funds [uid] [ticket]",
Short: "Withdraw funds from a campaign",
Long: "Withdrawal of the funds from a certain campaign",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
Expand Down
40 changes: 40 additions & 0 deletions x/reward/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package reward

import (
"fmt"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/sge-network/sge/x/reward/keeper"
"github.com/sge-network/sge/x/reward/types"
)

// NewHandler returns sdk.handler instance with configured message handler function
func NewHandler(k keeper.Keeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(k)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgCreateCampaign:
res, err := msgServer.CreateCampaign(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateCampaign:
res, err := msgServer.UpdateCampaign(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgWithdrawFunds:
res, err := msgServer.WithdrawFunds(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgGrantReward:
res, err := msgServer.GrantReward(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrtypes.ErrUnknownRequest, errMsg)
}
}
}
4 changes: 4 additions & 0 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam
return nil, sdkerrors.Wrapf(sdkerrtypes.ErrKeyNotFound, "campaign with the id: %s does not exist", msg.Uid)
}

if !campaign.IsActive {
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "inactive campaign can not be updated")
}

// Checks if the msg creator is the same as the current owner
if msg.Creator != campaign.Promoter {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, campaign.Promoter, msg,
Expand Down
2 changes: 1 addition & 1 deletion x/reward/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewAppModule(
}

// Deprecated: use RegisterServices
func (am AppModule) Route() sdk.Route { return sdk.Route{} }
func (am AppModule) Route() sdk.Route { return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper)) }

// Deprecated: use RegisterServices
func (AppModule) QuerierRoute() string { return types.RouterKey }
Expand Down
8 changes: 8 additions & 0 deletions x/reward/types/campaign_authorizaton.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (a CreateCampaignAuthorization) ValidateBasic() error {
return sdkerrtypes.ErrInvalidCoins.Wrap("spend limit cannot be less than or equal to zero")
}

if a.SpendLimit.LT(minCampaignFunds) {
return sdkerrtypes.ErrInvalidCoins.Wrapf("spend limit cannot be less than %s", minCampaignFunds)
}

return nil
}

Expand Down Expand Up @@ -109,5 +113,9 @@ func (a UpdateCampaignAuthorization) ValidateBasic() error {
return sdkerrtypes.ErrInvalidCoins.Wrap("spend limit cannot be less than or equal to zero")
}

if a.SpendLimit.LT(minCampaignFunds) {
return sdkerrtypes.ErrInvalidCoins.Wrapf("spend limit cannot be less than %s", minCampaignFunds)
}

return nil
}
2 changes: 2 additions & 0 deletions x/reward/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgCreateCampaign{}, "reward/CreateCampaign")
legacy.RegisterAminoMsg(cdc, &MsgUpdateCampaign{}, "reward/UpdateCampaign")
legacy.RegisterAminoMsg(cdc, &MsgWithdrawFunds{}, "reward/WithdrawFunds")
legacy.RegisterAminoMsg(cdc, &MsgGrantReward{}, "reward/GrantReward")
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateCampaign{},
&MsgUpdateCampaign{},
&MsgWithdrawFunds{},
&MsgGrantReward{},
)

Expand Down
6 changes: 6 additions & 0 deletions x/reward/types/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

import sdkmath "cosmossdk.io/math"

// minCampaignFunds is the minimum campaign funds allowed grant.
var minCampaignFunds = sdkmath.NewInt(100)
Loading