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.