mukan-ignite/docs/versioned_docs/version-v28/02-guide/03-hello-world.md
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

2.8 KiB

description title
Build your first blockchain and your first Cosmos SDK query. 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 passed 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:
ignite scaffold chain hello
  1. Navigate to the Blockchain Directory:
cd hello

Adding a Custom Query

  • Scaffold the Query:
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

In the Cosmos SDK, queries are requests for information from the blockchain, used to access data like the ledger's current state or transaction details. While the SDK offers several built-in query methods, developers can also craft custom queries for specific data retrieval or complex operations.

  • Modify query_say_hello.go:

Update the SayHello function in x/hello/keeper/query_say_hello.go to return a personalized greeting query.

package keeper

import (
	"context"
	"fmt"

	"hello/x/hello/types"

	sdk "github.com/cosmos/cosmos-sdk/types"
	"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")
	}

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

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

Running the Blockchain

  1. Start the Blockchain:
ignite chain serve
  1. 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.