Skip to content

Commit

Permalink
feat(gov,cli): Create AddGovPropFlagsToCmd and ReadGovPropFlags. (bac…
Browse files Browse the repository at this point in the history
…kport #14718) (#15471)

Co-authored-by: Daniel Wedul <github@wedul.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored Mar 20, 2023
1 parent 4151a58 commit 35e4765
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (x/gov,cli) [#14718](https://github.com/cosmos/cosmos-sdk/pull/14718) Added `AddGovPropFlagsToCmd` and `ReadGovPropFlags` functions.
* (cli) [#14655](https://github.com/cosmos/cosmos-sdk/pull/14655) Add a new command to list supported algos.
* (x/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user defined initial block height.
* (x/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user-defined initial block height.

### Improvements

Expand Down
12 changes: 6 additions & 6 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

// Proposal flags
const (
// Deprecated: only used for v1beta1 legacy proposals.
FlagTitle = "title"
// Deprecated: only used for v1beta1 legacy proposals.
FlagDescription = "description"
Expand All @@ -30,7 +29,8 @@ const (
flagVoter = "voter"
flagDepositor = "depositor"
flagStatus = "status"
flagMetadata = "metadata"
FlagMetadata = "metadata"
FlagSummary = "summary"
// Deprecated: only used for v1beta1 legacy proposals.
FlagProposal = "proposal"
)
Expand Down Expand Up @@ -303,7 +303,7 @@ $ %s tx gov vote 1 yes --from mykey
return err
}

metadata, err := cmd.Flags().GetString(flagMetadata)
metadata, err := cmd.Flags().GetString(FlagMetadata)
if err != nil {
return err
}
Expand All @@ -315,7 +315,7 @@ $ %s tx gov vote 1 yes --from mykey
},
}

cmd.Flags().String(flagMetadata, "", "Specify metadata of the vote")
cmd.Flags().String(FlagMetadata, "", "Specify metadata of the vote")
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down Expand Up @@ -358,7 +358,7 @@ $ %s tx gov weighted-vote 1 yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05 --from
return err
}

metadata, err := cmd.Flags().GetString(flagMetadata)
metadata, err := cmd.Flags().GetString(FlagMetadata)
if err != nil {
return err
}
Expand All @@ -369,7 +369,7 @@ $ %s tx gov weighted-vote 1 yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05 --from
},
}

cmd.Flags().String(flagMetadata, "", "Specify metadata of the weighted vote")
cmd.Flags().String(FlagMetadata, "", "Specify metadata of the weighted vote")
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
51 changes: 51 additions & 0 deletions x/gov/client/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

type legacyProposal struct {
Expand Down Expand Up @@ -118,3 +121,51 @@ func parseSubmitProposal(cdc codec.Codec, path string) ([]sdk.Msg, string, strin

return msgs, proposal.Metadata, proposal.Title, proposal.Summary, deposit, nil
}

// AddGovPropFlagsToCmd adds flags for defining MsgSubmitProposal fields.
//
// See also ReadGovPropFlags.
func AddGovPropFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().String(FlagDeposit, "", "The deposit to include with the governance proposal")
cmd.Flags().String(FlagMetadata, "", "The metadata to include with the governance proposal")
cmd.Flags().String(FlagTitle, "", "The title to put on the governance proposal")
cmd.Flags().String(FlagSummary, "", "The summary to include with the governance proposal")
}

// ReadGovPropFlags parses a MsgSubmitProposal from the provided context and flags.
// Setting the messages is up to the caller.
//
// See also AddGovPropFlagsToCmd.
func ReadGovPropFlags(clientCtx client.Context, flagSet *pflag.FlagSet) (*govv1.MsgSubmitProposal, error) {
rv := &govv1.MsgSubmitProposal{}

deposit, err := flagSet.GetString(FlagDeposit)
if err != nil {
return nil, fmt.Errorf("could not read deposit: %w", err)
}
if len(deposit) > 0 {
rv.InitialDeposit, err = sdk.ParseCoinsNormalized(deposit)
if err != nil {
return nil, fmt.Errorf("invalid deposit: %w", err)
}
}

rv.Metadata, err = flagSet.GetString(FlagMetadata)
if err != nil {
return nil, fmt.Errorf("could not read metadata: %w", err)
}

rv.Title, err = flagSet.GetString(FlagTitle)
if err != nil {
return nil, fmt.Errorf("could not read title: %w", err)
}

rv.Summary, err = flagSet.GetString(FlagSummary)
if err != nil {
return nil, fmt.Errorf("could not read summary: %w", err)
}

rv.Proposer = clientCtx.GetFromAddress().String()

return rv, nil
}
Loading

0 comments on commit 35e4765

Please sign in to comment.