2017-07-18 11:12:51 -07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2017-07-18 13:21:57 -07:00
|
|
|
"github.com/tendermint/basecoin"
|
2017-07-18 12:57:37 -07:00
|
|
|
"github.com/tendermint/basecoin/client/commands"
|
|
|
|
txcmd "github.com/tendermint/basecoin/client/commands/txs"
|
2017-07-18 11:12:51 -07:00
|
|
|
"github.com/tendermint/basecoin/modules/coin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SendTxCmd is CLI command to send tokens between basecoin accounts
|
|
|
|
var SendTxCmd = &cobra.Command{
|
|
|
|
Use: "send",
|
|
|
|
Short: "send tokens from one account to another",
|
2017-07-18 22:23:13 -07:00
|
|
|
RunE: commands.RequireInit(sendTxCmd),
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
const (
|
|
|
|
FlagTo = "to"
|
|
|
|
FlagAmount = "amount"
|
2017-07-18 11:45:48 -07:00
|
|
|
FlagFrom = "from"
|
2017-07-18 11:12:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flags := SendTxCmd.Flags()
|
|
|
|
flags.String(FlagTo, "", "Destination address for the bits")
|
|
|
|
flags.String(FlagAmount, "", "Coins to send in the format <amt><coin>,<amt><coin>...")
|
2017-07-18 11:45:48 -07:00
|
|
|
flags.String(FlagFrom, "", "Address sending coins, if not first signer")
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
2017-07-18 22:23:13 -07:00
|
|
|
// sendTxCmd is an example of how to make a tx
|
|
|
|
func sendTxCmd(cmd *cobra.Command, args []string) error {
|
2017-07-18 11:12:51 -07:00
|
|
|
tx, err := readSendTxFlags()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-19 05:25:17 -07:00
|
|
|
return txcmd.DoTx(tx)
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func readSendTxFlags() (tx basecoin.Tx, err error) {
|
|
|
|
// parse to address
|
2017-07-19 05:25:17 -07:00
|
|
|
toAddr, err := commands.ParseActor(viper.GetString(FlagTo))
|
2017-07-18 11:12:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
2017-07-18 11:45:48 -07:00
|
|
|
fromAddr, err := readFromAddr()
|
|
|
|
if err != nil {
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
2017-07-18 11:12:51 -07:00
|
|
|
amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount))
|
|
|
|
if err != nil {
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// craft the inputs and outputs
|
2017-07-27 11:05:28 -07:00
|
|
|
tx = coin.NewSendOneTx(fromAddr, toAddr, amountCoins)
|
|
|
|
return
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
2017-07-18 11:45:48 -07:00
|
|
|
|
|
|
|
func readFromAddr() (basecoin.Actor, error) {
|
|
|
|
from := viper.GetString(FlagFrom)
|
|
|
|
if from == "" {
|
2017-07-18 13:21:57 -07:00
|
|
|
return txcmd.GetSignerAct(), nil
|
2017-07-18 11:45:48 -07:00
|
|
|
}
|
2017-07-19 05:25:17 -07:00
|
|
|
return commands.ParseActor(from)
|
2017-07-18 11:45:48 -07:00
|
|
|
}
|