Skip to content

Commit

Permalink
feat: add param query to x/tariff module (#277)
Browse files Browse the repository at this point in the history
(cherry picked from commit c8829e9)

# Conflicts:
#	.changelog/v4.1.0-rc.0/improvements/277-tariff-query.md
#	x/tariff/module.go
  • Loading branch information
johnletey authored and mergify[bot] committed Nov 20, 2023
1 parent 0dae09c commit e93ad10
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 9 deletions.
1 change: 1 addition & 0 deletions .changelog/v4.1.0-rc.0/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/v5/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/v5/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/v5/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
}
27 changes: 18 additions & 9 deletions x/tariff/module.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
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"
<<<<<<< HEAD
"github.com/strangelove-ventures/noble/v4/x/tariff/keeper"
"github.com/strangelove-ventures/noble/v4/x/tariff/types"
=======
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/noble/v5/x/tariff/client/cli"
"github.com/strangelove-ventures/noble/v5/x/tariff/keeper"
"github.com/strangelove-ventures/noble/v5/x/tariff/types"
abci "github.com/tendermint/tendermint/abci/types"
>>>>>>> c8829e9 (feat: add param query to `x/tariff` module (#277))
)

var (
Expand Down Expand Up @@ -67,7 +72,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 +83,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 +125,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 e93ad10

Please sign in to comment.