Allow registering plugin subcommand to apptx

This commit is contained in:
Ethan Frey 2017-01-30 15:56:47 +01:00
parent cb4f6a4bca
commit 3fbd282f2e
3 changed files with 63 additions and 43 deletions

View File

@ -0,0 +1,49 @@
package commands
import (
"fmt"
"github.com/tendermint/basecoin/plugins/counter"
"github.com/tendermint/basecoin/types"
wire "github.com/tendermint/go-wire"
"github.com/urfave/cli"
)
var (
CounterTxCmd = cli.Command{
Name: "counter",
Usage: "Craft a transaction to the counter plugin",
Action: func(c *cli.Context) error {
return cmdCounterTx(c)
},
Flags: []cli.Flag{
ValidFlag,
},
}
)
func init() {
RegisterPlugin(CounterTxCmd)
}
func cmdCounterTx(c *cli.Context) error {
valid := c.Bool("valid")
parent := c.Parent()
counterTx := counter.CounterTx{
Valid: valid,
Fee: types.Coins{
{
Denom: parent.String("coin"),
Amount: int64(parent.Int("fee")),
},
},
}
fmt.Println("CounterTx:", string(wire.JSONBytes(counterTx)))
data := wire.BinaryBytes(counterTx)
name := "counter"
return AppTx(parent, name, data)
}

View File

@ -132,7 +132,7 @@ func cmdIBCRegisterTx(c *cli.Context) error {
}{ibcTx}))
name := "IBC"
return appTx(parent, name, data)
return AppTx(parent, name, data)
}
func cmdIBCUpdateTx(c *cli.Context) error {
@ -167,7 +167,7 @@ func cmdIBCUpdateTx(c *cli.Context) error {
}{ibcTx}))
name := "IBC"
return appTx(c.Parent(), name, data)
return AppTx(c.Parent(), name, data)
}
func cmdIBCPacketCreateTx(c *cli.Context) error {
@ -200,7 +200,7 @@ func cmdIBCPacketCreateTx(c *cli.Context) error {
ibc.IBCTx `json:"unwrap"`
}{ibcTx}))
return appTx(c.Parent().Parent(), "IBC", data)
return AppTx(c.Parent().Parent(), "IBC", data)
}
func cmdIBCPacketPostTx(c *cli.Context) error {
@ -238,7 +238,7 @@ func cmdIBCPacketPostTx(c *cli.Context) error {
ibc.IBCTx `json:"unwrap"`
}{ibcTx}))
return appTx(c.Parent().Parent(), "IBC", data)
return AppTx(c.Parent().Parent(), "IBC", data)
}
func getIBCSequence(c *cli.Context) (uint64, error) {

View File

@ -7,7 +7,6 @@ import (
"github.com/urfave/cli"
"github.com/tendermint/basecoin/plugins/counter"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
@ -63,23 +62,17 @@ var (
NameFlag,
DataFlag,
},
Subcommands: []cli.Command{
CounterTxCmd,
},
}
CounterTxCmd = cli.Command{
Name: "counter",
Usage: "Craft a transaction to the counter plugin",
Action: func(c *cli.Context) error {
return cmdCounterTx(c)
},
Flags: []cli.Flag{
ValidFlag,
},
// Subcommands are dynamically registered with plugins as needed
Subcommands: []cli.Command{},
}
)
// RegisterPlugin is used to add another subcommand and create a custom
// apptx encoding. Look at counter.go for an example
func RegisterPlugin(cmd cli.Command) {
AppTxCmd.Subcommands = append(AppTxCmd.Subcommands, cmd)
}
func cmdSendTx(c *cli.Context) error {
toHex := c.String("to")
fromFile := c.String("from")
@ -136,10 +129,10 @@ func cmdAppTx(c *cli.Context) error {
data, _ = hex.DecodeString(dataString)
}
name := c.String("name")
return appTx(c, name, data)
return AppTx(c, name, data)
}
func appTx(c *cli.Context, name string, data []byte) error {
func AppTx(c *cli.Context, name string, data []byte) error {
fromFile := c.String("from")
amount := int64(c.Int("amount"))
coin := c.String("coin")
@ -174,28 +167,6 @@ func appTx(c *cli.Context, name string, data []byte) error {
return nil
}
func cmdCounterTx(c *cli.Context) error {
valid := c.Bool("valid")
parent := c.Parent()
counterTx := counter.CounterTx{
Valid: valid,
Fee: types.Coins{
{
Denom: parent.String("coin"),
Amount: int64(parent.Int("fee")),
},
},
}
fmt.Println("CounterTx:", string(wire.JSONBytes(counterTx)))
data := wire.BinaryBytes(counterTx)
name := "counter"
return appTx(parent, name, data)
}
// broadcast the transaction to tendermint
func broadcastTx(c *cli.Context, tx types.Tx) ([]byte, error) {
tmResult := new(ctypes.TMResult)