0g-chain/x/auction/client/cli/tx.go

70 lines
1.9 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package cli
import (
"fmt"
"strconv"
"strings"
2019-11-25 19:46:02 +00:00
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
2019-11-25 19:46:02 +00:00
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/kava-labs/kava/x/auction/types"
2019-11-25 19:46:02 +00:00
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
auctionTxCmd := &cobra.Command{
Use: "auction",
Short: "auction transactions subcommands",
}
auctionTxCmd.AddCommand(client.PostCommands(
GetCmdPlaceBid(cdc),
)...)
return auctionTxCmd
}
// GetCmdPlaceBid cli command for placing bids on auctions
2019-11-25 19:46:02 +00:00
func GetCmdPlaceBid(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "bid [auction-id] [amount]",
2019-11-25 19:46:02 +00:00
Short: "place a bid on an auction",
Long: strings.TrimSpace(
fmt.Sprintf(`Place a bid on any type of auction, updating the latest bid amount to [amount]. Collateral auctions must be bid up to their maxbid before entering reverse phase.
Example:
$ %s tx %s bid 34 1000usdx --from myKeyName
`, version.ClientName, types.ModuleName)),
Args: cobra.ExactArgs(2),
2019-11-25 19:46:02 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
id, err := strconv.ParseUint(args[0], 10, 64)
2019-11-25 19:46:02 +00:00
if err != nil {
return fmt.Errorf("auction-id '%s' not a valid uint", args[0])
2019-11-25 19:46:02 +00:00
}
amt, err := sdk.ParseCoin(args[1])
2019-11-25 19:46:02 +00:00
if err != nil {
return err
}
msg := types.NewMsgPlaceBid(id, cliCtx.GetFromAddress(), amt)
2019-11-25 19:46:02 +00:00
err = msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}