2021-11-22 23:13:54 +00:00
# Building a Module
2021-11-10 19:44:02 +00:00
2021-11-22 23:13:54 +00:00
In this tutorial we will be going over building a module in Kava to show how easy it is to build on top of the Kava ecosystem. This module will be simple in nature but will show how to set up and connect a module to Kava and can be used as a starting point for more complex modules.
2021-11-23 17:51:24 +00:00
2021-11-22 23:13:54 +00:00
## Set up
```
2021-12-06 23:19:27 +00:00
this tutorial is based on the v44 cosmos version which Kava is currently migrating to, to follow this tutorial clone the kava repo but ensure you
clone the upgrade-v44 branch as the master branch is currently on v39 & ensure you have kvtool, docker & go installed on your machine.
git clone -b upgrade-v44 https://github.com/Kava-Labs/kava.git
2021-11-22 23:13:54 +00:00
```
2021-11-24 02:24:36 +00:00
## Defining Protocol Buffer Types
2021-11-22 23:13:54 +00:00
The first step in building a new Kava Module is to define our Module's types. To do that we use Protocol Buffers which is a used for serializing structured data and generating code for multiple target languages, Protocol Buffers are also smaller than JSON & XML so sending data around the network will be less expensive. [Learn More ](https://developers.google.com/protocol-buffers ).
Our Protobuf files will all live in ```proto/kava``` directory. we will create a new directory with the new module ```greet``` and add the following files in the ```proto/greet/v1beta1/``` directory
```
2021-11-24 02:24:36 +00:00
genesis.proto
greet.proto
query.proto
tx.proto
2021-11-22 23:13:54 +00:00
```
### Defining The Greet Type
Inside the ```proto/greet/v1beta1/greet.proto``` file lets define our greet type:
```
syntax = "proto3";
package kava.greet.v1beta1;
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
option go_package = "github.com/kava-labs/kava/x/greet/types";
message Greet {
string owner = 1;
string id = 2;
string message = 3;
}
```
2021-11-24 02:24:36 +00:00
Here we are saying that we have a Greet type that will have an owner, an id and a message that will contain the greet string. Once we have that defined we are ready to set up a way to create this greet message and query it.
2021-11-22 23:13:54 +00:00
### Creating a new Greeting
Inside the ```proto/greet/v1beta1/tx.proto``` file lets define our Msg Type:
```
syntax = "proto3";
package kava.greet.v1beta1;
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
option go_package = "github.com/kava-labs/kava/x/greet/types";
service Msg {
2021-12-06 23:19:27 +00:00
rpc CreateGreet(MsgCreateGreet) returns (MsgCreateGreetResponse);
2021-11-22 23:13:54 +00:00
}
message MsgCreateGreet {
string message = 1;
string owner = 2;
}
2021-12-06 23:19:27 +00:00
message MsgCreateGreetResponse {}
2021-11-22 23:13:54 +00:00
```
2021-11-24 02:24:36 +00:00
Now that we have defined how to create a new Greeting let's finish up by setting up our queries to view a specific greeting or all of them.
One thing to note here is that any state changing actions are transactions and for that reason we put them in our ```tx.proto``` files, we essentially said we are creating a new state changing message & defined the types for that message in our proto file, we will later add clients to trigger state change, which in our case will be adding a new message to our chain.
2021-11-22 23:13:54 +00:00
### Querying Greetings
2021-11-24 02:24:36 +00:00
Code inside the ```proto/greet/v1beta1/query.proto``` :
2021-11-22 23:13:54 +00:00
```
syntax = "proto3";
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
package kava.greet.v1beta1;
option go_package = "github.com/kava-labs/kava/x/greet/types";
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos_proto/cosmos.proto";
import "kava/greet/v1beta1/greet.proto";
service Query {
rpc Greet(QueryGetGreetRequest) returns (QueryGetGreetResponse) {
option (google.api.http).get = "/kava/greet/v1beta1/greetings/{id}";
}
rpc GreetAll(QueryAllGreetRequest) returns (QueryAllGreetResponse) {
option (google.api.http).get = "/kava/swap/v1beta1/greetings";
}
}
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
message QueryGetGreetRequest {
string id = 1;
}
message QueryGetGreetResponse {
Greet greeting = 1;
}
message QueryAllGreetRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}
message QueryAllGreetResponse {
repeated Greet greetings = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
```
2021-11-24 02:24:36 +00:00
Our ```query.proto``` now contains the types for our queries, we have defined a request type & a response type and those types will be returned once we trigger a query through the CLI, REST API, or Grpc. The response will follow the same structure regardless of the type of client initiating the request.
2021-11-22 23:13:54 +00:00
2021-11-24 02:24:36 +00:00
We defined our query, tx, and greet proto files we finally need to set up the genesis file and then we are ready to generate these types. In the genesis file we will create a minimal ```genesis.proto``` for this tutorial to keep things simple.
2021-11-22 23:13:54 +00:00
```
syntax = "proto3";
package kava.greet.v1beta1;
import "kava/greet/v1beta1/greet.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "cosmos_proto/cosmos.proto";
option go_package = "github.com/kava-labs/kava/x/greet/types";
// our gensis state message will be empty for this tutorial
message GenesisState {}
```
Once all the files are filled in we are ready to generate our proto types. in the Kava Directory run ```make proto-gen ``` to generate the types, this will create a folder inside the ```x/greet``` and will contain the auto-generated proto types.
2021-11-24 02:24:36 +00:00
## Developing Our Greet Module
2021-12-06 23:19:27 +00:00
we have successfully set up our Proto files & generated them, we now have a ```x/greet``` directory generated, this is where we will write our module's code. For starters we will define our module's types in a new file inside ```x/greet/types/greet.go```.
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
### Setting up constants & importing packages
Let's set up some basic constants for our module to help with routing, & fetching items from our store.
2021-11-22 23:13:54 +00:00
```
package types
import (
2021-12-06 23:19:27 +00:00
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
2021-11-22 23:13:54 +00:00
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
2021-12-06 23:19:27 +00:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/msgservice"
2021-11-22 23:13:54 +00:00
)
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// defined our module's constants such as name, routerkey
// and prefixes for getting items from the store
const (
ModuleName = "greet"
StoreKey = ModuleName
RouterKey = ModuleName
QuerierRoute = ModuleName
GreetKey = "greet-value-" // used for getting a greeting from our store
GreetCountKey = "greet-count-" // used for getting count from out store
QueryGetGreeting = "get-greeting" // used for legacy querier routing
QueryListGreetings = "list-greetings"// used for legacy querier routing
)
// heler function simply returns []byte out of a prefix string
func KeyPrefix(p string) []byte {
return []byte(p)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// returns default genesis state
func DefaultGenesisState() GenesisState {
return GenesisState{}
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// validates genesis state
func (gs GenesisState) Validate() error {
return nil
}
2021-11-22 23:13:54 +00:00
```
2021-11-24 02:24:36 +00:00
2021-12-06 23:19:27 +00:00
### Setting up our Msg for creating a new greeting
Our ```MsgCreateGreet``` struct was created when we generated our Proto Types, we now need to use that struct to implement the ```sdk.Msg``` interface such that we can create new greetings. the first thing we will do is defined an unnamed variable with the ```_``` syntax and have it implement the ```sdk.Msg``` type. This will help us catch unimplemented functions and guide us with syntax highlighting.
2021-11-23 17:51:24 +00:00
2021-11-22 23:13:54 +00:00
```
2021-12-06 23:19:27 +00:00
// MsgCreateGreet we defined it here to get type checking
//to make sure we are immplementing it correctly
2021-11-22 23:13:54 +00:00
var _ sdk.Msg = & MsgCreateGreet{}
2021-12-06 23:19:27 +00:00
// constructor for creating a new greeting
func NewMsgCreateGreet(owner string, message string) *MsgCreateGreet{
return & MsgCreateGreet{
Owner: owner,
Message: message,
2021-11-22 23:13:54 +00:00
}
}
2021-12-06 23:19:27 +00:00
// does a quick stateless validation on our new greeting
func (m *MsgCreateGreet) ValidateBasic() error {
// ensures address is valid
if _, err := sdk.AccAddressFromBech32(m.Owner); err != nil {
2021-11-22 23:13:54 +00:00
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid owner address", err)
}
2021-12-06 23:19:27 +00:00
// ensures the greeting is not empty
if len(strings.TrimSpace(m.Message)) == 0 {
return fmt.Errorf("must provide a greeting message")
2021-11-22 23:13:54 +00:00
}
return nil
}
2021-12-06 23:19:27 +00:00
// gets the signer of the new message which will be the owner of the greeting
func (m *MsgCreateGreet) GetSigners() []sdk.AccAddress {
owner, err := sdk.AccAddressFromBech32(m.Owner);
if err != nil {
panic(err)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
return []sdk.AccAddress{owner}
2021-11-22 23:13:54 +00:00
}
```
2021-11-24 02:24:36 +00:00
2021-12-06 23:19:27 +00:00
### Registering our Codec & interfaces
now that we have our ```MsgCreateGreet``` implement the ```sdk.Msg``` interface let's register our codec for marshaling/unmarshaling our greeting we will register both the deprecated legacy amino and the new Interface registry.
2021-11-22 23:13:54 +00:00
```
2021-12-06 23:19:27 +00:00
// registers the marshal/unmarsahl for greating a new greeting for our legacy amino codec
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino){
cdc.RegisterConcrete(& MsgCreateGreet{}, "greet/CreateGreet", nil)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// registers a module's interface types and their concrete implementations as proto.Message.
func RegisterInterfaces(registry types.InterfaceRegistry){
registry.RegisterImplementations((*sdk.Msg)(nil), & MsgCreateGreet{})
msgservice.RegisterMsgServiceDesc(registry, & _Msg_serviceDesc)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
var amino = codec.NewLegacyAmino()
var ModuleCdc = codec.NewAminoCodec(amino)
```
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
### Setting up a basic Keeper
we have finished up setting up our types, now it's time to implement our greet module's keeper, lets do that in a new folder & package named keeper, create ```x/greet/keeper/greet_keeper.go``` .
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
### Setting up the Keeper Struct & imports
keepers are an abstraction over the state defined by a module, every module would have a keeper which would be used to access the state of that module, or if given access a keeper can also use other module's keepers by providing reference to the other module's keeper.
2021-11-22 23:13:54 +00:00
```
package keeper
import (
2021-12-06 23:19:27 +00:00
"context"
"strconv"
2021-11-22 23:13:54 +00:00
"github.com/cosmos/cosmos-sdk/codec"
2021-12-06 23:19:27 +00:00
"github.com/cosmos/cosmos-sdk/store/prefix"
2021-11-22 23:13:54 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2021-12-06 23:19:27 +00:00
abci "github.com/tendermint/tendermint/abci/types"
2021-11-24 02:24:36 +00:00
"github.com/kava-labs/kava/x/greet/types"
2021-12-06 23:19:27 +00:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2021-11-22 23:13:54 +00:00
)
2021-12-06 23:19:27 +00:00
type Keeper struct {
cdc codec.Codec // used to marshall and unmarshall structs from & to []byte
key sdk.StoreKey // grant access to the store
}
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// our constructor for creating a new Keeper for this module
func NewKeeper(c codec.Codec, k sdk.StoreKey) Keeper {
return Keeper{
cdc: c,
key: k,
2021-11-22 23:13:54 +00:00
}
}
```
2021-12-06 23:19:27 +00:00
### Wiring up our methods for handling new transactions & queries
Now that we have our Keeper Struct written, let's create some receiver functions on our keeper to handle adding a new greeting & looking up a greeting.
```
// get greet count will be used for setting an Id when a new greeting is created
2021-11-22 23:13:54 +00:00
func (k Keeper) GetGreetCount(ctx sdk.Context) int64 {
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetCountKey))
byteKey := types.KeyPrefix(types.GreetCountKey)
bz := store.Get(byteKey)
if bz == nil {
2021-12-06 23:19:27 +00:00
return 0
2021-11-22 23:13:54 +00:00
}
count, err := strconv.ParseInt(string(bz), 10, 64)
if err != nil {
2021-12-06 23:19:27 +00:00
panic("cannot decode count")
2021-11-22 23:13:54 +00:00
}
return count
}
2021-12-06 23:19:27 +00:00
// sets the greet count
2021-11-22 23:13:54 +00:00
func (k Keeper) SetGreetCount(ctx sdk.Context, count int64){
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetCountKey))
2021-12-06 23:19:27 +00:00
key := types.KeyPrefix(types.GreetCountKey)
value := []byte(strconv.FormatInt(count, 10))
store.Set(key, value)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// creates a new greeting
func (k Keeper) CreateGreet(ctx sdk.Context, m types.MsgCreateGreet){
count := k.GetGreetCount(ctx)
greet := types.Greet{
Id: strconv.FormatInt(count, 10),
Owner: m.Owner,
Message: m.Message,
2021-11-22 23:13:54 +00:00
}
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetKey))
key := types.KeyPrefix(types.GreetKey + greet.Id)
value := k.cdc.MustMarshal(& greet)
store.Set(key, value)
2021-12-06 23:19:27 +00:00
k.SetGreetCount(ctx, count + 1)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// gets a greeting from the store
func (k Keeper) GetGreeting(ctx sdk.Context, key string) types.Greet {
2021-11-22 23:13:54 +00:00
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetKey))
var Greet types.Greet
k.cdc.Unmarshal(store.Get(types.KeyPrefix(types.GreetKey + key)), & Greet)
return Greet
}
2021-12-06 23:19:27 +00:00
// checks if a greeting exists by an id
2021-11-22 23:13:54 +00:00
func (k Keeper) HasGreet(ctx sdk.Context, id string) bool {
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetKey))
return store.Has(types.KeyPrefix(types.GreetKey + id))
}
2021-12-06 23:19:27 +00:00
// gets the owner of a greeting
func (k Keeper) GetGreetOwner(ctx sdk.Context, key string) string {
return k.GetGreeting(ctx, key).Owner
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// gets a list of all greetings in the store
func (k Keeper) GetAllGreetings(ctx sdk.Context) (msgs []types.Greet){
2021-11-22 23:13:54 +00:00
store := prefix.NewStore(ctx.KVStore(k.key), types.KeyPrefix(types.GreetKey))
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefix(types.GreetKey))
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
defer iterator.Close()
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
for ; iterator.Valid(); iterator.Next() {
var msg types.Greet
k.cdc.Unmarshal(iterator.Value(), & msg)
msgs = append(msgs, msg)
}
return
}
```
2021-12-06 23:19:27 +00:00
### Handling queries
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
We have added methods for interacting with greetings such as creating or reading them, now let's set up our two query services so we can route them to the correct method, we will set up our legacy Querier & gRPC querier below the methods we defined above on our keeper.
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
```
func (k Keeper) GreetAll(c context.Context, req *types.QueryAllGreetRequest) (*types.QueryAllGreetResponse, error){
ctx := sdk.UnwrapSDKContext(c)
var greetings []*types.Greet
for _, g := range k.GetAllGreetings(ctx) {
var greeting = & g
greetings = append(greetings,greeting)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
return & types.QueryAllGreetResponse{Greetings: greetings, Pagination: nil}, nil
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
func (k Keeper) Greet(c context.Context, req *types.QueryGetGreetRequest) (*types.QueryGetGreetResponse, error){
sdk.UnwrapSDKContext(c)
var greeting = k.GetGreeting(sdk.UnwrapSDKContext(c), req.Id)
return & types.QueryGetGreetResponse{Greeting: & greeting}, nil
}
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// LEGACY QUERIER will be deperacted but for the sake of competeness this is how to set it up
2021-11-22 23:13:54 +00:00
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
2021-12-06 23:19:27 +00:00
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryGetGreeting:
var getGreetRequest types.QueryGetGreetRequest
err := legacyQuerierCdc.UnmarshalJSON(req.Data, & getGreetRequest)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
val := k.GetGreeting(ctx, getGreetRequest.GetId())
bz, err := legacyQuerierCdc.MarshalJSON(val)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case types.QueryListGreetings:
val := k.GetAllGreetings(ctx)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, val)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
2021-11-22 23:13:54 +00:00
default:
2021-12-06 23:19:27 +00:00
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknow request at %s query endpoint", types.ModuleName)
2021-11-22 23:13:54 +00:00
}
}
}
```
2021-12-06 23:19:27 +00:00
### Setting up a command to create a new greeting
let's set up a way for clients to submit a new greeting & query existing greetings, we can do that with a CLI, REST, & gRPC clients. for this tutorial we will focus on setting up our CLI client. create ```x/greet/client/cli/tx.go```.
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
here We will define a command to create a new greeting:
2021-11-22 23:13:54 +00:00
```
2021-12-06 23:19:27 +00:00
package cli
2021-11-22 23:13:54 +00:00
import (
2021-12-06 23:19:27 +00:00
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/kava-labs/kava/x/greet/types"
"github.com/spf13/cobra"
2021-11-22 23:13:54 +00:00
)
2021-12-06 23:19:27 +00:00
func GetTxCmd() *cobra.Command {
cmd := & cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
cmd.AddCommand(CmdCreateGreeting())
return cmd
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
func CmdCreateGreeting() *cobra.Command {
cmd:= & cobra.Command{
Use: "create-greeting [message]",
Short: "creates a new greetings",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
message := string(args[0])
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgCreateGreet(clientCtx.GetFromAddress().String(), string(message))
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
flags.AddTxFlagsToCmd(cmd)
return cmd
2021-11-22 23:13:54 +00:00
}
```
2021-12-06 23:19:27 +00:00
### Querying greetings
We will now set up two different commands for querying, one will be to list all greetings & the other will be to get a greeting by it's id. inside ```x/greet/cli/query.go```:
2021-11-22 23:13:54 +00:00
```
2021-12-06 23:19:27 +00:00
package cli
2021-11-22 23:13:54 +00:00
import (
2021-12-06 23:19:27 +00:00
"context"
2021-11-22 23:13:54 +00:00
"fmt"
2021-12-06 23:19:27 +00:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
2021-11-22 23:13:54 +00:00
"github.com/kava-labs/kava/x/greet/types"
2021-12-06 23:19:27 +00:00
"github.com/spf13/cobra"
2021-11-22 23:13:54 +00:00
)
2021-12-06 23:19:27 +00:00
// this is the parent query command for the greet module everytime we add a new command we will register it here
func GetQueryCmd(queryRoute string) *cobra.Command {
// Group todos queries under a subcommand
cmd := & cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
cmd.AddCommand(CmdListGreetings())
cmd.AddCommand(CmdShowGreeting())
return cmd
}
// build the list greet command function
func CmdListGreetings() *cobra.Command {
cmd := & cobra.Command{
Use: "list-greetings",
Short: "list all greetings",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := & types.QueryAllGreetRequest{
Pagination: pageReq,
}
res, err := queryClient.GreetAll(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
flags.AddQueryFlagsToCmd(cmd)
return cmd
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// build the show greet command function
func CmdShowGreeting() *cobra.Command {
cmd := & cobra.Command{
Use: "get-greeting [id]",
Short: "shows a greeting",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := & types.QueryGetGreetRequest{
Id: args[0],
}
res, err := queryClient.Greet(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
```
2021-12-06 23:19:27 +00:00
### Setting up our Module's package
Now that we have all the basic functionality set up for our greet module, let's bring it all together and get our module ready to be used & tested, create a new file ```x/greet/module.go```.
Here we will start by implementing our ```AppModuleBasic``` & & ```AppModule``` interfaces.
2021-11-22 23:13:54 +00:00
```
package greet
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
import (
"context"
"encoding/json"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/kava-labs/kava/x/greet/client/cli"
"github.com/kava-labs/kava/x/greet/keeper"
"github.com/kava-labs/kava/x/greet/types"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
)
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)
2021-12-06 23:19:27 +00:00
/*
The AppModuleBasic interface defines the independent methods modules need to implement
it follows this interface below
type AppModuleBasic interface {
Name() string
RegisterLegacyAminoCodec(*codec.LegacyAmino)
RegisterInterfaces(codectypes.InterfaceRegistry)
DefaultGenesis(codec.JSONMarshaler) json.RawMessage
ValidateGenesis(codec.JSONMarshaler, client.TxEncodingConfig, json.RawMessage) error
// client functionality
RegisterRESTRoutes(client.Context, *mux.Router)
RegisterGRPCRoutes(client.Context, *runtime.ServeMux)
GetTxCmd() *cobra.Command
GetQueryCmd() *cobra.Command
}
*/
type AppModuleBasic struct{}
// Returns the name of the module as a string
2021-11-22 23:13:54 +00:00
func (AppModuleBasic) Name() string {
return types.ModuleName
}
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
gs := types.DefaultGenesisState()
return cdc.MustMarshalJSON(& gs)
}
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
2021-12-06 23:19:27 +00:00
return nil
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// Registers the amino codec for the module, which is used to marshal
// and unmarshal structs to/from []byte in order to persist them in the module's KVStore.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino){
types.RegisterLegacyAminoCodec(cdc)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// Registers a module's interface types and their concrete implementations as proto.Message
func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
2021-11-22 23:13:54 +00:00
}
2021-12-06 23:19:27 +00:00
// Registers gRPC routes for the module.
2021-11-22 23:13:54 +00:00
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
2021-12-06 23:19:27 +00:00
// Registers the REST routes for the module. These routes will be used to map REST request to the module in order to process them
func (a AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { }
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// Returns the root Tx command for the module. The subcommands of this root command are used by end-users
// to generate new transactions containing messages defined in the module
2021-11-22 23:13:54 +00:00
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
}
2021-12-06 23:19:27 +00:00
// Return the root query command for the module. The subcommands of this root command are used by end-users
// to generate new queries to the subset of the state defined by the module.
2021-11-22 23:13:54 +00:00
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(types.StoreKey)
}
2021-12-06 23:19:27 +00:00
// -------------------------------------APPMODULE BELOW------------------------------------------------- //
/*
The AppModule interface defines the inter-dependent methods that modules need to implement
follows the interface below
type AppModule interface {
AppModuleGenesis
// registers
RegisterInvariants(sdk.InvariantRegistry)
// routes
Route() sdk.Route
// Deprecated: use RegisterServices
QuerierRoute() string
// Deprecated: use RegisterServices
LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier
// RegisterServices allows a module to register services
RegisterServices(Configurator)
// ABCI
BeginBlock(sdk.Context, abci.RequestBeginBlock)
EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
}
*/
type AppModule struct{
2021-11-22 23:13:54 +00:00
AppModuleBasic
keeper keeper.Keeper
}
2021-12-06 23:19:27 +00:00
// constructor
2021-11-22 23:13:54 +00:00
func NewAppModule(keeper keeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
}
}
2021-12-06 23:19:27 +00:00
// Returns the route for messages to be routed to the module by BaseApp.
2021-11-22 23:13:54 +00:00
func (am AppModule) Name() string {
return am.AppModuleBasic.Name()
}
2021-12-06 23:19:27 +00:00
// registers the invariants of the module. If an invariant deviates from its predicted value,
// the InvariantRegistry triggers appropriate logic (most often the chain will be halted).
func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { }
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// Returns the route for messages to be routed to the module by BaseApp.
func (AppModule) Route() sdk.Route {
2021-11-22 23:13:54 +00:00
return sdk.Route{}
}
2021-12-06 23:19:27 +00:00
2021-11-22 23:13:54 +00:00
2021-12-06 23:19:27 +00:00
// Returns the name of the module's query route, for queries to be routes to the module by BaseApp.deprecated
2021-11-22 23:13:54 +00:00
func (AppModule) QuerierRoute() string {
return types.QuerierRoute
}
2021-12-06 23:19:27 +00:00
// Returns a querier given the query path, in order to process the query.
2021-11-22 23:13:54 +00:00
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return keeper.NewQuerier(am.keeper, legacyQuerierCdc)
}
func (AppModule) ConsensusVersion() uint64 {
return 1
}
2021-12-06 23:19:27 +00:00
// Allows a module to register services.
2021-11-22 23:13:54 +00:00
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
2021-12-06 23:19:27 +00:00
gs := types.DefaultGenesisState()
2021-11-22 23:13:54 +00:00
return cdc.MustMarshalJSON(& gs)
}
2021-12-06 23:19:27 +00:00
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) { }
2021-11-22 23:13:54 +00:00
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
2021-12-06 23:19:27 +00:00
// ----------------------------------MSGSERVER REGISTER------------------------//
var _ types.MsgServer = msgServer{}
2021-11-22 23:13:54 +00:00
type msgServer struct {
keeper keeper.Keeper
}
func (m msgServer) CreateGreet(c context.Context, msg *types.MsgCreateGreet) (*types.MsgCreateGreetResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
m.keeper.CreateGreet(ctx, types.MsgCreateGreet{Owner: msg.Owner, Message: msg.Message})
return & types.MsgCreateGreetResponse{}, nil
}
func NewMsgServerImpl(keeper keeper.Keeper) types.MsgServer {
return & msgServer{keeper: keeper}
}
2021-12-06 23:19:27 +00:00
```
### Hooking up our module inside App.go
inside ```app/app.go``` start off importing the greet module, it's types & keeper packages and add them to the following places:
1. ```module.NewBasicManager()``` add ```greet.AppModuleBasic{}```
2. ```type App struct {}``` add ```greetkeeper.Keeper```
3. ```sdk.NewKVStoreKeys()``` inside ```NewApp``` func add ```greettypes.StoreKey```
4. inside ```NewApp``` func add ```app.greetKeeper = greetKeeper.NewKeeper()``` and add arguments ```appCodec``` & ```keys[greettypes.StoreKey]```
5. inside ```NewApp``` find where we define ```app.mm``` & add ```greet.NewAppModule(app.greetKeeper),```
6. finally add the greet module's name to ```SetOrderBeginBlockers```, ```SetOrderEndBlockers``` && ```SetOrderInitGenesis```
## Testing our new Module
1. inside the root of our directory run ```docker build -t kava/kava:tutorial-demo .```
2. find the directory for ```kvtool``` and open in your favorite code editor
3. run ```kvtool testnet gen-config kava --kava.configTemplate upgrade-v44``` which will create a bunch of files inside ```full_configs/generated```
4. open up the two ```docker-compose.yaml``` files the one inside ```generated``` & the one inside ```generated/kava``` and change the image to point to ```kava/kava:tutorial-demo``` this will point to the local image we just built
5. change into the ```full_configs/generated``` directory and run ```docker compose up -d```
6. now run ```docker compose exec kavanode bash``` to bash into our ```kava``` cli inside the running container
We should now have access to our greet commands that we defined first we will test creating a new greeting, for that we will run the following command:
```kava tx greet create-greeting "hello world from kava chain" --from whale```
now let's test to see if the greeting message is able to be queried:
```kava q greet list-greetings```
We should see something like this below:
```
greetings:
- id: "0"
message: hello world from kava chain
owner: kava173w2zz287s36ewnnkf4mjansnthnnsz7rtrxqc
pagination: null
```
Now let's test if we can query the greeting by it's id which in our case will be ```"0"```, run the following:
```kava q greet get-greeting 0```
We should see:
```
greeting:
id: "0"
message: hello world from kava chain
owner: kava173w2zz287s36ewnnkf4mjansnthnnsz7rtrxqc
```
2021-11-22 23:13:54 +00:00