Skip to content

Commit

Permalink
[CORE-823] Implement queries for x/ratelimit; get limiter and capacit…
Browse files Browse the repository at this point in the history
…y together in keeper (#1013)

* Implement queries for x/ratelimit

* nit

* fix test

* comments
  • Loading branch information
teddyding authored Jan 26, 2024
1 parent 39046a5 commit 514aca5
Show file tree
Hide file tree
Showing 23 changed files with 915 additions and 589 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Limiter, LimiterSDKType } from "./limit_params";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** DenomCapacity stores a list of rate limit capacity for a denom. */
Expand Down Expand Up @@ -32,6 +33,18 @@ export interface DenomCapacitySDKType {

capacity_list: Uint8Array[];
}
/** LimiterCapacity contains a pair of limiter and its corresponding capacity. */

export interface LimiterCapacity {
limiter?: Limiter;
capacity: Uint8Array;
}
/** LimiterCapacity contains a pair of limiter and its corresponding capacity. */

export interface LimiterCapacitySDKType {
limiter?: LimiterSDKType;
capacity: Uint8Array;
}

function createBaseDenomCapacity(): DenomCapacity {
return {
Expand Down Expand Up @@ -86,4 +99,59 @@ export const DenomCapacity = {
return message;
}

};

function createBaseLimiterCapacity(): LimiterCapacity {
return {
limiter: undefined,
capacity: new Uint8Array()
};
}

export const LimiterCapacity = {
encode(message: LimiterCapacity, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.limiter !== undefined) {
Limiter.encode(message.limiter, writer.uint32(10).fork()).ldelim();
}

if (message.capacity.length !== 0) {
writer.uint32(18).bytes(message.capacity);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): LimiterCapacity {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseLimiterCapacity();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.limiter = Limiter.decode(reader, reader.uint32());
break;

case 2:
message.capacity = reader.bytes();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<LimiterCapacity>): LimiterCapacity {
const message = createBaseLimiterCapacity();
message.limiter = object.limiter !== undefined && object.limiter !== null ? Limiter.fromPartial(object.limiter) : undefined;
message.capacity = object.capacity ?? new Uint8Array();
return message;
}

};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LimitParams, LimitParamsSDKType } from "./limit_params";
import { Duration, DurationSDKType } from "../../google/protobuf/duration";
import { LimiterCapacity, LimiterCapacitySDKType } from "./capacity";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** ListLimitParamsRequest is a request type of the ListLimitParams RPC method. */
Expand All @@ -11,13 +11,11 @@ export interface ListLimitParamsRequestSDKType {}
/** ListLimitParamsResponse is a response type of the ListLimitParams RPC method. */

export interface ListLimitParamsResponse {
/** ListLimitParamsResponse is a response type of the ListLimitParams RPC method. */
limitParamsList: LimitParams[];
}
/** ListLimitParamsResponse is a response type of the ListLimitParams RPC method. */

export interface ListLimitParamsResponseSDKType {
/** ListLimitParamsResponse is a response type of the ListLimitParams RPC method. */
limit_params_list: LimitParamsSDKType[];
}
/**
Expand All @@ -44,41 +42,21 @@ export interface QueryCapacityByDenomRequestSDKType {
*/
denom: string;
}
/** CapacityResult is a specific rate limit for a denom. */

export interface CapacityResult {
period?: Duration;
capacity: Uint8Array;
}
/** CapacityResult is a specific rate limit for a denom. */

export interface CapacityResultSDKType {
period?: DurationSDKType;
capacity: Uint8Array;
}
/**
* QueryCapacityByDenomResponse is a response type of the CapacityByDenom RPC
* method.
*/

export interface QueryCapacityByDenomResponse {
/**
* QueryCapacityByDenomResponse is a response type of the CapacityByDenom RPC
* method.
*/
results: CapacityResult[];
limiterCapacityList: LimiterCapacity[];
}
/**
* QueryCapacityByDenomResponse is a response type of the CapacityByDenom RPC
* method.
*/

export interface QueryCapacityByDenomResponseSDKType {
/**
* QueryCapacityByDenomResponse is a response type of the CapacityByDenom RPC
* method.
*/
results: CapacityResultSDKType[];
limiter_capacity_list: LimiterCapacitySDKType[];
}

