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

[CORE-689] - CLI commands for x/clob params #699

Merged
merged 4 commits into from
Oct 25, 2023

Conversation

clemire
Copy link
Contributor

@clemire clemire commented Oct 24, 2023

Changelist

Added cli query support for liquidations config, block rate limit config, equity tier limit config.

Test Plan

standard gRPC and CLI tests. Manual test results below.

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

CLI commands on locahost docker:

~ # dydxprotocold query clob
Querying commands for the clob module

Usage:
  dydxprotocold query clob [flags]
  dydxprotocold query clob [command]

Available Commands:
  get-block-rate-limit-config  get the block rate limit configuration
  get-equity-tier-limit-config get the equity tier limit configuration
  get-liquidations-config      get the liquidations configuration
  list-clob-pair               list all clob_pair
  show-clob-pair               shows a clob_pair

Flags:
  -h, --help   help for clob

Global Flags:
      --chain-id string     The network chain ID
      --home string         directory for config and data (default "/dydxprotocol/.dydxprotocol")
      --log_format string   The logging format (json|plain) (default "plain")
      --log_level string    The logging level (trace|debug|info|warn|error|fatal|panic) (default "info")
      --trace               print out full stack trace on errors

Use "dydxprotocold query clob [command] --help" for more information about a command.
~ # dydxprotocold query clob get-liquidations-config 
liquidations_config:
  fillable_price_config:
    bankruptcy_adjustment_ppm: 1000000
    spread_to_maintenance_margin_ratio_ppm: 1500000
  max_liquidation_fee_ppm: 15000
  position_block_limits:
    max_position_portion_liquidated_ppm: 100000
    min_position_notional_liquidated: "1000000000"
  subaccount_block_limits:
    max_notional_liquidated: "100000000000"
    max_quantums_insurance_lost: "1000000000000"
~ # dydxprotocold query clob get-equity-tier-limit-config
equity_tier_limit_config:
  short_term_order_equity_tiers:
  - limit: 0
    usd_tnc_required: "0"
  - limit: 1
    usd_tnc_required: "20000000"
  - limit: 5
    usd_tnc_required: "100000000"
  - limit: 10
    usd_tnc_required: "1000000000"
  - limit: 100
    usd_tnc_required: "10000000000"
  - limit: 1000
    usd_tnc_required: "100000000000"
  stateful_order_equity_tiers:
  - limit: 0
    usd_tnc_required: "0"
  - limit: 1
    usd_tnc_required: "20000000"
  - limit: 5
    usd_tnc_required: "100000000"
  - limit: 10
    usd_tnc_required: "1000000000"
  - limit: 100
    usd_tnc_required: "10000000000"
  - limit: 200
    usd_tnc_required: "100000000000"
~ # 
~ # dydxprotocold query clob get-block-rate-limit-config
block_rate_limit_config:
  max_short_term_order_cancellations_per_n_blocks:
  - limit: 200
    num_blocks: 1
  max_short_term_orders_per_n_blocks:
  - limit: 200
    num_blocks: 1
  max_stateful_orders_per_n_blocks:
  - limit: 2
    num_blocks: 1
  - limit: 20
    num_blocks: 100
~ # 

@linear
Copy link

linear bot commented Oct 24, 2023

CORE-689 gRPC queries for clob params

Block rate limit
Liquidations config
Equity tier limit

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2023

Walkthrough

The changes introduce new functionalities to query block rate limit configuration, liquidations configuration, and equity tier limit configuration. This is achieved through the addition of new RPC methods, CLI commands, and corresponding tests. The changes span across multiple files, including protobuf definitions, client-side CLI commands, server-side gRPC handlers, and their respective test files.

Changes

