Skip to content

Commit

Permalink
Improve DEX place-order CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil committed Dec 13, 2024
1 parent f842002 commit ae5be21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
48 changes: 22 additions & 26 deletions x/dex/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const (
GoodTilBlockHeightFlag = "good-til-block-height"
// GoodTilBlockTimeFlag is good til block time flag.
GoodTilBlockTimeFlag = "good-til-block-time"
// OrderTypeLimit is limit order type.
OrderTypeLimit = "limit"
// OrderTypeMarket is limit order market.
OrderTypeMarket = "market"
// TimeInForce is time-in-force flag.
TimeInForce = "time-in-force"
)
Expand Down Expand Up @@ -60,15 +56,19 @@ func GetTxCmd() *cobra.Command {
func CmdPlaceOrder() *cobra.Command {
availableTimeInForces := lo.Values(types.TimeInForce_name)
sort.Strings(availableTimeInForces)
availableOrderTypes := lo.Values(types.OrderType_name)
sort.Strings(availableTimeInForces)
availableSides := lo.Values(types.Side_name)
sort.Strings(availableTimeInForces)
cmd := &cobra.Command{
Use: "place-order [type (limit,market)] [id] [base_denom] [quote_denom] [quantity] [side] --price 123e-2 --time-in-force=" + strings.Join(availableTimeInForces, ",") + " --good-til-block-height=123 --good-til-block-time=1727124446 --from [sender]", //nolint:lll // string example
Use: "place-order [type (" + strings.Join(availableOrderTypes, ",") + ")] [id] [base_denom] [quote_denom] [quantity] [side (" + strings.Join(availableSides, ",") + ")] --price 123e-2 --time-in-force=" + strings.Join(availableTimeInForces, ",") + " --good-til-block-height=123 --good-til-block-time=1727124446 --from [sender]", //nolint:lll // string example
Args: cobra.ExactArgs(6),
Short: "Place new order",
Long: strings.TrimSpace(
fmt.Sprintf(`Place new order.
Example:
$ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]
$ %s tx %s cored tx dex place-order ORDER_TYPE_LIMIT "my-order-id1" denom1 denom2 1000 SIDE_SELL --price 12e-1 --time-in-force=TIME_IN_FORCE_GTC --from [sender]
`,
version.AppName, types.ModuleName,
),
Expand All @@ -81,25 +81,8 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]

sender := clientCtx.GetFromAddress()

var orderType types.OrderType
timeInForceString, err := cmd.Flags().GetString(TimeInForce)
timeInForceInt, ok := types.TimeInForce_value[timeInForceString]
orderType, ok := types.OrderType_value[args[0]]
if !ok {
return errors.Errorf(
"unknown TimeInForce '%s',available TimeInForces: %s",
timeInForceString, strings.Join(availableTimeInForces, ","),
)
}
if err != nil {
return errors.WithStack(err)
}
timeInForce := types.TimeInForce(timeInForceInt)
switch args[0] {
case OrderTypeLimit:
orderType = types.ORDER_TYPE_LIMIT
case OrderTypeMarket:
orderType = types.ORDER_TYPE_MARKET
default:
return errors.Errorf("unknown type '%s'", args[0])
}

Expand All @@ -109,7 +92,7 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]

quantity, ok := sdkmath.NewIntFromString(args[4])
if !ok {
return sdkerrors.Wrap(err, "invalid quantity")
return errors.New("invalid quantity")

Check warning on line 95 in x/dex/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/dex/client/cli/tx.go#L95

Added line #L95 was not covered by tests
}

side, ok := types.Side_value[args[5]]
Expand Down Expand Up @@ -144,9 +127,22 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]
goodTilBlockTime = lo.ToPtr(time.Unix(goodTilBlockTimeNum, 0))
}

timeInForceString, err := cmd.Flags().GetString(TimeInForce)
timeInForceInt, ok := types.TimeInForce_value[timeInForceString]
if !ok {
return errors.Errorf(
"unknown TimeInForce '%s',available TimeInForces: %s",
timeInForceString, strings.Join(availableTimeInForces, ","),
)
}

Check warning on line 137 in x/dex/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/dex/client/cli/tx.go#L133-L137

Added lines #L133 - L137 were not covered by tests
if err != nil {
return errors.WithStack(err)
}

Check warning on line 140 in x/dex/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/dex/client/cli/tx.go#L139-L140

Added lines #L139 - L140 were not covered by tests
timeInForce := types.TimeInForce(timeInForceInt)

msg := &types.MsgPlaceOrder{
Sender: sender.String(),
Type: orderType,
Type: types.OrderType(orderType),
ID: id,
BaseDenom: baseDenom,
QuoteDenom: quoteDenom,
Expand Down
12 changes: 1 addition & 11 deletions x/dex/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,8 @@ func placeOrder(
testNetwork *network.Network,
order types.Order,
) {
var orderType string
switch order.Type {
case types.ORDER_TYPE_LIMIT:
orderType = cli.OrderTypeLimit
case types.ORDER_TYPE_MARKET:
orderType = cli.OrderTypeMarket
default:
requireT.Fail(fmt.Sprintf("unknown type '%s'", order.Type))
}

args := []string{
orderType,
order.Type.String(),
order.ID,
order.BaseDenom,
order.QuoteDenom,
Expand Down

0 comments on commit ae5be21

Please sign in to comment.