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/x/{{moduleName}}/keeper/query.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/query.go.plush index 81c87a479e..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 @@ -6,6 +6,8 @@ import ( 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} } 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 a3edee5a3b..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,12 +4,11 @@ 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 %>(ctx 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") } 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/list.go b/ignite/templates/typed/list/list.go index 1bfbd678cb..ae5b17e1f3 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -272,8 +272,8 @@ func typesKeyModify(opts *typed.Options) genny.RunFn { } content := f.String() + fmt.Sprintf(` const ( - %[1]vKey= "%[1]v/value/" - %[1]vCountKey= "%[1]v/count/" + %[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/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 4ecb8d5933..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 @@ -19,7 +19,7 @@ func (q queryServer) <%= TypeName.UpperCamel %>(ctx context.Context, req *types. 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.NotFound, "not found") } return nil, status.Error(codes.Internal, "internal error") 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 10d8cc45c8..16e4a8b0c2 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -90,7 +90,7 @@ func typesKeyModify(opts *typed.Options) genny.RunFn { } content := f.String() + fmt.Sprintf(` const ( - %[1]vKey= "%[1]v/value/" + %[1]vKey= collections.NewPrefix("%[1]v/value/") ) `, opts.TypeName.UpperCamel) newFile := genny.NewFileS(path, content)