Skip to content

Commit

Permalink
Modify Hello world tutorial to be shorter and easier
Browse files Browse the repository at this point in the history
  • Loading branch information
toschdev committed Nov 22, 2023
1 parent a5863e0 commit d3593ce
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 378 deletions.
141 changes: 0 additions & 141 deletions docs/docs/02-guide/03-hello/00-express.md

This file was deleted.

85 changes: 85 additions & 0 deletions docs/docs/02-guide/03-hello/00-hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
description: Build your first blockchain and your first Cosmos SDK module.
title: Hello World
---

# "Hello world!" Blockchain Tutorial with Ignite CLI

**Introduction**

In this tutorial, you'll build a simple blockchain using Ignite CLI that responds to a custom query with "Hello %s!", where %s is a name provided in the query.
This will enhance your understanding of creating custom queries in a Cosmos SDK blockchain.

## Setup and Scaffold

1. **Create a New Blockchain:**

```bash
ignite scaffold chain hello
```

2. **Navigate to the Blockchain Directory:**

```bash
cd hello
```

## Adding a Custom Query

- **Scaffold the Query:**

```bash
ignite scaffold query say-hello name --response name
```

This command generates code for a new query, `say-hello`, which accepts a name, an input and returns it in the response.

- **Understanding the Scaffolded Code:**

- `proto/hello/hello/query.proto`: Defines the request and response structure.
- `x/hello/client/cli/query_say_hello.go`: Contains the CLI commands for the query.
- `x/hello/keeper/query_say_hello.go`: Houses the logic for the query response.


## Customizing the Query Response

- **Modify `query_say_hello.go`:
Update the `SayHello` function in `x/hello/keeper/query_say_hello.go` to return a personalized greeting.

```go title="x/hello/keeper/query_say_hello.go"
func (k Keeper) SayHello(goCtx context.Context, req *types.QuerySayHelloRequest) (*types.QuerySayHelloResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

// Validation and Context unwrapping
ctx := sdk.UnwrapSDKContext(goCtx)

_ = ctx
// Custom Response
// highlight-next-line
return &types.QuerySayHelloResponse{Name: fmt.Sprintf("Hello %s!", req.Name)}, nil
}
```

## Running the Blockchain

1. **Start the Blockchain:**

```bash
ignite chain serve
```

2. **Test the Query:**

Use the command-line interface to submit a query.

```
hellod q hello say-hello world
```

Expect a response: `Hello world!`

## Conclusion

Congratulations! 🎉 You've successfully created a blockchain module with a custom query using Ignite CLI. Through this tutorial, you've learned how to scaffold a chain, add a custom query, and modify the logic for personalized responses. This experience illustrates the power of Ignite CLI in streamlining blockchain development and the importance of understanding the underlying code for customization.
Loading

0 comments on commit d3593ce

Please sign in to comment.