File(s) Summary
.../v4-protos/src/codegen/dydxprotocol/clob/query.*.ts
proto/dydxprotocol/clob/query.proto
Added new RPC methods and corresponding TypeScript functions to query block rate limit and liquidations configurations.
protocol/mocks/QueryClient.go Added mock implementations for the new query functions.
protocol/x/clob/client/cli/query_*.go Added new CLI commands to query the configurations.
protocol/x/clob/client/cli/query_*_test.go Added tests for the new CLI commands.
protocol/x/clob/keeper/grpc_query_*.go Added gRPC handlers for the new query methods.
protocol/x/clob/keeper/grpc_query_*_test.go Added tests for the new gRPC handlers.
protocol/x/clob/module_test.go Updated the GetQueryCmd function to include the new commands.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ed6429f and 9b309ec.
Files ignored due to filter (2)
  • protocol/x/clob/types/query.pb.go
  • protocol/x/clob/types/query.pb.gw.go
Files selected for processing (16)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.lcd.ts (3 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.rpc.Query.ts (5 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.ts (3 hunks)
  • proto/dydxprotocol/clob/query.proto (3 hunks)
  • protocol/mocks/QueryClient.go (2 hunks)
  • protocol/x/clob/client/cli/query.go (1 hunks)
  • protocol/x/clob/client/cli/query_block_rate_limit_configuration.go (1 hunks)
  • protocol/x/clob/client/cli/query_block_rate_limit_configuration_test.go (1 hunks)
  • protocol/x/clob/client/cli/query_equity_tier_limit_config.go (1 hunks)
  • protocol/x/clob/client/cli/query_equity_tier_limit_config_test.go (1 hunks)
  • protocol/x/clob/client/cli/query_liquidations_configuration.go (1 hunks)
  • protocol/x/clob/client/cli/query_liquidations_configuration_test.go (1 hunks)
  • protocol/x/clob/keeper/grpc_query_block_rate_limit_configuration.go (1 hunks)
  • protocol/x/clob/keeper/grpc_query_block_rate_limit_configuration_test.go (1 hunks)
  • protocol/x/clob/keeper/grpc_query_liquidations_configuration.go (1 hunks)
  • protocol/x/clob/keeper/grpc_query_liquidations_configuration_test.go (1 hunks)
Files skipped from review due to trivial changes (5)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.ts
  • protocol/x/clob/client/cli/query.go
  • protocol/x/clob/client/cli/query_block_rate_limit_configuration_test.go
  • protocol/x/clob/keeper/grpc_query_block_rate_limit_configuration_test.go
  • protocol/x/clob/keeper/grpc_query_liquidations_configuration_test.go
Additional comments: 19
protocol/x/clob/client/cli/query_liquidations_configuration.go (1)
  • 1-34: The new hunk introduces a new CLI command get-liquidations-config to query the liquidations configuration. It uses the QueryLiquidationsConfigurationRequest message to make the query and prints the response. The error handling is done correctly, and the code is clean and maintainable. However, it's important to ensure that the LiquidationsConfiguration function in the QueryClient interface is implemented correctly and returns the expected results.
protocol/x/clob/client/cli/query_block_rate_limit_configuration.go (1)
  • 1-34: The code looks good overall. It correctly sets up a new CLI command to query the block rate limit configuration. The command uses the QueryClient to make the request and handles any errors that might occur. The result is then printed using the PrintProto function. The command is also correctly added to the Cobra command tree with the AddQueryFlagsToCmd function. However, it's important to ensure that the BlockRateLimitConfiguration function in the QueryClient is implemented correctly and returns the expected results.
protocol/x/clob/client/cli/query_equity_tier_limit_config.go (1)
  • 1-34: The new hunk introduces a new CLI command get-equity-tier-limit-config to query the equity tier limit configuration. The command uses the EquityTierLimitConfiguration function from the QueryClient interface to make the query and prints the result using the PrintProto function from the clientCtx. The error handling is done correctly, and the function signature is consistent with the other CLI commands in the codebase. The code is modular, maintainable, and follows best practices.
protocol/x/clob/client/cli/query_liquidations_configuration_test.go (1)
  • 15-32: The test TestCmdGetLiquidationsConfiguration is well written and covers the main functionality of the CmdGetLiquidationsConfiguration command. It checks that the command executes without errors and that the output is correctly unmarshalled into a QueryLiquidationsConfigurationResponse object. It also verifies that the LiquidationsConfig field of the response is not nil and matches the expected default value. This test ensures that the command is working as expected and returning the correct configuration.
protocol/x/clob/keeper/grpc_query_liquidations_configuration.go (1)
  • 1-25: The function LiquidationsConfiguration is well implemented. It checks if the request is nil and returns an error if it is, which is good for error handling. It then retrieves the liquidations configuration using the GetLiquidationsConfig method and returns it in the response. The use of UnwrapSDKContext is correct to convert the gRPC context to the SDK context. The error message in line 17 is clear and informative.
protocol/x/clob/keeper/grpc_query_block_rate_limit_configuration.go (1)
  • 11-25: The function BlockRateLimitConfiguration is well implemented. It checks if the request is nil and returns an error if it is, which is a good practice for error handling. It then retrieves the block rate limit configuration and returns it in the response. The function is simple, clear, and follows the best practices for gRPC handlers.
proto/dydxprotocol/clob/query.proto (3)
  • 7-10: The new imports for block_rate_limit_config.proto and liquidations_config.proto are added to support the new RPC methods for querying block rate limit and liquidations configurations. Ensure these proto files exist and are correctly defined.

  • 46-57: Two new RPC methods BlockRateLimitConfiguration and LiquidationsConfiguration are added to the service. These methods are used to query the block rate limit and liquidations configurations respectively. The HTTP GET endpoints for these methods are also defined. Ensure these endpoints are correctly integrated into the server routing.

  • 134-153: New request and response message types are defined for the new RPC methods. The request messages are empty as no parameters are required for these queries. The response messages contain the queried configurations as non-nullable fields. Ensure that the server always returns a valid configuration in the response to avoid null value errors.

indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.lcd.ts (3)
  • 3-3: The import statement has been extended to include new types related to block rate limit configuration and liquidations configuration. Ensure that these types are correctly defined and used in the rest of the code.

  • 16-17: Two new methods blockRateLimitConfiguration and liquidationsConfiguration are being bound to the class instance. Ensure that these methods are correctly defined and used in the rest of the code.

  • 50-63: Two new methods blockRateLimitConfiguration and liquidationsConfiguration have been added to the LCDQueryClient class. These methods are used to query block rate limit configuration and liquidations configuration respectively. They construct the API endpoint and make a GET request to that endpoint using the req object. They return a promise that resolves to a QueryBlockRateLimitConfigurationResponseSDKType and QueryLiquidationsConfigurationResponseSDKType object respectively. Ensure that these methods are correctly used in the rest of the code.

protocol/mocks/QueryClient.go (2)
  • 149-177: The new mock function BlockRateLimitConfiguration is added correctly. It follows the same pattern as the other mock functions in the QueryClient struct. It takes a context, a QueryBlockRateLimitConfigurationRequest object, and a variadic parameter of grpc.CallOption. It returns a QueryBlockRateLimitConfigurationResponse object and an error. The function uses the testify mock package's Called method to simulate a function call and return a Call object. The Call object's Get method is used to retrieve the return values of the function call. The function handles the case where the return value is a function and calls it with the provided arguments. It also handles the case where the return value is not a function and directly assigns it to the return variable. The function then returns the response object and error.

  • 299-327: The new mock function LiquidationsConfiguration is added correctly. It follows the same pattern as the other mock functions in the QueryClient struct. It takes a context, a QueryLiquidationsConfigurationRequest object, and a variadic parameter of grpc.CallOption. It returns a QueryLiquidationsConfigurationResponse object and an error. The function uses the testify mock package's Called method to simulate a function call and return a Call object. The Call object's Get method is used to retrieve the return values of the function call. The function handles the case where the return value is a function and calls it with the provided arguments. It also handles the case where the return value is not a function and directly assigns it to the return variable. The function then returns the response object and error.

indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.rpc.Query.ts (5)
  • 4-4: The import statement has been extended to include new request and response types for querying block rate limit configuration and liquidations configuration. This change is necessary to support the new query methods added to the Query interface and QueryClientImpl class.

  • 22-27: New methods blockRateLimitConfiguration and liquidationsConfiguration have been added to the Query interface. These methods take optional request parameters and return promises that resolve to the corresponding response types. These changes are in line with the existing structure of the interface and provide additional functionality for querying block rate limit and liquidations configurations.

  • 39-40: The blockRateLimitConfiguration and liquidationsConfiguration methods are bound to the this context in the constructor of the QueryClientImpl class. This is a standard practice in JavaScript to ensure that the methods have the correct this context when they are called.

  • 75-85: The blockRateLimitConfiguration and liquidationsConfiguration methods are implemented in the QueryClientImpl class. These methods encode the request parameters, send a gRPC request using the rpc instance, and return a promise that resolves to the decoded response. The implementation of these methods is consistent with the other methods in the class and follows best practices for handling gRPC requests and responses.

  • 110-117: The blockRateLimitConfiguration and liquidationsConfiguration methods are added to the QueryClient extension returned by the createRpcQueryExtension function. These methods call the corresponding methods on the queryService instance, which is an instance of QueryClientImpl. This allows the extended QueryClient to use the new query methods.

Comment on lines +22 to +39
func TestCmdGetEquityTierLimitConfig(t *testing.T) {
net, _ := networkWithClobPairObjects(t, 2)
ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}

out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
require.NoError(t, err)
var resp types.QueryEquityTierLimitConfigurationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.EquityTierLimitConfig)
require.Equal(
t,
emptyEquityTierLimitConfig,
resp.EquityTierLimitConfig,
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test TestCmdGetEquityTierLimitConfig is checking if the CmdGetEquityTierLimitConfig command returns the expected emptyEquityTierLimitConfig. However, it does not test the case where the configuration is not empty. It would be beneficial to add a test case where the configuration has some values to ensure that the command works correctly in all scenarios.

func TestCmdGetEquityTierLimitConfig(t *testing.T) {
	net, _ := networkWithClobPairObjects(t, 2)
	ctx := net.Validators[0].ClientCtx
	common := []string{
		fmt.Sprintf("--%s=json", tmcli.OutputFlag),
	}

	out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
	require.NoError(t, err)
	var resp types.QueryEquityTierLimitConfigurationResponse
	require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
	require.NotNil(t, resp.EquityTierLimitConfig)
	require.Equal(
		t,
		emptyEquityTierLimitConfig,
		resp.EquityTierLimitConfig,
	)
+   // Add a test case for non-empty configuration
+   nonEmptyConfig := types.EquityTierLimitConfiguration{
+       ShortTermOrderEquityTiers: []types.EquityTierLimit{{Tier: 1, Limit: 100}},
+       StatefulOrderEquityTiers:  []types.EquityTierLimit{{Tier: 2, Limit: 200}},
+   }
+   // Set the configuration in the network
+   // ...
+   // Execute the command again
+   out, err = clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
+   require.NoError(t, err)
+   require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
+   require.NotNil(t, resp.EquityTierLimitConfig)
+   require.Equal(
+       t,
+       nonEmptyConfig,
+       resp.EquityTierLimitConfig,
+   )
}
Committable suggestion (Beta)
Suggested change
func TestCmdGetEquityTierLimitConfig(t *testing.T) {
net, _ := networkWithClobPairObjects(t, 2)
ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
require.NoError(t, err)
var resp types.QueryEquityTierLimitConfigurationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.EquityTierLimitConfig)
require.Equal(
t,
emptyEquityTierLimitConfig,
resp.EquityTierLimitConfig,
)
}
func TestCmdGetEquityTierLimitConfig(t *testing.T) {
net, _ := networkWithClobPairObjects(t, 2)
ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
require.NoError(t, err)
var resp types.QueryEquityTierLimitConfigurationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.EquityTierLimitConfig)
require.Equal(
t,
emptyEquityTierLimitConfig,
resp.EquityTierLimitConfig,
)
// Add a test case for non-empty configuration
nonEmptyConfig := types.EquityTierLimitConfiguration{
ShortTermOrderEquityTiers: []types.EquityTierLimit{{Tier: 1, Limit: 100}},
StatefulOrderEquityTiers: []types.EquityTierLimit{{Tier: 2, Limit: 200}},
}
// Set the configuration in the network
// ...
// Execute the command again
out, err = clitestutil.ExecTestCLICmd(ctx, cli.CmdGetEquityTierLimitConfig(), common)
require.NoError(t, err)
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.EquityTierLimitConfig)
require.Equal(
t,
nonEmptyConfig,
resp.EquityTierLimitConfig,
)
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9b309ec and 535d542.
Files selected for processing (1)
  • protocol/x/clob/module_test.go (1 hunks)
