Skip to content

Commit

Permalink
[Commitment]: Do not allow vested tokens to be staked (#965)
Browse files Browse the repository at this point in the history
not allow vested tokens to be staked
  • Loading branch information
amityadav0 authored Nov 20, 2024
1 parent c423e71 commit 1dc7e6e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func NewAppKeeper(
app.CommitmentKeeper = commitmentmodulekeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(app.keys[commitmentmoduletypes.StoreKey]),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
app.AssetprofileKeeper,
Expand Down
4 changes: 3 additions & 1 deletion testutil/keeper/commitment.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"testing"

"cosmossdk.io/store/metrics"
"github.com/cosmos/cosmos-sdk/runtime"
"testing"

"cosmossdk.io/log"
"cosmossdk.io/store"
Expand Down Expand Up @@ -39,6 +40,7 @@ func CommitmentKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
nil,
govAddress.String(),
)

Expand Down
6 changes: 5 additions & 1 deletion x/commitment/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package keeper

import (
"context"
"cosmossdk.io/core/store"
"fmt"

"cosmossdk.io/core/store"

"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -33,6 +34,7 @@ type (
storeService store.KVStoreService
hooks types.CommitmentHooks

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
assetProfileKeeper types.AssetProfileKeeper
Expand All @@ -43,6 +45,7 @@ type (
func NewKeeper(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
stakingKeeper types.StakingKeeper,
assetProfileKeeper types.AssetProfileKeeper,
Expand All @@ -52,6 +55,7 @@ func NewKeeper(
cdc: cdc,
storeService: storeService,

accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
assetProfileKeeper: assetProfileKeeper,
Expand Down
13 changes: 13 additions & 0 deletions x/commitment/keeper/msg_server_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/elys-network/elys/x/commitment/types"
Expand Down Expand Up @@ -56,6 +58,17 @@ func (k msgServer) performStakeElys(ctx sdk.Context, msg *types.MsgStake) error
if !amount.IsValid() || amount.Amount.IsZero() {
return fmt.Errorf("invalid amount")
}

// Don't allow vested tokens to be staked
// Retrieve the delegator account
delegatorAcc := k.accountKeeper.GetAccount(ctx, address)
if _, ok := delegatorAcc.(banktypes.VestingAccount); ok {
spendableCoins := k.bankKeeper.SpendableCoins(ctx, address)
if msg.Amount.GT(spendableCoins.AmountOf(msg.Asset)) {
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "cannot delegate vested tokens")
}
}

msgMsgDelegate := stakingtypes.NewMsgDelegate(address.String(), validator_address.String(), amount)

if _, err := msgServer.Delegate(ctx, msgMsgDelegate); err != nil { // Discard the response because it's empty
Expand Down

0 comments on commit 1dc7e6e

Please sign in to comment.