Skip to content

Commit

Permalink
CLI fee and amount now use string inputs specifying the coin type
Browse files Browse the repository at this point in the history
readme update

intermin
  • Loading branch information
rigelrozanski committed Feb 13, 2017
1 parent 50e787a commit e3302bb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ This will create the `basecoin` binary in `$GOPATH/bin`.

The basecoin CLI can be used to start a stand-alone basecoin instance (`basecoin start`),
or to start basecoin with Tendermint in the same process (`basecoin start --in-proc`).
It can also be used to send transactions, eg. `basecoin tx send --to 0x4793A333846E5104C46DD9AB9A00E31821B2F301 --amount 100`
It can also be used to send transactions, eg. `basecoin tx send --to 0x4793A333846E5104C46DD9AB9A00E31821B2F301 --amount 100btc,10gold`
See `basecoin --help` and `basecoin [cmd] --help` for more details`.

## Learn more
Expand Down
18 changes: 6 additions & 12 deletions cmd/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ var (
Usage: "Destination address for the transaction",
}

AmountFlag = cli.IntFlag{
AmountFlag = cli.StringFlag{
Name: "amount",
Value: 0,
Usage: "Amount of coins to send in the transaction",
Value: "",
Usage: "Coins to send in transaction of the format <amt><coin>,<amt2><coin2>,... (eg: 1gold,2silver,5btc)",
}

FromFlag = cli.StringFlag{
Expand All @@ -66,22 +66,16 @@ var (
Usage: "Sequence number for the account",
}

CoinFlag = cli.StringFlag{
Name: "coin",
Value: "mycoin",
Usage: "Specify a coin denomination",
}

GasFlag = cli.IntFlag{
Name: "gas",
Value: 0,
Usage: "The amount of gas for the transaction",
}

FeeFlag = cli.IntFlag{
FeeFlag = cli.StringFlag{
Name: "fee",
Value: 0,
Usage: "The transaction fee",
Value: "",
Usage: "Coins for the transaction fee of the format <amt><coin>,<amt2><coin2>,... (eg: 1gold,2silver,5btc)",
}

DataFlag = cli.StringFlag{
Expand Down
56 changes: 35 additions & 21 deletions cmd/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var TxFlags = []cli.Flag{
FromFlag,

AmountFlag,
CoinFlag,
GasFlag,
FeeFlag,
SeqFlag,
Expand Down Expand Up @@ -71,9 +70,9 @@ func RegisterTxSubcommand(cmd cli.Command) {
func cmdSendTx(c *cli.Context) error {
toHex := c.String("to")
fromFile := c.String("from")
amount := int64(c.Int("amount"))
coin := c.String("coin")
gas, fee := c.Int("gas"), int64(c.Int("fee"))
amount := c.String("amount")
gas := int64(c.Int("gas"))
fee := c.String("fee")
chainID := c.String("chain_id")

// convert destination address to bytes
Expand All @@ -91,12 +90,22 @@ func cmdSendTx(c *cli.Context) error {
return err
}

//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
if err != nil {
return err
}

// craft the tx
input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
output := newOutput(to, coin, amount)
input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
output := newOutput(to, amountCoins)
tx := &types.SendTx{
Gas: int64(gas),
Fee: types.Coin{coin, fee},
Gas: gas,
Fee: feeCoin,
Inputs: []types.TxInput{input},
Outputs: []types.TxOutput{output},
}
Expand Down Expand Up @@ -128,9 +137,9 @@ func cmdAppTx(c *cli.Context) error {

func AppTx(c *cli.Context, name string, data []byte) error {
fromFile := c.String("from")
amount := int64(c.Int("amount"))
coin := c.String("coin")
gas, fee := c.Int("gas"), int64(c.Int("fee"))
amount := c.String("amount")
fee := c.String("fee")
gas := int64(c.Int("gas"))
chainID := c.String("chain_id")

privKey := tmtypes.LoadPrivValidator(fromFile)
Expand All @@ -140,10 +149,20 @@ func AppTx(c *cli.Context, name string, data []byte) error {
return err
}

input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
if err != nil {
return err
}

input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
tx := &types.AppTx{
Gas: int64(gas),
Fee: types.Coin{coin, fee},
Gas: gas,
Fee: feeCoin,
Name: name,
Input: input,
Data: data,
Expand Down Expand Up @@ -205,15 +224,10 @@ func getSeq(c *cli.Context, address []byte) (int, error) {
return acc.Sequence + 1, nil
}

func newOutput(to []byte, coin string, amount int64) types.TxOutput {
func newOutput(to []byte, amount types.Coins) types.TxOutput {
return types.TxOutput{
Address: to,
Coins: types.Coins{
types.Coin{
Denom: coin,
Amount: amount,
},
},
Coins: amount,
}

}
44 changes: 43 additions & 1 deletion cmd/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package commands
import (
"encoding/hex"
"errors"
"regexp"
"strconv"
"strings"

"github.com/urfave/cli"

"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/types"

abci "github.com/tendermint/abci/types"
Expand Down Expand Up @@ -35,6 +39,44 @@ func StripHex(s string) string {
return s
}

func ParseCoin(str string) (types.Coin, error) {

var coin types.Coin

coins, err := ParseCoins(str)

if err != nil {
return coin, err
}

if len(coins) > 0 {
coin = coins[0]
}

return coin, nil
}

//regex codes from
var reAmt = regexp.MustCompile("(\\d+)")
var reCoin = regexp.MustCompile("([^\\d\\W]+)")

func ParseCoins(str string) (types.Coins, error) {

split := strings.Split(str, ",")
var coins []types.Coin

for _, el := range split {
amt, err := strconv.Atoi(reAmt.FindString(el))
if err != nil {
return coins, err
}
coin := reCoin.FindString(el)
coins = append(coins, types.Coin{coin, int64(amt)})
}

return coins, nil
}

func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
clientURI := client.NewClientURI(tmAddr)
tmResult := new(ctypes.TMResult)
Expand All @@ -58,7 +100,7 @@ func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
// fetch the account by querying the app
func getAcc(tmAddr string, address []byte) (*types.Account, error) {

key := append([]byte("base/a/"), address...)
key := state.AccountKey(address)
response, err := Query(tmAddr, key)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/src/example-plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (ep *ExamplePlugin) RunTx(store types.KVStore, ctx types.CallContext, txByt
return abci.OK
}

func (ep *ExamplePlugin) InitChain(store types.KVStore, vals []*abci.Validator) {
func (cp *ExamplePlugin) InitChain(store types.KVStore, vals []*abci.Validator) {
}

func (ep *ExamplePlugin) BeginBlock(store types.KVStore, hash []byte, header *abci.Header) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/counter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCounterPlugin(t *testing.T) {
tx := &types.AppTx{
Gas: gas,
Fee: fee,
Name: "counter",
Name: counterPlugin.Name(),
Input: types.NewTxInput(test1Acc.PubKey, inputCoins, inputSequence),
Data: wire.BinaryBytes(CounterTx{Valid: true, Fee: appFee}),
}
Expand Down

0 comments on commit e3302bb

Please sign in to comment.