From c1fc5ae3c8ecaa7394902c6d6fd1934d39eea773 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Jul 2017 20:51:07 +0200 Subject: [PATCH] Add --fee flag to sendtx --- cmd/basecli/commands/cmds.go | 47 +++++++++++++++++++++++------------- tests/cli/basictx.sh | 18 ++++++++++++++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/cmd/basecli/commands/cmds.go b/cmd/basecli/commands/cmds.go index 1f477e988c..631c371d6a 100644 --- a/cmd/basecli/commands/cmds.go +++ b/cmd/basecli/commands/cmds.go @@ -16,6 +16,7 @@ import ( "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/base" "github.com/tendermint/basecoin/modules/coin" + "github.com/tendermint/basecoin/modules/fee" ) //------------------------- @@ -64,7 +65,10 @@ func doSendTx(cmd *cobra.Command, args []string) error { } // TODO: make this more flexible for middleware - // add the chain info + tx, err = WrapFeeTx(tx) + if err != nil { + return err + } tx, err = WrapChainTx(tx) if err != nil { return err @@ -83,6 +87,21 @@ func doSendTx(cmd *cobra.Command, args []string) error { return txcmd.OutputTx(bres) } +// WrapFeeTx checks for FlagFee and if present wraps the tx with a +// FeeTx of the given amount, paid by the signer +func WrapFeeTx(tx basecoin.Tx) (res basecoin.Tx, err error) { + //parse the fee and amounts into coin types + toll, err := coin.ParseCoin(viper.GetString(FlagFee)) + if err != nil { + return res, err + } + // if no fee, do nothing, otherwise wrap it + if toll.IsZero() { + return tx, nil + } + return fee.NewFee(tx, toll, getSignerAddr()), nil +} + // WrapChainTx will wrap the tx with a ChainTx from the standard flags func WrapChainTx(tx basecoin.Tx) (res basecoin.Tx, err error) { expires := viper.GetInt64(FlagExpires) @@ -94,6 +113,15 @@ func WrapChainTx(tx basecoin.Tx) (res basecoin.Tx, err error) { return res, nil } +func getSignerAddr() (res basecoin.Actor) { + // this could be much cooler with multisig... + signer := txcmd.GetSigner() + if !signer.Empty() { + res = auth.SigPerm(signer.Address()) + } + return res +} + func readSendTxFlags() (tx basecoin.Tx, err error) { // parse to address chain, to, err := parseChainAddress(viper.GetString(FlagTo)) @@ -103,29 +131,14 @@ func readSendTxFlags() (tx basecoin.Tx, err error) { toAddr := auth.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) - amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount)) if err != nil { return tx, err } - // this could be much cooler with multisig... - var fromAddr basecoin.Actor - signer := txcmd.GetSigner() - if !signer.Empty() { - fromAddr = auth.SigPerm(signer.Address()) - } - // craft the inputs and outputs ins := []coin.TxInput{{ - Address: fromAddr, + Address: getSignerAddr(), Coins: amountCoins, }} outs := []coin.TxOutput{{ diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index c46f4d78cf..46ef2a3ee3 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -49,6 +49,24 @@ test01SendTx() { checkSendTx $HASH $TX_HEIGHT $SENDER "992" } +test02SendTxWithFee() { + SENDER=$(getAddr $RICH) + RECV=$(getAddr $POOR) + + TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=90mycoin --fee=10mycoin --sequence=2 --to=$RECV --name=$RICH) + txSucceeded $? "$TX" "$RECV" + HASH=$(echo $TX | jq .hash | tr -d \") + TX_HEIGHT=$(echo $TX | jq .height) + + # deduct 100 from sender, add 90 to receiver... fees "vanish" + checkAccount $SENDER "9007199254739900" + checkAccount $RECV "1082" + + # Make sure tx is indexed + # checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" +} + + # Load common then run these tests with shunit2! DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #get this files directory . $DIR/common.sh