Implement Types
Now that you've defined messages that trigger state transitions, it's time to implement types and methods that operate on the state.
The Cosmos SDK relies on keepers. A keeper is an abstraction that lets your blockchain app interact with the state. Functions like create, read, update, and delete (CRUD) are defined as keeper methods.
For the nameservice blockchain, define a whois type and the create and delete methods.
Because Ignite CLI does the heavy lifting for you, choose from several ignite scaffold commands to create CRUD functionality code for data stored in different ways:
- Array, a list-like data structure
- Map (key-value pairs)
- In a single location
Add the whois Type
Use the ignite scaffold map command to scaffold the whois type and create the code that implements CRUD functionality to create, read, update, and delete information about names.
In this example, the whois type is stored in a map-like data structure:
ignite scaffold map whois name value price owner --no-message
where:
whois is the type
name is the name the user sets
value is the name that name resolves to
price is the bid
--no-messageflag skips message creationBy default, generic CRUD messages are scaffolded. However, you've already created messages specifically for this blockchain, so you can skip message creation with the
--no-messageflag.
The ignite scaffold map whois name value price --no-message command created and modified several files:
proto/nameservice/whois.protoDefines the
Whoistype as a proto message.proto/nameservice/query.proto- Queries to get data from the blockchain.
- Define queries as proto messages.
- Register the messages in the
Queryservice.
proto/nameservice/genesis.protoA type for exporting the state of the blockchain, for example, during software upgrades.
x/nameservice/keeper/grpc_query_whois.goKeeper methods to query the blockchain.
x/nameservice/keeper/grpc_query_whois_test.goTests for query keeper methods.
x/nameservice/keeper/whois.goKeeper methods to get, set, and remove whois information from the store.
x/nameservice/keeper/whois_test.goTests for keeper methods.
x/nameservice/client/cli/query_whois.goCLI commands for querying the blockchain.
x/nameservice/client/cli/query.goRegisters the CLI commands.
x/nameservice/client/cli/query_whois_test.goTests for CLI commands.
x/nameservice/types/keys.goString prefix in the key to store whois information in the state.
x/nameservice/genesis.goLogic for exporting the state.
x/nameservice/types/genesis.goLogic for validating the genesis file.
x/nameservice/module.goRegisters gRPC gateway routes.
Keeper Package
In the x/nameservice/keeper/whois.go file, take at a look at the keeper package.
SetWhoisuses a key-value store with a prefix for theWhoistype and uses astore.Setmethod to write aWhoisinto the store.
GetWhoisselects a store using theWhoisprefix and uses astore.Getmethod to fetch aWhoiswith a particular index.
The keeper package also includes RemoveWhois and GetAllWhois.