function createBaseListLimitParamsRequest(): ListLimitParamsRequest {
Expand Down Expand Up @@ -205,71 +183,16 @@ export const QueryCapacityByDenomRequest = {

};

function createBaseCapacityResult(): CapacityResult {
return {
period: undefined,
capacity: new Uint8Array()
};
}

export const CapacityResult = {
encode(message: CapacityResult, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.period !== undefined) {
Duration.encode(message.period, writer.uint32(10).fork()).ldelim();
}

if (message.capacity.length !== 0) {
writer.uint32(18).bytes(message.capacity);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): CapacityResult {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseCapacityResult();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.period = Duration.decode(reader, reader.uint32());
break;

case 2:
message.capacity = reader.bytes();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<CapacityResult>): CapacityResult {
const message = createBaseCapacityResult();
message.period = object.period !== undefined && object.period !== null ? Duration.fromPartial(object.period) : undefined;
message.capacity = object.capacity ?? new Uint8Array();
return message;
}

};

function createBaseQueryCapacityByDenomResponse(): QueryCapacityByDenomResponse {
return {
results: []
limiterCapacityList: []
};
}

export const QueryCapacityByDenomResponse = {
encode(message: QueryCapacityByDenomResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
for (const v of message.results) {
CapacityResult.encode(v!, writer.uint32(10).fork()).ldelim();
for (const v of message.limiterCapacityList) {
LimiterCapacity.encode(v!, writer.uint32(10).fork()).ldelim();
}

return writer;
Expand All @@ -285,7 +208,7 @@ export const QueryCapacityByDenomResponse = {

switch (tag >>> 3) {
case 1:
message.results.push(CapacityResult.decode(reader, reader.uint32()));
message.limiterCapacityList.push(LimiterCapacity.decode(reader, reader.uint32()));
break;

default:
Expand All @@ -299,7 +222,7 @@ export const QueryCapacityByDenomResponse = {

fromPartial(object: DeepPartial<QueryCapacityByDenomResponse>): QueryCapacityByDenomResponse {
const message = createBaseQueryCapacityByDenomResponse();
message.results = object.results?.map(e => CapacityResult.fromPartial(e)) || [];
message.limiterCapacityList = object.limiterCapacityList?.map(e => LimiterCapacity.fromPartial(e)) || [];
return message;
}

Expand Down
11 changes: 11 additions & 0 deletions proto/dydxprotocol/ratelimit/capacity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package dydxprotocol.ratelimit;

import "gogoproto/gogo.proto";
import "dydxprotocol/ratelimit/limit_params.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types";

Expand All @@ -19,3 +20,13 @@ message DenomCapacity {
(gogoproto.nullable) = false
];
}

// LimiterCapacity contains a pair of limiter and its corresponding capacity.
message LimiterCapacity {
Limiter limiter = 1 [ (gogoproto.nullable) = false ];
bytes capacity = 2 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}
22 changes: 8 additions & 14 deletions proto/dydxprotocol/ratelimit/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package dydxprotocol.ratelimit;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "dydxprotocol/ratelimit/limit_params.proto";
import "google/protobuf/duration.proto";
import "dydxprotocol/ratelimit/capacity.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types";

Expand All @@ -29,23 +29,17 @@ service Query {
message ListLimitParamsRequest {}

// ListLimitParamsResponse is a response type of the ListLimitParams RPC method.
message ListLimitParamsResponse { repeated LimitParams limit_params_list = 1; }
message ListLimitParamsResponse {
repeated LimitParams limit_params_list = 1 [ (gogoproto.nullable) = false ];
}

// QueryCapacityByDenomRequest is a request type for the CapacityByDenom RPC
// method.
message QueryCapacityByDenomRequest { string denom = 1; }

// CapacityResult is a specific rate limit for a denom.
message CapacityResult {
google.protobuf.Duration period = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
bytes capacity = 2 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}

// QueryCapacityByDenomResponse is a response type of the CapacityByDenom RPC
// method.
message QueryCapacityByDenomResponse { repeated CapacityResult results = 1; }
message QueryCapacityByDenomResponse {
repeated LimiterCapacity limiter_capacity_list = 1
[ (gogoproto.nullable) = false ];
}
20 changes: 20 additions & 0 deletions protocol/testutil/bank/bank_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,23 @@ func FundModuleAccount(

return bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, moduleName, amounts)
}

func FilterDenomFromBalances(
balances []banktypes.Balance,
denom string,
) []banktypes.Balance {
newBalances := make([]banktypes.Balance, len(balances))
for i, balance := range balances {
newCoins := []sdk.Coin{}
for _, coin := range balance.Coins {
if coin.Denom != denom {
newCoins = append(newCoins, coin)
}
}
newBalances[i] = banktypes.Balance{
Address: balance.Address,
Coins: newCoins,
}
}
return newBalances
}
35 changes: 35 additions & 0 deletions protocol/x/ratelimit/client/cli/list_limit_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types"
"github.com/spf13/cobra"
)

func CmdListLimitParams() *cobra.Command {
cmd := &cobra.Command{
Use: "list-limit-params",
Short: "list all limit params",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.ListLimitParams(cmd.Context(), &types.ListLimitParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit 514aca5

Please sign in to comment.