A minimal Cosmos SDK example application demonstrating best practices for building blockchain modules. This repository serves as a learning resource and starting point for new Cosmos SDK developers.
- Custom Counter Module - A simple module demonstrating the full module development lifecycle
- Complete Test Suite - Unit tests, integration tests, and simulation tests
- AutoCLI Integration - Automatic CLI command generation
- Local Development - Scripts for single-node and multi-node local networks
- Docker Support - Containerized builds and local network deployment
# Clone the repository
git clone https://github.com/cosmos/example.git
cd example
# Build the application
make build
# Install the binary
make install
# Initialize and start a local node
make start.
βββ app.go # Application wiring and module registration
βββ exampled/ # CLI entrypoint
β βββ main.go
β βββ cmd/
β βββ commands.go # CLI commands wiring
β βββ root.go # Root command setup
βββ x/counter/ # Custom counter module
β βββ autocli.go # AutoCLI configuration
β βββ module.go # Module definition and interfaces
β βββ keeper/ # State management
β β βββ keeper.go # Keeper implementation
β β βββ msg_server.go # Transaction handlers
β β βββ query_server.go# Query handlers
β βββ types/ # Proto-generated types
β β βββ codec.go # Type registration
β βββ simulation/ # Simulation testing
β βββ genesis.go # Randomized genesis
β βββ msg_factory.go # Message factories
βββ proto/ # Protobuf definitions
β βββ example/counter/v1/
β βββ tx.proto # Transaction messages
β βββ query.proto # Query definitions
β βββ genesis.proto # Genesis state
βββ tests/ # Integration tests
βββ scripts/ # Development scripts
βββ Makefile # Build and development commands
The counter module is a minimal example that demonstrates core Cosmos SDK patterns:
A single uint64 counter value stored using the collections library.
| Message | Description |
|---|---|
MsgAdd |
Increments the counter by a specified amount |
| Query | Description |
|---|---|
Count |
Returns the current counter value |
The module emits events when the counter is updated:
{
"type": "counter_added",
"attributes": [
{"key": "previous_count", "value": "0"},
{"key": "added", "value": "5"},
{"key": "new_count", "value": "5"}
]
}After starting a local node, interact with the counter module:
# Query the current count
exampled query counter count
# Add to the counter (alice is a pre-funded account from the local_node.sh script in /scripts)
exampled tx counter add 5 --from alice
# Check transaction result
exampled query tx <txhash># Build binary to ./build/myapp
make build
# Install to $GOPATH/bin
make install# Single node (installs, initializes, and starts)
make start
# Or manually:
./scripts/local_node.sh # Initialize
exampled start # Start the node# Initialize and start 4-node network
make localnet
# View logs
make localnet-logs
# Stop network
make localnet-stop
# Clean up
make localnet-clean# Build the proto builder image (first time only)
make proto-image-build
# Generate Go code from proto files
make proto-gengo test ./x/counter/keeper/...Test the module within a full application context:
go test ./tests/...Fuzz testing with randomized inputs and state:
# Run full simulation (all seeds)
make test-sim-full
# Run determinism test
make test-sim-determinism
# Run single seed (faster)
go test -tags sims -run "TestFullAppSimulation/seed:_1$" -vgo test ./...make lint # Check for issues
make lint-fix # Auto-fix issues-
Define the proto message in
proto/example/counter/v1/tx.proto:message MsgNewAction { option (cosmos.msg.v1.signer) = "sender"; string sender = 1; // your fields }
-
Regenerate proto files:
make proto-gen
-
Implement the handler in
x/counter/keeper/msg_server.go:func (m msgServer) NewAction(ctx context.Context, msg *types.MsgNewAction) (*types.MsgNewActionResponse, error) { // implementation }
-
Add AutoCLI config in
x/counter/autocli.go -
Write tests in
x/counter/keeper/msg_server_test.go -
Add simulation in
x/counter/simulation/msg_factory.go
- Define in proto (
query.proto) - Regenerate:
make proto-gen - Implement in
keeper/query_server.go - Add AutoCLI config
- Write tests
| Module | Description |
|---|---|
auth |
Account management and authentication |
bank |
Token transfers and balances |
staking |
Proof-of-stake validator management |
distribution |
Reward distribution |
gov |
On-chain governance |
slashing |
Validator punishment |
consensus |
Consensus parameters |
vesting |
Vesting accounts |
counter |
Example custom module |
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.