github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/docs/building-modules/structure.md (about) 1 <!-- 2 order: 12 3 synopsis: This document outlines the recommended structure of Cosmos SDK modules. These ideas are meant to be applied as suggestions. Application developers are encouraged to improve upon and contribute to module structure and development design. 4 --> 5 6 # Recommended Folder Structure 7 8 ## Structure 9 10 A typical Cosmos SDK module can be structured as follows: 11 12 ```shell 13 x/{module} 14 ├── client 15 │ ├── cli 16 │ │ ├── query.go 17 │ │ └── tx.go 18 │ └── rest 19 │ ├── query.go 20 │ └── tx.go 21 ├── exported 22 │ └── exported.go 23 ├── internal 24 │ ├── keeper 25 │ │ ├── invariants.go 26 │ │ ├── keeper.go 27 │ │ ├── ... 28 │ │ └── querier.go 29 │ └── types 30 │ ├── codec.go 31 │ ├── errors.go 32 │ ├── events.go 33 │ ├── expected_keepers.go 34 │ ├── genesis.go 35 │ ├── keys.go 36 │ ├── msgs.go 37 │ ├── params.go 38 │ ├── ... 39 │ └── querier.go 40 ├── abci.go 41 ├── alias.go 42 ├── genesis.go 43 ├── handler.go 44 ├── module.go 45 ├── ... 46 └── simulation.go 47 ``` 48 49 - `abci.go`: The module's `BeginBlocker` and `EndBlocker` implementations (if any). 50 - `alias.go`: The module's exported types, constants, and variables. These are mainly 51 to improve developer ergonomics by only needing to import a single package. Note, 52 there is nothing preventing developers from importing other packages from the module 53 (excluding`internal/`) but it is recommended that `alias.go` have everything 54 exposed that other modules may need. The majority of the exported values here will 55 typically come from `internal/` (see below). 56 - `client/`: The module's CLI and REST client functionality implementation and 57 testing. 58 - `exported/`: The module's exported types -- typically type interfaces. If a module 59 relies on other module keepers, it is expected to receive them as interface 60 contracts through the `expected_keepers.go` (which are detailed below) design to 61 avoid having a direct dependency on the implementing module. However, these 62 contracts can define methods that operate on and/or return types that are specific 63 to the contract's implementing module and this is where `exported/` comes into play. 64 Types defined here allow for `expected_keepers.go` in other modules to define 65 contracts that use single canonical types. This pattern allows for code to remain 66 DRY and also alleviates import cycle chaos. 67 - `genesis.go`: The module's genesis related business logic (e.g. `InitGenesis`). 68 Note, genesis types are defined in `internal/types`. 69 - `handler.go`: The module's message handlers. 70 - `internal/`: The module's internal types and implementations. The purpose of 71 this package is mainly two fold. First, it signals that this package is not 72 intended to be used or imported anywhere outside the defining module. Secondly, 73 it goes hand-in-hand with `alias.go` in that it allows public types and functions 74 to be used internally while not being exposed outside to the outside world. This 75 allows for greater freedom of development while maintaining API stability. 76 - `internal/types`: The module's type definitions such as messages, `KVStore` 77 keys, parameter types, and `expected_keepers.go` contracts. 78 - `internal/keeper`: The module's keeper implementation along with any auxiliary 79 implementations such as the querier and invariants. 80 - `module.go`: The module's implementation of the `AppModule` and `AppModuleBasic` 81 interfaces. 82 - `simulation.go`: The module's simulation messages and related types (if any). 83 84 ## Next {hide} 85 86 Learn about [interfaces](../interfaces/interfaces-intro.md) {hide}