Skip to content

yolkhovyy/hello-cosmos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cosmos SDK - Hello World

This is a Hello World application built using Cosmos SDK.

Install Ignite CLI

curl https://get.ignite.com/cli! | bash

Create a New Blockchain Project

  • Set up a new blockchain application, including basic files and configurations, such as modules, state management, and routes for handling messages and transactions.
  • Generate a custom message type sayHello, within the blockchain application. The --response message flag specifies that this message will have a response of type message, which could contain data (e.g. a reply or confirmation to the sayHello message).
ignite scaffold chain helloworld
cd helloworld
ignite scaffold message sayHello --response message

Add a Custom Message

  • The following code defines the SayHello method in the msgServer for handling the MsgSayHello message.

x/helloworld/msg_server_say_hello.go

func (k msgServer) SayHello(goCtx context.Context, msg *types.MsgSayHello) (*types.MsgSayHelloResponse, error) {
	responseMessage := fmt.Sprintf("Hello, %s!", msg.Creator)

	ctx := sdk.UnwrapSDKContext(goCtx)
	ctx.EventManager().EmitEvent(
		sdk.NewEvent(
			"helloworld",
			sdk.NewAttribute("creator", msg.Creator),
			sdk.NewAttribute("message", responseMessage),
		),
	)

	return &types.MsgSayHelloResponse{Message: responseMessage}, nil
}

Build and Start

  • Generate Go code from Protocol Buffers (proto) files
  • Build the blockchain application
  • Start the blockchain node locally
ignite generate proto-go
ignite chain build
ignite chain serve

Output:

  Blockchain is running
  
  πŸ‘€ alice's account address: cosmos13n02efqrf7heleudt37tmnc9jwv7fdue40wwqw
  πŸ‘€ bob's account address: cosmos1xwfnh3xr7pusns0gexkh6ezjvsfquf4ghne3ql
  πŸ‘€ foo's account address: cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu

Add Key

Add a new key to the blockchain application:

  • helloworldd - the binary for blockchain application, used to interact with the blockchain, such as adding keys or querying data
  • keys add - subcommand is used to create a new keypair (public and private keys) in the blockchain's keyring
  • foo - the name of the key to be added. This is the alias you can use to reference the key later
  • --keyring-backend test - specifies the keyring backend to use. The test option tells the application to use an in-memory keyring, which is useful for local testing without persisting keys to disk
$ helloworldd keys add foo --keyring-backend test

Output:

- address: cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu
  name: foo
  pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A4KdTHpSjS0y6OBLtk1Q+BLDIUGfEGtQuLKbqwW8GGl0"}'
  type: local

**Important** write this mnemonic phrase in a safe place. It is the only way to recover your account if you ever forget your password.

layer enter surge fly happy eyebrow include rifle achieve youth winter chair simple clip mango pause problem wheat cinnamon gain purity extend cement leaf

Query Bank Balances

$ helloworldd query bank balances cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu
balances: []
pagination: {}

Credit Request

curl -X POST http://localhost:4500/credit -d '{"address": "cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu", "amount": "1000000token"}'

Query Bank Balances (after credit)

$ helloworldd query bank balances cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu
balances:
- amount: "100000"
  denom: stake
- amount: "5"
  denom: token
pagination:
  total: "2"

Send Transaction

$ helloworldd tx helloworld say-hello --from foo --keyring-backend test --chain-id helloworld
auth_info:
  fee:
    amount: []
    gas_limit: "200000"
    granter: ""
    payer: ""
  signer_infos: []
  tip: null
body:
  extension_options: []
  memo: ""
  messages:
  - '@type': /helloworld.helloworld.MsgSayHello
    creator: cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu
  non_critical_extension_options: []
  timeout_height: "0"
signatures: []
confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: ""
events: []
gas_used: "0"
gas_wanted: "0"
height: "0"
info: ""
logs: []
raw_log: ""
timestamp: ""
tx: null
txhash: B4F87AB7370DA69221633E4471B880AB5F4B639E03CD05C1C4A34507D7952A94

