Skip to content

Commit

Permalink
[incentivization] add: query and tx cmds (#488)
Browse files Browse the repository at this point in the history
* add: query and tx cmds
* add: cli to module
  • Loading branch information
testinginprod authored May 25, 2022
1 parent 3413950 commit c9a13f8
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 4 deletions.
99 changes: 99 additions & 0 deletions x/incentivization/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/spf13/cobra"

"github.com/NibiruChain/nibiru/x/incentivization/types"
)

func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
GetQueryProgramCmd(),
GetQueryProgramsCmd(),
)

return cmd
}

func GetQueryProgramsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "programs [offset] [limit]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

offset, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

limit, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

reverse, err := cmd.Flags().GetBool("reverse")
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.IncentivizationPrograms(cmd.Context(), &types.QueryIncentivizationProgramsRequest{Pagination: &query.PageRequest{
Offset: offset,
Limit: limit,
Reverse: reverse,
}})

if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

cmd.Flags().Bool("reverse", false, "reverse iteration")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func GetQueryProgramCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "program [id]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

id, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.IncentivizationProgram(cmd.Context(), &types.QueryIncentivizationProgramRequest{Id: id})
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
140 changes: 140 additions & 0 deletions x/incentivization/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package cli

import (
"fmt"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/NibiruChain/nibiru/x/incentivization/types"
)

func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
GetCreateIncentivizationProgramCmd(),
GetFundIncentivizationProgramCmd(),
)

return cmd
}

func GetFundIncentivizationProgramCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "fund [program-id] [coins]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

programID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

coins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}

msg := &types.MsgFundIncentivizationProgram{
Sender: clientCtx.GetFromAddress().String(),
Id: programID,
Funds: coins,
}

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func GetCreateIncentivizationProgramCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [denom] [min lockup duration] [number of epochs]",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

denom := args[0]

duration, err := time.ParseDuration(args[1])
if err != nil {
return err
}

epochs, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return err
}

var t *time.Time
startTimeStr, err := cmd.Flags().GetString("start-time")
if err != nil {
return err
}

if startTimeStr != "" {
startTime, err := time.Parse(time.RFC3339, startTimeStr)
if err != nil {
return err
}
t = &startTime
}

var coins sdk.Coins
coinsStr, err := cmd.Flags().GetString("initial-funds")
if err != nil {
return err
}

if coinsStr != "" {
coins, err = sdk.ParseCoinsNormalized(coinsStr)
if err != nil {
return err
}
}

msg := &types.MsgCreateIncentivizationProgram{
Sender: clientCtx.GetFromAddress().String(),
LpDenom: denom,
MinLockupDuration: &duration,
StartTime: t,
Epochs: epochs,
InitialFunds: coins,
}
if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String("start-time", "", "start time (RFC3339) of the incentivization program [default: block time]")
cmd.Flags().String("initial-funds", "", "initial funds to deploy on the incentivization program [default: 0]")

return cmd
}
8 changes: 4 additions & 4 deletions x/incentivization/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"

"github.com/NibiruChain/nibiru/x/incentivization/client/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -83,14 +85,12 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r

// GetTxCmd returns the capability module's root tx command.
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
// return cli.GetTxCmd()
return cli.GetTxCmd()
}

// GetQueryCmd returns the capability module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
// return cli.GetQueryCmd(types.StoreKey)
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit c9a13f8

Please sign in to comment.