From e96831c2445988150309773fa7fd3b5e61cb88fc Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 11 May 2023 12:59:39 -0400 Subject: [PATCH] feat: Bundle Tx CLI (#129) --- x/builder/client/cli/tx.go | 83 ++++++++++++++++++++++++++++++++++++++ x/builder/module.go | 4 +- x/builder/types/msgs.go | 8 ++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 x/builder/client/cli/tx.go diff --git a/x/builder/client/cli/tx.go b/x/builder/client/cli/tx.go new file mode 100644 index 0000000..b545d63 --- /dev/null +++ b/x/builder/client/cli/tx.go @@ -0,0 +1,83 @@ +package cli + +import ( + "encoding/hex" + "errors" + "fmt" + "strings" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/skip-mev/pob/x/builder/types" + "github.com/spf13/cobra" +) + +// NewTxCmd returns a root CLI command handler for all x/builder transaction +// commands. +func NewTxCmd() *cobra.Command { + txCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Builder transaction subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + txCmd.AddCommand( + NewAuctionBidTx(), + ) + + return txCmd +} + +func NewAuctionBidTx() *cobra.Command { + cmd := &cobra.Command{ + Use: "auction-bid [bidder] [bid] [bundled_tx1,bundled_tx2,...,bundled_txN]", + Short: "Create an auction bid transaction with signed bundled transactions", + Long: `Create an auction bid transaction with a list of signed bundled transactions, +where each transaction is a hex-encoded string of a signed transaction. +`, + Args: cobra.ExactArgs(3), + Example: "auction-bid cosmos1... 10000uatom 0xFF...,0xCC...,0xAA...", + RunE: func(cmd *cobra.Command, args []string) error { + cmd.Flags().Set(flags.FlagFrom, args[0]) + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + bid, err := sdk.ParseCoinNormalized(args[1]) + if err != nil { + return err + } + + // ensure timeout is non-zero + timeoutHeight, _ := cmd.Flags().GetUint64(flags.FlagTimeoutHeight) + if timeoutHeight == 0 { + return errors.New("timeout height must be greater than 0") + } + + tokens := strings.Split(args[2], ",") + bundledTxs := make([][]byte, len(tokens)) + for i, token := range tokens { + rawTx, err := hex.DecodeString(token) + if err != nil { + return fmt.Errorf("failed to HEX decode bundled transaction %d: %w", i, err) + } + + bundledTxs[i] = rawTx + } + + msg := types.NewMsgAuctionBid(clientCtx.GetFromAddress(), bid, bundledTxs) + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/builder/module.go b/x/builder/module.go index 27d7490..c7b9e02 100644 --- a/x/builder/module.go +++ b/x/builder/module.go @@ -76,7 +76,9 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r } // GetTxCmd returns the root tx command for the builder module. -func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.NewTxCmd() +} // GetQueryCmd returns the root query command for the builder module. func (AppModuleBasic) GetQueryCmd() *cobra.Command { diff --git a/x/builder/types/msgs.go b/x/builder/types/msgs.go index 10e5a74..3209ce1 100644 --- a/x/builder/types/msgs.go +++ b/x/builder/types/msgs.go @@ -33,6 +33,14 @@ func (m MsgUpdateParams) ValidateBasic() error { return m.Params.Validate() } +func NewMsgAuctionBid(bidder sdk.AccAddress, bid sdk.Coin, transactions [][]byte) *MsgAuctionBid { + return &MsgAuctionBid{ + Bidder: bidder.String(), + Bid: bid, + Transactions: transactions, + } +} + // GetSignBytes implements the LegacyMsg interface. func (m MsgAuctionBid) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))