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
This commit is contained in:
Denali Marsh 2022-03-31 11:07:19 +02:00 committed by GitHub
parent 60489c10a1
commit 9170090f67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 1 deletions

View File

@ -537,7 +537,7 @@ func NewApp(
app.stakingKeeper, app.stakingKeeper,
&swapKeeper, &swapKeeper,
) )
app.savingsKeeper = savingskeeper.NewKeeper( savingsKeeper := savingskeeper.NewKeeper(
appCodec, appCodec,
keys[savingstypes.StoreKey], keys[savingstypes.StoreKey],
savingsSubspace, savingsSubspace,
@ -570,6 +570,7 @@ func NewApp(
app.swapKeeper = *swapKeeper.SetHooks(app.incentiveKeeper.Hooks()) app.swapKeeper = *swapKeeper.SetHooks(app.incentiveKeeper.Hooks())
app.cdpKeeper = *cdpKeeper.SetHooks(cdptypes.NewMultiCDPHooks(app.incentiveKeeper.Hooks())) app.cdpKeeper = *cdpKeeper.SetHooks(cdptypes.NewMultiCDPHooks(app.incentiveKeeper.Hooks()))
app.hardKeeper = *hardKeeper.SetHooks(hardtypes.NewMultiHARDHooks(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 // create the module manager (Note: Any module instantiated in the module manager that is later modified
// must be passed by reference here.) // must be passed by reference here.)

View File

@ -6,6 +6,7 @@ import (
cdptypes "github.com/kava-labs/kava/x/cdp/types" cdptypes "github.com/kava-labs/kava/x/cdp/types"
hardtypes "github.com/kava-labs/kava/x/hard/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" swaptypes "github.com/kava-labs/kava/x/swap/types"
) )
@ -18,6 +19,7 @@ var _ cdptypes.CDPHooks = Hooks{}
var _ hardtypes.HARDHooks = Hooks{} var _ hardtypes.HARDHooks = Hooks{}
var _ stakingtypes.StakingHooks = Hooks{} var _ stakingtypes.StakingHooks = Hooks{}
var _ swaptypes.SwapHooks = Hooks{} var _ swaptypes.SwapHooks = Hooks{}
var _ savingstypes.SavingsHooks = Hooks{}
// Hooks create new incentive hooks // Hooks create new incentive hooks
func (k Keeper) Hooks() Hooks { return Hooks{k} } 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) { func (h Hooks) BeforePoolDepositModified(ctx sdk.Context, poolID string, depositor sdk.AccAddress, sharesOwned sdk.Int) {
h.k.SynchronizeSwapReward(ctx, poolID, depositor, sharesOwned) 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
}

24
x/savings/keeper/hooks.go Normal file
View File

@ -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)
}
}

View File

@ -20,6 +20,7 @@ type Keeper struct {
paramSubspace paramtypes.Subspace paramSubspace paramtypes.Subspace
accountKeeper types.AccountKeeper accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper bankKeeper types.BankKeeper
hooks types.SavingsHooks
} }
// NewKeeper returns a new keeper for the savings module. // NewKeeper returns a new keeper for the savings module.
@ -37,9 +38,19 @@ func NewKeeper(
paramSubspace: paramstore, paramSubspace: paramstore,
accountKeeper: ak, accountKeeper: ak,
bankKeeper: bk, 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 // GetSavingsModuleAccount returns the savings ModuleAccount
func (k Keeper) GetSavingsModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI { func (k Keeper) GetSavingsModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName) return k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)

View File

@ -25,3 +25,9 @@ type AccountKeeper interface {
GetModuleAddress(name string) sdk.AccAddress GetModuleAddress(name string) sdk.AccAddress
GetModuleAccount(ctx sdk.Context, name string) authtypes.ModuleAccountI 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)
}

25
x/savings/types/hooks.go Normal file
View File

@ -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)
}
}