forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
103 lines (83 loc) · 2.51 KB
/
commands.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
package cli
import (
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/simplestake"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/crypto/ed25519"
)
const (
flagStake = "stake"
flagValidator = "validator"
)
// simple bond tx
func BondTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "bond",
Short: "Bond to a validator",
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
stakeString := viper.GetString(flagStake)
if len(stakeString) == 0 {
return fmt.Errorf("specify coins to bond with --stake")
}
valString := viper.GetString(flagValidator)
if len(valString) == 0 {
return fmt.Errorf("specify pubkey to bond to with --validator")
}
stake, err := sdk.ParseCoin(stakeString)
if err != nil {
return err
}
// TODO: bech32 ...
rawPubKey, err := hex.DecodeString(valString)
if err != nil {
return err
}
var pubKeyEd ed25519.PubKeyEd25519
copy(pubKeyEd[:], rawPubKey)
msg := simplestake.NewMsgBond(from, stake, pubKeyEd)
// Build and sign the transaction, then broadcast to a Tendermint
// node.
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().String(flagStake, "", "Amount of coins to stake")
cmd.Flags().String(flagValidator, "", "Validator address to stake")
return cmd
}
// simple unbond tx
func UnbondTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unbond",
Short: "Unbond from a validator",
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
msg := simplestake.NewMsgUnbond(from)
// Build and sign the transaction, then broadcast to a Tendermint
// node.
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
},
}
return cmd
}