-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from terra-money/feat/bank/hooks
feat: bank hooks
- Loading branch information
Showing
7 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
customterratypes "github.com/terra-money/core/v2/custom/bank/types" | ||
) | ||
|
||
// Implements StakingHooks interface | ||
var _ customterratypes.BankHooks = Keeper{} | ||
|
||
// TrackBeforeSend executes the TrackBeforeSend hook if registered. | ||
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 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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" | ||
|
||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
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 | ||
ak accountkeeper.AccountKeeper | ||
} | ||
|
||
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, | ||
ak: ak, | ||
} | ||
|
||
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.SendCoins(ctx, fromAddr, toAddr, amt) | ||
} | ||
|
||
// SendCoinsFromModuleToManyAccounts transfers coins from a ModuleAccount to multiple AccAddresses. | ||
// It will panic if the module account does not exist. An error is returned if | ||
// the recipient address is black-listed or if sending the tokens fails. | ||
func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error { | ||
senderAddr := k.ak.GetModuleAddress(senderModule) | ||
if senderAddr == nil { | ||
panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", senderModule)) | ||
} | ||
|
||
recipientAcc := k.ak.GetModuleAccount(ctx, recipientModule) | ||
if recipientAcc == nil { | ||
panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", recipientModule)) | ||
} | ||
|
||
return k.Keeper.SendCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package bank | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"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/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 | ||
} | ||
|
||
// 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.Keeper, accountKeeper, ss) | ||
|
||
return AppModule{ | ||
AppModule: mod, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters