Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DEX place-order CLI #1050

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions x/dex/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
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,16 +56,19 @@
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 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]`, //nolint:lll // string example
version.AppName, types.ModuleName,
),
),
Expand All @@ -81,25 +80,8 @@

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 +91,7 @@

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L94 was not covered by tests
}

side, ok := types.Side_value[args[5]]
Expand Down Expand Up @@ -144,9 +126,22 @@
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 136 in x/dex/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/dex/client/cli/tx.go#L132-L136

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L138 - L139 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
Loading