Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v0.45.16 send restriction #173

Merged
merged 9 commits into from
Sep 11, 2024
33 changes: 31 additions & 2 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type BaseKeeper struct {

type MintingRestrictionFn func(ctx sdk.Context, coins sdk.Coins) error

type SendRestrictionFn func(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error)

// GetPaginatedTotalSupply queries for the supply, ignoring 0 coins, with a given pagination
func (k BaseKeeper) GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -133,6 +135,23 @@ func (k BaseKeeper) WithMintCoinsRestriction(check MintingRestrictionFn) BaseKee
return k
}

// WithSendCoinsRestriction restricts the bank Keeper used within a specific module to
// have restricted permissions on sending via function passed in parameter.
// Previous restriction functions can be nested as such:
//
// bankKeeper.WithSendCoinsRestriction(restriction1).WithSendCoinsRestriction(restriction2)
func (k BaseKeeper) WithSendCoinsRestriction(check SendRestrictionFn) BaseKeeper {
oldRestrictionFn := k.sendCoinsRestrictionFn
dongsam marked this conversation as resolved.
Show resolved Hide resolved
k.sendCoinsRestrictionFn = func(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error) {
toAddr, err = check(ctx, fromAddr, toAddr, amt)
if err != nil {
return toAddr, err
}
return oldRestrictionFn(ctx, fromAddr, toAddr, amt)
}
return k
}

// DelegateCoins performs delegation by deducting amt coins from an account with
// address addr. For vesting accounts, delegations amounts are tracked for both
// vesting and vested coins. The coins are then transferred from the delegator
Expand All @@ -148,6 +167,11 @@ func (k BaseKeeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}

moduleAccAddr, err := k.sendCoinsRestrictionFn(ctx, delegatorAddr, moduleAccAddr, amt)
if err != nil {
return err
}

balances := sdk.NewCoins()

for _, coin := range amt {
Expand All @@ -173,7 +197,7 @@ func (k BaseKeeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr
types.NewCoinSpentEvent(delegatorAddr, amt),
)

err := k.addCoins(ctx, moduleAccAddr, amt)
err = k.addCoins(ctx, moduleAccAddr, amt)
if err != nil {
return err
}
Expand All @@ -196,7 +220,12 @@ func (k BaseKeeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAdd
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}

err := k.subUnlockedCoins(ctx, moduleAccAddr, amt)
delegatorAddr, err := k.sendCoinsRestrictionFn(ctx, moduleAccAddr, delegatorAddr, amt)
if err != nil {
return err
}

err = k.subUnlockedCoins(ctx, moduleAccAddr, amt)
if err != nil {
return err
}
Expand Down
Loading
Loading