Skip to content

Commit

Permalink
wip: bank hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Aug 31, 2023
1 parent 308a104 commit 8438c9d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
12 changes: 9 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ import (
tokenfactorykeeper "github.com/terra-money/core/v2/x/tokenfactory/keeper"
tokenfactorytypes "github.com/terra-money/core/v2/x/tokenfactory/types"

bankkeeper "github.com/terra-money/alliance/custom/bank/keeper"
"github.com/terra-money/alliance/x/alliance"
allianceclient "github.com/terra-money/alliance/x/alliance/client"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
alliancetypes "github.com/terra-money/alliance/x/alliance/types"
terracustombank "github.com/terra-money/core/v2/custom/bank"
custombankkeeper "github.com/terra-money/core/v2/custom/bank/keeper"

pobabci "github.com/skip-mev/pob/abci"
pobmempool "github.com/skip-mev/pob/mempool"
Expand Down Expand Up @@ -315,7 +315,7 @@ type TerraApp struct {

// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
BankKeeper custombankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
Expand Down Expand Up @@ -451,7 +451,7 @@ func NewTerraApp(
terraappconfig.AccountAddressPrefix,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.BankKeeper = bankkeeper.NewBaseKeeper(
app.BankKeeper = custombankkeeper.NewBaseKeeper(
appCodec,
keys[banktypes.StoreKey],
app.AccountKeeper,
Expand Down Expand Up @@ -544,6 +544,12 @@ func NewTerraApp(
appCodec,
)

app.BankKeeper.SetHooks(
custombankkeeper.NewMultiBankHooks(
app.TokenFactoryKeeper.Hooks(),
),
)

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
Expand Down
33 changes: 29 additions & 4 deletions custom/bank/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,49 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
customterratypes "github.com/terra-money/core/v2/custom/bank/types"
)

// Implements StakingHooks interface
var _ customterratypes.BankHooks = types.BaseSendKeeper{}
var _ customterratypes.BankHooks = Keeper{}

// TrackBeforeSend executes the TrackBeforeSend hook if registered.
func (k types.BaseSendKeeper) TrackBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) {
func (k Keeper) TrackBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) {
if k.hooks != nil {
k.hooks.TrackBeforeSend(ctx, from, to, amount)
}
}

// BlockBeforeSend executes the BlockBeforeSend hook if registered.
func (k types.BaseSendKeeper) BlockBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) error {
func (k Keeper) BlockBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) error {
if k.hooks != nil {
return k.hooks.BlockBeforeSend(ctx, from, to, amount)
}
return nil
}

// MultiBankHooks combine multiple bank hooks, all hook functions are run in array sequence
type MultiBankHooks []customterratypes.BankHooks

// NewMultiBankHooks takes a list of BankHooks and returns a MultiBankHooks
func NewMultiBankHooks(hooks ...customterratypes.BankHooks) MultiBankHooks {
return hooks
}

// TrackBeforeSend runs the TrackBeforeSend hooks in order for each BankHook in a MultiBankHooks struct
func (h MultiBankHooks) TrackBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) {
for i := range h {
h[i].TrackBeforeSend(ctx, from, to, amount)
}
}

// BlockBeforeSend runs the BlockBeforeSend hooks in order for each BankHook in a MultiBankHooks struct
func (h MultiBankHooks) BlockBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) error {
for i := range h {
err := h[i].BlockBeforeSend(ctx, from, to, amount)
if err != nil {
return err
}
}
return nil
}
25 changes: 25 additions & 0 deletions custom/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

Expand Down Expand Up @@ -31,3 +32,27 @@ func NewBaseKeeper(

return keeper
}

// Set the bank hooks
func (k *Keeper) SetHooks(bh customterratypes.BankHooks) *Keeper {
if k.hooks != nil {
panic("cannot set bank hooks twice")
}

k.hooks = bh

return k
}

// SendCoins transfers amt coins from a sending account to a receiving account.
// An error is returned upon failure.
func (k Keeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
// BlockBeforeSend hook should always be called before the TrackBeforeSend hook.
err := k.BlockBeforeSend(ctx, fromAddr, toAddr, amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, fromAddr, toAddr, amt)

return k.Keeper.BaseKeeper.SendCoins(ctx, fromAddr, toAddr, amt)
}
32 changes: 2 additions & 30 deletions custom/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,25 @@ package bank

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/exported"
"github.com/cosmos/cosmos-sdk/x/bank/types"

customalliancemod "github.com/terra-money/alliance/custom/bank"
custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper"
customtypes "github.com/terra-money/core/v2/custom/bank/types"
custombankkeeper "github.com/terra-money/core/v2/custom/bank/keeper"
)

// AppModule wraps around the bank module and the bank keeper to return the right total supply ignoring bonded tokens
// that the alliance module minted to rebalance the voting power
// It modifies the TotalSupply and SupplyOf GRPC queries
type AppModule struct {
customalliancemod.AppModule
hooks customtypes.BankHooks
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper custombankkeeper.Keeper, accountKeeper types.AccountKeeper, ss exported.Subspace) AppModule {
mod := customalliancemod.NewAppModule(cdc, keeper, accountKeeper, ss)
mod := customalliancemod.NewAppModule(cdc, keeper.Keeper, accountKeeper, ss)

return AppModule{
AppModule: mod,
hooks: nil,
}
}

// Set the bank hooks
func (am *AppModule) SetHooks(bh customtypes.BankHooks) *AppModule {
if am.hooks != nil {
panic("cannot set bank hooks twice")
}

am.hooks = bh

return am
}

// SendCoins transfers amt coins from a sending account to a receiving account.
// An error is returned upon failure.
func (am AppModule) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
// BlockBeforeSend hook should always be called before the TrackBeforeSend hook.
err := am.hooks.BlockBeforeSend(ctx, fromAddr, toAddr, amt)
if err != nil {
return err
}
am.hooks.TrackBeforeSend(ctx, fromAddr, toAddr, amt)

return am.SendCoins(ctx, fromAddr, toAddr, amt)
}
9 changes: 5 additions & 4 deletions x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cometbft/cometbft/libs/log"
Expand All @@ -10,9 +11,9 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/terra-money/core/v2/x/tokenfactory/types"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
customtypes "github.com/terra-money/core/v2/custom/bank/keeper"
"github.com/terra-money/core/v2/x/tokenfactory/types"
)

type (
Expand All @@ -22,7 +23,7 @@ type (
paramSpace paramtypes.Subspace

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
bankKeeper customtypes.Keeper
contractKeeper types.ContractKeeper

communityPoolKeeper types.CommunityPoolKeeper
Expand All @@ -36,7 +37,7 @@ func NewKeeper(
storeKey storetypes.StoreKey,
paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
bankKeeper customtypes.Keeper,
communityPoolKeeper types.CommunityPoolKeeper,
cdc codec.BinaryCodec,
) Keeper {
Expand Down

0 comments on commit 8438c9d

Please sign in to comment.