Skip to content

Commit

Permalink
fix(baseapp/docs): create new section
Browse files Browse the repository at this point in the history
  • Loading branch information
ziscky committed Sep 13, 2024
1 parent e6d65f7 commit 83b248a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 102 deletions.
102 changes: 0 additions & 102 deletions docs/build/building-apps/01-app-go-di.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,108 +118,6 @@ More information on how work `depinject.Configs` and `depinject.Supply` can be f
https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/simapp/app_v2.go#L114-L146
```

### Modifying the `DefaultGenesis`

It is possible to modify the DefaultGenesis parameters for modules by wrapping the module and

Previously, the `ModuleBasics` was a global variable that was used to register all modules' `AppModuleBasic` implementation.
Example ( staking ):
```go
type CustomStakingModule struct {
staking.AppModuleBasic
}
// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method.
func (CustomStakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
params := stakingtypes.DefaultParams()
params.BondDenom = "mydenom"
return cdc.MustMarshalJSON(&stakingtypes.GenesisState{
Params: params,
})
}
// option 1: using the previously removed global variable
ModuleBasics = module.NewBasicManager(
CustomStakingModule{}, // wrapped staking module
auth.AppModuleBasic{},
distr.AppModuleBasic{},
params.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
authzmodule.AppModuleBasic{},
ibc.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
...
)
// option 2: the basic module manager can be now created from the module manager
app.BasicModuleManager = module.NewBasicManagerFromManager(
app.moduleManager,
// override staking AppModule
map[string]module.AppModuleBasic{
stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}},
},
)
// option 3: using depinject
depinject.Supply(
// supply custom module basics
map[string]module.AppModuleBasic{
stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}},
},
)
```
The basic module manager has been deleted. It was not necessary anymore and was simplified to use the `module.Manager` directly.
Example ( staking ) :
```go
type CustomStakingModule struct {
staking.AppModule
cdc codec.Codec
}
// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method.
func (cm CustomStakingModule) DefaultGenesis() json.RawMessage {
params := stakingtypes.DefaultParams()
params.BondDenom = "mydenom"
return cm.cdc.MustMarshalJSON(&stakingtypes.GenesisState{
Params: params,
})
}
// option 1: use new module manager
moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{
stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)},
// other modules ...
})
// option 2: override previous module manager
oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule)
moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{
AppModule: oldStakingModule,
cdc: appCodec,
}
// depinject users
depinject.Inject(
// ...
&moduleManager,
)
// non-depinject users
moduleManager.RegisterLegacyAminoCodec(legacyAmino)
moduleManager.RegisterInterfaces(interfaceRegistry)
```
### Registering non app wiring modules

It is possible to combine app wiring / depinject enabled modules with non app wiring modules.
Expand Down
54 changes: 54 additions & 0 deletions docs/build/building-apps/06-app-go-genesis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
sidebar_position: 1
---


### Modifying the `DefaultGenesis`

It is possible to modify the DefaultGenesis parameters for modules by wrapping the module, providing it to the `*module.Manager` and injecting it with `depinject`.


Example ( staking ) :

```go
type CustomStakingModule struct {
staking.AppModule
cdc codec.Codec
}

// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method.
func (cm CustomStakingModule) DefaultGenesis() json.RawMessage {
params := stakingtypes.DefaultParams()
params.BondDenom = "mydenom"

return cm.cdc.MustMarshalJSON(&stakingtypes.GenesisState{
Params: params,
})
}

// option 1: use new module manager
moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{
stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)},
// other modules ...
})

// option 2: override previous module manager
oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule)
moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{
AppModule: oldStakingModule,
cdc: appCodec,
}


// depinject users
depinject.Inject(
// ...
&moduleManager,
)

// non-depinject users
moduleManager.RegisterLegacyAminoCodec(legacyAmino)
moduleManager.RegisterInterfaces(interfaceRegistry)

```

0 comments on commit 83b248a

Please sign in to comment.