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
47 changes: 47 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,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.
ziscky marked this conversation as resolved.
Show resolved Hide resolved
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
```
Comment on lines +27 to +47
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve the flow of the integration example

The current example could be restructured to provide a clearer flow of operations. Consider the following suggestions:

  1. First, show how to create or obtain a module manager.
  2. Then, demonstrate how to modify it (option 2 for non-depinject users, option 1 for depinject users).
  3. Finally, show how to set the modified module manager on the app.

Also, for option 1, it's not clear where moduleManager comes from. Consider adding a brief explanation or showing its initialization.

Here's a suggested restructure:

// Option 1: For non-depinject users
moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{
    stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)},
    // other modules ...
})

// Option 2: For depinject users
// Assuming moduleManager is injected
depinject.Inject(
    // ... provider/invoker/supplier
    &moduleManager,
)

// Modify the module manager (applies to both options)
oldStakingModule, _ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule)
moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{
    AppModule: oldStakingModule,
    cdc: appCodec,
}

// Set the modified module manager on the app
app.ModuleManager = moduleManager

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion from the bot imho @ziscky

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, option 1 should still be depinject

Loading