Hooks
Hooks are functions that are called before and/or after certain events in the module's lifecycle.
Defining Hooks
Define the hook interface and a wrapper implementing
depinject.OnePerModuleType
:testutil/x/counter/types/expected_keepers.goloading...
Add a
CounterHooks
field to the keeper:testutil/x/counter/keeper/keeper.goloading...
Create a
depinject
invoker functiontestutil/x/counter/depinject.goloading...
Inject the hooks during app initialization:
appConfig = appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
// ....
{
Name: types.ModuleName,
Config: appconfig.WrapAny(&types.Module{}),
},
}
})
appConfig = depinject.Configs(
AppConfig(),
runtime.DefaultServiceBindings(),
depinject.Supply(
logger,
viper,
map[string]types.CounterHooksWrapper{
"counter": types.CounterHooksWrapper{&types.Hooks{}},
},
))
Examples in the SDK
For examples of hooks implementation in the Cosmos SDK, refer to the Epochs Hooks documentation and Distribution Hooks Documentation.