cosmos-sdk/x/stake/commands/tx.go

256 lines
7.5 KiB
Go
Raw Normal View History

2018-02-24 05:19:32 -08:00
package commands
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
2018-03-19 06:13:30 -07:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
2018-02-25 15:45:20 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-17 09:22:11 -07:00
"github.com/cosmos/cosmos-sdk/wire"
2018-02-25 15:45:20 -08:00
"github.com/cosmos/cosmos-sdk/x/stake"
2018-02-24 05:19:32 -08:00
)
// nolint
const (
2018-03-20 14:21:18 -07:00
FlagAddressDelegator = "addressD"
FlagAddressCandidate = "addressC"
FlagPubKey = "pubkey"
FlagAmount = "amount"
FlagShares = "shares"
2018-02-24 05:19:32 -08:00
FlagMoniker = "moniker"
FlagIdentity = "keybase-sig"
FlagWebsite = "website"
FlagDetails = "details"
)
2018-03-17 09:22:11 -07:00
// common flagsets to add to various functions
2018-02-24 05:19:32 -08:00
var (
2018-03-17 09:22:11 -07:00
fsPk = flag.NewFlagSet("", flag.ContinueOnError)
fsAmount = flag.NewFlagSet("", flag.ContinueOnError)
fsShares = flag.NewFlagSet("", flag.ContinueOnError)
fsCandidate = flag.NewFlagSet("", flag.ContinueOnError)
2018-03-20 14:21:18 -07:00
fsDelegator = flag.NewFlagSet("", flag.ContinueOnError)
2018-02-24 05:19:32 -08:00
)
func init() {
fsPk.String(FlagPubKey, "", "PubKey of the validator-candidate")
fsAmount.String(FlagAmount, "1fermion", "Amount of coins to bond")
fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)")
fsCandidate.String(FlagMoniker, "", "validator-candidate name")
fsCandidate.String(FlagIdentity, "", "optional keybase signature")
fsCandidate.String(FlagWebsite, "", "optional website")
2018-03-20 14:21:18 -07:00
fsCandidate.String(FlagAddressCandidate, "", "hex address of the validator/candidate")
fsDelegator.String(FlagAddressDelegator, "", "hex address of the delegator")
2018-02-24 05:19:32 -08:00
}
2018-03-19 06:13:30 -07:00
//TODO refactor to common functionality
func getNamePassword() (name, passphrase string, err error) {
name = viper.GetString(client.FlagName)
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err = client.GetPassword(prompt, buf)
return
}
2018-03-17 09:22:11 -07:00
//_________________________________________________________________________________________
2018-03-17 09:22:11 -07:00
// create declare candidacy command
func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "declare-candidacy",
Short: "create new validator-candidate account and delegate some coins to it",
RunE: func(cmd *cobra.Command, args []string) error {
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil {
return err
}
2018-03-20 14:21:18 -07:00
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
pk, err := GetPubKey(viper.GetString(FlagPubKey))
if err != nil {
return err
}
if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
}
description := stake.Description{
Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
2018-03-20 14:21:18 -07:00
msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description)
2018-03-17 09:22:11 -07:00
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
2018-04-02 05:13:13 -07:00
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsAmount)
cmd.Flags().AddFlagSet(fsCandidate)
return cmd
2018-02-24 05:19:32 -08:00
}
2018-03-17 09:22:11 -07:00
// create edit candidacy command
func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "edit-candidacy",
Short: "edit and existing validator-candidate account",
RunE: func(cmd *cobra.Command, args []string) error {
2018-03-20 14:21:18 -07:00
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
description := stake.Description{
Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
2018-03-20 14:21:18 -07:00
msg := stake.NewMsgEditCandidacy(candidateAddr, description)
2018-03-17 09:22:11 -07:00
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
2018-04-02 05:13:13 -07:00
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsCandidate)
return cmd
}
2018-02-24 05:19:32 -08:00
2018-03-17 09:22:11 -07:00
// create edit candidacy command
func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegate",
Short: "delegate coins to an existing validator/candidate",
RunE: func(cmd *cobra.Command, args []string) error {
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil {
return err
}
2018-02-24 05:19:32 -08:00
2018-03-20 14:21:18 -07:00
delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator))
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
2018-02-24 05:19:32 -08:00
2018-03-20 14:21:18 -07:00
msg := stake.NewMsgDelegate(delegatorAddr, candidateAddr, amount)
2018-02-24 05:19:32 -08:00
2018-03-17 09:22:11 -07:00
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
2018-04-02 05:13:13 -07:00
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
2018-02-24 05:19:32 -08:00
2018-03-17 09:22:11 -07:00
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
2018-02-24 05:19:32 -08:00
}
2018-03-17 09:22:11 -07:00
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsAmount)
2018-03-20 14:21:18 -07:00
cmd.Flags().AddFlagSet(fsDelegator)
2018-03-17 09:22:11 -07:00
return cmd
2018-02-24 05:19:32 -08:00
}
2018-03-17 09:22:11 -07:00
// create edit candidacy command
func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unbond",
Short: "unbond coins from a validator/candidate",
RunE: func(cmd *cobra.Command, args []string) error {
// check the shares before broadcasting
sharesStr := viper.GetString(FlagShares)
var shares sdk.Rat
if sharesStr != "MAX" {
var err error
shares, err = sdk.NewRatFromDecimal(sharesStr)
if err != nil {
return err
}
if !shares.GT(sdk.ZeroRat) {
return fmt.Errorf("shares must be positive integer or decimal (ex. 123, 1.23456789)")
}
}
2018-03-20 14:21:18 -07:00
delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator))
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
2018-03-20 14:21:18 -07:00
msg := stake.NewMsgUnbond(delegatorAddr, candidateAddr, sharesStr)
2018-03-17 09:22:11 -07:00
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
2018-04-02 05:13:13 -07:00
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
2018-03-17 09:22:11 -07:00
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsShares)
2018-03-20 14:21:18 -07:00
cmd.Flags().AddFlagSet(fsDelegator)
2018-03-17 09:22:11 -07:00
return cmd
2018-02-24 05:19:32 -08:00
}
//______________________________________________________________________________________
2018-03-17 11:18:04 -07:00
// create the pubkey from a pubkey string
// TODO move to a better reusable place
2018-02-24 05:19:32 -08:00
func GetPubKey(pubKeyStr string) (pk crypto.PubKey, err error) {
if len(pubKeyStr) == 0 {
err = fmt.Errorf("must use --pubkey flag")
return
}
if len(pubKeyStr) != 64 { //if len(pkBytes) != 32 {
err = fmt.Errorf("pubkey must be Ed25519 hex encoded string which is 64 characters long")
return
}
var pkBytes []byte
pkBytes, err = hex.DecodeString(pubKeyStr)
if err != nil {
return
}
var pkEd crypto.PubKeyEd25519
copy(pkEd[:], pkBytes[:])
pk = pkEd.Wrap()
return
}