# Build Your Module
You will integrate your module with the minimal chain that you cloned previously. To facilitate integration and speed up feedback, you will direct the Go package manager to look for this dependency on a local disk. The simplest way is to put the module folder into a folder next to the minimal chain's folder:
The ls
command should give you:
You can now define the module:
In the new file, you define your module name and the Go version:
# Protobuf files
On top of the regular Protobuf files, like message and query types, your checkers module type will also be identified with a Protobuf file. You have chosen the package github.com/alice/checkers
, so go ahead and create the necessary folders:
# Module proto
You define your module in a new proto/alice/checkers/module/v1/module.proto
, taking inspiration from minimal-module-example
:
Note how the package is alice.checkers.module.v1
and the message name is Module
. This means that later on, when you integrate the module in your chain, you will identify it with:
# Necessary Protobuf files
The compilation of your Protobuf files will be done with the scripts/protocgen.sh
script, which you can copy from minimal-module-example
:
Note how it mentions two files: buf.gen.gogo.yaml
and buf.gen.pulsar.yaml
. Go ahead and copy them from minimal-module-example
. In fact, also copy buf.yaml
and the version lock file to avoid surprises:
To make your life easier, minimal-module-example
also provides a make
target to compile all the Protobuf files. Copy the whole Makefile
:
Note how it uses the ghcr.io/cosmos/proto-builder:0.14.0
(opens new window) Docker image here to run the protocgen.sh
script. This helps with making sure all necessary software is available.
# First Protobuf compilation
Before you run the compilation, because you have not yet defined any Protobuf files for messages, you can comment out this protocgen.sh
line:
Now run the compilation:
This creates a new api/module/v1/module.pulsar.go
(opens new window) file and updates your go.mod
. It also creates a new go.sum
.
Now you need a couple more Protobuf files.
# Minimum Protobuf objects
You are not defining your games, messages, and queries just yet. However, you already define the facts that your module:
- Has a genesis, which has a type.
- Uses params, which also have a type.
Go ahead and define them in a new proto/alice/checkers/v1/types.proto
, taking inspiration from minimal-module-example
:
Because you added a new Protobuf file, you need to compile it too. Uncomment the cp
line in protocgen.sh
:
And run it:
If the process tells you that it downloads a Cosmos SDK version different from 0.50.1, make sure to manually edit your go.mod
after the fact.
Next, tidy up the dependencies:
The script has created two new files: api/v1/types.pulsar.go
and types.pb.go
.
Your module is not viable yet. You need to define it and at least have it conform to the interface expected by an app, in this case chain-minimal
.
# Module interface
Once again, you can take inspiration from minimal-module-example
.
# General files
Prepare the basics around codec, params, genesis, and module name. You can keep them in the root folder:
The files have a lot of missing dependencies, so go ahead and, once more, run:
See the previous troubleshoot if it updates to a cosmos-sdk
other than v0.50.1
.
With this done, you can move to defining some necessary functions of the module's keeper.
# Keeper files
Again minimal-module-example
is a good guide. Create the keeper folder:
In it, you define what the keeper holds, its genesis actions, and eventually its message and query servers:
You may need to run go mod tidy
again.
At the moment you have not defined any message or query types, so you do not bother with defining the corresponding servers.
# Module files
To be correctly called, the module needs to conform to the chain-minimal
expectations. You define functions for the module, the dependency injection, and accessorily to open the possibility to add CLI commands. Create the module
folder:
In it, again copying from minimal-module-example
:
Once more, you need to run:
Your checkers module is not prepared as a standalone module. It is time to integrate it into chain-minimal
.
# Integrate checkers in chain-minimal
You now have to direct chain-minimal
to integrate your checkers module. Your checkers module is known with the github.com/alice/checkers
package, and located in a parallel folder.
Direct chain-minimal to the right dependency, so it does not look for the module on github.com
. Go to your go.mod
and add:
Now, you can define the module in the app/app.yaml
file in two locations:
What remains is to update app/app.go
, where you place your checkers keeper at the right locations:
And that's about it. depinject
and the rest take care of initialization and runtime.
The side-effects comments refer to the init()
function of Go packages. If you omit the new empty import
call, the corresponding init
function will not be called. Because of that, your alice.checkers.module.v1.Module
Protobuf objects will not be known, which can lead to an unpleasant runtime error such as unable to resolve "alice.checkers.module.v1.Module"
.
The missing object is only detected at runtime because its value is in app.yaml
.
# Run your checkers chain
Just like you did in the previous section, you compile the minimal chain, re-initialize, and start it. You need to re-initialize because your genesis has changed:
There you have it: your minimal chain with a checkers module is running. After stopping it with CTRL-C, confirm that the checkers module was correctly integrated by calling up:
In there, you can find:
This is as expected.
# Up next
Now that you have integrated your empty checkers module, it is time to make it interesting.