feat: Bundle Tx CLI (#129)

This commit is contained in:
Aleksandr Bezobchuk 2023-05-11 12:59:39 -04:00 committed by GitHub
parent 9d8d682567
commit e96831c244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 1 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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))