Check Transaction

curl "http://localhost:26657/tx_search?query=\"tx.hash='B4F87AB7370DA69221633E4471B880AB5F4B639E03CD05C1C4A34507D7952A94'\""

Example output:

{
    "jsonrpc": "2.0",
    "id": -1,
    "result": {
        "txs": [
            {
                "hash": "B4F87AB7370DA69221633E4471B880AB5F4B639E03CD05C1C4A34507D7952A94",
                "height": "3972",
                "index": 0,
                "tx_result": {
                    "code": 0,
                    "data": "EmUKKi9oZWxsb3dvcmxkLmhlbGxvd29ybGQuTXNnU2F5SGVsbG9SZXNwb25zZRI3CjVIZWxsbywgY29zbW9zMWFteW0zNndqc2NlZG1mbXRtNnU5OGhqd3p6bGZzMnc3OG1qM3d1IQ==",
                    "log": "",
                    "info": "",
                    "gas_wanted": "200000",
                    "gas_used": "42846",
                    "events": [
                        {
                            "type": "tx",
                            "attributes": [
                                {
                                    "key": "fee",
                                    "value": "",
                                    "index": true
                                },
                                {
                                    "key": "fee_payer",
                                    "value": "cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu",
                                    "index": true
                                }
                            ]
                        },
                        {
                            "type": "tx",
                            "attributes": [
                                {
                                    "key": "acc_seq",
                                    "value": "cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu/0",
                                    "index": true
                                }
                            ]
                        },
                        {
                            "type": "tx",
                            "attributes": [
                                {
                                    "key": "signature",
                                    "value": "H7ZlaDTE8Me1Xt2oHt1u1/a5zD1GEhvpM1X5ra554xVhHFOOp5E0nmUAC384Pt+Fjy/hzPnBHz6qGpP8I5NACQ==",
                                    "index": true
                                }
                            ]
                        },
                        {
                            "type": "message",
                            "attributes": [
                                {
                                    "key": "action",
                                    "value": "/helloworld.helloworld.MsgSayHello",
                                    "index": true
                                },
                                {
                                    "key": "sender",
                                    "value": "cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu",
                                    "index": true
                                },
                                {
                                    "key": "module",
                                    "value": "helloworld",
                                    "index": true
                                },
                                {
                                    "key": "msg_index",
                                    "value": "0",
                                    "index": true
                                }
                            ]
                        },
                        {
                            "type": "helloworld",
                            "attributes": [
                                {
                                    "key": "creator",
                                    "value": "cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu",
                                    "index": true
                                },
                                {
                                    "key": "message",
                                    "value": "Hello, cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu!",
                                    "index": true
                                },
                                {
                                    "key": "msg_index",
                                    "value": "0",
                                    "index": true
                                }
                            ]
                        }
                    ],
                    "codespace": ""
                },
                "tx": "ClcKVQoiL2hlbGxvd29ybGQuaGVsbG93b3JsZC5Nc2dTYXlIZWxsbxIvCi1jb3Ntb3MxYW15bTM2d2pzY2VkbWZtdG02dTk4aGp3enpsZnMydzc4bWozd3USVgpOCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA4KdTHpSjS0y6OBLtk1Q+BLDIUGfEGtQuLKbqwW8GGl0EgQKAggBEgQQwJoMGkAftmVoNMTwx7Ve3age3W7X9rnMPUYSG+kzVfmtrnnjFWEcU46nkTSeZQALfzg+34WPL+HM+cEfPqoak/wjk0AJ"
            }
        ],
        "total_count": "1"
    }
}

Query Bank Balances (after transaction)

$ helloworldd query bank balances cosmos1amym36wjscedmfmtm6u98hjwzzlfs2w78mj3wu
balances:
- amount: "100000"
  denom: stake
- amount: "5"
  denom: token
pagination:
  total: "2"

About

Hello Cosmos SDK

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published