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

Receive Denom as Query String in Req #518

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion proto/cosmos/bank/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ service Query {

// SupplyOf queries the supply of a single coin.
rpc SupplyOf(QuerySupplyOfRequest) returns (QuerySupplyOfResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply/{denom}";
option (google.api.http).get = "/cosmos/bank/v1beta1/supply/by_denom";
}

// Params queries the parameters of x/bank module.
Expand All @@ -47,6 +47,12 @@ service Query {
option (google.api.http).get = "/cosmos/bank/v1beta1/denoms_metadata/{denom}";
}

// DenomMetadataByQueryString queries the client metadata of a given coin denomination.
// Identical with DenomsMetadata but receives denom as query string.
rpc DenomMetadataByQueryString(QueryDenomMetadataByQueryStringRequest) returns (QueryDenomMetadataByQueryStringResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/denoms_metadata_by_query_string";
}

// DenomsMetadata queries the client metadata for all registered coin denominations.
rpc DenomsMetadata(QueryDenomsMetadataRequest) returns (QueryDenomsMetadataResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/denoms_metadata";
Expand Down Expand Up @@ -185,9 +191,23 @@ message QueryDenomMetadataRequest {
string denom = 1;
}

// QueryDenomMetadataByQueryStringRequest is the request type for the Query/DenomMetadata RPC method.
// Identical with QueryDenomMetadataRequest but receives denom as query string.
message QueryDenomMetadataByQueryStringRequest {
// denom is the coin denom to query the metadata for.
string denom = 1;
}

// QueryDenomMetadataResponse is the response type for the Query/DenomMetadata RPC
// method.
message QueryDenomMetadataResponse {
// metadata describes and provides all the client information for the requested token.
Metadata metadata = 1 [(gogoproto.nullable) = false];
}

// QueryDenomMetadataByQueryStringResponse is the response type for the Query/DenomMetadata RPC
// method. Identical with QueryDenomMetadataResponse but receives denom as query string in request.
message QueryDenomMetadataByQueryStringResponse {
// metadata describes and provides all the client information for the requested token.
Metadata metadata = 1 [(gogoproto.nullable) = false];
}
16 changes: 16 additions & 0 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,19 @@
Metadata: metadata,
}, nil
}

// DenomMetadataByQueryString is identical to DenomMetadata query, but receives request via query string.
func (k BaseKeeper) DenomMetadataByQueryString(ctx context.Context, req *types.QueryDenomMetadataByQueryStringRequest) (*types.QueryDenomMetadataByQueryStringResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

Check warning on line 208 in x/bank/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/bank/keeper/grpc_query.go#L205-L208

Added lines #L205 - L208 were not covered by tests

res, err := k.DenomMetadata(ctx, &types.QueryDenomMetadataRequest{
Denom: req.Denom,
})
if err != nil {
return nil, err
}

Check warning on line 215 in x/bank/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/bank/keeper/grpc_query.go#L210-L215

Added lines #L210 - L215 were not covered by tests

return &types.QueryDenomMetadataByQueryStringResponse{Metadata: res.Metadata}, nil

Check warning on line 217 in x/bank/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/bank/keeper/grpc_query.go#L217

Added line #L217 was not covered by tests
}
77 changes: 77 additions & 0 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,80 @@ func (suite *IntegrationTestSuite) QueryDenomMetadataRequest() {
})
}
}

func (suite *IntegrationTestSuite) QueryDenomMetadataByQueryStringRequest() {
var (
req *types.QueryDenomMetadataByQueryStringRequest
expMetadata = types.Metadata{}
)

testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"empty denom",
func() {
req = &types.QueryDenomMetadataByQueryStringRequest{}
},
false,
},
{
"not found denom",
func() {
req = &types.QueryDenomMetadataByQueryStringRequest{
Denom: "foo",
}
},
false,
},
{
"success",
func() {
expMetadata := types.Metadata{
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Denom: "uatom",
Exponent: 0,
Aliases: []string{"microatom"},
},
{
Denom: "atom",
Exponent: 6,
Aliases: []string{"ATOM"},
},
},
Base: "uatom",
Display: "atom",
}

suite.app.BankKeeper.SetDenomMetaData(suite.ctx, expMetadata)
req = &types.QueryDenomMetadataByQueryStringRequest{
Denom: expMetadata.Base,
}
},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

tc.malleate()
ctx := sdk.WrapSDKContext(suite.ctx)

res, err := suite.queryClient.DenomMetadataByQueryString(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expMetadata, res.Metadata)
} else {
suite.Require().Error(err)
}
})
}
}
Loading
Loading