basecli works for sendtx and cli tests
This commit is contained in:
@@ -9,11 +9,15 @@ import (
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/tendermint/basecoin"
|
||||
"github.com/tendermint/light-client/commands"
|
||||
txcmd "github.com/tendermint/light-client/commands/txs"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
|
||||
"github.com/tendermint/basecoin/modules/coin"
|
||||
"github.com/tendermint/basecoin/stack"
|
||||
"github.com/tendermint/basecoin/txs"
|
||||
btypes "github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
@@ -48,27 +52,25 @@ func init() {
|
||||
// runDemo is an example of how to make a tx
|
||||
func doSendTx(cmd *cobra.Command, args []string) error {
|
||||
// load data from json or flags
|
||||
tx := new(btypes.SendTx)
|
||||
found, err := txcmd.LoadJSON(tx)
|
||||
var tx basecoin.Tx
|
||||
found, err := txcmd.LoadJSON(&tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
err = readSendTxFlags(tx)
|
||||
tx, err = readSendTxFlags()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wrap and add signer
|
||||
send := &SendTx{
|
||||
chainID: commands.GetChainID(),
|
||||
Tx: tx,
|
||||
}
|
||||
send.AddSigner(txcmd.GetSigner())
|
||||
// TODO: make this more flexible for middleware
|
||||
// add the chain info
|
||||
tx = txs.NewChain(commands.GetChainID(), tx)
|
||||
stx := txs.NewSig(tx)
|
||||
|
||||
// Sign if needed and post. This it the work-horse
|
||||
bres, err := txcmd.SignAndPostTx(send)
|
||||
bres, err := txcmd.SignAndPostTx(stx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -77,40 +79,50 @@ func doSendTx(cmd *cobra.Command, args []string) error {
|
||||
return txcmd.OutputTx(bres)
|
||||
}
|
||||
|
||||
func readSendTxFlags(tx *btypes.SendTx) error {
|
||||
func readSendTxFlags() (tx basecoin.Tx, err error) {
|
||||
// parse to address
|
||||
to, err := parseChainAddress(viper.GetString(FlagTo))
|
||||
chain, to, err := parseChainAddress(viper.GetString(FlagTo))
|
||||
if err != nil {
|
||||
return err
|
||||
return tx, err
|
||||
}
|
||||
toAddr := stack.SigPerm(to)
|
||||
toAddr.ChainID = chain
|
||||
|
||||
// //parse the fee and amounts into coin types
|
||||
// tx.Fee, err = btypes.ParseCoin(viper.GetString(FlagFee))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// // set the gas
|
||||
// tx.Gas = viper.GetInt64(FlagGas)
|
||||
|
||||
//parse the fee and amounts into coin types
|
||||
tx.Fee, err = btypes.ParseCoin(viper.GetString(FlagFee))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
amountCoins, err := btypes.ParseCoins(viper.GetString(FlagAmount))
|
||||
if err != nil {
|
||||
return err
|
||||
return tx, err
|
||||
}
|
||||
|
||||
// set the gas
|
||||
tx.Gas = viper.GetInt64(FlagGas)
|
||||
// this could be much cooler with multisig...
|
||||
var fromAddr basecoin.Actor
|
||||
signer := txcmd.GetSigner()
|
||||
if !signer.Empty() {
|
||||
fromAddr = stack.SigPerm(signer.Address())
|
||||
}
|
||||
|
||||
// craft the inputs and outputs
|
||||
tx.Inputs = []btypes.TxInput{{
|
||||
ins := []coin.TxInput{{
|
||||
Address: fromAddr,
|
||||
Coins: amountCoins,
|
||||
Sequence: viper.GetInt(FlagSequence),
|
||||
}}
|
||||
tx.Outputs = []btypes.TxOutput{{
|
||||
Address: to,
|
||||
outs := []coin.TxOutput{{
|
||||
Address: toAddr,
|
||||
Coins: amountCoins,
|
||||
}}
|
||||
|
||||
return nil
|
||||
return coin.NewSendTx(ins, outs), nil
|
||||
}
|
||||
|
||||
func parseChainAddress(toFlag string) ([]byte, error) {
|
||||
func parseChainAddress(toFlag string) (string, []byte, error) {
|
||||
var toHex string
|
||||
var chainPrefix string
|
||||
spl := strings.Split(toFlag, "/")
|
||||
@@ -121,19 +133,16 @@ func parseChainAddress(toFlag string) ([]byte, error) {
|
||||
chainPrefix = spl[0]
|
||||
toHex = spl[1]
|
||||
default:
|
||||
return nil, errors.Errorf("To address has too many slashes")
|
||||
return "", nil, errors.Errorf("To address has too many slashes")
|
||||
}
|
||||
|
||||
// convert destination address to bytes
|
||||
to, err := hex.DecodeString(cmn.StripHex(toHex))
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("To address is invalid hex: %v\n", err)
|
||||
return "", nil, errors.Errorf("To address is invalid hex: %v\n", err)
|
||||
}
|
||||
|
||||
if chainPrefix != "" {
|
||||
to = []byte(chainPrefix + "/" + string(to))
|
||||
}
|
||||
return to, nil
|
||||
return chainPrefix, to, nil
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/tendermint/basecoin"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
lc "github.com/tendermint/light-client"
|
||||
lcmd "github.com/tendermint/light-client/commands"
|
||||
@@ -12,7 +13,6 @@ import (
|
||||
|
||||
"github.com/tendermint/basecoin/modules/coin"
|
||||
"github.com/tendermint/basecoin/stack"
|
||||
btypes "github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
var AccountQueryCmd = &cobra.Command{
|
||||
@@ -45,7 +45,7 @@ type BaseTxPresenter struct {
|
||||
}
|
||||
|
||||
func (_ BaseTxPresenter) ParseData(raw []byte) (interface{}, error) {
|
||||
var tx btypes.TxS
|
||||
var tx basecoin.Tx
|
||||
err := wire.ReadBinaryBytes(raw, &tx)
|
||||
return tx, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user