Skip to content

Commit

Permalink
feat: add param query to x/tariff module (backport #277) (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 20, 2023
1 parent e258945 commit 26a2470
Show file tree
Hide file tree
Showing 7 changed files with 779 additions and 9 deletions.
1 change: 1 addition & 0 deletions .changelog/unreleased/improvements/277-tariff-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Implement a parameter query for the `x/tariff` module. ([#277](https://github.com/strangelove-ventures/noble/pull/277))
21 changes: 21 additions & 0 deletions proto/tariff/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

package noble.tariff;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "tariff/params.proto";

option go_package = "github.com/strangelove-ventures/noble/x/tariff/types";

service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/noble/tariff/v1/params";
}
}

message QueryParamsRequest {}

message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}
49 changes: 49 additions & 0 deletions x/tariff/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cli

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/noble/x/tariff/types"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())

return cmd
}

func CmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/tariff/keeper/query_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/noble/x/tariff/types"
)

var _ types.QueryServer = Keeper{}

func (k Keeper) Params(goCtx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)

return &types.QueryParamsResponse{Params: params}, nil
}
22 changes: 13 additions & 9 deletions x/tariff/module.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package tariff

import (
"context"
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/noble/x/tariff/client/cli"
"github.com/strangelove-ventures/noble/x/tariff/keeper"
"github.com/strangelove-ventures/noble/x/tariff/types"
abci "github.com/tendermint/tendermint/abci/types"
)

var (
Expand Down Expand Up @@ -67,7 +67,9 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
}

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

// 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
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
Expand All @@ -76,7 +78,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {

// 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
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
return cli.GetQueryCmd()
}

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

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

// 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)
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
Expand Down
Loading

0 comments on commit 26a2470

Please sign in to comment.