Skip to content

Commit

Permalink
Modification to not allow staking on unvested tokens (#938)
Browse files Browse the repository at this point in the history
* add to ante handler

* add more types

* update

* update

* fixes

---------

Co-authored-by: Cosmic Vagabond <[email protected]>
  • Loading branch information
amityadav0 and cosmic-vagabond authored Nov 15, 2024
1 parent adb40b8 commit 804c5ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
NewMinCommissionDecorator(options.Cdc, options.StakingKeeper, options.BankKeeper, options.ParameterKeeper),
NewVestedAnteHandlerDecorator(options.AccountKeeper, options.BankKeeper),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
Expand Down
57 changes: 57 additions & 0 deletions app/ante/vested.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// CustomAnteHandlerDecorator checks for the use of vested tokens in various operations
type VestedAnteHandlerDecorator struct {
ak authante.AccountKeeper
bk bankkeeper.Keeper
}

func NewVestedAnteHandlerDecorator(ak authante.AccountKeeper, bk bankkeeper.Keeper) VestedAnteHandlerDecorator {
return VestedAnteHandlerDecorator{ak: ak, bk: bk}
}

func (cad VestedAnteHandlerDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
// Iterate over all messages in the transaction
for _, m := range tx.GetMsgs() {
switch msg := m.(type) {
case *stakingtypes.MsgDelegate:
if err := cad.checkDelegation(ctx, msg); err != nil {
return ctx, err
}
}
}
return next(ctx, tx, simulate)
}

func (cad VestedAnteHandlerDecorator) checkDelegation(ctx sdk.Context, msg *stakingtypes.MsgDelegate) error {
// Retrieve the delegator account
delegatorAcc := cad.ak.GetAccount(ctx, sdk.MustAccAddressFromBech32(msg.DelegatorAddress))

// Check if the account is a vesting account
if _, ok := delegatorAcc.(types.VestingAccount); ok {
// Calculate the spendable coins
spendableCoins := cad.bk.SpendableCoins(ctx, sdk.MustAccAddressFromBech32(msg.DelegatorAddress))
// Ensure the amount to be delegated is less than or equal to the spendable coins
if msg.Amount.Amount.GT(spendableCoins.AmountOf(msg.Amount.Denom)) {
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "cannot delegate vested tokens")
}
}

// For non-vesting accounts, no additional checks are required
return nil
}

0 comments on commit 804c5ce

Please sign in to comment.