Skip to content

Commit

Permalink
add custom parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Jun 17, 2023
1 parent 16be294 commit 629dea2
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import "gogoproto/gogo.proto";

option go_package = "<%= modulePath %>/x/<%= moduleName %>/types";

// Params defines the parameters for the auth module.
// Params defines the parameters for the module.
message Params {
option (amino.name) = "<%= appName %>/x/<%= moduleName %>/Params";
option (gogoproto.equal) = true;

<%= for (i, param) in params { %>
<%= param.ProtoType(i+1) %> [(gogoproto.moretags) = "yaml:\"<%= param.Name.Snake %>\""];<% } %>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ option go_package = "<%= modulePath %>/x/<%= moduleName %>/types";

// Query defines the gRPC querier service.
service Query {
// Params queries all parameters.
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/<%= appName %>/<%= moduleName %>/v1/params";
option (google.api.http).get = "<%= apiPath %>/params";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
// QueryParamsResponse is response type for the Query/Params RPC method.
message QueryParamsResponse {
// params defines the parameters of the module.
// params holds all the parameters of this module.
Params params = 1 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package keeper
import (
"testing"

tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"

"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmdb "github.com/cometbft/cometbft-db"
)

func <%= title(moduleName) %>Keeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
Expand All @@ -28,11 +31,13 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (*keeper.Keeper, sdk.Context)

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey, <%= for (dependency) in dependencies { %>
memStoreKey,
authority.String(), <%= for (dependency) in dependencies { %>
nil,<% } %>
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"<%= modulePath %>/x/<%= moduleName %>/types"
)

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, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

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

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the module's exported genesis
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

// this line is used by starport scaffolding # genesis/module/export

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package keeper_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

keepertest "<%= modulePath %>/testutil/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
)

func TestMsgUpdateParams(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ import (
"<%= modulePath %>/x/<%= moduleName %>/types"
)

// SetParams sets the gov module's parameters.

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params
}

k.cdc.MustUnmarshal(bz, &params)
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&params)
Expand All @@ -17,15 +30,3 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {

return nil
}

// GetParams gets the gov module's parameters.
func (k Keeper) GetParams(clientCtx sdk.Context) (params types.Params) {
store := clientCtx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params
}

k.cdc.MustUnmarshal(bz, &params)
return params
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (AppModuleBasic) Name() string {

// RegisterLegacyAminoCodec registers the amino codec for the module, which is used to marshal and unmarshal structs to/from []byte in order to persist them in the module's KVStore
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterCodec(cdc)
types.RegisterLegacyAminoCodec(cdc)
}

// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
// this line is used by starport scaffolding # 1
)

func RegisterCodec(cdc *codec.LegacyAmino) {
// RegisterLegacyAminoCodec registers concrete types on the LegacyAmino codec
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
// this line is used by starport scaffolding # 2

cdc.RegisterConcrete(Params{}, "<%= appName %>/x/<%= moduleName %>/Params", nil)
legacy.RegisterAminoMsg(cdc, &MsgUpdateParams{}, "<%= appName %>/x/<%= moduleName %>/MsgUpdateParams")
}
}


func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
// this line is used by starport scaffolding # 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const DefaultIndex uint64 = 1
func DefaultGenesis() *GenesisState {
return &GenesisState{
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
}
}

Expand All @@ -19,5 +20,5 @@ func DefaultGenesis() *GenesisState {
func (gs GenesisState) Validate() error {
// this line is used by starport scaffolding # genesis/types/validate

return nil
return gs.Params.Validate()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
<%= if (len(params) > 0) { %>"fmt"<% } %>

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

Expand All @@ -12,21 +14,48 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params instance
func NewParams() Params {
return Params{}
func NewParams(<%= for (param) in params { %>
<%= param.Name.LowerCamel %> <%= param.DataType() %>,<% } %>
) Params {
return Params{<%= for (param) in params { %>
<%= param.Name.UpperCamel %>: <%= param.Name.LowerCamel %>,<% } %>
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams()
return NewParams(<%= for (param) in params { %>
Default<%= param.Name.UpperCamel %>,<% } %>
)
}

// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{}
return paramtypes.ParamSetPairs{<%= for (param) in params { %>
paramtypes.NewParamSetPair(Key<%= param.Name.UpperCamel %>, &p.<%= param.Name.UpperCamel %>, validate<%= param.Name.UpperCamel %>),<% } %>
}
}

// Validate validates the set of params
func (p Params) Validate() error {
func (p Params) Validate() error {<%= for (param) in params { %>
if err := validate<%= param.Name.UpperCamel %>(p.<%= param.Name.UpperCamel %>); err != nil {
return err
}
<% } %>
return nil
}

<%= for (param) in params { %>
// validate<%= param.Name.UpperCamel %> validates the <%= param.Name.UpperCamel %> param
func validate<%= param.Name.UpperCamel %>(v interface{}) error {
<%= param.Name.LowerCamel %>, ok := v.(<%= param.DataType() %>)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}

// TODO implement validation
_ = <%= param.Name.LowerCamel %>

return nil
}
<% } %>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
<%= if (isIBC) { %>PortId: types.PortID,<% } %>
// this line is used by starport scaffolding # genesis/test/state
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ package keeper
import (
"testing"

"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/stretchr/testify/require"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmdb "github.com/cometbft/cometbft-db"

"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

// <%= moduleName %>ChannelKeeper is a stub of cosmosibckeeper.ChannelKeeper.
Expand Down Expand Up @@ -72,11 +74,13 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (*keeper.Keeper, sdk.Context)
registry := codectypes.NewInterfaceRegistry()
appCodec := codec.NewProtoCodec(registry)
capabilityKeeper := capabilitykeeper.NewKeeper(appCodec, storeKey, memStoreKey)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)

k := keeper.NewKeeper(
appCodec,
storeKey,
memStoreKey,
authority.String(),
<%= moduleName %>ChannelKeeper{},
<%= moduleName %>PortKeeper{},
capabilityKeeper.ScopeToModule("<%= title(moduleName) %>ScopedKeeper"),<%= for (dependency) in dependencies { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
for i, acc := range simState.Accounts {
accs[i] = acc.Address.String()
}
<%= moduleName %>Genesis := types.GenesisState{<%= if (isIBC) { %>
<%= moduleName %>Genesis := types.GenesisState{
Params: types.DefaultParams(),<%= if (isIBC) { %>
PortId: types.PortID,<% } %>
// this line is used by starport scaffolding # simapp/module/genesisState
}
Expand Down

0 comments on commit 629dea2

Please sign in to comment.