@@ -22,10 +22,10 @@ By completing this tutorial, you will learn about:
2222
2323## Prerequisites   
2424
25- This series of blog tutorials is based on a specific version of Ignite CLI, so to install Ignite CLI v0.20.0  use the following command:
25+ This series of blog tutorials is based on a specific version of Ignite CLI, so to install Ignite CLI v0.22.2  use the following command:
2626
2727``` bash 
28- curl https://get.ignite.com/cli@v0.22.1 !  |  bash
28+ curl https://get.ignite.com/cli@v0.22.2 !  |  bash
2929``` 
3030
3131## Create your blog chain  
@@ -139,14 +139,17 @@ You need to do two things:
139139func  (k  msgServer ) CreatePost  (goCtx  context .Context , msg  *types .MsgCreatePost ) (*types .MsgCreatePostResponse , error ) {
140140  //  Get the context
141141  ctx  :=  sdk.UnwrapSDKContext (goCtx)
142+ 
142143  //  Create variable of type Post
143144  var  post  = types.Post {
144145     Creator: msg.Creator ,
145146     Title:   msg.Title ,
146147     Body:    msg.Body ,
147148  }
149+ 
148150  //  Add a post to the store and get back the ID
149151  id  :=  k.AppendPost (ctx, post)
152+ 
150153  //  Return the ID of the post
151154  return  &types.MsgCreatePostResponse {Id: id}, nil 
152155}
@@ -162,7 +165,9 @@ Create the `proto/blog/post.proto` file and define the `Post` message:
162165
163166``` go 
164167syntax = " proto3" 
168+ 
165169package  blog.blog ;
170+ 
166171option go_package = " blog/x/blog/types" 
167172
168173message Post  {
@@ -175,8 +180,8 @@ message Post {
175180
176181The contents of the ` post.proto `  file are standard. The file defines:
177182
178- -  A package name ` username. blog.blog`  that is used to identify messages
179- -  The Go package ` go_package = "github.com/username/ blog/x/blog/types" `  where new files are generated 
183+ -  A package name ` blog.blog `  that is used to identify messages
184+ -  The Go package ` go_package = "blog/x/blog/types" `  where new files are generated 
180185-  The message ` message Post ` 
181186
182187Continue developing your blog chain.
@@ -196,7 +201,8 @@ Then, add these prefixes to the `x/blog/types/keys.go` file in the `const` and a
196201``` go 
197202const  (
198203  // ...
199- 	//  Keep track of the index of posts  
204+ 
205+   //  Keep track of the index of posts  
200206  PostKey       = " Post-value-" 
201207  PostCountKey  = " Post-count-" 
202208)
@@ -228,14 +234,18 @@ First, implement `GetPostCount`:
228234func  (k  Keeper ) GetPostCount  (ctx  sdk .Context ) uint64  {
229235  //  Get the store using storeKey (which is "blog") and PostCountKey (which is "Post-count-")
230236  store  :=  prefix.NewStore (ctx.KVStore (k.storeKey ), []byte (types.PostCountKey ))
237+ 
231238  //  Convert the PostCountKey to bytes
232239  byteKey  :=  []byte (types.PostCountKey )
240+ 
233241  //  Get the value of the count
234242  bz  :=  store.Get (byteKey)
243+ 
235244  //  Return zero if the count value is not found (for example, it's the first post)
236245  if  bz == nil  {
237246    return  0 
238247  }
248+ 
239249  //  Convert the count into a uint64
240250  return  binary.BigEndian .Uint64 (bz)
241251}
@@ -247,11 +257,14 @@ Now that `GetPostCount` returns the correct number of posts in the store, implem
247257func  (k  Keeper ) SetPostCount  (ctx  sdk .Context , count  uint64 ) {
248258  //  Get the store using storeKey (which is "blog") and PostCountKey (which is "Post-count-")
249259  store  :=  prefix.NewStore (ctx.KVStore (k.storeKey ), []byte (types.PostCountKey ))
260+ 
250261  //  Convert the PostCountKey to bytes
251262  byteKey  :=  []byte (types.PostCountKey )
263+ 
252264  //  Convert count from uint64 to string and get bytes
253265  bz  :=  make ([]byte , 8 )
254266  binary.BigEndian .PutUint64 (bz, count)
267+ 
255268  //  Set the value of Post-count- to count
256269  store.Set (byteKey, bz)
257270}
@@ -264,25 +277,33 @@ package keeper
264277
265278import  (
266279  " encoding/binary" 
267-    " blog/x/blog/types " 
280+ 
268281  " github.com/cosmos/cosmos-sdk/store/prefix" 
269282  sdk " github.com/cosmos/cosmos-sdk/types" 
283+ 
284+   " blog/x/blog/types" 
270285)
271286
272287func  (k  Keeper ) AppendPost  (ctx  sdk .Context , post  types .Post ) uint64  {
273288  //  Get the current number of posts in the store
274289  count  :=  k.GetPostCount (ctx)
290+ 
275291  //  Assign an ID to the post based on the number of posts in the store
276292  post.Id  = count
293+ 
277294  //  Get the store
278295  store  :=  prefix.NewStore (ctx.KVStore (k.storeKey ), []byte (types.PostKey ))
296+ 
279297  //  Convert the post ID into bytes
280298  byteKey  :=  make ([]byte , 8 )
281299  binary.BigEndian .PutUint64 (byteKey, post.Id )
300+ 
282301  //  Marshal the post into bytes
283302  appendedValue  :=  k.cdc .MustMarshal (&post)
303+ 
284304  //  Insert the post bytes using post ID as a key
285305  store.Set (byteKey, appendedValue)
306+ 
286307  //  Update the post count
287308  k.SetPostCount (ctx, count+1 )
288309  return  count
@@ -315,73 +336,83 @@ To define the types in proto files, make the following updates in `proto/blog/qu
315336
3163371 .  Add the ` import ` :
317338
318-      ``` go 
319-      import  " blog/post.proto" 
320-      ```
339+ ``` go 
340+ import  " blog/post.proto" 
341+ ``` 
321342
3223432 .  Add pagination to the post request:
323344
324-      ` ` ` go
325-      message QueryPostsRequest {
326-        // Adding pagination to request 
327-        cosmos.base.query.v1beta1.PageRequest pagination = 1; 
328-      }
329-      ` ` ` 
345+ ``` go 
346+ message QueryPostsRequest  {
347+   //  Adding pagination to request
348+   cosmos.base .query .v1beta1 .PageRequest  pagination = 1 ;
349+ }
350+ ``` 
330351
3313523 .  Add pagination to the post response:
332353
333-     ` ` ` go
334-     message QueryPostsResponse { 
335-       // Returning a list of posts 
336-       repeated Post Post = 1; 
337-       // Adding pagination to response 
338-       cosmos.base.query.v1beta1.PageResponse pagination = 2; 
339-     } 
340-     ` ` `  
354+ ``` go 
355+ message QueryPostsResponse  {
356+   //  Returning a list of posts
357+   repeated Post  Post  = 1 ;
358+ 
359+   //  Adding pagination to response
360+   cosmos.base .query .v1beta1 .PageResponse  pagination = 2 ;
361+ }
362+ ``` 
341363
342- To implement post querying logic in the ` grpc_query_posts.go` 
364+ To implement post querying logic in the ` x/blog/keeper/ grpc_query_posts.go`  file, delete the contents of that file and replace it with:
343365
344366``` go 
345367package  keeper
346368
347369import  (
348370  " context" 
349- 
350-   "blog/x/blog/types" 
351371
352372  " github.com/cosmos/cosmos-sdk/store/prefix" 
353373  sdk " github.com/cosmos/cosmos-sdk/types" 
354374  " github.com/cosmos/cosmos-sdk/types/query" 
355375  " google.golang.org/grpc/codes" 
356376  " google.golang.org/grpc/status" 
377+ 
378+   " blog/x/blog/types" 
357379)
358380
359381func  (k  Keeper ) Posts  (c  context .Context , req  *types .QueryPostsRequest ) (*types .QueryPostsResponse , error ) {
360382  //  Throw an error if request is nil
361383  if  req == nil  {
362384    return  nil , status.Error (codes.InvalidArgument , " invalid request" 
363385  }
386+ 
364387  //  Define a variable that will store a list of posts
365388  var  posts  []*types.Post 
389+ 
366390  //  Get context with the information about the environment
367391  ctx  :=  sdk.UnwrapSDKContext (c)
392+ 
368393  //  Get the key-value module store using the store key (in our case store key is "chain")
369394  store  :=  ctx.KVStore (k.storeKey )
395+ 
370396  //  Get the part of the store that keeps posts (using post key, which is "Post-value-")
371397  postStore  :=  prefix.NewStore (store, []byte (types.PostKey ))
398+ 
372399  //  Paginate the posts store based on PageRequest
373400  pageRes , err  :=  query.Paginate (postStore, req.Pagination , func (key []byte , value []byte ) error  {
374401    var  post  types.Post 
375402    if  err  :=  k.cdc .Unmarshal (value, &post); err != nil  {
376403      return  err
377404    }
405+ 
378406    posts = append (posts, &post)
407+ 
379408    return  nil 
380409  })
410+ 
381411  //  Throw an error if pagination failed
382412  if  err != nil  {
383413    return  nil , status.Error (codes.Internal , err.Error ())
384414  }
415+ 
385416  //  Return a struct containing a list of posts and pagination info
386417  return  &types.QueryPostsResponse {Post: posts, Pagination: pageRes}, nil 
387418}
@@ -393,28 +424,41 @@ In the `x/blog/module.go` file:
393424
3944251 .  Add ` "context" `  to the imports, don't save the file yet.
395426
396-     ```go 
397-     import ( 
398- 	    "context" 
427+ ``` go 
428+ import  (
429+ 	" context" 
430+ 
399431	//  ... other imports
400-      )
401-      ```
432+ )
433+ ``` 
402434
4034352 .  Update the ` RegisterGRPCGatewayRoutes `  function to register the query handler client:
404436
405-      ```go
406-      // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
407-      func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
408- 	     types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) 
409-      }
410-      ```
437+ ``` go 
438+ //  RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
439+ func  (AppModuleBasic ) RegisterGRPCGatewayRoutes  (clientCtx  client .Context , mux  *runtime .ServeMux ) {
440+ 	types.RegisterQueryHandlerClient (context.Background (), mux, types.NewQueryClient (clientCtx))
441+ }
442+ ``` 
411443
4124443 .  Now that you've modified the file with the two updates, now it's safe to save the file. 
413445
414446## Use the CLI to create a post  
415447
416448Now that you have implemented logic for creating and querying posts, you can interact with your blog chain using the command line. The blog chain binary is ` blogd ` .
417449
450+ First, start the chain on your development machine by running the following command in the ` blog `  directory:
451+ 
452+ ``` bash 
453+ ignite chain serve
454+ ``` 
455+ 
456+ The binary is built by the ` ignite chain serve `  command bit it can also be built by running:
457+ 
458+ ``` bash 
459+ ignite chain build
460+ ``` 
461+ 
418462To create a post at the command line:
419463
420464``` bash 
0 commit comments