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 30, 2023
1 parent e58aa1d commit 308a104
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
4 changes: 2 additions & 2 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"

alliancebank "github.com/terra-money/alliance/custom/bank"
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"

pobabci "github.com/skip-mev/pob/abci"
pobmempool "github.com/skip-mev/pob/mempool"
Expand Down Expand Up @@ -735,7 +735,7 @@ func NewTerraApp(
),
auth.NewAppModule(appCodec, app.AccountKeeper, nil, app.GetSubspace(authtypes.ModuleName)),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.StakingKeeper),
alliancebank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(alliancetypes.ModuleName)),
terracustombank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(alliancetypes.ModuleName)),
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
Expand Down
25 changes: 25 additions & 0 deletions custom/bank/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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{}

// TrackBeforeSend executes the TrackBeforeSend hook if registered.
func (k types.BaseSendKeeper) 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 {
if k.hooks != nil {
return k.hooks.BlockBeforeSend(ctx, from, to, amount)
}
return nil
}
33 changes: 33 additions & 0 deletions custom/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

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

type Keeper struct {
custombankkeeper.Keeper
hooks customterratypes.BankHooks
}

var _ bankkeeper.Keeper = Keeper{}

func NewBaseKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ak accountkeeper.AccountKeeper,
blockedAddrs map[string]bool,
authority string,
) Keeper {
keeper := Keeper{
Keeper: custombankkeeper.NewBaseKeeper(cdc, storeKey, ak, blockedAddrs, authority),
hooks: nil,
}

return keeper
}
54 changes: 54 additions & 0 deletions custom/bank/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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"
)

// 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)

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)
}
17 changes: 17 additions & 0 deletions custom/bank/types/expected_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Event Hooks
// These can be utilized to communicate between a bank keeper and another
// keeper which must take particular actions when sends happen.
// The second keeper must implement this interface, which then the
// bank keeper can call.

// BankHooks event hooks for bank sends
type BankHooks interface {
TrackBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) // Must be before any send is executed
BlockBeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) error // Must be before any send is executed
}

0 comments on commit 308a104

Please sign in to comment.