Skip to content

Commit 26a2470

Browse files
authored
feat: add param query to x/tariff module (backport #277) (#280)
1 parent e258945 commit 26a2470

File tree

7 files changed

+779
-9
lines changed

7 files changed

+779
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Implement a parameter query for the `x/tariff` module. ([#277](https://github.com/strangelove-ventures/noble/pull/277))

proto/tariff/query.proto

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto3";
2+
3+
package noble.tariff;
4+
5+
import "gogoproto/gogo.proto";
6+
import "google/api/annotations.proto";
7+
import "tariff/params.proto";
8+
9+
option go_package = "github.com/strangelove-ventures/noble/x/tariff/types";
10+
11+
service Query {
12+
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
13+
option (google.api.http).get = "/noble/tariff/v1/params";
14+
}
15+
}
16+
17+
message QueryParamsRequest {}
18+
19+
message QueryParamsResponse {
20+
Params params = 1 [(gogoproto.nullable) = false];
21+
}

x/tariff/client/cli/query.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/cosmos/cosmos-sdk/client"
8+
"github.com/cosmos/cosmos-sdk/client/flags"
9+
"github.com/spf13/cobra"
10+
"github.com/strangelove-ventures/noble/x/tariff/types"
11+
)
12+
13+
// GetQueryCmd returns the cli query commands for this module
14+
func GetQueryCmd() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: types.ModuleName,
17+
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
18+
DisableFlagParsing: true,
19+
SuggestionsMinimumDistance: 2,
20+
RunE: client.ValidateCmd,
21+
}
22+
23+
cmd.AddCommand(CmdQueryParams())
24+
25+
return cmd
26+
}
27+
28+
func CmdQueryParams() *cobra.Command {
29+
cmd := &cobra.Command{
30+
Use: "params",
31+
Short: "shows the parameters of the module",
32+
Args: cobra.NoArgs,
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
clientCtx := client.GetClientContextFromCmd(cmd)
35+
queryClient := types.NewQueryClient(clientCtx)
36+
37+
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
38+
if err != nil {
39+
return err
40+
}
41+
42+
return clientCtx.PrintProto(res)
43+
},
44+
}
45+
46+
flags.AddQueryFlagsToCmd(cmd)
47+
48+
return cmd
49+
}

x/tariff/keeper/query_server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package keeper
2+
3+
import (
4+
"context"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/strangelove-ventures/noble/x/tariff/types"
8+
)
9+
10+
var _ types.QueryServer = Keeper{}
11+
12+
func (k Keeper) Params(goCtx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
13+
ctx := sdk.UnwrapSDKContext(goCtx)
14+
params := k.GetParams(ctx)
15+
16+
return &types.QueryParamsResponse{Params: params}, nil
17+
}

x/tariff/module.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package tariff
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67

7-
"github.com/gorilla/mux"
8-
"github.com/grpc-ecosystem/grpc-gateway/runtime"
9-
"github.com/spf13/cobra"
10-
11-
abci "github.com/tendermint/tendermint/abci/types"
12-
138
"github.com/cosmos/cosmos-sdk/client"
149
"github.com/cosmos/cosmos-sdk/codec"
1510
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
1611
sdk "github.com/cosmos/cosmos-sdk/types"
1712
"github.com/cosmos/cosmos-sdk/types/module"
13+
"github.com/gorilla/mux"
14+
"github.com/grpc-ecosystem/grpc-gateway/runtime"
15+
"github.com/spf13/cobra"
16+
"github.com/strangelove-ventures/noble/x/tariff/client/cli"
1817
"github.com/strangelove-ventures/noble/x/tariff/keeper"
1918
"github.com/strangelove-ventures/noble/x/tariff/types"
19+
abci "github.com/tendermint/tendermint/abci/types"
2020
)
2121

2222
var (
@@ -67,7 +67,9 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
6767
}
6868

6969
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module
70-
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {}
70+
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
71+
_ = types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
72+
}
7173

7274
// GetTxCmd returns the root Tx command for the module. The subcommands of this root command are used by end-users to generate new transactions containing messages defined in the module
7375
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
@@ -76,7 +78,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {
7678

7779
// GetQueryCmd returns the root query command for the module. The subcommands of this root command are used by end-users to generate new queries to the subset of the state defined by the module
7880
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
79-
return nil
81+
return cli.GetQueryCmd()
8082
}
8183

8284
// ----------------------------------------------------------------------------
@@ -118,7 +120,9 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
118120
}
119121

120122
// RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries
121-
func (am AppModule) RegisterServices(cfg module.Configurator) {}
123+
func (am AppModule) RegisterServices(cfg module.Configurator) {
124+
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
125+
}
122126

123127
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
124128
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

0 commit comments

Comments
 (0)