Skip to main content
Version: v0.52

Hooks

Hooks are functions that are called before and/or after certain events in the module's lifecycle.

Defining Hooks

  1. Define the hook interface and a wrapper implementing depinject.OnePerModuleType:

    testutil/x/counter/types/expected_keepers.go
    loading...
  2. Add a CounterHooks field to the keeper:

    testutil/x/counter/keeper/keeper.go
    loading...
  3. Create a depinject invoker function

    testutil/x/counter/depinject.go
    loading...
  4. 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.