Skip to content

Commit

Permalink
docs: update tutorials to work with SDK v0.50 (#3705)
Browse files Browse the repository at this point in the history
* chore: add `--chain-id` flag to blog commands

* chore: use `cosmossdk.io/store`

* chore: change blog tutorial to use the store service

* chore: use `cosmossdk.io/errors`

* chore: change "PostID" to "PostId"

* chore: add missing import to code snippet

* fix: correct expected keepers interface args

* chore: add `--chain-id` flag to loan commands

* ci: fix broken links

* ci: fix broken references

* ci: fix broken reference

* chore: fix typo

---------

Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
jeronimoalbi and Pantani authored Nov 11, 2023
1 parent 336121c commit c7e38c6
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/md-link-checker-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
{
"pattern": "^index.md"
},
{
"pattern": "^https://docs.starport.network"
}
],
"replacementPatterns": [
Expand Down
10 changes: 5 additions & 5 deletions docs/docs/02-guide/04-blog/00-express.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ While `ignite chain serve` is running in one terminal window, open another
terminal and use the chain's binary to create a new blog post on the blockchain:

```
blogd tx blog create-post 'Hello, World!' 'This is a blog post' --from alice
blogd tx blog create-post 'Hello, World!' 'This is a blog post' --from alice --chain-id blog
```

When using the `--from` flag to specify the account that will be used to sign a
Expand Down Expand Up @@ -220,7 +220,7 @@ transaction will be broadcasted to the blockchain and the blog post will be
updated with the new body content.

```
blogd tx blog update-post 0 'Hello, World!' 'This is a blog post from Alice' --from alice
blogd tx blog update-post 0 'Hello, World!' 'This is a blog post from Alice' --from alice --chain-id blog
```

Now that we have updated the blog post with new content, let's query the
Expand Down Expand Up @@ -263,7 +263,7 @@ example of how the blockchain can enforce rules and permissions, and it shows
that only authorized users are able to make changes to the blockchain.

```
blogd tx blog delete-post 0 --from bob
blogd tx blog delete-post 0 --from bob --chain-id blog
raw_log: 'failed to execute message; message index: 0: incorrect owner: unauthorized'
```
Expand All @@ -273,7 +273,7 @@ account. Since Alice is the author of the blog post, she should be authorized to
delete it.

```
blogd tx blog delete-post 0 --from alice
blogd tx blog delete-post 0 --from alice --chain-id blog
```

To check whether the blog post has been successfully deleted by Alice, we can
Expand Down Expand Up @@ -306,4 +306,4 @@ some of the code ourselves, we can gain a deeper understanding of how Ignite
works and how it can be used to create applications on a blockchain. This will
help us learn more about the capabilities of Ignite CLI and how it can be used
to build robust and powerful applications. Keep an eye out for these tutorials
and get ready to dive deeper into the world of blockchains with Ignite!
and get ready to dive deeper into the world of blockchains with Ignite!
14 changes: 9 additions & 5 deletions docs/docs/02-guide/04-blog/03-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ import (

"blog/x/blog/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) AppendPost(ctx sdk.Context, post types.Post) uint64 {
count := k.GetPostCount(ctx)
post.Id = count
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))
appendedValue := k.cdc.MustMarshal(&post)
store.Set(GetPostIDBytes(post.Id), appendedValue)
k.SetPostCount(ctx, count+1)
Expand Down Expand Up @@ -126,7 +128,8 @@ In the file `post.go`, let's define the `GetPostCount` function as follows:

