Skip to content

Commit

Permalink
Add tx and proposal cli support (#17)
Browse files Browse the repository at this point in the history
* Add tx and proposal cli support

* Fix comments
  • Loading branch information
pinosu authored May 31, 2023
1 parent 02cef1a commit 9c4f0f9
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ proto-check-breaking:
@$(DOCKER_BUF) breaking --against $(HTTPS_GIT)#branch=main


.PHONY: all install install-debug \
go-mod-cache draw-deps clean build \
test test-all test-build test-cover test-unit test-race \
.PHONY: all install \
build test test-all \
proto-all proto-format proto-swagger-gen proto-lint proto-check-breaking
8 changes: 4 additions & 4 deletions x/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.15.0 // indirect
github.com/rakyll/statik v0.1.7 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/stretchr/testify v1.8.2
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44
google.golang.org/grpc v1.54.0
Expand All @@ -34,6 +34,8 @@ require (
require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/cometbft/cometbft v0.37.1
github.com/cometbft/cometbft-db v0.7.0
)

require (
Expand Down Expand Up @@ -61,8 +63,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/cometbft/cometbft v0.37.1 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
Expand Down
142 changes: 142 additions & 0 deletions x/meshsecurity/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cli

import (
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"
"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/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)

// DefaultGovAuthority is set to the gov module address.
// Extension point for chains to overwrite the default
var DefaultGovAuthority = sdk.AccAddress(address.Module("gov"))

func SubmitProposalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "submit-proposal",
Short: "Submit a mesh security proposal.",
SilenceUsage: true,
}
cmd.AddCommand(
ProposalSetVirtualStakingMaxCapCmd(),
)
return cmd
}

func ProposalSetVirtualStakingMaxCapCmd() *cobra.Command {
bech32Prefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
cmd := &cobra.Command{
Use: "set-virtual-staking-max-cap [contract_addr_bech32] [max_cap] --title [text] --summary [text] --authority [address]",
Short: "Submit a set virtual staking max cap proposal",
Args: cobra.ExactArgs(2),
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a proposal to set virtual staking maximum cap limit to the given contract.
Example:
$ %s tx meshsecurity submit-proposal set-virtual-staking-max-cap %s1l94ptufswr6v7qntax4m7nvn3jgf6k4gn2rknq 100stake --title "a title" --summary "a summary" --authority %s
`, version.AppName, bech32Prefix, DefaultGovAuthority.String())),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, proposalTitle, summary, metadata, deposit, err := getProposalInfo(cmd)
if err != nil {
return err
}
authority, err := cmd.Flags().GetString(flagAuthority)
if err != nil {
return fmt.Errorf("authority: %s", err)
}

if len(authority) == 0 {
return errors.New("authority address is required")
}

src, err := parseSetVirtualStakingMaxCapArgs(args, authority)
if err != nil {
return err
}

proposalMsg, err := v1.NewMsgSubmitProposal([]sdk.Msg{&src}, deposit, clientCtx.GetFromAddress().String(), metadata, proposalTitle, summary)
if err != nil {
return err
}
if err = proposalMsg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), proposalMsg)
},
SilenceUsage: true,
}

// proposal flags
addCommonProposalFlags(cmd)
return cmd
}

func parseSetVirtualStakingMaxCapArgs(args []string, authority string) (types.MsgSetVirtualStakingMaxCap, error) {
maxCap, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return types.MsgSetVirtualStakingMaxCap{}, errorsmod.Wrap(err, "max cap")
}

msg := types.MsgSetVirtualStakingMaxCap{
Authority: authority,
Contract: args[0],
MaxCap: maxCap,
}
return msg, nil
}

func addCommonProposalFlags(cmd *cobra.Command) {
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(cli.FlagTitle, "", "Title of proposal")
cmd.Flags().String(cli.FlagSummary, "", "Summary of proposal")
cmd.Flags().String(cli.FlagMetadata, "", "Metadata of proposal")
cmd.Flags().String(cli.FlagDeposit, "", "Deposit of proposal")
cmd.Flags().String(flagAuthority, DefaultGovAuthority.String(), "The address of the governance account. Default is the sdk gov module account")
}

func getProposalInfo(cmd *cobra.Command) (client.Context, string, string, string, sdk.Coins, error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return client.Context{}, "", "", "", nil, err
}

proposalTitle, err := cmd.Flags().GetString(cli.FlagTitle)
if err != nil {
return clientCtx, proposalTitle, "", "", nil, err
}

summary, err := cmd.Flags().GetString(cli.FlagSummary)
if err != nil {
return client.Context{}, proposalTitle, summary, "", nil, err
}

metadata, err := cmd.Flags().GetString(cli.FlagMetadata)
if err != nil {
return client.Context{}, proposalTitle, summary, metadata, nil, err
}

depositArg, err := cmd.Flags().GetString(cli.FlagDeposit)
if err != nil {
return client.Context{}, proposalTitle, summary, metadata, nil, err
}

deposit, err := sdk.ParseCoinsNormalized(depositArg)
if err != nil {
return client.Context{}, proposalTitle, summary, metadata, deposit, err
}

return clientCtx, proposalTitle, summary, metadata, deposit, nil
}
26 changes: 26 additions & 0 deletions x/meshsecurity/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)

const flagAuthority = "authority"

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Mesh security transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
SilenceUsage: true,
}
txCmd.AddCommand(
SubmitProposalCmd(),
)
return txCmd
}
3 changes: 2 additions & 1 deletion x/meshsecurity/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/client/cli"
"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/keeper"
"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func (b AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, _ client.TxEn

// GetTxCmd returns the root tx command for the mesh-security module.
func (b AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
return cli.GetTxCmd()
}

// GetQueryCmd returns no root query command for the mesh-security module.
Expand Down

0 comments on commit 9c4f0f9

Please sign in to comment.