Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add instructions to change DefaultGenesis #21680

Merged
merged 12 commits into from
Oct 1, 2024
102 changes: 102 additions & 0 deletions docs/build/building-apps/01-app-go-di.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,108 @@ 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`
ziscky marked this conversation as resolved.
Show resolved Hide resolved

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)

```

ziscky marked this conversation as resolved.
Show resolved Hide resolved
### Registering non app wiring modules

It is possible to combine app wiring / depinject enabled modules with non app wiring modules.
Expand Down
Loading