```go title="x/blog/keeper/post.go"
func (k Keeper) GetPostCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte{})
byteKey := types.KeyPrefix(types.PostCountKey)
bz := store.Get(byteKey)
if bz == nil {
Expand Down Expand Up @@ -209,7 +212,8 @@ in the database.

```go title="x/blog/keeper/post.go"
func (k Keeper) SetPostCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte{})
byteKey := types.KeyPrefix(types.PostCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
Expand Down Expand Up @@ -316,4 +320,4 @@ then returns a `MsgCreatePostResponse` object containing the ID of the newly
created post.

By implementing these methods, you have successfully implemented the necessary
logic for handling "create post" messages and adding posts to the blockchain.
logic for handling "create post" messages and adding posts to the blockchain.
13 changes: 8 additions & 5 deletions docs/docs/02-guide/04-blog/04-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Implement the `GetPost` keeper method in `post.go`:

```go title="x/blog/keeper/post.go"
func (k Keeper) GetPost(ctx sdk.Context, id uint64) (val types.Post, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))
b := store.Get(GetPostIDBytes(id))
if b == nil {
return val, false
Expand Down Expand Up @@ -48,7 +49,8 @@ Implement the `SetPost` keeper method in `post.go`:

```go title="x/blog/keeper/post.go"
func (k Keeper) SetPost(ctx sdk.Context, post types.Post) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))
b := k.cdc.MustMarshal(&post)
store.Set(GetPostIDBytes(post.Id), b)
}
Expand Down Expand Up @@ -78,6 +80,7 @@ import (

"blog/x/blog/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -92,10 +95,10 @@ func (k msgServer) UpdatePost(goCtx context.Context, msg *types.MsgUpdatePost) (
}
val, found := k.GetPost(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
if msg.Creator != val.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
k.SetPost(ctx, post)
return &types.MsgUpdatePostResponse{}, nil
Expand Down Expand Up @@ -124,4 +127,4 @@ can be useful for correcting mistakes or updating the content of a post as new
information becomes available.

Finally, you implemented the `UpdatePost` method, which is called whenever the
blockchain processes a message requesting an update to a post.
blockchain processes a message requesting an update to a post.
12 changes: 7 additions & 5 deletions docs/docs/02-guide/04-blog/05-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ message.

```go title="x/blog/keeper/post.go"
func (k Keeper) RemovePost(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PostKey))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))
store.Delete(GetPostIDBytes(id))
}
```
Expand All @@ -32,6 +33,7 @@ import (

"blog/x/blog/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -40,10 +42,10 @@ func (k msgServer) DeletePost(goCtx context.Context, msg *types.MsgDeletePost) (
ctx := sdk.UnwrapSDKContext(goCtx)
val, found := k.GetPost(ctx, msg.Id)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id))
}
if msg.Creator != val.Creator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
}
k.RemovePost(ctx, msg.Id)
return &types.MsgDeletePostResponse{}, nil
Expand All @@ -57,7 +59,7 @@ a pointer to a message of type `*types.MsgDeletePostResponse` and an `error`.
Inside the function, the context is unwrapped using the `sdk.UnwrapSDKContext`
function and the value of the post with the ID specified in the message is
retrieved using the `GetPost` function. If the post is not found, an error is
returned using the `sdkerrors.Wrap` function. If the creator of the message does
returned using the `errorsmod.Wrap` function. If the creator of the message does
not match the creator of the post, another error is returned. If both of these
checks pass, the `RemovePost` function is called with the context and the ID of
the post to delete the post. Finally, the function returns a response message
Expand All @@ -71,4 +73,4 @@ requester is the creator of the post before deleting it.
Congratulations on completing the implementation of the `RemovePost` and
`DeletePost` methods in the keeper package! These methods provide functionality
for removing a post from a store and handling a request to delete a post,
respectively.
respectively.
31 changes: 14 additions & 17 deletions docs/docs/02-guide/04-blog/07-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ import (

"blog/x/blog/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) ListPost(goCtx context.Context, req *types.QueryListPostRequest) (*types.QueryListPostResponse, error) {
func (k Keeper) ListPost(ctx context.Context, req *types.QueryListPostRequest) (*types.QueryListPostResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var posts []types.Post
ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
postStore := prefix.NewStore(store, types.KeyPrefix(types.PostKey))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))

