-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modification to not allow staking on unvested tokens (#938)
* add to ante handler * add more types * update * update * fixes --------- Co-authored-by: Cosmic Vagabond <[email protected]>
- Loading branch information
1 parent
adb40b8
commit 804c5ce
Showing
2 changed files
with
58 additions
and
0 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,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 | ||
} |