Additional comments: 1
protocol/x/clob/module_test.go (1)
  • 277-282: The number of commands and their names have been updated correctly to reflect the new functionality added to the clob module. Ensure that these new commands (get-block-rate-limit-config, get-equity-tier-limit-config, get-liquidations-config) are implemented correctly and their functionality is tested elsewhere in the codebase.

// Queries LiquidationsConfiguration.
rpc LiquidationsConfiguration(QueryLiquidationsConfigurationRequest)
returns (QueryLiquidationsConfigurationResponse) {
option (google.api.http).get = "/dydxprotocol/clob/liquidations";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe liquidations_config?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 535d542 and 98befaf.
Files ignored due to filter (2)
  • protocol/x/clob/types/query.pb.go
  • protocol/x/clob/types/query.pb.gw.go
Files selected for processing (2)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.lcd.ts (3 hunks)
  • proto/dydxprotocol/clob/query.proto (3 hunks)
Additional comments: 6
proto/dydxprotocol/clob/query.proto (3)
  • 7-10: The new imports for block_rate_limit_config.proto and liquidations_config.proto are necessary for the newly added RPC methods. Ensure that these proto files exist and are correctly defined.

  • 47-57: Two new RPC methods BlockRateLimitConfiguration and LiquidationsConfiguration have been added. These methods are used to query the block rate limit configuration and liquidations configuration respectively. The HTTP GET paths for these methods are correctly defined.

  • 134-153: New request and response message types have been defined for the newly added RPC methods. The request messages are empty as no parameters are required for these queries. The response messages contain the respective configurations as non-nullable fields. This is acceptable as long as the configurations are guaranteed to exist in the system.

indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/query.lcd.ts (3)
  • 3-3: The import statement has been updated to include new types related to block rate limit configuration and liquidations configuration. This is in line with the changes made in the rest of the file.

  • 16-17: The constructor of LCDQueryClient has been updated to bind the new methods blockRateLimitConfiguration and liquidationsConfiguration to the instance. This is necessary to ensure that the methods have the correct this context when called.

  • 50-63: Two new methods have been added to the LCDQueryClient class: blockRateLimitConfiguration and liquidationsConfiguration. These methods construct the respective API endpoints and make GET requests to them using the req object. They return promises that resolve to the respective response types. The methods are correctly implemented and follow the same pattern as the existing methods in the class.

@clemire clemire added the backport/protocol/v1.x Backport v1.x label Oct 25, 2023
@clemire clemire merged commit f1c7a9d into main Oct 25, 2023
30 of 31 checks passed
@clemire clemire deleted the crystal/CORE-689-clob-params branch October 25, 2023 21:54
mergify bot pushed a commit that referenced this pull request Oct 25, 2023
clemire pushed a commit that referenced this pull request Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants