/
tx.go
127 lines (103 loc) · 3.11 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cli
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govTypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/certikfoundation/shentu/v2/x/gov/client/utils"
)
// Proposal flags
const (
flagVoter = "voter"
flagDepositor = "depositor"
flagStatus = "status"
)
// NewTxCmd returns the transaction commands for gov module.
func NewTxCmd(pcmds []*cobra.Command) *cobra.Command {
govTxCmd := &cobra.Command{
Use: govTypes.ModuleName,
Short: "Governance transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmdSubmitProp := cli.NewCmdSubmitProposal()
cmdSubmitProp.Long = strings.TrimSpace(
fmt.Sprintf(`Submit a proposal along with an initial deposit.
Proposal title, description, type and deposit can be given directly or through a proposal JSON file.
Example:
$ %[1]s tx gov submit-proposal --proposal="path/to/proposal.json" --from mykey
Where proposal.json contains:
{
"title": "Test Proposal",
"description": "My awesome proposal",
"type": "Text",
"deposit": "10ctk"
}
Which is equivalent to:
$ %[1]s tx gov submit-proposal --title="Test Proposal" --description="My awesome proposal" --type="Text" --deposit="10ctk" --from mykey
`,
version.AppName,
),
)
for _, pcmd := range pcmds {
flags.AddTxFlagsToCmd(pcmd)
cmdSubmitProp.AddCommand(pcmd)
}
govTxCmd.AddCommand(
cli.NewCmdDeposit(),
NewCmdVote(),
cmdSubmitProp,
)
return govTxCmd
}
// NewCmdVote implements creating a new vote command.
func NewCmdVote() *cobra.Command {
cmd := &cobra.Command{
Use: "vote [proposal-id] [option]",
Args: cobra.ExactArgs(2),
Short: "Vote for an active proposal, options: yes/no/no_with_veto/abstain",
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a vote for an active proposal. You can
find the proposal-id by running "%[1]s query gov proposals".
Example:
$ %[1]s tx gov vote 1 yes --from mykey
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// get voting address
from := cliCtx.GetFromAddress()
// validate that the proposal id is a uint
proposalID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("proposal-id %s is not a valid int, please input a valid proposal-id", args[0])
}
// find out which vote option user chose
byteVoteOption, err := govTypes.VoteOptionFromString(utils.NormalizeVoteOption(args[1]))
if err != nil {
return err
}
// build vote message and run basic validation
msg := govTypes.NewMsgVote(from, proposalID, byteVoteOption)
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}