Skip to content

Commit

Permalink
Address validation on cli query input.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystal Lemire committed Oct 20, 2023
1 parent 76babe7 commit c6095cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
11 changes: 11 additions & 0 deletions protocol/x/clob/client/cli/query_stateful_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package cli

import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func CmdListStatefulOrders() *cobra.Command {
Expand Down Expand Up @@ -59,6 +63,13 @@ func CmdGetStatefulOrderCount() *cobra.Command {
return err
}

if _, err := sdk.AccAddressFromBech32(argOwner); err != nil {
return status.Error(
codes.InvalidArgument,
fmt.Sprintf("Invalid owner address: %v", err),
)
}

params := &types.QueryStatefulOrderCountRequest{
SubaccountId: &satypes.SubaccountId{
Owner: argOwner,
Expand Down
57 changes: 42 additions & 15 deletions protocol/x/clob/client/cli/query_stateful_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
clobcli "github.com/dydxprotocol/v4-chain/protocol/x/clob/client/cli"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"testing"
)

Expand All @@ -36,23 +38,48 @@ func TestCmdListStatefulOrders(t *testing.T) {

func TestCmdGetStatefulOrderCount(t *testing.T) {
net, _ := networkWithClobPairObjects(t, 2)

ctx := net.Validators[0].ClientCtx

args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
constants.AliceAccAddress.String(), // owner
"0", // subaccount number
}

out, err := cli.ExecTestCLICmd(ctx, clobcli.CmdGetStatefulOrderCount(), args)
require.NoError(t, err)
for name, tc := range map[string]struct {
owner string
number uint32
count uint32
expectedErr error
}{
"valid": {
owner: constants.AliceAccAddress.String(),
number: 0,
count: 0,
},
"invalid owner": {
owner: "invalid",
number: 0,
expectedErr: status.Error(
codes.InvalidArgument,
"Invalid owner address: decoding bech32 failed: invalid bech32 string length 7",
),
},
} {
t.Run(name, func(t *testing.T) {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
tc.owner,
fmt.Sprintf("%d", tc.number),
}
out, err := cli.ExecTestCLICmd(ctx, clobcli.CmdGetStatefulOrderCount(), args)
if tc.expectedErr != nil {
require.ErrorIs(t, err, tc.expectedErr)
} else {
require.NoError(t, err)

var res types.QueryStatefulOrderCountResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &res))
var res types.QueryStatefulOrderCountResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &res))

// Expect no stateful orders.
require.Equal(t, types.QueryStatefulOrderCountResponse{
Count: 0,
}, res)
// Expect no stateful orders.
require.Equal(t, types.QueryStatefulOrderCountResponse{
Count: tc.count,
}, res)
}
})
}
}

0 comments on commit c6095cb

Please sign in to comment.