From 617d54761f00b3d27a239337736ef44fd43af39d Mon Sep 17 00:00:00 2001 From: Eric Mokaya <4112301+ziscky@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:37:25 +0300 Subject: [PATCH] docs: add instructions to change DefaultGenesis (#21680) --- docs/build/building-apps/06-app-go-genesis.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 docs/build/building-apps/06-app-go-genesis.md diff --git a/docs/build/building-apps/06-app-go-genesis.md b/docs/build/building-apps/06-app-go-genesis.md new file mode 100644 index 000000000000..b9902858d7fe --- /dev/null +++ b/docs/build/building-apps/06-app-go-genesis.md @@ -0,0 +1,47 @@ +--- +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 ( for depinject users ): override previous module manager +depinject.Inject( +// ... provider/invoker/supplier +&moduleManager, +) + +oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule) +moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ + AppModule: oldStakingModule, + cdc: appCodec, +} + +// option 2 ( for non depinject users ): use new module manager +moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ +stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, +// other modules ... +}) + +// set the module manager +app.ModuleManager = moduleManager +```