Skip to content

Commit

Permalink
Merge pull request #301 from ComposableFi/configure-staking-middlewar…
Browse files Browse the repository at this point in the history
…e-module-into-cosmos-chain

integrate staking middleware module into cosmos chain
  • Loading branch information
RustNinja authored Dec 17, 2023
2 parents 15b1c49 + 1d19380 commit 3e82118
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 19 deletions.
7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import (
custombankmodule "github.com/notional-labs/composable/v6/custom/bank"

"github.com/notional-labs/composable/v6/app/ante"
"github.com/notional-labs/composable/v6/x/stakingmiddleware"
transfermiddleware "github.com/notional-labs/composable/v6/x/transfermiddleware"
transfermiddlewaretypes "github.com/notional-labs/composable/v6/x/transfermiddleware/types"

Expand All @@ -125,6 +126,7 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

upgrades "github.com/notional-labs/composable/v6/app/upgrades"
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

const (
Expand Down Expand Up @@ -219,6 +221,7 @@ var (
txBoundary.AppModuleBasic{},
ratelimitmodule.AppModuleBasic{},
consensus.AppModuleBasic{},
stakingmiddleware.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

Expand Down Expand Up @@ -359,6 +362,7 @@ func NewComposableApp(
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName)),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
customstaking.NewAppModule(appCodec, *&app.CustomStakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
stakingmiddleware.NewAppModule(appCodec, app.StakingMiddlewareKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
Expand Down Expand Up @@ -412,6 +416,7 @@ func NewComposableApp(
wasm08types.ModuleName,
icatypes.ModuleName,
wasm.ModuleName,
stakingmiddlewaretypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/beginBlockers
)

Expand Down Expand Up @@ -445,6 +450,7 @@ func NewComposableApp(
wasm08types.ModuleName,
icatypes.ModuleName,
wasm.ModuleName,
stakingmiddlewaretypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -482,6 +488,7 @@ func NewComposableApp(
wasm08types.ModuleName,
icatypes.ModuleName,
wasm.ModuleName,
stakingmiddlewaretypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/initGenesis
)

Expand Down
5 changes: 5 additions & 0 deletions 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"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

const (
Expand Down Expand Up @@ -150,6 +152,7 @@ type AppKeepers struct {
TxBoundaryKeepper txBoundaryKeeper.Keeper
RouterKeeper *routerkeeper.Keeper
RatelimitKeeper ratelimitmodulekeeper.Keeper
StakingMiddlewareKeeper stakingmiddleware.Keeper
}

// InitNormalKeepers initializes all 'normal' keepers.
Expand Down Expand Up @@ -194,6 +197,8 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appCodec /*appKeepers.keys[stakingtypes.StoreKey],*/, *appKeepers.StakingKeeper, appKeepers.AccountKeeper, &appKeepers.MintKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())

appKeepers.DistrKeeper = distrkeeper.NewKeeper(
appCodec, appKeepers.keys[distrtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper,
appKeepers.StakingKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
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,7 +41,9 @@ 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"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

// GenerateKeys generates new keys (KV Store, Transient store, and memory store).
Expand All @@ -53,7 +55,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, /*customstakingtypes.StoreKey,*/
authzkeeper.StoreKey, stakingmiddleware.StoreKey, /*customstakingtypes.StoreKey,*/
)

// Define transient store keys
Expand Down
1 change: 1 addition & 0 deletions proto/centauri/stakingmiddleware/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ service Msg {
//
// Since: cosmos-sdk 0.47
message MsgSetPower {
string from_address = 1;
}

// MsgSetPowerResponse defines the response structure for executing a
Expand Down
27 changes: 25 additions & 2 deletions x/stakingmiddleware/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"

"github.com/notional-labs/composable/v6/x/mint/types"
"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

// GetTxCmd returns the tx commands for mint
Expand All @@ -17,7 +19,28 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

txCmd.AddCommand()
txCmd.AddCommand(
GetCmdSetPower(),
)

return txCmd
}

func GetCmdSetPower() *cobra.Command {
cmd := &cobra.Command{
Use: "fund [amount]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgSetPower(clientCtx.GetFromAddress())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)

return cmd
}
11 changes: 6 additions & 5 deletions x/stakingmiddleware/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stakingmiddleware

import (
"context"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -42,12 +43,12 @@ func (AppModuleBasic) Name() string {

// RegisterLegacyAminoCodec registers the mint module's types on the given LegacyAmino codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
// types.RegisterLegacyAminoCodec(cdc)
types.RegisterLegacyAminoCodec(cdc)
}

// RegisterInterfaces registers the module interface
func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
// types.RegisterInterfaces(reg)
types.RegisterInterfaces(reg)
}

// DefaultGenesis returns default genesis state as raw bytes for the mint
Expand All @@ -68,9 +69,9 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingCo

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the mint module.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
// if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
// panic(err)
// }
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}

// GetTxCmd returns no root tx command for the mint module.
Expand Down
45 changes: 45 additions & 0 deletions x/stakingmiddleware/types/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
groupcodec "github.com/cosmos/cosmos-sdk/x/group/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgSetPower{}, "composable/MsgSetPower")
}

func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgSetPower{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be
// used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances
RegisterLegacyAminoCodec(authzcodec.Amino)
RegisterLegacyAminoCodec(govcodec.Amino)
RegisterLegacyAminoCodec(groupcodec.Amino)
}
5 changes: 3 additions & 2 deletions x/stakingmiddleware/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var (

const (
// module name
ModuleName = "customstaking"
ModuleName = "stakingmiddleware"

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

StoreKey = "customstmiddleware" // not using the module name because of collisions with key "ibc"
)

// GetDelegationKey creates the key for delegator bond with validator
Expand Down
44 changes: 44 additions & 0 deletions x/stakingmiddleware/types/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package types

import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ sdk.Msg = &MsgSetPower{}

// Route Implements Msg.
func (m MsgSetPower) Route() string { return sdk.MsgTypeURL(&m) }

// Type Implements Msg.
func (m MsgSetPower) Type() string { return sdk.MsgTypeURL(&m) }

// GetSigners returns the expected signers for a MsgMintAndAllocateExp .
func (m MsgSetPower) GetSigners() []sdk.AccAddress {
daoAccount, err := sdk.AccAddressFromBech32(m.FromAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{daoAccount}
}

// GetSignBytes Implements Msg.
func (m MsgSetPower) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&m))
}

// ValidateBasic does a sanity check on the provided data.
func (m MsgSetPower) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.FromAddress)
if err != nil {
return errorsmod.Wrap(err, "from address must be valid address")
}
return nil
}

func NewMsgSetPower(fromAddr sdk.AccAddress) *MsgSetPower {
return &MsgSetPower{
FromAddress: fromAddr.String(),
}
}
Loading

0 comments on commit 3e82118

Please sign in to comment.