/
tx.go
105 lines (89 loc) · 3.17 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
package cli
import (
"bufio"
"fmt"
"github.com/spf13/cobra"
"github.com/allinbits/modules/x/poa/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
poaTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
poaTxCmd.AddCommand(flags.PostCommands(
GetCmdCreateValidator(cdc),
GetCmdVoteValidator(cdc, "vote-validator", true),
GetCmdVoteValidator(cdc, "kick-validator", false),
)...)
return poaTxCmd
}
// GetCmdCreateValidator is the CLI command for sending a CreateValidator transaction
func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create-validator [name] [public key] [moniker] [identity] [website] [security_contract] [details]",
Short: "create a validatator with a name and public key",
Args: cobra.ExactArgs(7),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
// if err := cliCtx.EnsureAccountExists(); err != nil {
// return err
// }
accAddr := cliCtx.GetFromAddress()
consAddr := sdk.ValAddress(accAddr)
valPubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, args[1])
if err != nil {
return err
}
msg := types.NewMsgCreateValidatorPOA(
args[0],
consAddr,
valPubKey,
stakingtypes.NewDescription(args[2], args[3], args[4], args[5], args[6]),
accAddr,
)
err = msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
// GetCmdVoteValidator is the CLI command for sending a VoteValidator transaction
func GetCmdVoteValidator(cdc *codec.Codec, use string, inFavor bool) *cobra.Command {
return &cobra.Command{
Use: use + " [validator-name]",
Short: "vote for a validator using their name",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
// if err := cliCtx.EnsureAccountExists(); err != nil {
// return err
// }
accAddr := cliCtx.GetFromAddress()
valAddr := sdk.ValAddress(accAddr)
msg := types.NewMsgVoteValidator(args[0], valAddr, inFavor, accAddr)
err := msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}