Skip to content

Commit

Permalink
Merge pull request #285 from ComposableFi/reimplement-delegate-msg-se…
Browse files Browse the repository at this point in the history
…rver-func

Reimplement delegate msg server func
  • Loading branch information
RustNinja authored Dec 13, 2023
2 parents 280c9c7 + b2ae814 commit b3660a1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 22 deletions.
4 changes: 3 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ import (
ibc_hooks "github.com/notional-labs/composable/v6/x/ibc-hooks"
ibchookskeeper "github.com/notional-labs/composable/v6/x/ibc-hooks/keeper"
ibchookstypes "github.com/notional-labs/composable/v6/x/ibc-hooks/types"

customstakingtypes "github.com/notional-labs/composable/v6/custom/staking/types"
)

const (
Expand Down Expand Up @@ -186,7 +188,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
)

appKeepers.CustomStakingKeeper = customstaking.NewBaseKeeper2(
*appKeepers.StakingKeeper, appKeepers.AccountKeeper,
appCodec, appKeepers.keys[customstakingtypes.StoreKey], *appKeepers.StakingKeeper, appKeepers.AccountKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

appKeepers.MintKeeper = mintkeeper.NewKeeper(
Expand Down
4 changes: 3 additions & 1 deletion app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (

"github.com/CosmWasm/wasmd/x/wasm"
wasm08types "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"

customstakingtypes "github.com/notional-labs/composable/v6/custom/staking/types"
)

// GenerateKeys generates new keys (KV Store, Transient store, and memory store).
Expand All @@ -52,7 +54,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, icqtypes.StoreKey, capabilitytypes.StoreKey, consensusparamtypes.StoreKey, wasm08types.StoreKey,
crisistypes.StoreKey, routertypes.StoreKey, transfermiddlewaretypes.StoreKey, group.StoreKey, minttypes.StoreKey, wasm.StoreKey, ibchookstypes.StoreKey, icahosttypes.StoreKey, ratelimitmoduletypes.StoreKey, txBoundaryTypes.StoreKey,
authzkeeper.StoreKey,
authzkeeper.StoreKey, customstakingtypes.StoreKey,
)

// Define transient store keys
Expand Down
55 changes: 36 additions & 19 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,62 @@ package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"

customstakingtypes "github.com/notional-labs/composable/v6/custom/staking/types"
)

type Keeper struct {
stakingkeeper.Keeper
acck accountkeeper.AccountKeeper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
acck accountkeeper.AccountKeeper
authority string
}

var _ stakingkeeper.Keeper = stakingkeeper.Keeper{} //???

Check failure on line 22 in custom/staking/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

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

func NewBaseKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
ak types.AccountKeeper,
acck accountkeeper.AccountKeeper,
bk bankkeeper.Keeper,
authority string,
) Keeper {
keeper := Keeper{
Keeper: *stakingkeeper.NewKeeper(cdc, key, ak, bk, authority),
acck: acck,
}
return keeper
}
// func NewBaseKeeper(
// cdc codec.BinaryCodec,
// key storetypes.StoreKey,
// ak types.AccountKeeper,
// acck accountkeeper.AccountKeeper,
// bk bankkeeper.Keeper,
// authority string,
// ) Keeper {
// keeper := Keeper{
// Keeper: *stakingkeeper.NewKeeper(cdc, key, ak, bk, authority),
// acck: acck,
// }
// return keeper
// }

func NewBaseKeeper2(
cdc codec.BinaryCodec,
keys storetypes.StoreKey,
staking stakingkeeper.Keeper,
acck accountkeeper.AccountKeeper,
authority string,
) Keeper {
keeper := Keeper{
Keeper: staking,
acck: acck,
Keeper: staking,
acck: acck,
authority: authority,
}
return keeper
}

// func (k *Keeper) RegisterKeepers(akk banktypes.StakingKeeper) {
// k.acck = sk
// }

func (k Keeper) StoreDelegation(ctx sdk.Context, delegation types.Delegation) {
delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

store := ctx.KVStore(k.storeKey)
b := types.MustMarshalDelegation(k.cdc, delegation)
store.Set(customstakingtypes.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr()), b)
}
23 changes: 22 additions & 1 deletion custom/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand All @@ -27,7 +29,26 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
}

func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
return k.msgServer.Delegate(goCtx, msg)

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

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
ctx := sdk.UnwrapSDKContext(goCtx)

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

Check failure on line 37 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,
)
}

delegation := types.Delegation{
DelegatorAddress: msg.DelegatorAddress,
ValidatorAddress: msg.ValidatorAddress,
Shares: msg.Amount.Amount.ToLegacyDec(),
}
k.StoreDelegation(ctx, delegation)

return &types.MsgDelegateResponse{}, nil
// return nil, fmt.Errorf("My custom error: Nikita")
// return k.msgServer.Delegate(goCtx, msg)
}

func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) {
Expand Down
39 changes: 39 additions & 0 deletions custom/staking/types/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)

// MinterKey is the key to use for the keeper store.
var (
DelegationKey = []byte{0x37} // key for a delegation
)

const (
// module name
ModuleName = "customstaking"

// StoreKey is the default store key for mint
StoreKey = ModuleName

// QuerierRoute is the querier route for the minting store.
QuerierRoute = StoreKey

// // Query endpoints supported by the minting querier
// QueryParameters = "parameters"
// QueryInflation = "inflation"
// QueryAnnualProvisions = "annual_provisions"
)

var ()

Check failure on line 29 in custom/staking/types/keys.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

// GetDelegationKey creates the key for delegator bond with validator
// VALUE: staking/Delegation
func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
return append(GetDelegationsKey(delAddr), address.MustLengthPrefix(valAddr)...)
}

func GetDelegationsKey(delAddr sdk.AccAddress) []byte {
return append(DelegationKey, address.MustLengthPrefix(delAddr)...)
}

0 comments on commit b3660a1

Please sign in to comment.