Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [#4702](https://github.com/ignite/cli/pull/4702) Improve app detection by checking for inheritance instead of interface implementation.
- [#4707](https://github.com/ignite/cli/pull/4707) Show `buf` version in `ignite version` only when in a go module.
- [#4709](https://github.com/ignite/cli/pull/4709) Remove legacy msgServer support
- [#4710](https://github.com/ignite/cli/pull/4710) Remove the `nullify` pkg from the chain `testutil`

### Fixes

Expand Down
22 changes: 11 additions & 11 deletions docs/docs/02-guide/03-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ Update the `SayHello` function in `x/hello/keeper/query_say_hello.go` to return
package keeper

import (
"context"
"fmt"
"context"
"fmt"

"hello/x/hello/types"
"hello/x/hello/types"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (q queryServer) SayHello(ctx context.Context, req *types.QuerySayHelloRequest) (*types.QuerySayHelloResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

// TODO: Process the query
// TODO: Process the query

// Custom Response
return &types.QuerySayHelloResponse{Name: fmt.Sprintf("Hello %s!", req.Name)}, nil
// Custom Response
return &types.QuerySayHelloResponse{Name: fmt.Sprintf("Hello %s!", req.Name)}, nil
}
```

Expand Down
176 changes: 88 additions & 88 deletions docs/docs/02-guide/08-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Ignite creates all the necessary boilerplate for collections in the `x/<module>/

```go
type Keeper struct {
// ...
// ...

Params collections.Item[Params]
Counters collections.Map[string, uint64]
Profiles collections.Map[sdk.AccAddress, Profile]
Params collections.Item[Params]
Counters collections.Map[string, uint64]
Profiles collections.Map[sdk.AccAddress, Profile]
}
```

Expand All @@ -42,14 +42,14 @@ To read values from state, use the `Get` method:
// getting a single item
params, err := k.Params.Get(ctx)
if err != nil {
// handle error
// collections.ErrNotFound is returned when an item doesn't exist
// handle error
// collections.ErrNotFound is returned when an item doesn't exist
}

// getting a map entry
counter, err := k.Counters.Get(ctx, "my-counter")
if err != nil {
// handle error
// handle error
}
```

Expand All @@ -61,13 +61,13 @@ To write values to state, use the `Set` method:
// setting a single item
err := k.Params.Set(ctx, params)
if err != nil {
// handle error
// handle error
}

// setting a map entry
err = k.Counters.Set(ctx, "my-counter", 42)
if err != nil {
// handle error
// handle error
}
```

Expand All @@ -78,10 +78,10 @@ Use the `Has` method to check if a value exists without retrieving it:
```go
exists, err := k.Counters.Has(ctx, "my-counter")
if err != nil {
// handle error
// handle error
}
if exists {
// value exists
// value exists
}
```

Expand All @@ -92,7 +92,7 @@ To remove values from state, use the `Remove` method:
```go
err := k.Counters.Remove(ctx, "my-counter")
if err != nil {
// handle error
// handle error
}
```

Expand All @@ -102,53 +102,53 @@ Messages in Cosmos SDK modules modify state based on user transactions. Here's h

```go
func (k msgServer) CreateProfile(ctx context.Context, msg *types.MsgCreateProfile) (*types.MsgCreateProfileResponse, error) {
// validate message
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
// parse sender address
senderBz, err := k.addressCodec.StringToBytes(msg.Creator)
if err != nil {
return nil, err
}
sender := sdk.AccAddress(senderBz)
// check if profile already exists
exists, err := k.Profiles.Has(ctx, sender)
if err != nil {
return nil, err
}
if exists {
return nil, sdkerrors.Wrap(types.ErrProfileExists, "profile already exists")
}
// create new profile
sdkCtx := sdk.UnwrapSDKContext(ctx)
profile := types.Profile{
Name: msg.Name,
Bio: msg.Bio,
CreatedAt: sdkCtx.BlockTime().Unix(),
}
// store the profile
err = k.Profiles.Set(ctx, sender, profile)
if err != nil {
return nil, err
}
// increment profile counter
counter, err := k.Counters.Get(ctx, "profiles")
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return nil, err
}
// set the counter (adding 1)
err = k.Counters.Set(ctx, "profiles", counter+1)
if err != nil {
return nil, err
}
return &types.MsgCreateProfileResponse{}, nil
// validate message
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

// parse sender address
senderBz, err := k.addressCodec.StringToBytes(msg.Creator)
if err != nil {
return nil, err
}
sender := sdk.AccAddress(senderBz)

// check if profile already exists
exists, err := k.Profiles.Has(ctx, sender)
if err != nil {
return nil, err
}
if exists {
return nil, sdkerrors.Wrap(types.ErrProfileExists, "profile already exists")
}

// create new profile
sdkCtx := sdk.UnwrapSDKContext(ctx)
profile := types.Profile{
Name: msg.Name,
Bio: msg.Bio,
CreatedAt: sdkCtx.BlockTime().Unix(),
}

// store the profile
err = k.Profiles.Set(ctx, sender, profile)
if err != nil {
return nil, err
}

// increment profile counter
counter, err := k.Counters.Get(ctx, "profiles")
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return nil, err
}
// set the counter (adding 1)
err = k.Counters.Set(ctx, "profiles", counter+1)
if err != nil {
return nil, err
}

return &types.MsgCreateProfileResponse{}, nil
}
```

Expand All @@ -158,27 +158,27 @@ Queries allow users to read state without modifying it. Here's how to implement

```go
func (q queryServer) GetProfile(ctx context.Context, req *types.QueryGetProfileRequest) (*types.QueryGetProfileResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
// parse address
addressBz, err := k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid address")
}
address := sdk.AccAddress(addressBz)
// get profile
profile, err := q.k.Profiles.Get(ctx, address)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "profile not found")
}
return nil, status.Error(codes.Internal, "internal error")
}
return &types.QueryGetProfileResponse{Profile: profile}, nil
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

// parse address
addressBz, err := k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid address")
}
address := sdk.AccAddress(addressBz)

// get profile
profile, err := q.k.Profiles.Get(ctx, address)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "profile not found")
}
return nil, status.Error(codes.Internal, "internal error")
}

return &types.QueryGetProfileResponse{Profile: profile}, nil
}
```

Expand All @@ -190,7 +190,7 @@ When working with collections, proper error handling is essential:
// example from a query function
params, err := q.k.Params.Get(ctx)
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.Internal, "internal error")
return nil, status.Error(codes.Internal, "internal error")
}
```

Expand All @@ -203,23 +203,23 @@ Collections also support iteration:
```go
// iterate over all profiles
err := k.Profiles.Walk(ctx, nil, func(key sdk.AccAddress, value types.Profile) (bool, error) {
// process each profile
// return true to stop iteration, false to continue
return false, nil
// process each profile
// return true to stop iteration, false to continue
return false, nil
})
if err != nil {
// handle error
// handle error
}

// iterate over a range of counters
startKey := "a"
endKey := "z"
err = k.Counters.Walk(ctx, collections.NewPrefixedPairRange[string, uint64](startKey, endKey), func(key string, value uint64) (bool, error) {
// process each counter in the range
return false, nil
// process each counter in the range
return false, nil
})
if err != nil {
// handle error
// handle error
}
```

Expand Down
Loading