pageRes, err := query.Paginate(postStore, req.Pagination, func(key []byte, value []byte) error {
var posts []types.Post
pageRes, err := query.Paginate(store, req.Pagination, func(key []byte, value []byte) error {
var post types.Post
if err := k.cdc.Unmarshal(value, &post); err != nil {
return err
Expand All @@ -63,14 +62,12 @@ func (k Keeper) ListPost(goCtx context.Context, req *types.QueryListPostRequest)
`QueryListPostResponse` and an error.

The function first checks if the request object is `nil` and returns an error
with a `InvalidArgument` code if it is. It then initializes an empty slice of
`Post` objects and unwraps the context object.

It retrieves a key-value store from the context using the `storeKey` field of
the keeper struct and creates a new store using a prefix of the `PostKey`. It
then calls the `Paginate` function from the `query` package on the store and the
pagination information in the request object. The function passed as an argument
to Paginate iterates over the key-value pairs in the store and unmarshals the
with a `InvalidArgument` code if it is.

It creates a new store using a prefix of the `PostKey` and then calls the
`Paginate` function from the `query` package on the store and the pagination
information in the request object. The function passed as an argument to
Paginate iterates over the key-value pairs in the store and unmarshals the
values into `Post` objects, which are then appended to the `posts` slice.

If an error occurs during pagination, the function returns an `Internal error`
Expand All @@ -94,4 +91,4 @@ Run the command to generate Go files from proto:

```
ignite generate proto-go
```
```
12 changes: 6 additions & 6 deletions docs/docs/02-guide/04-blog/08-play.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Create a blog post by Alice

```
blogd tx blog create-post hello world --from alice
blogd tx blog create-post hello world --from alice --chain-id blog
```

## Show a blog post
Expand All @@ -23,7 +23,7 @@ post:
## Create a blog post by Bob
```
blogd tx blog create-post foo bar --from bob
blogd tx blog create-post foo bar --from bob --chain-id blog
```

## List all blog posts with pagination
Expand All @@ -50,7 +50,7 @@ post:
## Update a blog post
```
blogd tx blog update-post hello cosmos 0 --from alice
blogd tx blog update-post hello cosmos 0 --from alice --chain-id blog
```

```
Expand All @@ -68,7 +68,7 @@ post:
## Delete a blog post
```
blogd tx blog delete-post 0 --from alice
blogd tx blog delete-post 0 --from alice --chain-id blog
```

```
Expand All @@ -89,9 +89,9 @@ post:
## Delete a blog post unsuccessfully
```
blogd tx blog delete-post 1 --from alice
blogd tx blog delete-post 1 --from alice --chain-id blog
```

```yml
raw_log: 'failed to execute message; message index: 0: incorrect owner: unauthorized'
```
```
8 changes: 4 additions & 4 deletions docs/docs/02-guide/05-loan/02-bank.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
// highlight-start
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoins(ctx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
// highlight-end
}
```
```
17 changes: 9 additions & 8 deletions docs/docs/02-guide/05-loan/03-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,41 @@ import (
// highlight-next-line
"strconv"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func (msg *MsgRequestLoan) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
// highlight-start
amount, _ := sdk.ParseCoinsNormalized(msg.Amount)
if !amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "amount is not a valid Coins object")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "amount is not a valid Coins object")
}
if amount.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "amount is empty")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "amount is empty")
}
fee, _ := sdk.ParseCoinsNormalized(msg.Fee)
if !fee.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee is not a valid Coins object")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "fee is not a valid Coins object")
}
deadline, err := strconv.ParseInt(msg.Deadline, 10, 64)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deadline is not an integer")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "deadline is not an integer")
}
if deadline <= 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deadline should be a positive integer")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "deadline should be a positive integer")
}
collateral, _ := sdk.ParseCoinsNormalized(msg.Collateral)
if !collateral.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "collateral is not a valid Coins object")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "collateral is not a valid Coins object")
}
if collateral.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "collateral is empty")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "collateral is empty")
}
// highlight-end
return nil
Expand Down
Loading

0 comments on commit c7e38c6

Please sign in to comment.