From 9be0fb667498beab6e291d7d240f09aa75ec3d0a Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 26 Nov 2024 21:14:32 +0100 Subject: [PATCH] feat: integrate with v0.52.x (2/n) (#4423) --- changelog.md | 2 +- ignite/cmd/scaffold_module.go | 2 - ignite/templates/app/files/go.mod.plush | 1 + .../msg_server_{{packetName}}_test.go.plush | 9 +- .../testutil/keeper/{{moduleName}}.go.plush | 55 ----- .../x/{{moduleName}}/keeper/genesis.go.plush | 28 +++ .../{module => keeper}/genesis_test.go.plush | 15 +- .../x/{{moduleName}}/keeper/keeper.go.plush | 75 +++---- .../keeper/keeper_test.go.plush | 59 ++++++ .../keeper/msg_update_params.go.plush | 9 +- .../keeper/msg_update_params_test.go.plush | 16 +- .../keeper/query_params_test.go.plush | 9 +- .../{{moduleName}}/module/depinject.go.plush | 79 +++++++ .../x/{{moduleName}}/module/genesis.go.plush | 29 --- .../x/{{moduleName}}/module/module.go.plush | 195 +++++------------- .../x/{{moduleName}}/types/errors.go.plush | 9 +- .../base/x/{{moduleName}}/types/keys.go.plush | 5 + .../keeper/keeper_test.go.plush} | 47 +++-- ignite/templates/module/create/ibc.go | 2 +- .../keeper/query_{{typeName}}_test.go.plush | 23 +-- .../msg_server_{{typeName}}_test.go.plush | 33 ++- ignite/templates/typed/list/genesis.go | 4 +- .../keeper/query_{{typeName}}_test.go.plush | 23 +-- .../msg_server_{{typeName}}_test.go.plush | 41 ++-- ignite/templates/typed/map/map.go | 4 +- .../keeper/query_{{typeName}}_test.go.plush | 9 +- .../msg_server_{{typeName}}_test.go.plush | 42 ++-- ignite/templates/typed/singleton/singleton.go | 4 +- 28 files changed, 409 insertions(+), 420 deletions(-) delete mode 100644 ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush create mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis.go.plush rename ignite/templates/module/create/files/base/x/{{moduleName}}/{module => keeper}/genesis_test.go.plush (65%) create mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper_test.go.plush create mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/module/depinject.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush rename ignite/templates/module/create/files/ibc/{testutil/keeper/{{moduleName}}.go.plush => x/{{moduleName}}/keeper/keeper_test.go.plush} (66%) diff --git a/changelog.md b/changelog.md index 13a54de489..87daf2c813 100644 --- a/changelog.md +++ b/changelog.md @@ -21,7 +21,7 @@ - [#4377](https://github.com/ignite/cli/pull/4377) Add multi node (validator) testnet. - [#4326](https://github.com/ignite/cli/pull/4326) Add `buf.build` version to `ignite version` command - [#4362](https://github.com/ignite/cli/pull/4362) Scaffold `Makefile` -- [#4289](https://github.com/ignite/cli/pull/4289) Cosmos SDK v0.52 support +- [#4289](https://github.com/ignite/cli/pull/4289), [#4423](https://github.com/ignite/cli/pull/4423) Cosmos SDK v0.52 support ### Changes diff --git a/ignite/cmd/scaffold_module.go b/ignite/cmd/scaffold_module.go index 731b209972..b0e866585f 100644 --- a/ignite/cmd/scaffold_module.go +++ b/ignite/cmd/scaffold_module.go @@ -44,8 +44,6 @@ This command does the following: * Creates a directory with module's protocol buffer files in "proto/" * Creates a directory with module's boilerplate Go code in "x/" * Imports the newly created module by modifying "app/app.go" -* Creates a file in "testutil/keeper/" that contains logic to create a keeper - for testing purposes This command will proceed with module scaffolding even if "app/app.go" doesn't have the required default placeholders. If the placeholders are missing, you diff --git a/ignite/templates/app/files/go.mod.plush b/ignite/templates/app/files/go.mod.plush index bf60b401f6..b92337bb67 100644 --- a/ignite/templates/app/files/go.mod.plush +++ b/ignite/templates/app/files/go.mod.plush @@ -46,6 +46,7 @@ require ( cosmossdk.io/core v1.0.0 cosmossdk.io/depinject v1.0.0 cosmossdk.io/errors v1.0.1 + cosmossdk.io/errors/v2 v2.0.0-20241122152243-fdb7688804d1 cosmossdk.io/log v1.4.1 cosmossdk.io/math v1.4.0 cosmossdk.io/store v1.1.1-0.20240909133312-50288938d1b6 diff --git a/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/keeper/msg_server_{{packetName}}_test.go.plush b/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/keeper/msg_server_{{packetName}}_test.go.plush index b42f57fc8a..b9e7b12729 100644 --- a/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/keeper/msg_server_{{packetName}}_test.go.plush +++ b/ignite/templates/ibc/files/packet/messages/x/{{moduleName}}/keeper/msg_server_{{packetName}}_test.go.plush @@ -8,16 +8,15 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" - keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/x/<%= moduleName %>/keeper" "<%= ModulePath %>/x/<%= moduleName %>/types" ) func TestMsgServerSend<%= packetName.UpperCamel %>(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(moduleName) %>Keeper(t) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - srv := keeper.NewMsgServerImpl(k) tests := []struct { name string @@ -73,7 +72,7 @@ func TestMsgServerSend<%= packetName.UpperCamel %>(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err = srv.Send<%= packetName.UpperCamel %>(ctx, &tt.msg) + _, err = srv.Send<%= packetName.UpperCamel %>(f.ctx, &tt.msg) if tt.err != nil { require.ErrorContains(t, err, tt.err.Error()) return diff --git a/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush deleted file mode 100644 index e3eec7cca0..0000000000 --- a/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush +++ /dev/null @@ -1,55 +0,0 @@ -package keeper - -import ( - "testing" - - "cosmossdk.io/core/address" - "cosmossdk.io/log" - "cosmossdk.io/store" - "cosmossdk.io/store/metrics" - storetypes "cosmossdk.io/store/types" - dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/codec" - addresscodec "github.com/cosmos/cosmos-sdk/codec/address" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/runtime" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - govtypes "cosmossdk.io/x/gov/types" - "github.com/stretchr/testify/require" - - "<%= modulePath %>/x/<%= moduleName %>/keeper" - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context, address.Codec) { - storeKey := storetypes.NewKVStoreKey(types.StoreKey) - - db := dbm.NewMemDB() - stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) - stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) - require.NoError(t, stateStore.LoadLatestVersion()) - - registry := codectypes.NewInterfaceRegistry() - cdc := codec.NewProtoCodec(registry) - authority := authtypes.NewModuleAddress(govtypes.ModuleName) - addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()) - - k := keeper.NewKeeper( - cdc, - addressCodec, - runtime.NewKVStoreService(storeKey), - log.NewNopLogger(), - authority.String(), <%= for (dependency) in dependencies { %> - nil,<% } %> - ) - - ctx := sdk.NewContext(stateStore, false, log.NewNopLogger()) - - // Initialize params - if err := k.Params.Set(ctx, types.DefaultParams()); err != nil { - t.Fatalf("failed to set params: %v", err) - } - - return k, ctx, addressCodec -} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis.go.plush new file mode 100644 index 0000000000..04a3a06bfe --- /dev/null +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis.go.plush @@ -0,0 +1,28 @@ +package keeper + +import ( + "context" + + "<%= modulePath %>/x/<%= moduleName %>/types" +) + +// InitGenesis initializes the module's state from a provided genesis state. +func (k Keeper) InitGenesis(ctx context.Context, genState types.GenesisState) error { + // this line is used by starport scaffolding # genesis/module/init + return k.Params.Set(ctx, genState.Params) +} + +// ExportGenesis returns the module's exported genesis. +func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) { + var err error + + genesis := types.DefaultGenesis() + genesis.Params, err = k.Params.Get(ctx) + if err != nil { + return nil, err + } + + // this line is used by starport scaffolding # genesis/module/export + + return genesis, nil +} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis_test.go.plush similarity index 65% rename from ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush rename to ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis_test.go.plush index e7e886ce47..4bef36433a 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/genesis_test.go.plush @@ -1,26 +1,25 @@ -package <%= moduleName %>_test +package keeper_test import ( "testing" - keepertest "<%= modulePath %>/testutil/keeper" "<%= modulePath %>/testutil/nullify" - <%= moduleName %> "<%= modulePath %>/x/<%= moduleName %>/module" "<%= modulePath %>/x/<%= moduleName %>/types" + "github.com/stretchr/testify/require" ) func TestGenesis(t *testing.T) { genesisState := types.GenesisState{ - Params: types.DefaultParams(), + Params: types.DefaultParams(), <%= if (isIBC) { %>PortId: types.PortID,<% } %> // this line is used by starport scaffolding # genesis/test/state } - k, ctx, _ := keepertest.<%= title(moduleName) %>Keeper(t) - err := <%= moduleName %>.InitGenesis(ctx, k, genesisState) + f := initFixture(t) + err := f.keeper.InitGenesis(f.ctx, genesisState) require.NoError(t, err) - got, err := <%= moduleName %>.ExportGenesis(ctx, k) + got, err := f.keeper.ExportGenesis(f.ctx) require.NoError(t, err) require.NotNil(t, got) @@ -30,4 +29,4 @@ func TestGenesis(t *testing.T) { <%= if (isIBC) { %>require.Equal(t, genesisState.PortId, got.PortId)<% } %> require.Equal(t, genesisState.Params, got.Params) // this line is used by starport scaffolding # genesis/test/assert -} +} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush index 4d7fd279ed..5d4aba712e 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush @@ -4,8 +4,8 @@ import ( "fmt" "cosmossdk.io/collections" - "cosmossdk.io/core/store" "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,52 +22,47 @@ import ( "<%= modulePath %>/x/<%= moduleName %>/types" ) -type ( - Keeper struct { - cdc codec.BinaryCodec - addressCodec address.Codec - storeService store.KVStoreService - logger log.Logger +type Keeper struct { + appmodule.Environment - // the address capable of executing a MsgUpdateParams message. - // Typically, this should be the x/gov module account. - authority string + cdc codec.BinaryCodec + addressCodec address.Codec + // Address capable of executing a MsgUpdateParams message. + // Typically, this should be the x/gov module account. + authority []byte - Schema collections.Schema - Params collections.Item[types.Params] - // this line is used by starport scaffolding # collection/type + Schema collections.Schema + Params collections.Item[types.Params] + // this line is used by starport scaffolding # collection/type - <%= if (isIBC) { %> - ibcKeeperFn func() *ibckeeper.Keeper - capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper<% } %> - <%= for (dependency) in dependencies { %> - <%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %><% } %> - } -) + <%= if (isIBC) { %> + ibcKeeperFn func() *ibckeeper.Keeper + capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper<% } %> + <%= for (dependency) in dependencies { %> + <%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %><% } %> +} func NewKeeper( + env appmodule.Environment, cdc codec.BinaryCodec, addressCodec address.Codec, - storeService store.KVStoreService, - logger log.Logger, - authority string,<%= if (isIBC) { %> + authority []byte,<%= if (isIBC) { %> ibcKeeperFn func() *ibckeeper.Keeper, capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper,<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %>,<% } %> ) Keeper { - if _, err := addressCodec.StringToBytes(authority); err != nil { + if _, err := addressCodec.BytesToString(authority); err != nil { panic(fmt.Sprintf("invalid authority address %s: %s", authority, err)) } - sb := collections.NewSchemaBuilder(storeService) + sb := collections.NewSchemaBuilder(env.KVStoreService) k := Keeper{ + Environment: env, cdc: cdc, addressCodec: addressCodec, - storeService: storeService, - authority: authority, - logger: logger,<%= if (isIBC) { %> + authority: authority,<%= if (isIBC) { %> ibcKeeperFn: ibcKeeperFn, capabilityScopedFn: capabilityScopedFn,<% } %> <%= for (dependency) in dependencies { %> @@ -86,21 +81,17 @@ func NewKeeper( } // GetAuthority returns the module's authority. -func (k Keeper) GetAuthority() string { +func (k Keeper) GetAuthority() []byte { return k.authority } -// Logger returns a module-specific logger. -func (k Keeper) Logger() log.Logger { - return k.logger.With("module", fmt.Sprintf("x/%s", types.ModuleName)) -} - -<%= if (isIBC) { %>// ---------------------------------------------------------------------------- +<%= if (isIBC) { %> +// ---------------------------------------------------------------------------- // IBC Keeper Logic // ---------------------------------------------------------------------------- // ChanCloseInit defines a wrapper function for the channel Keeper's function. -func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error { +func (k Keeper) ChanCloseInit(ctx context.Context, portID, channelID string) error { capName := host.ChannelCapabilityPath(portID, channelID) chanCap, ok := k.ScopedKeeper().GetCapability(ctx, capName) if !ok { @@ -110,7 +101,7 @@ func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error { } // ShouldBound checks if the IBC app module can be bound to the desired port -func (k Keeper) ShouldBound(ctx sdk.Context, portID string) bool { +func (k Keeper) ShouldBound(ctx context.Context, portID string) bool { scopedKeeper := k.ScopedKeeper() if scopedKeeper == nil { return false @@ -121,33 +112,33 @@ func (k Keeper) ShouldBound(ctx sdk.Context, portID string) bool { // BindPort defines a wrapper function for the port Keeper's function in // order to expose it to module's InitGenesis function -func (k Keeper) BindPort(ctx sdk.Context, portID string) error { +func (k Keeper) BindPort(ctx context.Context, portID string) error { cap := k.ibcKeeperFn().PortKeeper.BindPort(ctx, portID) return k.ClaimCapability(ctx, cap, host.PortPath(portID)) } // GetPort returns the portID for the IBC app module. Used in ExportGenesis -func (k Keeper) GetPort(ctx sdk.Context) string { +func (k Keeper) GetPort(ctx context.Context) string { storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) store := prefix.NewStore(storeAdapter, []byte{}) return string(store.Get(types.PortKey)) } // SetPort sets the portID for the IBC app module. Used in InitGenesis -func (k Keeper) SetPort(ctx sdk.Context, portID string) { +func (k Keeper) SetPort(ctx context.Context, portID string) { storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) store := prefix.NewStore(storeAdapter, []byte{}) store.Set(types.PortKey, []byte(portID)) } // AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function -func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool { +func (k Keeper) AuthenticateCapability(ctx context.Context, cap *capabilitytypes.Capability, name string) bool { return k.ScopedKeeper().AuthenticateCapability(ctx, cap, name) } // ClaimCapability allows the IBC app module to claim a capability that core IBC // passes to it -func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error { +func (k Keeper) ClaimCapability(ctx context.Context, cap *capabilitytypes.Capability, name string) error { return k.ScopedKeeper().ClaimCapability(ctx, cap, name) } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper_test.go.plush new file mode 100644 index 0000000000..3afa254b18 --- /dev/null +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper_test.go.plush @@ -0,0 +1,59 @@ +package keeper_test + +import ( + "context" + "testing" + + "cosmossdk.io/core/address" + "cosmossdk.io/log" + storetypes "cosmossdk.io/store/types" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + + "<%= modulePath %>/x/<%= moduleName %>/keeper" + module "<%= modulePath %>/x/<%= moduleName %>/module" + "<%= modulePath %>/x/<%= moduleName %>/types" +) + +type fixture struct { + ctx context.Context + keeper keeper.Keeper + addressCodec address.Codec +} + +func initFixture(t *testing.T) *fixture { + t.Helper() + + encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{}) + addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()) + storeKey := storetypes.NewKVStoreKey(types.StoreKey) + + env := runtime.NewEnvironment(runtime.NewKVStoreService(storeKey), log.NewTestLogger(t)) + ctx := testutil.DefaultContextWithDB(t, storeKey, storetypes.NewTransientStoreKey("transient_test")).Ctx + + authority := authtypes.NewModuleAddress(types.GovModuleName) + + k := keeper.NewKeeper( + env, + encCfg.Codec, + addressCodec, + authority,<%= for (dependency) in dependencies { %> + nil,<% } %> + ) + + // Initialize params + if err := k.Params.Set(ctx, types.DefaultParams()); err != nil { + t.Fatalf("failed to set params: %v", err) + } + + return &fixture{ + ctx: ctx, + keeper: k, + addressCodec: addressCodec, + } +} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush index f93f75b12c..b022a281a3 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params.go.plush @@ -1,6 +1,7 @@ package keeper import ( + "bytes" "context" errorsmod "cosmossdk.io/errors" @@ -9,12 +10,14 @@ import ( ) func (k msgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { - if _, err := k.addressCodec.StringToBytes(req.Authority); err != nil { + authority, err := k.addressCodec.StringToBytes(req.Authority); + if err != nil { return nil, errorsmod.Wrap(err, "invalid authority address") } - if k.GetAuthority() != req.Authority { - return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority) + if !bytes.Equal(k.GetAuthority(), authority) { + expectedAuthorityStr, _ := k.addressCodec.BytesToString(k.GetAuthority()) + return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", expectedAuthorityStr, req.Authority) } if err := req.Params.Validate(); err != nil { diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush index 2bd5ea45d5..a0e8deb8e0 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/msg_update_params_test.go.plush @@ -6,17 +6,19 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - keepertest "<%= modulePath %>/testutil/keeper" "<%= modulePath %>/x/<%= moduleName %>/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" ) func TestMsgUpdateParams(t *testing.T) { - k, ctx, _ := keepertest.<%= title(moduleName) %>Keeper(t) - ms := keeper.NewMsgServerImpl(k) + f := initFixture(t) + ms := keeper.NewMsgServerImpl(f.keeper) params := types.DefaultParams() - require.NoError(t, k.Params.Set(ctx, params)) + require.NoError(t, f.keeper.Params.Set(f.ctx, params)) + + authorityStr, err := f.addressCodec.BytesToString(f.keeper.GetAuthority()) + require.NoError(t, err) // default params testCases := []struct { @@ -37,7 +39,7 @@ func TestMsgUpdateParams(t *testing.T) { { name: "send enabled param", input: &types.MsgUpdateParams{ - Authority: k.GetAuthority(), + Authority: authorityStr, Params: types.Params{}, }, expErr: false, @@ -45,7 +47,7 @@ func TestMsgUpdateParams(t *testing.T) { { name: "all good", input: &types.MsgUpdateParams{ - Authority: k.GetAuthority(), + Authority: authorityStr, Params: params, }, expErr: false, @@ -54,7 +56,7 @@ func TestMsgUpdateParams(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - _, err := ms.UpdateParams(ctx, tc.input) + _, err := ms.UpdateParams(f.ctx, tc.input) if tc.expErr { require.Error(t, err) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush index 3d3d30dabe..d0ba8611b7 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params_test.go.plush @@ -5,20 +5,19 @@ import ( "github.com/stretchr/testify/require" - keepertest "<%= modulePath %>/testutil/keeper" "<%= modulePath %>/x/<%= moduleName %>/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" "<%= modulePath %>/x/<%= moduleName %>/keeper" ) func TestParamsQuery(t *testing.T) { - k, ctx, _ := keepertest.<%= title(moduleName) %>Keeper(t) + f := initFixture(t) - qs := keeper.NewQueryServerImpl(k) + qs := keeper.NewQueryServerImpl(f.keeper) params := types.DefaultParams() - require.NoError(t, k.Params.Set(ctx, params)) + require.NoError(t, f.keeper.Params.Set(f.ctx, params)) - response, err := qs.Params(ctx, &types.QueryParamsRequest{}) + response, err := qs.Params(f.ctx, &types.QueryParamsRequest{}) require.NoError(t, err) require.Equal(t, &types.QueryParamsResponse{Params: params}, response) } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/depinject.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/depinject.go.plush new file mode 100644 index 0000000000..7335ce59ea --- /dev/null +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/depinject.go.plush @@ -0,0 +1,79 @@ +package <%= moduleName %> + +import ( + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/codec" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + <%= if (isIBC) { %>capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" + ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"<% } %> + + // this line is used by starport scaffolding # 1 + + "<%= modulePath %>/x/<%= moduleName %>/keeper" + "<%= modulePath %>/x/<%= moduleName %>/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.Register( + &types.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *types.Module + Environment appmodule.Environment + Cdc codec.Codec + AddressCodec address.Codec + + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper<%= for (dependency) in dependencies { %><%= if (dependency.Name != "Bank" && dependency.Name != "Account") { %> + <%= dependency.KeeperName() %> types.<%= dependency.KeeperName() %><% } %><% } %> + + <%= if (isIBC) { %>IBCKeeperFn func() *ibckeeper.Keeper `optional:"true"` + CapabilityScopedFn func(string) capabilitykeeper.ScopedKeeper `optional:"true"`<% } %> +} + +type ModuleOutputs struct { + depinject.Out + + <%= title(moduleName) %>Keeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + k := keeper.NewKeeper( + in.Environment, + in.Cdc, + in.AddressCodec, + authority, <%= if (isIBC) { %> + in.IBCKeeperFn, + in.CapabilityScopedFn,<% } %><%= for (dependency) in dependencies { %> + in.<%= dependency.KeeperName() %>,<% } %> + ) + m := NewAppModule( + in.Cdc, + k, + in.AccountKeeper, + in.BankKeeper, + ) + + return ModuleOutputs{<%= title(moduleName) %>Keeper: k, Module: m} +} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush deleted file mode 100644 index 1090ba3cbb..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush +++ /dev/null @@ -1,29 +0,0 @@ -package <%= moduleName %> - -import ( - "context" - - "<%= modulePath %>/x/<%= moduleName %>/keeper" - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -// InitGenesis initializes the module's state from a provided genesis state. -func InitGenesis(ctx context.Context, k keeper.Keeper, genState types.GenesisState) error { - // this line is used by starport scaffolding # genesis/module/init - return k.Params.Set(ctx, genState.Params) -} - -// ExportGenesis returns the module's exported genesis. -func ExportGenesis(ctx context.Context, k keeper.Keeper) (*types.GenesisState, error) { - var err error - - genesis := types.DefaultGenesis() - genesis.Params, err = k.Params.Get(ctx) - if err != nil { - return nil, err - } - - // this line is used by starport scaffolding # genesis/module/export - - return genesis, nil -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush index 4097059b54..c296a1bbc8 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush @@ -5,23 +5,13 @@ import ( "encoding/json" "fmt" - "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" "cosmossdk.io/core/registry" - "cosmossdk.io/depinject" - "cosmossdk.io/depinject/appconfig" - "cosmossdk.io/log" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/types/module" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - govtypes "cosmossdk.io/x/gov/types" "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" <%= if (isIBC) { %>porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types" - capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" - ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper" "github.com/spf13/cobra"<% } %> // this line is used by starport scaffolding # 1 @@ -32,78 +22,18 @@ import ( ) var ( - _ module.AppModuleSimulation = (*AppModule)(nil) - + _ appmodule.AppModule = (*AppModule)(nil) _ appmodule.HasGenesis = (*AppModule)(nil) _ appmodule.HasConsensusVersion = (*AppModule)(nil) - _ appmodule.AppModule = (*AppModule)(nil) + _ appmodule.HasRegisterInterfaces = AppModule{} _ appmodule.HasBeginBlocker = (*AppModule)(nil) _ appmodule.HasEndBlocker = (*AppModule)(nil) <%= if (isIBC) { %>_ porttypes.IBCModule = (*IBCModule)(nil)<% } %> ) -// ---------------------------------------------------------------------------- -// AppModuleBasic -// ---------------------------------------------------------------------------- - -// AppModuleBasic implements the AppModuleBasic interface that defines the -// independent methods a Cosmos SDK module needs to implement. -type AppModuleBasic struct { - cdc codec.Codec -} - -func NewAppModuleBasic(cdc codec.Codec) AppModuleBasic { - return AppModuleBasic{cdc: cdc} -} - -// Name returns the name of the module as a string. -func (AppModuleBasic) Name() string { - return types.ModuleName -} - -// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message. -func (a AppModuleBasic) RegisterInterfaces(registrar registry.InterfaceRegistrar) { - types.RegisterInterfaces(registrar) -} - -// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage. -// The default GenesisState need to be defined by the module developer and is primarily used for testing. -func (am AppModuleBasic) DefaultGenesis() json.RawMessage { - return am.cdc.MustMarshalJSON(types.DefaultGenesis()) -} - -// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form. -func (am AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { - var genState types.GenesisState - if err := am.cdc.UnmarshalJSON(bz, &genState); err != nil { - return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) - } - return genState.Validate() -} - -// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. -func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { - if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { - panic(err) - } -} - -<%= if (isIBC) { %> -// GetTxCmd returns the root Tx command for the module. -// These commands enrich the AutoCLI tx commands. -func (a AppModuleBasic) GetTxCmd() *cobra.Command { - return cli.GetTxCmd() -} -<% } %> - -// ---------------------------------------------------------------------------- -// AppModule -// ---------------------------------------------------------------------------- - // AppModule implements the AppModule interface that defines the inter-dependent methods that modules need to implement type AppModule struct { - AppModuleBasic - + cdc codec.Codec keeper keeper.Keeper accountKeeper types.AccountKeeper bankKeeper types.BankKeeper @@ -116,13 +46,33 @@ func NewAppModule( bankKeeper types.BankKeeper, ) AppModule { return AppModule{ - AppModuleBasic: NewAppModuleBasic(cdc), - keeper: keeper, - accountKeeper: accountKeeper, - bankKeeper: bankKeeper, + cdc: cdc, + keeper: keeper, + accountKeeper: accountKeeper, + bankKeeper: bankKeeper, + } +} + +// IsAppModule implements the appmodule.AppModule interface. +func (AppModule) IsAppModule() {} + +// Name returns the name of the module as a string. +func (AppModule) Name() string { + return types.ModuleName +} + +// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. +func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { + panic(err) } } +// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message. +func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) { + types.RegisterInterfaces(registrar) +} + // RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper)) @@ -131,6 +81,22 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { return nil } +// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage. +// The default GenesisState need to be defined by the module developer and is primarily used for testing. +func (am AppModule) DefaultGenesis() json.RawMessage { + return am.cdc.MustMarshalJSON(types.DefaultGenesis()) +} + +// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form. +func (am AppModule) ValidateGenesis(bz json.RawMessage) error { + var genState types.GenesisState + if err := am.cdc.UnmarshalJSON(bz, &genState); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) + } + + return genState.Validate() +} + // InitGenesis performs the module's genesis initialization. It returns no validator updates. func (am AppModule) InitGenesis(ctx context.Context, gs json.RawMessage) error { var genState types.GenesisState @@ -139,12 +105,12 @@ func (am AppModule) InitGenesis(ctx context.Context, gs json.RawMessage) error { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } - return InitGenesis(ctx, am.keeper, genState) + return am.keeper.InitGenesis(ctx, genState) } // ExportGenesis returns the module's exported genesis state as raw JSON bytes. func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) { - genState, err := ExportGenesis(ctx, am.keeper) + genState, err := am.keeper.ExportGenesis(ctx) if err != nil { return nil, err } @@ -169,69 +135,10 @@ func (am AppModule) EndBlock(_ context.Context) error { return nil } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - -// IsAppModule implements the appmodule.AppModule interface. -func (am AppModule) IsAppModule() {} - -// ---------------------------------------------------------------------------- -// App Wiring Setup -// ---------------------------------------------------------------------------- - -func init() { - appconfig.Register( - &types.Module{}, - appconfig.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - AddressCodec address.Codec - StoreService store.KVStoreService - Cdc codec.Codec - Config *types.Module - Logger log.Logger - - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper<%= for (dependency) in dependencies { %><%= if (dependency.Name != "Bank" && dependency.Name != "Account") { %> - <%= dependency.KeeperName() %> types.<%= dependency.KeeperName() %><% } %><% } %> - - <%= if (isIBC) { %>IBCKeeperFn func() *ibckeeper.Keeper `optional:"true"` - CapabilityScopedFn func(string) capabilitykeeper.ScopedKeeper `optional:"true"`<% } %> -} - -type ModuleOutputs struct { - depinject.Out - - <%= title(moduleName) %>Keeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(govtypes.ModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - k := keeper.NewKeeper( - in.Cdc, - in.AddressCodec, - in.StoreService, - in.Logger, - authority.String(), <%= if (isIBC) { %> - in.IBCKeeperFn, - in.CapabilityScopedFn,<% } %><%= for (dependency) in dependencies { %> - in.<%= dependency.KeeperName() %>,<% } %> - ) - m := NewAppModule( - in.Cdc, - k, - in.AccountKeeper, - in.BankKeeper, - ) - - return ModuleOutputs{<%= title(moduleName) %>Keeper: k, Module: m} +<%= if (isIBC) { %> +// GetTxCmd returns the root Tx command for the module. +// These commands enrich the AutoCLI tx commands. +func (AppModule) GetTxCmd() *cobra.Command { + return cli.GetTxCmd() } +<% } %> \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/errors.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/errors.go.plush index d0cbd86044..11b4c09c00 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/errors.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/errors.go.plush @@ -3,13 +3,12 @@ package types // DONTCOVER import ( - sdkerrors "cosmossdk.io/errors" + "cosmossdk.io/errors/v2" ) // x/<%= moduleName %> module sentinel errors var ( - ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message") - ErrSample = sdkerrors.Register(ModuleName, 1101, "sample error") - <%= if (isIBC) { %>ErrInvalidPacketTimeout = sdkerrors.Register(ModuleName, 1500, "invalid packet timeout") - ErrInvalidVersion = sdkerrors.Register(ModuleName, 1501, "invalid version")<% } %> + ErrInvalidSigner = errors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message") + <%= if (isIBC) { %>ErrInvalidPacketTimeout = errors.Register(ModuleName, 1500, "invalid packet timeout") + ErrInvalidVersion = errors.Register(ModuleName, 1501, "invalid version")<% } %> ) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush index d2fe258aa8..77f6241c47 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/keys.go.plush @@ -9,6 +9,11 @@ const ( // StoreKey defines the primary module store key StoreKey = ModuleName + // GovModuleName duplicates the gov module's name to avoid a dependency with x/gov. + // It should be synced with the gov module's name if it is ever changed. + // See: https://github.com/cosmos/cosmos-sdk/blob/v0.52.0-beta.2/x/gov/types/keys.go#L9 + GovModuleName = "gov" + <%= if (isIBC) { %>// this line is used by starport scaffolding # ibc/keys/name<% } %> ) diff --git a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/ibc/x/{{moduleName}}/keeper/keeper_test.go.plush similarity index 66% rename from ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush rename to ignite/templates/module/create/files/ibc/x/{{moduleName}}/keeper/keeper_test.go.plush index 6ec777321d..a87e73fe45 100644 --- a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/ibc/x/{{moduleName}}/keeper/keeper_test.go.plush @@ -1,15 +1,13 @@ -package keeper +package keeper_test import ( "testing" "cosmossdk.io/core/address" "cosmossdk.io/log" - "cosmossdk.io/store" - "cosmossdk.io/store/metrics" storetypes "cosmossdk.io/store/types" dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/codec" + codectestutil "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,33 +20,38 @@ import ( "github.com/stretchr/testify/require" "<%= modulePath %>/x/<%= moduleName %>/keeper" + module "<%= modulePath %>/x/<%= moduleName %>/module" "<%= modulePath %>/x/<%= moduleName %>/types" ) -func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context, address.Codec) { +type fixture struct { + ctx context.Context + keeper keeper.Keeper + addressCodec address.Codec +} + +func initFixture(t *testing.T) *fixture { + t.Helper() + + encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{}) + addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()) storeKey := storetypes.NewKVStoreKey(types.StoreKey) - db := dbm.NewMemDB() - stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) - stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) - require.NoError(t, stateStore.LoadLatestVersion()) + env := runtime.NewEnvironment(runtime.NewKVStoreService(storeKey), log.NewTestLogger(t)) + ctx := testutil.DefaultContextWithDB(t, storeKey, storetypes.NewTransientStoreKey("transient_test")).Ctx - registry := codectypes.NewInterfaceRegistry() - appCodec := codec.NewProtoCodec(registry) - capabilityKeeper := capabilitykeeper.NewKeeper(appCodec, storeKey, nil) authority := authtypes.NewModuleAddress(govtypes.ModuleName) - addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()) + capabilityKeeper := capabilitykeeper.NewKeeper(encCfg.Codec, storeKey, nil) scopedKeeper := capabilityKeeper.ScopeToModule(ibcexported.ModuleName) portKeeper := portkeeper.NewKeeper(scopedKeeper) scopeModule := capabilityKeeper.ScopeToModule(types.ModuleName) k := keeper.NewKeeper( - appCodec, + env, + encCfg.Codec, addressCodec, - runtime.NewKVStoreService(storeKey), - log.NewNopLogger(), - authority.String(), + authority, func() *ibckeeper.Keeper { return &ibckeeper.Keeper{ PortKeeper: &portKeeper, @@ -60,12 +63,14 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context, a nil,<% } %> ) - ctx := sdk.NewContext(stateStore, false, log.NewNopLogger()) - // Initialize params if err := k.Params.Set(ctx, types.DefaultParams()); err != nil { t.Fatalf("failed to set params: %v", err) } - - return k, ctx, addressCodec + + return &fixture{ + ctx: ctx, + keeper: k, + addressCodec: addressCodec, + } } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index a7eefb2671..3e5ad04196 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -58,7 +58,7 @@ func NewIBC(replacer placeholder.Replacer, opts *CreateOptions) (*genny.Generato func genesisModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go") f, err := r.Disk.Find(path) if err != nil { return err diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush index 7ad32d4fc0..e4829be925 100644 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush @@ -13,7 +13,6 @@ import ( "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/testutil/nullify" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" - keepertest "<%= ModulePath %>/testutil/keeper" ) func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> { @@ -28,9 +27,9 @@ func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context } func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { - k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t) - qs := keeper.NewQueryServerImpl(k) - msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 2) + f := initFixture(t) + qs := keeper.NewQueryServerImpl(f.keeper) + msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 2) tests := []struct { desc string request *types.QueryGet<%= TypeName.UpperCamel %>Request @@ -59,7 +58,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request) + response, err := qs.Get<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -74,9 +73,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { - k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t) - qs := keeper.NewQueryServerImpl(k) - msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 5) + f := initFixture(t) + qs := keeper.NewQueryServerImpl(f.keeper) + msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 5) request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request { return &types.QueryAll<%= TypeName.UpperCamel %>Request{ @@ -91,7 +90,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { t.Run("ByOffset", func(t *testing.T) { step := 2 for i := 0; i < len(msgs); i += step { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, uint64(i), uint64(step), false)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, uint64(i), uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -104,7 +103,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { step := 2 var next []byte for i := 0; i < len(msgs); i += step { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(next, 0, uint64(step), false)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(next, 0, uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -115,7 +114,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { } }) t.Run("Total", func(t *testing.T) { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, 0, 0, true)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, 0, 0, true)) require.NoError(t, err) require.Equal(t, len(msgs), int(resp.Pagination.Total)) require.ElementsMatch(t, @@ -124,7 +123,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { ) }) t.Run("InvalidRequest", func(t *testing.T) { - _, err := qs.List<%= TypeName.UpperCamel %>(ctx, nil) + _, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) }) } diff --git a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush index 8297f5f246..6c0296fdf0 100644 --- a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush @@ -7,36 +7,35 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" - keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/types" ) func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - srv := keeper.NewMsgServerImpl(k) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) for i := 0; i < 5; i++ { - resp, err := srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) + resp, err := srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) require.NoError(t, err) require.Equal(t, i, int(resp.Id)) } } func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - srv := keeper.NewMsgServerImpl(k) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) require.NoError(t, err) tests := []struct { @@ -66,7 +65,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Update<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Update<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -77,16 +76,16 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - srv := keeper.NewMsgServerImpl(k) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) require.NoError(t, err) tests := []struct { @@ -116,7 +115,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Delete<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Delete<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index 5999446885..bf60552b23 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -117,7 +117,7 @@ for _, elem := range gs.%[3]vList { func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go") f, err := r.Disk.Find(path) if err != nil { return err @@ -172,7 +172,7 @@ if err != nil { func genesisTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis_test.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis_test.go") f, err := r.Disk.Find(path) if err != nil { return err diff --git a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush index 6750c3c02c..2559260b0d 100644 --- a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush @@ -12,7 +12,6 @@ import ( "<%= ModulePath %>/x/<%= ModuleName %>/types" "<%= ModulePath %>/testutil/nullify" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" - keepertest "<%= ModulePath %>/testutil/keeper" ) // Prevent strconv unused error @@ -29,9 +28,9 @@ func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context } func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { - k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t) - qs := keeper.NewQueryServerImpl(k) - msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 2) + f := initFixture(t) + qs := keeper.NewQueryServerImpl(f.keeper) + msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 2) tests := []struct { desc string request *types.QueryGet<%= TypeName.UpperCamel %>Request @@ -66,7 +65,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request) + response, err := qs.Get<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -81,9 +80,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { - k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t) - qs := keeper.NewQueryServerImpl(k) - msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 5) + f := initFixture(t) + qs := keeper.NewQueryServerImpl(f.keeper) + msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 5) request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request { return &types.QueryAll<%= TypeName.UpperCamel %>Request{ @@ -98,7 +97,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { t.Run("ByOffset", func(t *testing.T) { step := 2 for i := 0; i < len(msgs); i += step { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, uint64(i), uint64(step), false)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, uint64(i), uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -111,7 +110,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { step := 2 var next []byte for i := 0; i < len(msgs); i += step { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(next, 0, uint64(step), false)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(next, 0, uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -122,7 +121,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { } }) t.Run("Total", func(t *testing.T) { - resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, 0, 0, true)) + resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, 0, 0, true)) require.NoError(t, err) require.Equal(t, len(msgs), int(resp.Pagination.Total)) require.ElementsMatch(t, @@ -131,7 +130,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { ) }) t.Run("InvalidRequest", func(t *testing.T) { - _, err := qs.List<%= TypeName.UpperCamel %>(ctx, nil) + _, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) }) } diff --git a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush index 4a2358211d..9dc1367e3f 100644 --- a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush @@ -7,7 +7,6 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" - keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/types" ) @@ -16,36 +15,37 @@ import ( var _ = strconv.IntSize func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - srv := keeper.NewMsgServerImpl(k) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) for i := 0; i < 5; i++ { expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>, <%= Index.Name.UpperCamel %>: <%= Index.ValueLoop() %>, } - _, err := srv.Create<%= TypeName.UpperCamel %>(ctx, expected) + _, err := srv.Create<%= TypeName.UpperCamel %>(f.ctx, expected) require.NoError(t, err) - rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx, expected.<%= Index.Name.UpperCamel %>) + rst, err := f.keeper.<%= TypeName.UpperCamel %>.Get(f.ctx, expected.<%= Index.Name.UpperCamel %>) require.NoError(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } } func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - srv := keeper.NewMsgServerImpl(k) expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>, <%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>, } - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, expected) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, expected) require.NoError(t, err) tests := []struct { @@ -83,12 +83,12 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Update<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Update<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx, expected.<%= Index.Name.UpperCamel %>) + rst, err := f.keeper.<%= TypeName.UpperCamel %>.Get(f.ctx, expected.<%= Index.Name.UpperCamel %>) require.NoError(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } @@ -97,15 +97,16 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - srv := keeper.NewMsgServerImpl(k) - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>, + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>, <%= Index.Name.UpperCamel %>: <%= Index.ValueIndex() %>, }) require.NoError(t, err) @@ -145,12 +146,12 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Delete<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Delete<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - found, err := k.<%= TypeName.UpperCamel %>.Has(ctx, tc.request.<%= Index.Name.UpperCamel %>) + found, err := f.keeper.<%= TypeName.UpperCamel %>.Has(f.ctx, tc.request.<%= Index.Name.UpperCamel %>) require.NoError(t, err) require.False(t, found) } diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index c8aa92f3ed..2dd0408242 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -394,7 +394,7 @@ for _, elem := range gs.%[3]vList { func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go") f, err := r.Disk.Find(path) if err != nil { return err @@ -438,7 +438,7 @@ for _, elem := range genState.%[3]vList { func genesisTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis_test.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis_test.go") f, err := r.Disk.Find(path) if err != nil { return err diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush index 6115f12326..5f5ac251f2 100644 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}_test.go.plush @@ -7,17 +7,16 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/testutil/nullify" "<%= ModulePath %>/x/<%= ModuleName %>/types" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" ) func Test<%= TypeName.UpperCamel %>Query(t *testing.T) { - k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t) - qs := keeper.NewQueryServerImpl(k) + f := initFixture(t) + qs := keeper.NewQueryServerImpl(f.keeper) item := types.<%= TypeName.UpperCamel %>{} - err := k.<%= TypeName.UpperCamel %>.Set(ctx, item) + err := f.keeper.<%= TypeName.UpperCamel %>.Set(f.ctx, item) require.NoError(t, err) tests := []struct { @@ -38,7 +37,7 @@ func Test<%= TypeName.UpperCamel %>Query(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request) + response, err := qs.Get<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { diff --git a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush index cdccb1842a..f1ead60462 100644 --- a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}_test.go.plush @@ -6,35 +6,36 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" - keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/types" ) func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - srv := keeper.NewMsgServerImpl(k) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>} - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, expected) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, expected) require.NoError(t, err) - rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + rst, err := f.keeper.<%= TypeName.UpperCamel %>.Get(f.ctx) require.Nil(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - srv := keeper.NewMsgServerImpl(k) expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>} - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, expected) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, expected) require.NoError(t, err) tests := []struct { @@ -59,12 +60,12 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Update<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Update<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + rst, err := f.keeper.<%= TypeName.UpperCamel %>.Get(f.ctx) require.Nil(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } @@ -73,15 +74,16 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { } func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { - k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t) - <%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________")) + f := initFixture(t) + srv := keeper.NewMsgServerImpl(f.keeper) + + <%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________")) require.NoError(t, err) - unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________")) + unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________")) require.NoError(t, err) - srv := keeper.NewMsgServerImpl(k) - _, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) + _, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>}) require.NoError(t, err) tests := []struct { @@ -106,12 +108,12 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - _, err = srv.Delete<%= TypeName.UpperCamel %>(ctx, tc.request) + _, err = srv.Delete<%= TypeName.UpperCamel %>(f.ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - found, err := k.<%= TypeName.UpperCamel %>.Has(ctx) + found, err := f.keeper.<%= TypeName.UpperCamel %>.Has(f.ctx) require.NoError(t, err) require.False(t, found) } diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index a3dfb1cdd5..dd8b855329 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -289,7 +289,7 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn func genesisTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis_test.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis_test.go") f, err := r.Disk.Find(path) if err != nil { return err @@ -366,7 +366,7 @@ func genesisTypesTestsModify(replacer placeholder.Replacer, opts *typed.Options) func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { - path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go") + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go") f, err := r.Disk.Find(path) if err != nil { return err