Create Modules & Types for Cosmos SDK based Blockchains

Why Create Modules?
Modules provide modularity and compartmentalize the code, making it easier to manage and maintain. For example, if Yoda wants to track the Sith as well as the Jedi, he can create types and functions specifically related to Jedi and Sith tracking and keep them separate.
Step 1: Create a New Blockchain
First, let's create a new blockchain:
ignite scaffold chain forceRegistry --address-prefix force
Step 2: Create Modules
Now, we can add two modules: Jedi and Sith.
ignite scaffold module Jedi
ignite scaffold module Sith
You can verify they have been created by listing the files:
ls
Now that we have modules, we can create types and start scaffolding lists based on our custom types.
Creating Types
Let's say that we want to create a custom type based on our registerJedi
message, which contains three arguments: name
, homePlanet
, and forcePower:uint
.
One approach is to create a type that contains these arguments and pass it to the registerJedi
function.
Why Create a Custom Type?
If Yoda also wants to track known Sith in his database, instead of creating a whole new type, he can create one called forceUser
that both Jedi and Sith would share, as they have the same attributes.
To demonstrate this, since both Jedi and Sith are force users, let's create a forceUser
type.
ignite scaffold list regSith name homePlanet forcePower:uint --module sith
ignite scaffold list regJedi name homePlanet forcePower:uint --module Jedi
Remember to change bob
to yoda
in the config.yml
file:
ignite chain serve
You can see that the Jedi module has been added by using the help command:
forceRegistryd tx --help
The Sith module has been added as well.
Step 3: Modular Independence
If, for some reason, the Sith module needed to be removed, the Jedi module could remain independently.
Additional Feature Idea: Passing Types Between Lists
It would be beneficial to have the ability to pass types created from other lists to new lists or as arguments passed to that list.
For example:
ignite scaffold list regSith:forceUser --module sith
ignite scaffold list regJedi:forceUser --module jedi
This approach would allow for more streamlined and modular code management, especially when dealing with similar types across different modules.
Final Thoughts
The modular approach provided by the Cosmos SDK allows for clear and maintainable code organization. By using shared types like forceUser
, you can efficiently manage and track different entities within your blockchain application.