From 0675047a4fe9f7995b73e5f0df0b1412d2794238 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Sat, 25 Nov 2023 20:55:00 +0100 Subject: [PATCH] feat: support collections (wip) --- changelog.md | 1 + ignite/templates/app/files/app/ibc.go.plush | 7 ++ .../keeper/query_{{queryName}}.go.plush | 4 +- .../keeper/{{queryName}}.go.plush | 46 -------- .../testutil/keeper/{{moduleName}}.go.plush | 2 +- .../x/{{moduleName}}/keeper/keeper.go.plush | 25 +++- .../keeper/msg_update_params.go.plush | 6 +- .../keeper/msg_update_params_test.go.plush | 2 +- .../x/{{moduleName}}/keeper/params.go.plush | 34 ------ .../keeper/params_test.go.plush | 18 --- .../x/{{moduleName}}/keeper/query.go.plush | 12 +- .../keeper/query_params.go.plush | 18 ++- .../keeper/query_params_test.go.plush | 11 +- .../x/{{moduleName}}/module/genesis.go.plush | 9 +- .../x/{{moduleName}}/module/module.go.plush | 2 +- .../base/x/{{moduleName}}/types/keys.go.plush | 8 +- .../x/{{moduleName}}/types/params.go.plush | 16 --- .../testutil/keeper/{{moduleName}}.go.plush | 2 +- ignite/templates/module/create/ibc.go | 2 +- .../keeper/query_{{queryName}}.go.plush | 6 +- .../keeper/query_{{typeName}}.go.plush | 37 +++--- .../keeper/query_{{typeName}}_test.go.plush | 27 +++-- .../keeper/{{typeName}}.go.plush | 111 ------------------ .../keeper/{{typeName}}_test.go.plush | 59 ---------- .../keeper/msg_server_{{typeName}}.go.plush | 54 +++++---- .../simulation/{{typeName}}.go.plush | 22 +++- ignite/templates/typed/list/genesis.go | 24 +++- ignite/templates/typed/list/list.go | 13 +- .../keeper/query_{{typeName}}.go.plush | 14 ++- .../keeper/{{typeName}}.go.plush | 70 ----------- .../keeper/msg_server_{{typeName}}.go.plush | 32 ++--- .../simulation/{{typeName}}.go.plush | 22 +++- .../keeper/{{typeName}}_test.go.plush | 66 ----------- ignite/templates/typed/map/map.go | 5 + .../keeper/query_{{typeName}}.go.plush | 17 ++- .../keeper/query_{{typeName}}_test.go.plush | 6 +- .../keeper/{{typeName}}.go.plush | 38 ------ .../keeper/{{typeName}}_test.go.plush | 38 ------ .../keeper/msg_server_{{typeName}}.go.plush | 12 +- .../msg_server_{{typeName}}_test.go.plush | 10 +- .../simulation/{{typeName}}.go.plush | 14 ++- ignite/templates/typed/singleton/singleton.go | 4 +- 42 files changed, 288 insertions(+), 638 deletions(-) delete mode 100644 ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/{{queryName}}.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush delete mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush delete mode 100644 ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush delete mode 100644 ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush delete mode 100644 ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush diff --git a/changelog.md b/changelog.md index 6211360d24..ac58d6f5ad 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ - [#3536](https://github.com/ignite/cli/pull/3536) Change app.go to v2 and add AppWiring feature - [#3659](https://github.com/ignite/cli/pull/3659) cosmos-sdk `v0.50.x` - [#3670](https://github.com/ignite/cli/pull/3670) Remove nodetime binaries +- [#3707](https://github.com/ignite/cli/pull/3707) Add collections support - [#3724](https://github.com/ignite/cli/pull/3724) Add or vendor proto packages from Go dependencies - [#3715](https://github.com/ignite/cli/pull/3715) Add test suite for the cli tests - [#3756](https://github.com/ignite/cli/pull/3756) Add faucet compatibility for latest sdk chains diff --git a/ignite/templates/app/files/app/ibc.go.plush b/ignite/templates/app/files/app/ibc.go.plush index 2fa1ea3766..d094560294 100644 --- a/ignite/templates/app/files/app/ibc.go.plush +++ b/ignite/templates/app/files/app/ibc.go.plush @@ -67,6 +67,13 @@ func (app *App) registerIBCModules() { app.GetMemKey(capabilitytypes.MemStoreKey), ) + // add capability keeper and ScopeToModule for ibc module + app.CapabilityKeeper = capabilitykeeper.NewKeeper( + app.AppCodec(), + app.GetKey(capabilitytypes.StoreKey), + app.GetMemKey(capabilitytypes.MemStoreKey), + ) + // add capability keeper and ScopeToModule for ibc module scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName) scopedIBCTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName) diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush index 7b6bf1ef8e..ff761a3294 100644 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush +++ b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush @@ -8,7 +8,7 @@ import ( ) // <%= queryName.UpperCamel %>Result returns the <%= queryName.UpperCamel %> result by RequestId -func (k Keeper) <%= queryName.UpperCamel %>Result(c context.Context, req *types.Query<%= queryName.UpperCamel %>Request) (*types.Query<%= queryName.UpperCamel %>Response, error) { +func (q queryServer) <%= queryName.UpperCamel %>Result(c context.Context, req *types.Query<%= queryName.UpperCamel %>Request) (*types.Query<%= queryName.UpperCamel %>Response, error) { ctx := sdk.UnwrapSDKContext(c) result, err := k.Get<%= queryName.UpperCamel %>Result(ctx, types.OracleRequestID(req.RequestId)) if err != nil { @@ -18,7 +18,7 @@ func (k Keeper) <%= queryName.UpperCamel %>Result(c context.Context, req *types. } // Last<%= queryName.UpperCamel %>Id returns the last <%= queryName.UpperCamel %> request Id -func (k Keeper) Last<%= queryName.UpperCamel %>Id(c context.Context, req *types.QueryLast<%= queryName.UpperCamel %>IdRequest) (*types.QueryLast<%= queryName.UpperCamel %>IdResponse, error) { +func (q queryServer) Last<%= queryName.UpperCamel %>Id(c context.Context, req *types.QueryLast<%= queryName.UpperCamel %>IdRequest) (*types.QueryLast<%= queryName.UpperCamel %>IdResponse, error) { ctx := sdk.UnwrapSDKContext(c) id := k.GetLast<%= queryName.UpperCamel %>ID(ctx) return &types.QueryLast<%= queryName.UpperCamel %>IdResponse{RequestId: id}, nil diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/{{queryName}}.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/{{queryName}}.go.plush deleted file mode 100644 index f5bf0e2c40..0000000000 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/{{queryName}}.go.plush +++ /dev/null @@ -1,46 +0,0 @@ -package keeper - -import ( - "context" - - errorsmod "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/runtime" - gogotypes "github.com/cosmos/gogoproto/types" - "<%= ModulePath %>/x/<%= moduleName %>/types" -) - -// Set<%= queryName.UpperCamel %>Result saves the <%= queryName.UpperCamel %> result -func (k Keeper) Set<%= queryName.UpperCamel %>Result(ctx context.Context, requestID types.OracleRequestID, result types.<%= queryName.UpperCamel %>Result) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store.Set(types.<%= queryName.UpperCamel %>ResultStoreKey(requestID), k.cdc.MustMarshal(&result)) -} - -// Get<%= queryName.UpperCamel %>Result returns the <%= queryName.UpperCamel %> by requestId -func (k Keeper) Get<%= queryName.UpperCamel %>Result(ctx context.Context, id types.OracleRequestID) (types.<%= queryName.UpperCamel %>Result, error) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz := store.Get(types.<%= queryName.UpperCamel %>ResultStoreKey(id)) - if bz == nil { - return types.<%= queryName.UpperCamel %>Result{}, errorsmod.Wrapf(types.ErrSample, - "GetResult: Result for request ID %d is not available.", id, - ) - } - var result types.<%= queryName.UpperCamel %>Result - k.cdc.MustUnmarshal(bz, &result) - return result, nil -} - -// GetLast<%= queryName.UpperCamel %>ID return the id from the last <%= queryName.UpperCamel %> request -func (k Keeper) GetLast<%= queryName.UpperCamel %>ID(ctx context.Context) int64 { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz := store.Get(types.KeyPrefix(types.Last<%= queryName.UpperCamel %>IDKey)) - intV := gogotypes.Int64Value{} - k.cdc.MustUnmarshalLengthPrefixed(bz, &intV) - return intV.GetValue() -} - -// SetLast<%= queryName.UpperCamel %>ID saves the id from the last <%= queryName.UpperCamel %> request -func (k Keeper) SetLast<%= queryName.UpperCamel %>ID(ctx context.Context, id types.OracleRequestID) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store.Set(types.KeyPrefix(types.Last<%= queryName.UpperCamel %>IDKey), - k.cdc.MustMarshalLengthPrefixed(&gogotypes.Int64Value{Value: int64(id)})) -} 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 index bde969ac1b..2bf14af185 100644 --- a/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/base/testutil/keeper/{{moduleName}}.go.plush @@ -44,7 +44,7 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger()) // Initialize params - k.SetParams(ctx, types.DefaultParams()) + _ = k.Params.Set(ctx, types.DefaultParams()) return k, ctx } 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 d4ed36dd16..9c073ec7b1 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 @@ -3,6 +3,7 @@ package keeper import ( "fmt" + "cosmossdk.io/collections" "cosmossdk.io/core/store" "cosmossdk.io/log" "github.com/cosmos/cosmos-sdk/codec" @@ -26,9 +27,14 @@ type ( storeService store.KVStoreService logger log.Logger - // the address capable of executing a MsgUpdateParams message. Typically, this - // should be the x/gov module account. - authority string + // the address capable of executing a MsgUpdateParams message. + // Typically, this should be the x/gov module account. + authority string + + Schema collections.Schema + Params collections.Item[types.Params] + // this line is used by starport scaffolding # 1 + <%= if (isIBC) { %> ibcKeeperFn func() *ibckeeper.Keeper capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper @@ -52,7 +58,9 @@ func NewKeeper( panic(fmt.Sprintf("invalid authority address: %s", authority)) } - return Keeper{ + sb := collections.NewSchemaBuilder(storeService) + + k := Keeper{ cdc: cdc, storeService: storeService, authority: authority, @@ -61,7 +69,16 @@ func NewKeeper( capabilityScopedFn: capabilityScopedFn,<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %>: <%= toVariableName(dependency.KeeperName()) %>,<% } %> + Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), } + + schema, err := sb.Build() + if err != nil { + panic(err) + } + k.Schema = schema + + return k } // GetAuthority returns the module's authority. 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 93137c8ab9..2a29e4e669 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 @@ -4,18 +4,16 @@ import ( "context" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" "<%= modulePath %>/x/<%= moduleName %>/types" ) -func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { +func (k msgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { if k.GetAuthority() != req.Authority { return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority) } - ctx := sdk.UnwrapSDKContext(goCtx) - if err := k.SetParams(ctx, req.Params); err != nil { + if err := k.Params.Set(ctx, req.Params); err != nil { return nil, err } 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 9742f5cdd8..a9f05d47ed 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 @@ -12,7 +12,7 @@ import ( func TestMsgUpdateParams(t *testing.T) { k, ms, ctx := setupMsgServer(t) params := types.DefaultParams() - require.NoError(t, k.SetParams(ctx, params)) + require.NoError(t, k.Params.Set(ctx, params)) wctx := sdk.UnwrapSDKContext(ctx) // default params diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush deleted file mode 100644 index b64ed1ec37..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params.go.plush +++ /dev/null @@ -1,34 +0,0 @@ -package keeper - -import ( - "context" - - "github.com/cosmos/cosmos-sdk/runtime" - - "<%= modulePath %>/x/<%= moduleName %>/types" -) - - -// GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx context.Context) (params types.Params) { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz := store.Get(types.ParamsKey) - if bz == nil { - return params - } - - k.cdc.MustUnmarshal(bz, ¶ms) - return params -} - -// SetParams set the params -func (k Keeper) SetParams(ctx context.Context, params types.Params) error { - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - bz, err := k.cdc.Marshal(¶ms) - if err != nil { - return err - } - store.Set(types.ParamsKey, bz) - - return nil -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush deleted file mode 100644 index 04f1e1685a..0000000000 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/params_test.go.plush +++ /dev/null @@ -1,18 +0,0 @@ -package keeper_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - - keepertest "<%= modulePath %>/testutil/keeper" - "<%= modulePath %>/x/<%= moduleName %>/types" -) - -func TestGetParams(t *testing.T) { - k, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - params := types.DefaultParams() - - require.NoError(t, k.SetParams(ctx, params)) - require.EqualValues(t, params, k.GetParams(ctx)) -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush index dbdb2d2ea1..a2a531ca4d 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush @@ -4,4 +4,14 @@ import ( "<%= modulePath %>/x/<%= moduleName %>/types" ) -var _ types.QueryServer = Keeper{} +var _ types.QueryServer = queryServer{} + +// NewQueryServerImpl returns an implementation of the QueryServer interface +// for the provided Keeper. +func NewQueryServerImpl(k Keeper) types.QueryServer { + return queryServer{k} +} + +type queryServer struct { + k Keeper +} \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush index 4d54fc5cae..ee3f0f6399 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query_params.go.plush @@ -2,19 +2,29 @@ package keeper import ( "context" + "errors" + + "cosmossdk.io/collections" - sdk "github.com/cosmos/cosmos-sdk/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "<%= modulePath %>/x/<%= moduleName %>/types" ) -func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { +func (q queryServer) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - ctx := sdk.UnwrapSDKContext(goCtx) - return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil + params, err := q.k.Params.Get(ctx) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return nil, status.Error(codes.NotFound, "not found") + } + + return nil, status.Error(codes.Internal, "internal error") + } + + return &types.QueryParamsResponse{Params: params}, nil } 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 c3ff71962e..285e888e5a 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 @@ -7,14 +7,17 @@ import ( keepertest "<%= modulePath %>/testutil/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" + "<%= modulePath %>/x/<%= moduleName %>/keeper" ) func TestParamsQuery(t *testing.T) { - keeper, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - params := types.DefaultParams() - require.NoError(t, keeper.SetParams(ctx, params)) + k, ctx := keepertest.<%= title(moduleName) %>Keeper(t) - response, err := keeper.Params(ctx, &types.QueryParamsRequest{}) + params := types.DefaultParams() + require.NoError(t, k.Params.Set(ctx, params)) + + queryServer := keeper.NewQueryServerImpl(k) + response, err := queryServer.Params(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/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush index ed35249792..a5fc5f7808 100644 --- 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 @@ -10,13 +10,18 @@ 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) + k.Params.Set(ctx, genState.Params) } // ExportGenesis returns the module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { + var err error + genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) + genesis.Params, err = k.Params.Get(ctx) + if err != nil { + panic(err) + } // this line is used by starport scaffolding # genesis/module/export 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 558c9e745d..072eccecfa 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 @@ -128,7 +128,7 @@ func NewAppModule( // RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(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) 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 2e03a1573e..137a383212 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 @@ -1,5 +1,7 @@ package types +import "cosmossdk.io/collections" + const ( // ModuleName defines the module name ModuleName = "<%= moduleName %>" @@ -14,11 +16,7 @@ const ( ) var ( - ParamsKey = []byte("p_<%= moduleName %>") + ParamsKey = collections.NewPrefix("p_<%= moduleName %>") ) <%= if (isIBC) { %>// this line is used by starport scaffolding # ibc/keys/port<% } %> - -func KeyPrefix(p string) []byte { - return []byte(p) -} diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush index dbe9b6c49b..5e269790dd 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush @@ -2,12 +2,8 @@ package types import ( <%= if (len(params) > 0) { %>"fmt"<% } %> - - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) -var _ paramtypes.ParamSet = (*Params)(nil) - <%= for (param) in params { %> var ( Key<%= param.Name.UpperCamel %> = []byte("<%= param.Name.UpperCamel %>")<%= if (param.DataType() == "string") { %> @@ -18,11 +14,6 @@ var ( ) <% } %> -// ParamKeyTable the param key table for launch module -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - // NewParams creates a new Params instance func NewParams(<%= for (param) in params { %> <%= param.Name.LowerCamel %> <%= param.DataType() %>,<% } %> @@ -39,13 +30,6 @@ func DefaultParams() Params { ) } -// ParamSetPairs get the params.ParamSet -func (p *Params) ParamSetPairs() 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 {<%= for (param) in params { %> if err := validate<%= param.Name.UpperCamel %>(p.<%= param.Name.UpperCamel %>); err != nil { diff --git a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush index 9dbc5263f6..19bc674afd 100644 --- a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush @@ -63,7 +63,7 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger()) // Initialize params - k.SetParams(ctx, types.DefaultParams()) + k.Params.Set(ctx, types.DefaultParams()) return k, ctx } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 506d70e615..1b7feba645 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -169,7 +169,7 @@ PortID = "%[1]v"` // PlaceholderIBCKeysPort templatePort := `var ( // PortKey defines the key to store the port ID in store - PortKey = KeyPrefix("%[1]v-port-") + PortKey = collections.NewPrefix("%[1]v-port-") )` replacementPort := fmt.Sprintf(templatePort, opts.ModuleName) content = replacer.Replace(content, module.PlaceholderIBCKeysPort, replacementPort) diff --git a/ignite/templates/query/files/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush b/ignite/templates/query/files/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush index b4cccf5ffa..daecb42800 100644 --- a/ignite/templates/query/files/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush +++ b/ignite/templates/query/files/x/{{moduleName}}/keeper/query_{{queryName}}.go.plush @@ -4,20 +4,16 @@ import ( "context" "<%= ModulePath %>/x/<%= ModuleName %>/types" - sdk "github.com/cosmos/cosmos-sdk/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) -func (k Keeper) <%= QueryName.UpperCamel %>(goCtx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) { +func (q queryServer) <%= QueryName.UpperCamel %>(ctx context.Context, req *types.Query<%= QueryName.UpperCamel %>Request) (*types.Query<%= QueryName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Process the query - _ = ctx return &types.Query<%= QueryName.UpperCamel %>Response{}, nil } diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush index 672542dd15..2f7d652fb6 100644 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush +++ b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush @@ -2,9 +2,8 @@ package keeper import ( "context" + "collections" - "cosmossdk.io/store/prefix" - "github.com/cosmos/cosmos-sdk/runtime" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/query" "<%= ModulePath %>/x/<%= ModuleName %>/types" @@ -12,25 +11,21 @@ import ( "google.golang.org/grpc/status" ) -func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) { +func (q queryServer) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } var <%= TypeName.LowerCamel %>s []types.<%= TypeName.UpperCamel %> - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - <%= TypeName.LowerCamel %>Store := prefix.NewStore(store, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - - pageRes, err := query.Paginate(<%= TypeName.LowerCamel %>Store, req.Pagination, func(key []byte, value []byte) error { - var <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %> - if err := k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil { - return err - } - - <%= TypeName.LowerCamel %>s = append(<%= TypeName.LowerCamel %>s, <%= TypeName.LowerCamel %>) - return nil - }) + pageRes, err := query.CollectionPaginate( + ctx, + q.k.<%= TypeName.LowerCamel %>, + req.Pagination, + func(_ uint64, value types.<%= TypeName.UpperCamel %>) (types.<%= TypeName.UpperCamel %>, error){ + return value, nil + }, + ) if err != nil { return nil, status.Error(codes.Internal, err.Error()) @@ -39,14 +34,18 @@ func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.Qu return &types.QueryAll<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>s, Pagination: pageRes}, nil } -func (k Keeper) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { +func (q queryServer) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - <%= TypeName.LowerCamel %>, found := k.Get<%= TypeName.UpperCamel %>(ctx, req.Id) - if !found { - return nil, sdkerrors.ErrKeyNotFound + <%= TypeName.LowerCamel %>, err := q.k.<%= TypeName.UpperCamel %>Get(ctx, req.Id) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return nil, sdkerrors.ErrKeyNotFound + } + + return nil, status.Error(codes.Internal, "internal error") } return &types.QueryGet<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>}, nil 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 04d5ac1fa3..d1146501ec 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 @@ -10,12 +10,23 @@ import ( "google.golang.org/grpc/status" "<%= ModulePath %>/x/<%= ModuleName %>/types" + "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/testutil/nullify" keepertest "<%= ModulePath %>/testutil/keeper" ) +func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> { + items := make([]types.<%= TypeName.UpperCamel %>, n) + for i := range items { + items[i].Id = keeper.<%= TypeName.UpperCamel%>.Set(i, items[i]) + } + return items +} + func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) + k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) + queryServer := keeper.NewQueryServerImpl(k) + msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 2) tests := []struct { desc string @@ -45,7 +56,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - response, err := keeper.<%= TypeName.UpperCamel %>(ctx, tc.request) + response, err := queryServer.<%= TypeName.UpperCamel %>(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { @@ -60,7 +71,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) { } func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) + k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) + queryServer := keeper.NewQueryServerImpl(k) + msgs := createN<%= TypeName.UpperCamel %>(keeper, ctx, 5) request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request { @@ -76,7 +89,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 := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, uint64(i), uint64(step), false)) + resp, err := queryServer.<%= TypeName.UpperCamel %>All(ctx, request(nil, uint64(i), uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -89,7 +102,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { step := 2 var next []byte for i := 0; i < len(msgs); i += step { - resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(next, 0, uint64(step), false)) + resp, err := queryServer.<%= TypeName.UpperCamel %>All(ctx, request(next, 0, uint64(step), false)) require.NoError(t, err) require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step) require.Subset(t, @@ -100,7 +113,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { } }) t.Run("Total", func(t *testing.T) { - resp, err := keeper.<%= TypeName.UpperCamel %>All(ctx, request(nil, 0, 0, true)) + resp, err := queryServer.<%= TypeName.UpperCamel %>All(ctx, request(nil, 0, 0, true)) require.NoError(t, err) require.Equal(t, len(msgs), int(resp.Pagination.Total)) require.ElementsMatch(t, @@ -109,7 +122,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) { ) }) t.Run("InvalidRequest", func(t *testing.T) { - _, err := keeper.<%= TypeName.UpperCamel %>All(ctx, nil) + _, err := queryServer.<%= TypeName.UpperCamel %>All(ctx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) }) } diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush deleted file mode 100644 index c7f2423799..0000000000 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush +++ /dev/null @@ -1,111 +0,0 @@ -package keeper - -import ( - "context" - "encoding/binary" - - "cosmossdk.io/store/prefix" - storetypes "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/runtime" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -// Get<%= TypeName.UpperCamel %>Count get the total number of <%= TypeName.LowerCamel %> -func (k Keeper) Get<%= TypeName.UpperCamel %>Count(ctx context.Context) uint64 { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, []byte{}) - byteKey := types.KeyPrefix(types.<%= TypeName.UpperCamel %>CountKey) - bz := store.Get(byteKey) - - // Count doesn't exist: no element - if bz == nil { - return 0 - } - - // Parse bytes - return binary.BigEndian.Uint64(bz) -} - -// Set<%= TypeName.UpperCamel %>Count set the total number of <%= TypeName.LowerCamel %> -func (k Keeper) Set<%= TypeName.UpperCamel %>Count(ctx context.Context, count uint64) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, []byte{}) - byteKey := types.KeyPrefix(types.<%= TypeName.UpperCamel %>CountKey) - bz := make([]byte, 8) - binary.BigEndian.PutUint64(bz, count) - store.Set(byteKey, bz) -} - -// Append<%= TypeName.UpperCamel %> appends a <%= TypeName.LowerCamel %> in the store with a new id and update the count -func (k Keeper) Append<%= TypeName.UpperCamel %>( - ctx context.Context, - <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>, -) uint64 { - // Create the <%= TypeName.LowerCamel %> - count := k.Get<%= TypeName.UpperCamel %>Count(ctx) - - // Set the ID of the appended value - <%= TypeName.LowerCamel %>.Id = count - - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - appendedValue := k.cdc.MustMarshal(&<%= TypeName.LowerCamel %>) - store.Set(Get<%= TypeName.UpperCamel %>IDBytes(<%= TypeName.LowerCamel %>.Id), appendedValue) - - // Update <%= TypeName.LowerCamel %> count - k.Set<%= TypeName.UpperCamel %>Count(ctx, count+1) - - return count -} - -// Set<%= TypeName.UpperCamel %> set a specific <%= TypeName.LowerCamel %> in the store -func (k Keeper) Set<%= TypeName.UpperCamel %>(ctx context.Context, <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - b := k.cdc.MustMarshal(&<%= TypeName.LowerCamel %>) - store.Set(Get<%= TypeName.UpperCamel %>IDBytes(<%= TypeName.LowerCamel %>.Id), b) -} - -// Get<%= TypeName.UpperCamel %> returns a <%= TypeName.LowerCamel %> from its id -func (k Keeper) Get<%=TypeName.UpperCamel %>(ctx context.Context, id uint64) (val types.<%= TypeName.UpperCamel %>, found bool) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - b := store.Get(Get<%= TypeName.UpperCamel %>IDBytes(id)) - if b == nil { - return val, false - } - k.cdc.MustUnmarshal(b, &val) - return val, true -} - -// Remove<%= TypeName.UpperCamel %> removes a <%= TypeName.LowerCamel %> from the store -func (k Keeper) Remove<%= TypeName.UpperCamel %>(ctx context.Context, id uint64) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - store.Delete(Get<%= TypeName.UpperCamel %>IDBytes(id)) -} - -// GetAll<%= TypeName.UpperCamel %> returns all <%= TypeName.LowerCamel %> -func (k Keeper) GetAll<%= TypeName.UpperCamel %>(ctx context.Context) (list []types.<%= TypeName.UpperCamel %>) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - iterator := storetypes.KVStorePrefixIterator(store, []byte{}) - - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - var val types.<%= TypeName.UpperCamel %> - k.cdc.MustUnmarshal(iterator.Value(), &val) - list = append(list, val) - } - - return -} - -// Get<%= TypeName.UpperCamel %>IDBytes returns the byte representation of the ID -func Get<%= TypeName.UpperCamel %>IDBytes(id uint64) []byte { - bz := types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key) - bz = append(bz, []byte("/")...) - bz = binary.BigEndian.AppendUint64(bz, id) - return bz -} diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush deleted file mode 100644 index c1c27d7429..0000000000 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush +++ /dev/null @@ -1,59 +0,0 @@ -package keeper_test - -import ( - "context" - "testing" - - "<%= ModulePath %>/x/<%= ModuleName %>/keeper" - "<%= ModulePath %>/x/<%= ModuleName %>/types" - keepertest "<%= ModulePath %>/testutil/keeper" - "<%= ModulePath %>/testutil/nullify" - "github.com/stretchr/testify/require" -) - -func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> { - items := make([]types.<%= TypeName.UpperCamel %>, n) - for i := range items { - items[i].Id = keeper.Append<%= TypeName.UpperCamel%>(ctx, items[i]) - } - return items -} - -func Test<%= TypeName.UpperCamel %>Get(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - for _, item := range items { - got, found := keeper.Get<%= TypeName.UpperCamel %>(ctx, item.Id) - require.True(t, found) - require.Equal(t, - nullify.Fill(&item), - nullify.Fill(&got), - ) - } -} - -func Test<%= TypeName.UpperCamel %>Remove(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - for _, item := range items { - keeper.Remove<%= TypeName.UpperCamel %>(ctx, item.Id) - _, found := keeper.Get<%= TypeName.UpperCamel %>(ctx, item.Id) - require.False(t, found) - } -} - -func Test<%= TypeName.UpperCamel %>GetAll(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - require.ElementsMatch(t, - nullify.Fill(items), - nullify.Fill(keeper.GetAll<%= TypeName.UpperCamel %>(ctx)), - ) -} - -func Test<%= TypeName.UpperCamel %>Count(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - count := uint64(len(items)) - require.Equal(t, count, keeper.Get<%= TypeName.UpperCamel %>Count(ctx)) -} diff --git a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush index 270ab3f997..e12853ddc7 100644 --- a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush +++ b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush @@ -6,32 +6,36 @@ import ( "<%= ModulePath %>/x/<%= ModuleName %>/types" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) -func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Create<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) { var <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{ <%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>,<%= for (field) in Fields { %> <%= field.Name.UpperCamel %>: msg.<%= field.Name.UpperCamel %>,<% } %> } - id := k.Append<%= TypeName.UpperCamel %>( + nextId, err := k.<%= TypeName.UpperCamel %>Seq.Next(ctx) + if err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "failed to get next id") + } + + err = k.<%= TypeName.UpperCamel %>.Set( ctx, + nextId, <%= TypeName.LowerCamel %>, ) + if err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to set <%= TypeName.LowerCamel %>") + } return &types.MsgCreate<%= TypeName.UpperCamel %>Response{ - Id: id, + Id: nextId, }, nil } -func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) { var <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{ <%= MsgSigner.UpperCamel %>: msg.<%= MsgSigner.UpperCamel %>, Id: msg.Id,<%= for (field) in Fields { %> @@ -39,9 +43,13 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg } // Checks that the element exists - val, found := k.Get<%= TypeName.UpperCamel %>(ctx, msg.Id) - if !found { - return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, msg.Id) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + } + + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to get <%= TypeName.LowerCamel %>") } // Checks if the msg <%= MsgSigner.LowerCamel %> is the same as the current owner @@ -49,18 +57,22 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") } - k.Set<%= TypeName.UpperCamel %>(ctx, <%= TypeName.LowerCamel %>) + if err := k.<%= TypeName.UpperCamel %>.Set(ctx, msg.Id, <%= TypeName.LowerCamel %>); err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to update <%= TypeName.LowerCamel %>") + } return &types.MsgUpdate<%= TypeName.UpperCamel %>Response{}, nil } -func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Delete<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) { // Checks that the element exists - val, found := k.Get<%= TypeName.UpperCamel %>(ctx, msg.Id) - if !found { - return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + val, err := k.<%= TypeName.UpperCamel %>.Get(ctx, msg.Id) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + } + + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to get <%= TypeName.LowerCamel %>") } // Checks if the msg <%= MsgSigner.LowerCamel %> is the same as the current owner @@ -68,7 +80,9 @@ func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") } - k.Remove<%= TypeName.UpperCamel %>(ctx, msg.Id) + if err := k.<%= TypeName.UpperCamel %>.Remove(ctx, msg.Id); err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to delete <%= TypeName.LowerCamel %>") + } return &types.MsgDelete<%= TypeName.UpperCamel %>Response{}, nil } diff --git a/ignite/templates/typed/list/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush b/ignite/templates/typed/list/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush index 2b4b871bd7..2ef71cfb0b 100644 --- a/ignite/templates/typed/list/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush +++ b/ignite/templates/typed/list/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush @@ -53,9 +53,18 @@ func SimulateMsgUpdate<%= TypeName.UpperCamel %>( simAccount = simtypes.Account{} <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx) found = false ) + + var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %> + err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key uint64, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) { + all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value) + return false, nil + }) + if err != nil { + panic(err) + } + for _, obj := range all<%= TypeName.UpperCamel %> { simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>) if found { @@ -97,9 +106,18 @@ func SimulateMsgDelete<%= TypeName.UpperCamel %>( simAccount = simtypes.Account{} <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx) found = false ) + + var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %> + err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key uint64, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) { + all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value) + return false, nil + }) + if err != nil { + panic(err) + } + for _, obj := range all<%= TypeName.UpperCamel %> { simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>) if found { diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index b9bd4f81e8..eb5f1c7e54 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -123,11 +123,15 @@ func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) gen templateModuleInit := `// Set all the %[2]v for _, elem := range genState.%[3]vList { - k.Set%[3]v(ctx, elem) + if err := k.%[3]v.Set(ctx, elem.Id, elem); err != nil { + panic(err) + } } // Set %[2]v count -k.Set%[3]vCount(ctx, genState.%[3]vCount) +if err := k.%[3]vSeq.Set(ctx, genState.%[3]vCount); err != nil { + panic(err) +} %[1]v` replacementModuleInit := fmt.Sprintf( templateModuleInit, @@ -137,8 +141,20 @@ k.Set%[3]vCount(ctx, genState.%[3]vCount) ) content := replacer.Replace(f.String(), typed.PlaceholderGenesisModuleInit, replacementModuleInit) - templateModuleExport := `genesis.%[2]vList = k.GetAll%[2]v(ctx) -genesis.%[2]vCount = k.Get%[2]vCount(ctx) + templateModuleExport := ` +err = k.%[2]v.Walk(ctx, nil, func(key uint64, elem types.%[2]v) (bool, error) { + genesis.%[2]vList = append(genesis.%[2]vList, elem) + return false, nil +}) +if err != nil { + panic(err) +} + +genesis.%[2]vCount, err = k.%[2]vSeq.Peek(ctx) +if err != nil { + panic(err) +} + %[1]v` replacementModuleExport := fmt.Sprintf( templateModuleExport, diff --git a/ignite/templates/typed/list/list.go b/ignite/templates/typed/list/list.go index e3603b5ff8..2889a63e20 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -18,6 +18,13 @@ import ( "github.com/ignite/cli/ignite/templates/typed" ) +/* +TODO add the following in keeper.go + + TypeNameSeq collections.Sequence + TypeName collections.Map[uint64, types.TypeName] +*/ + var ( //go:embed files/component/* files/component/**/* fsComponent embed.FS @@ -271,9 +278,9 @@ func typesKeyModify(opts *typed.Options) genny.RunFn { return err } content := f.String() + fmt.Sprintf(` -const ( - %[1]vKey= "%[1]v/value/" - %[1]vCountKey= "%[1]v/count/" +var ( + %[1]vKey= collections.NewPrefix("%[1]v/value/") + %[1]vCountKey= collections.NewPrefix("%[1]v/count/") ) `, opts.TypeName.UpperCamel) newFile := genny.NewFileS(path, content) diff --git a/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush b/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush index baf3e232c2..874ad77f1b 100644 --- a/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush +++ b/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush @@ -11,19 +11,21 @@ import ( "google.golang.org/grpc/status" ) -func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) { +func (q queryServer) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.QueryAll<%= TypeName.UpperCamel %>Request) (*types.QueryAll<%= TypeName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } var <%= TypeName.LowerCamel %>s []types.<%= TypeName.UpperCamel %> - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) + // TODO use collections + + store := runtime.KVStoreAdapter(q.k.storeService.OpenKVStore(ctx)) <%= TypeName.LowerCamel %>Store := prefix.NewStore(store, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix)) pageRes, err := query.Paginate(<%= TypeName.LowerCamel %>Store, req.Pagination, func(key []byte, value []byte) error { var <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %> - if err := k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil { + if err := q.k.cdc.Unmarshal(value, &<%= TypeName.LowerCamel %>); err != nil { return err } @@ -38,16 +40,16 @@ func (k Keeper) <%= TypeName.UpperCamel %>All(ctx context.Context, req *types.Qu return &types.QueryAll<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: <%= TypeName.LowerCamel %>s, Pagination: pageRes}, nil } -func (k Keeper) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { +func (q queryServer) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - val, found := k.Get<%= TypeName.UpperCamel %>( + val, err := q.k.<%= TypeName.UpperCamel %>.Get( ctx, <%= for (i, index) in Indexes { %>req.<%= index.Name.UpperCamel %>, <% } %>) - if !found { + if err != nil { return nil, status.Error(codes.NotFound, "not found") } diff --git a/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush b/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush deleted file mode 100644 index a0c681ce6a..0000000000 --- a/ignite/templates/typed/map/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush +++ /dev/null @@ -1,70 +0,0 @@ -package keeper - -import ( - "context" - - "cosmossdk.io/store/prefix" - storetypes "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/runtime" - "<%= ModulePath %>/x/<%= ModuleName %>/types" -) - -// Set<%= TypeName.UpperCamel %> set a specific <%= TypeName.LowerCamel %> in the store from its index -func (k Keeper) Set<%= TypeName.UpperCamel %>(ctx context.Context, <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix)) - b := k.cdc.MustMarshal(&<%= TypeName.LowerCamel %>) - store.Set(types.<%= TypeName.UpperCamel %>Key( - <%= for (i, index) in Indexes { %><%= TypeName.LowerCamel %>.<%= index.Name.UpperCamel %>, - <% } %>), b) -} - -// Get<%= TypeName.UpperCamel %> returns a <%= TypeName.LowerCamel %> from its index -func (k Keeper) Get<%= TypeName.UpperCamel %>( - ctx context.Context, - <%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %> <%= index.DataType() %>, - <% } %> -) (val types.<%= TypeName.UpperCamel %>, found bool) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix)) - - b := store.Get(types.<%= TypeName.UpperCamel %>Key( - <%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %>, - <% } %>)) - if b == nil { - return val, false - } - - k.cdc.MustUnmarshal(b, &val) - return val, true -} - -// Remove<%= TypeName.UpperCamel %> removes a <%= TypeName.LowerCamel %> from the store -func (k Keeper) Remove<%= TypeName.UpperCamel %>( - ctx context.Context, - <%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %> <%= index.DataType() %>, - <% } %> -) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix)) - store.Delete(types.<%= TypeName.UpperCamel %>Key( - <%= for (i, index) in Indexes { %><%= index.Name.LowerCamel %>, - <% } %>)) -} - -// GetAll<%= TypeName.UpperCamel %> returns all <%= TypeName.LowerCamel %> -func (k Keeper) GetAll<%= TypeName.UpperCamel %>(ctx context.Context) (list []types.<%= TypeName.UpperCamel %>) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>KeyPrefix)) - iterator := storetypes.KVStorePrefixIterator(store, []byte{}) - - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - var val types.<%= TypeName.UpperCamel %> - k.cdc.MustUnmarshal(iterator.Value(), &val) - list = append(list, val) - } - - return -} diff --git a/ignite/templates/typed/map/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush b/ignite/templates/typed/map/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush index 3a7cc6ea22..982f01b479 100644 --- a/ignite/templates/typed/map/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush +++ b/ignite/templates/typed/map/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush @@ -5,20 +5,19 @@ import ( "<%= ModulePath %>/x/<%= ModuleName %>/types" errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) -func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Create<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgCreate<%= TypeName.UpperCamel %>) (*types.MsgCreate<%= TypeName.UpperCamel %>Response, error) { // Check if the value already exists - _, isFound := k.Get<%= TypeName.UpperCamel %>( + ok, err := k.<%= TypeName.UpperCamel %>.Has( ctx, - <%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>, + <%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>, // TODO need to limit max nb of indexes <% } %>) - if isFound { + if err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error()) + } else if ok { return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "index already set") } @@ -29,16 +28,19 @@ func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg <% } %> } - k.Set<%= TypeName.UpperCamel %>( + if err := k.Set<%= TypeName.UpperCamel %>( ctx, + <%= for (i, index) in Indexes { %>msg.<%= index.Name.UpperCamel %>, + <% } %> <%= TypeName.LowerCamel %>, - ) - return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil + ); err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "failed to set <%= index.Name.UpperCamel %>") + } + + return &types.MsgCreate<%= TypeName.UpperCamel %>Response{}, nil } -func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Update<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgUpdate<%= TypeName.UpperCamel %>) (*types.MsgUpdate<%= TypeName.UpperCamel %>Response, error) { // Check if the value exists valFound, isFound := k.Get<%= TypeName.UpperCamel %>( ctx, @@ -65,9 +67,7 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg return &types.MsgUpdate<%= TypeName.UpperCamel %>Response{}, nil } -func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - +func (k msgServer) Delete<%= TypeName.UpperCamel %>(ctx context.Context, msg *types.MsgDelete<%= TypeName.UpperCamel %>) (*types.MsgDelete<%= TypeName.UpperCamel %>Response, error) { // Check if the value exists valFound, isFound := k.Get<%= TypeName.UpperCamel %>( ctx, diff --git a/ignite/templates/typed/map/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush b/ignite/templates/typed/map/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush index fb2392f0d4..0c3fced73b 100644 --- a/ignite/templates/typed/map/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush +++ b/ignite/templates/typed/map/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush @@ -64,9 +64,18 @@ func SimulateMsgUpdate<%= TypeName.UpperCamel %>( simAccount = simtypes.Account{} <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx) found = false ) + + var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %> + err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key uint64, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) { + all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value) + return false, nil + }) + if err != nil { + panic(err) + } + for _, obj := range all<%= TypeName.UpperCamel %> { simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>) if found { @@ -109,9 +118,18 @@ func SimulateMsgDelete<%= TypeName.UpperCamel %>( simAccount = simtypes.Account{} <%= TypeName.LowerCamel %> = types.<%= TypeName.UpperCamel %>{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - all<%= TypeName.UpperCamel %> = k.GetAll<%= TypeName.UpperCamel %>(ctx) found = false ) + + var all<%= TypeName.UpperCamel %> []types.<%= TypeName.UpperCamel %> + err := k.<%= TypeName.UpperCamel %>.Walk(ctx, nil, func(key uint64, value types.<%= TypeName.UpperCamel %>) (stop bool, err error) { + all<%= TypeName.UpperCamel %> = append(all<%= TypeName.UpperCamel %>, value) + return false, nil + }) + if err != nil { + panic(err) + } + for _, obj := range all<%= TypeName.UpperCamel %> { simAccount, found = FindAccount(accs, obj.<%= MsgSigner.UpperCamel %>) if found { diff --git a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush deleted file mode 100644 index 13f9c5e404..0000000000 --- a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush +++ /dev/null @@ -1,66 +0,0 @@ -package keeper_test - -import ( - "context" - "strconv" - "testing" - - "<%= ModulePath %>/x/<%= ModuleName %>/keeper" - "<%= ModulePath %>/x/<%= ModuleName %>/types" - keepertest "<%= ModulePath %>/testutil/keeper" - "<%= ModulePath %>/testutil/nullify" - "github.com/stretchr/testify/require" -) - -// Prevent strconv unused error -var _ = strconv.IntSize - -func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> { - items := make([]types.<%= TypeName.UpperCamel %>, n) - for i := range items { - <%= for (i, index) in Indexes { %>items[i].<%= index.Name.UpperCamel %> = <%= index.ValueLoop() %> - <% } %> - keeper.Set<%= TypeName.UpperCamel %>(ctx, items[i]) - } - return items -} - -func Test<%= TypeName.UpperCamel %>Get(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - for _, item := range items { - rst, found := keeper.Get<%= TypeName.UpperCamel %>(ctx, - <%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>, - <% } %> - ) - require.True(t, found) - require.Equal(t, - nullify.Fill(&item), - nullify.Fill(&rst), - ) - } -} -func Test<%= TypeName.UpperCamel %>Remove(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - for _, item := range items { - keeper.Remove<%= TypeName.UpperCamel %>(ctx, - <%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>, - <% } %> - ) - _, found := keeper.Get<%= TypeName.UpperCamel %>(ctx, - <%= for (i, index) in Indexes { %>item.<%= index.Name.UpperCamel %>, - <% } %> - ) - require.False(t, found) - } -} - -func Test<%= TypeName.UpperCamel %>GetAll(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - items := createN<%= TypeName.UpperCamel %>(keeper, ctx, 10) - require.ElementsMatch(t, - nullify.Fill(items), - nullify.Fill(keeper.GetAll<%= TypeName.UpperCamel %>(ctx)), - ) -} diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 5e60c4b4bb..208774fc3f 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -19,6 +19,11 @@ import ( "github.com/ignite/cli/ignite/templates/typed" ) +/* +TODO add the following in keeper.go + TypeName collections.Map[string, types.TypeName] +*/ + var ( //go:embed files/messages/* files/messages/**/* fsMessages embed.FS diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush index 7bae036019..14124c30bb 100644 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush +++ b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/query_{{typeName}}.go.plush @@ -2,22 +2,27 @@ package keeper import ( "context" + "errors" + + "cosmossdk.io/collections" - sdk "github.com/cosmos/cosmos-sdk/types" "<%= ModulePath %>/x/<%= ModuleName %>/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) -func (k Keeper) <%= TypeName.UpperCamel %>(goCtx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { +func (q queryServer) <%= TypeName.UpperCamel %>(ctx context.Context, req *types.QueryGet<%= TypeName.UpperCamel %>Request) (*types.QueryGet<%= TypeName.UpperCamel %>Response, error) { if req == nil { return nil, status.Error(codes.InvalidArgument, "invalid request") } - ctx := sdk.UnwrapSDKContext(goCtx) - val, found := k.Get<%= TypeName.UpperCamel %>(ctx) - if !found { - return nil, status.Error(codes.NotFound, "not found") + val, err := q.k.<%= TypeName.UpperCamel %>.Get(ctx) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return nil, status.Error(codes.NotFound, "not found") + } + + return nil, status.Error(codes.Internal, "internal error") } return &types.QueryGet<%= TypeName.UpperCamel %>Response{<%= TypeName.UpperCamel %>: val}, nil 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 47f50cfb76..5f534864bc 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 @@ -10,10 +10,11 @@ import ( keepertest "<%= ModulePath %>/testutil/keeper" "<%= ModulePath %>/testutil/nullify" "<%= ModulePath %>/x/<%= ModuleName %>/types" + "<%= ModulePath %>/x/<%= ModuleName %>/keeper" ) func Test<%= TypeName.UpperCamel %>Query(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) + k, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) item := createTest<%= TypeName.UpperCamel %>(keeper, ctx) tests := []struct { desc string @@ -33,7 +34,8 @@ func Test<%= TypeName.UpperCamel %>Query(t *testing.T) { } for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - response, err := keeper.<%= TypeName.UpperCamel %>(ctx, tc.request) + queryServer := keeper.NewQueryServerImpl(k) + response, err := queryServer.<%= TypeName.UpperCamel %>(ctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) } else { diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush deleted file mode 100644 index b20f0d2df5..0000000000 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}.go.plush +++ /dev/null @@ -1,38 +0,0 @@ -package keeper - -import ( - "context" - - "github.com/cosmos/cosmos-sdk/runtime" - "<%= ModulePath %>/x/<%= ModuleName %>/types" - "cosmossdk.io/store/prefix" -) - -// Set<%= TypeName.UpperCamel %> set <%= TypeName.LowerCamel %> in the store -func (k Keeper) Set<%= TypeName.UpperCamel %>(ctx context.Context, <%= TypeName.LowerCamel %> types.<%= TypeName.UpperCamel %>) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - b := k.cdc.MustMarshal(&<%= TypeName.LowerCamel %>) - store.Set([]byte{0}, b) -} - -// Get<%= TypeName.UpperCamel %> returns <%= TypeName.LowerCamel %> -func (k Keeper) Get<%= TypeName.UpperCamel %>(ctx context.Context) (val types.<%= TypeName.UpperCamel %>, found bool) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - - b := store.Get([]byte{0}) - if b == nil { - return val, false - } - - k.cdc.MustUnmarshal(b, &val) - return val, true -} - -// Remove<%= TypeName.UpperCamel %> removes <%= TypeName.LowerCamel %> from the store -func (k Keeper) Remove<%= TypeName.UpperCamel %>(ctx context.Context) { - storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.<%= TypeName.UpperCamel %>Key)) - store.Delete([]byte{0}) -} diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush deleted file mode 100644 index 18c362873c..0000000000 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/keeper/{{typeName}}_test.go.plush +++ /dev/null @@ -1,38 +0,0 @@ -package keeper_test - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - - "<%= ModulePath %>/x/<%= ModuleName %>/keeper" - "<%= ModulePath %>/x/<%= ModuleName %>/types" - keepertest "<%= ModulePath %>/testutil/keeper" - "<%= ModulePath %>/testutil/nullify" -) - -func createTest<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context) types.<%= TypeName.UpperCamel %> { - item := types.<%= TypeName.UpperCamel %>{} - keeper.Set<%= TypeName.UpperCamel %>(ctx, item) - return item -} - -func Test<%= TypeName.UpperCamel %>Get(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - item := createTest<%= TypeName.UpperCamel %>(keeper, ctx) - rst, found := keeper.Get<%= TypeName.UpperCamel %>(ctx) - require.True(t, found) - require.Equal(t, - nullify.Fill(&item), - nullify.Fill(&rst), - ) -} - -func Test<%= TypeName.UpperCamel %>Remove(t *testing.T) { - keeper, ctx := keepertest.<%= title(ModuleName) %>Keeper(t) - createTest<%= TypeName.UpperCamel %>(keeper, ctx) - keeper.Remove<%= TypeName.UpperCamel %>(ctx) - _, found := keeper.Get<%= TypeName.UpperCamel %>(ctx) - require.False(t, found) -} diff --git a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush index a22a1a9996..473da95c0c 100644 --- a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush +++ b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/keeper/msg_server_{{typeName}}.go.plush @@ -14,8 +14,8 @@ func (k msgServer) Create<%= TypeName.UpperCamel %>(goCtx context.Context, msg ctx := sdk.UnwrapSDKContext(goCtx) // Check if the value already exists - _, isFound := k.Get<%= TypeName.UpperCamel %>(ctx) - if isFound { + _, found := k.<%= TypeName.UpperCamel %>.Has(ctx) + if found { return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "already set") } @@ -35,8 +35,8 @@ func (k msgServer) Update<%= TypeName.UpperCamel %>(goCtx context.Context, msg ctx := sdk.UnwrapSDKContext(goCtx) // Check if the value exists - valFound, isFound := k.Get<%= TypeName.UpperCamel %>(ctx) - if !isFound { + valFound, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + if err != nil { return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "not set") } @@ -59,8 +59,8 @@ func (k msgServer) Delete<%= TypeName.UpperCamel %>(goCtx context.Context, msg ctx := sdk.UnwrapSDKContext(goCtx) // Check if the value exists - valFound, isFound := k.Get<%= TypeName.UpperCamel %>(ctx) - if !isFound { + valFound, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + if err != nil { return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, "not set") } 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 ac5055420d..72a93e9c65 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 @@ -18,8 +18,8 @@ func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) { expected := &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>} _, err := srv.Create<%= TypeName.UpperCamel %>(ctx, expected) require.NoError(t, err) - rst, found := k.Get<%= TypeName.UpperCamel %>(ctx) - require.True(t, found) + rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + require.Nil(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } @@ -54,8 +54,8 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - rst, found := k.Get<%= TypeName.UpperCamel %>(ctx) - require.True(t, found) + rst, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + require.Nil(t, err) require.Equal(t, expected.<%= MsgSigner.UpperCamel %>, rst.<%= MsgSigner.UpperCamel %>) } }) @@ -92,7 +92,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) { require.ErrorIs(t, err, tc.err) } else { require.NoError(t, err) - _, found := k.Get<%= TypeName.UpperCamel %>(ctx) + _, found := k.<%= TypeName.UpperCamel %>.Has(ctx) require.False(t, found) } }) diff --git a/ignite/templates/typed/singleton/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush b/ignite/templates/typed/singleton/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush index ca6ca9b8c0..d460a3a240 100644 --- a/ignite/templates/typed/singleton/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush +++ b/ignite/templates/typed/singleton/files/simapp/x/{{moduleName}}/simulation/{{typeName}}.go.plush @@ -3,6 +3,8 @@ package simulation import ( "math/rand" + "cosmossdk.io/collections" + "<%= ModulePath %>/x/<%= ModuleName %>/keeper" "<%= ModulePath %>/x/<%= ModuleName %>/types" "github.com/cosmos/cosmos-sdk/baseapp" @@ -25,8 +27,8 @@ func SimulateMsgCreate<%= TypeName.UpperCamel %>( <%= MsgSigner.UpperCamel %>: simAccount.Address.String(), } - _, found := k.Get<%= TypeName.UpperCamel %>(ctx) - if found { + _, err := k.<%= TypeName.UpperCamel %>.Get(ctx) + if !errors.Is(err, collections.ErrNotFound) { return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "<%= TypeName.UpperCamel %> already exist"), nil, nil } @@ -57,9 +59,9 @@ func SimulateMsgUpdate<%= TypeName.UpperCamel %>( var ( simAccount = simtypes.Account{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - <%= TypeName.LowerCamel %>, found = k.Get<%= TypeName.UpperCamel %>(ctx) + <%= TypeName.LowerCamel %>, err = k.<%= TypeName.UpperCamel %>.Get(ctx) ) - if !found { + if err != nil { return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "<%= TypeName.LowerCamel %> store is empty"), nil, nil } simAccount, found = FindAccount(accs, <%= TypeName.LowerCamel %>.<%= MsgSigner.UpperCamel %>) @@ -95,9 +97,9 @@ func SimulateMsgDelete<%= TypeName.UpperCamel %>( var ( simAccount = simtypes.Account{} msg = &types.MsgUpdate<%= TypeName.UpperCamel %>{} - <%= TypeName.LowerCamel %>, found = k.Get<%= TypeName.UpperCamel %>(ctx) + <%= TypeName.LowerCamel %>, err = k.<%= TypeName.UpperCamel %>.Get(ctx) ) - if !found { + if !errors.Is(err, collections.ErrNotFound) { return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "<%= TypeName.LowerCamel %> store is empty"), nil, nil } simAccount, found = FindAccount(accs, <%= TypeName.LowerCamel %>.<%= MsgSigner.UpperCamel %>) diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index 9059678129..b644a39451 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -90,8 +90,8 @@ func typesKeyModify(opts *typed.Options) genny.RunFn { return err } content := f.String() + fmt.Sprintf(` -const ( - %[1]vKey= "%[1]v/value/" +var ( + %[1]vKey= collections.NewPrefix("%[1]v/value/") ) `, opts.TypeName.UpperCamel) newFile := genny.NewFileS(path, content)