Skip to content

Commit

Permalink
Merge pull request #314 from ComposableFi/override-end-blocker-custom…
Browse files Browse the repository at this point in the history
…-staking

override end block custom staking module implementation
  • Loading branch information
RustNinja authored Dec 20, 2023
2 parents 78e50e1 + a93ec9f commit 81a2901
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
4 changes: 2 additions & 2 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Keeper struct {
cdc codec.BinaryCodec
acck accountkeeper.AccountKeeper
mintkeeper *mintkeeper.Keeper
stakingmiddleware *stakingmiddleware.Keeper
Stakingmiddleware *stakingmiddleware.Keeper
authority string
}

Expand Down Expand Up @@ -46,7 +46,7 @@ func NewKeeper(
acck: acck,
authority: authority,
mintkeeper: mintkeeper,
stakingmiddleware: stakingmiddleware,
Stakingmiddleware: stakingmiddleware,
cdc: cdc,
}
return keeper
Expand Down
33 changes: 14 additions & 19 deletions custom/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand Down Expand Up @@ -32,28 +31,24 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// bondDenom := k.BondDenom(ctx)
// if msg.Amount.Denom != bondDenom {
bondDenom := k.BondDenom(ctx)
if msg.Amount.Denom != bondDenom {
return nil, sdkerrors.Wrapf(

Check failure on line 36 in custom/staking/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Amount.Denom, bondDenom,
)
}

// k.mintkeeper.SetLastTotalPower(ctx, math.Int{})
// k.stakingmiddleware.SetLastTotalPower(ctx, math.Int{})

// delegations := k.Stakingmiddleware.DequeueAllDelegation(ctx)
// if len(delegations) > 2 {
// return nil, sdkerrors.Wrapf(
// sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Amount.Denom, bondDenom,
// sdkerrors.ErrInvalidRequest, "should always be less then X : got %s, expected %s", len(delegations), 1,
// )
// }

// delegation := types.Delegation{
// DelegatorAddress: msg.DelegatorAddress,
// ValidatorAddress: msg.ValidatorAddress,
// Shares: msg.Amount.Amount.ToLegacyDec(),
// }
k.mintkeeper.SetLastTotalPower(ctx, math.Int{})
k.stakingmiddleware.SetLastTotalPower(ctx, math.Int{})

k.stakingmiddleware.SetDelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount)
delegations := k.stakingmiddleware.DequeueAllDelegation(ctx)
if len(delegations) >= 1 {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "should always be less then 2 : got %s, expected %s", len(delegations),
)
}
k.Stakingmiddleware.SetDelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount)

return &types.MsgDelegateResponse{}, nil
// return nil, fmt.Errorf("My custom error: Nikita")
Expand Down
36 changes: 36 additions & 0 deletions custom/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bank
import (
"fmt"

abcitype "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
stakingmodule "github.com/cosmos/cosmos-sdk/x/staking"
Expand All @@ -11,6 +12,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

// custombankkeeper "github.com/notional-labs/composable/v6/custom/bank/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
customstakingkeeper "github.com/notional-labs/composable/v6/custom/staking/keeper"
)

Expand Down Expand Up @@ -53,3 +55,37 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
panic(fmt.Sprintf("failed to migrate x/staking from version 3 to 4: %v", err))
}
}

// func (am AppModule) BeginBlock(ctx sdk.Context, _abc abcitype.RequestBeginBlock) {
// //Define the logic around the batching.
// am.AppModule.BeginBlock(ctx, _abc)
// }

func (am AppModule) EndBlock(ctx sdk.Context, _abc abcitype.RequestEndBlock) []abcitype.ValidatorUpdate {
//Define the logic around the batching.

Check failure on line 65 in custom/staking/module.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//TODO!!!

println("EndBlock Custom Staking Module")

delegations := am.keeper.Stakingmiddleware.DequeueAllDelegation(ctx)
println("Delegations: ", delegations)
println("Delegations len: ", len(delegations))
//for delegations print the delegator address and the validator address

Check failure on line 73 in custom/staking/module.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
for _, delegation := range delegations {
println("Delegator Address: ", delegation.DelegatorAddress)
println("Validator Address: ", delegation.ValidatorAddress)
fmt.Println("Amount", delegation.Amount.Amount)
}

// am.keeper.Delegate()
ctx.EventManager().EmitEvent(
sdk.NewEvent(
"DequeueAllDelegation",
// sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
// sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress),
// sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress),
),
)

return am.AppModule.EndBlock(ctx, _abc)
}

0 comments on commit 81a2901

Please sign in to comment.