-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incentivization] add: query and tx cmds (#488)
* add: query and tx cmds * add: cli to module
- Loading branch information
1 parent
3413950
commit c9a13f8
Showing
3 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters