From 9170090f67649ff53d91b53d583457549d15a798 Mon Sep 17 00:00:00 2001 From: Denali Marsh Date: Thu, 31 Mar 2022 11:07:19 +0200 Subject: [PATCH] Implement savings module hooks interface/types (#1202) * implement savings hooks types * implement saving hooks keeper * add savings hooks to app.go + implement incentive type compliance --- app/app.go | 3 ++- x/incentive/keeper/hooks.go | 14 ++++++++++++++ x/savings/keeper/hooks.go | 24 ++++++++++++++++++++++++ x/savings/keeper/keeper.go | 11 +++++++++++ x/savings/types/expected_keepers.go | 6 ++++++ x/savings/types/hooks.go | 25 +++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 x/savings/keeper/hooks.go create mode 100644 x/savings/types/hooks.go diff --git a/app/app.go b/app/app.go index 30686431..bfd3b1a3 100644 --- a/app/app.go +++ b/app/app.go @@ -537,7 +537,7 @@ func NewApp( app.stakingKeeper, &swapKeeper, ) - app.savingsKeeper = savingskeeper.NewKeeper( + savingsKeeper := savingskeeper.NewKeeper( appCodec, keys[savingstypes.StoreKey], savingsSubspace, @@ -570,6 +570,7 @@ func NewApp( app.swapKeeper = *swapKeeper.SetHooks(app.incentiveKeeper.Hooks()) app.cdpKeeper = *cdpKeeper.SetHooks(cdptypes.NewMultiCDPHooks(app.incentiveKeeper.Hooks())) app.hardKeeper = *hardKeeper.SetHooks(hardtypes.NewMultiHARDHooks(app.incentiveKeeper.Hooks())) + app.savingsKeeper = *savingsKeeper.SetHooks(savingstypes.NewMultiSavingsHooks(app.incentiveKeeper.Hooks())) // create the module manager (Note: Any module instantiated in the module manager that is later modified // must be passed by reference here.) diff --git a/x/incentive/keeper/hooks.go b/x/incentive/keeper/hooks.go index a845f588..dec450b0 100644 --- a/x/incentive/keeper/hooks.go +++ b/x/incentive/keeper/hooks.go @@ -6,6 +6,7 @@ import ( cdptypes "github.com/kava-labs/kava/x/cdp/types" hardtypes "github.com/kava-labs/kava/x/hard/types" + savingstypes "github.com/kava-labs/kava/x/savings/types" swaptypes "github.com/kava-labs/kava/x/swap/types" ) @@ -18,6 +19,7 @@ var _ cdptypes.CDPHooks = Hooks{} var _ hardtypes.HARDHooks = Hooks{} var _ stakingtypes.StakingHooks = Hooks{} var _ swaptypes.SwapHooks = Hooks{} +var _ savingstypes.SavingsHooks = Hooks{} // Hooks create new incentive hooks func (k Keeper) Hooks() Hooks { return Hooks{k} } @@ -161,3 +163,15 @@ func (h Hooks) AfterPoolDepositCreated(ctx sdk.Context, poolID string, depositor func (h Hooks) BeforePoolDepositModified(ctx sdk.Context, poolID string, depositor sdk.AccAddress, sharesOwned sdk.Int) { h.k.SynchronizeSwapReward(ctx, poolID, depositor, sharesOwned) } + +// ------------------- Savings Module Hooks ------------------- + +// AfterSavingsDepositCreated function that runs after a deposit is created +func (h Hooks) AfterSavingsDepositCreated(ctx sdk.Context, deposit savingstypes.Deposit) { + // TODO: InitializeSavingsReward +} + +// BeforeSavingsDepositModified function that runs before a deposit is modified +func (h Hooks) BeforeSavingsDepositModified(ctx sdk.Context, deposit savingstypes.Deposit, incomingDenoms []string) { + // TODO: SynchronizeSavingsReward +} diff --git a/x/savings/keeper/hooks.go b/x/savings/keeper/hooks.go new file mode 100644 index 00000000..9bc577de --- /dev/null +++ b/x/savings/keeper/hooks.go @@ -0,0 +1,24 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/kava-labs/kava/x/savings/types" +) + +// Implements StakingHooks interface +var _ types.SavingsHooks = Keeper{} + +// AfterSavingsDepositCreated - call hook if registered +func (k Keeper) AfterSavingsDepositCreated(ctx sdk.Context, deposit types.Deposit) { + if k.hooks != nil { + k.hooks.AfterSavingsDepositCreated(ctx, deposit) + } +} + +// BeforeSavingsDepositModified - call hook if registered +func (k Keeper) BeforeSavingsDepositModified(ctx sdk.Context, deposit types.Deposit, incomingDenoms []string) { + if k.hooks != nil { + k.hooks.BeforeSavingsDepositModified(ctx, deposit, incomingDenoms) + } +} diff --git a/x/savings/keeper/keeper.go b/x/savings/keeper/keeper.go index be786f40..32a1e893 100644 --- a/x/savings/keeper/keeper.go +++ b/x/savings/keeper/keeper.go @@ -20,6 +20,7 @@ type Keeper struct { paramSubspace paramtypes.Subspace accountKeeper types.AccountKeeper bankKeeper types.BankKeeper + hooks types.SavingsHooks } // NewKeeper returns a new keeper for the savings module. @@ -37,9 +38,19 @@ func NewKeeper( paramSubspace: paramstore, accountKeeper: ak, bankKeeper: bk, + hooks: nil, } } +// SetHooks adds hooks to the keeper. +func (k *Keeper) SetHooks(hooks types.MultiSavingsHooks) *Keeper { + if k.hooks != nil { + panic("cannot set savings hooks twice") + } + k.hooks = hooks + return k +} + // GetSavingsModuleAccount returns the savings ModuleAccount func (k Keeper) GetSavingsModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI { return k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName) diff --git a/x/savings/types/expected_keepers.go b/x/savings/types/expected_keepers.go index 201bc877..f0f218d2 100644 --- a/x/savings/types/expected_keepers.go +++ b/x/savings/types/expected_keepers.go @@ -25,3 +25,9 @@ type AccountKeeper interface { GetModuleAddress(name string) sdk.AccAddress GetModuleAccount(ctx sdk.Context, name string) authtypes.ModuleAccountI } + +// SavingsHooks event hooks for other keepers to run code in response to Savings modifications +type SavingsHooks interface { + AfterSavingsDepositCreated(ctx sdk.Context, deposit Deposit) + BeforeSavingsDepositModified(ctx sdk.Context, deposit Deposit, incomingDenoms []string) +} diff --git a/x/savings/types/hooks.go b/x/savings/types/hooks.go new file mode 100644 index 00000000..b4441b59 --- /dev/null +++ b/x/savings/types/hooks.go @@ -0,0 +1,25 @@ +package types + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// MultiSavingsHooks combine multiple Savings hooks, all hook functions are run in array sequence +type MultiSavingsHooks []SavingsHooks + +// NewMultiSavingsHooks returns a new MultiSavingsHooks +func NewMultiSavingsHooks(hooks ...SavingsHooks) MultiSavingsHooks { + return hooks +} + +// AfterSavingsDepositCreated runs after a deposit is created +func (s MultiSavingsHooks) AfterSavingsDepositCreated(ctx sdk.Context, deposit Deposit) { + for i := range s { + s[i].AfterSavingsDepositCreated(ctx, deposit) + } +} + +// BeforeSavingsDepositModified runs before a deposit is modified +func (s MultiSavingsHooks) BeforeSavingsDepositModified(ctx sdk.Context, deposit Deposit, incomingDenoms []string) { + for i := range s { + s[i].BeforeSavingsDepositModified(ctx, deposit, incomingDenoms) + } +}