Skip to content

Commit

Permalink
add missing wasmbinding on estaking
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Apr 17, 2024
1 parent f58f650 commit a9863e6
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ func NewElysApp(
&app.TokenomicsKeeper,
&app.TransferhookKeeper,
&app.MasterchefKeeper,
&app.EstakingKeeper,
),
wasmOpts...,
)
Expand Down
18 changes: 18 additions & 0 deletions wasmbindings/client/register_custom_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import (
commitmentkeeper "github.com/elys-network/elys/x/commitment/keeper"
epochsclientwasm "github.com/elys-network/elys/x/epochs/client/wasm"
epochskeeper "github.com/elys-network/elys/x/epochs/keeper"
estakingclientwasm "github.com/elys-network/elys/x/estaking/client/wasm"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"
incentiveclientwasm "github.com/elys-network/elys/x/incentive/client/wasm"
incentivekeeper "github.com/elys-network/elys/x/incentive/keeper"
leveragelpclientwasm "github.com/elys-network/elys/x/leveragelp/client/wasm"
leveragelpkeeper "github.com/elys-network/elys/x/leveragelp/keeper"
masterchefclientwasm "github.com/elys-network/elys/x/masterchef/client/wasm"
masterchefkeeper "github.com/elys-network/elys/x/masterchef/keeper"
oracleclientwasm "github.com/elys-network/elys/x/oracle/client/wasm"
oraclekeeper "github.com/elys-network/elys/x/oracle/keeper"
Expand Down Expand Up @@ -61,6 +64,7 @@ func RegisterCustomPlugins(
tokenomics *tokenomicskeeper.Keeper,
transferhook *transferhookkeeper.Keeper,
masterchef *masterchefkeeper.Keeper,
estaking *estakingkeeper.Keeper,
) []wasmkeeper.Option {
accountedpoolQuerier := accountedpoolclientwasm.NewQuerier(accountedpool)
accountedpoolMessenger := accountedpoolclientwasm.NewMessenger(accountedpool)
Expand Down Expand Up @@ -110,6 +114,12 @@ func RegisterCustomPlugins(
transferhookQuerier := transferhookclientwasm.NewQuerier(transferhook)
transferhookMessenger := transferhookclientwasm.NewMessenger(transferhook)

masterchefQuerier := masterchefclientwasm.NewQuerier(masterchef, staking)
masterchefMessenger := masterchefclientwasm.NewMessenger(masterchef, staking, commitment, parameter)

estakingQuerier := estakingclientwasm.NewQuerier(estaking)
estakingMessenger := estakingclientwasm.NewMessenger(estaking, parameter)

moduleQueriers := []types.ModuleQuerier{
accountedpoolQuerier,
ammQuerier,
Expand All @@ -127,6 +137,8 @@ func RegisterCustomPlugins(
stablestakeQuerier,
tokenomicsQuerier,
transferhookQuerier,
masterchefQuerier,
estakingQuerier,
}

wasmQueryPlugin := types.NewQueryPlugin(
Expand All @@ -149,6 +161,8 @@ func RegisterCustomPlugins(
staking,
tokenomics,
transferhook,
masterchef,
estaking,
)

queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Expand All @@ -172,6 +186,8 @@ func RegisterCustomPlugins(
stablestakeMessenger,
tokenomicsMessenger,
transferhookMessenger,
masterchefMessenger,
estakingMessenger,
}

messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator(
Expand All @@ -195,6 +211,8 @@ func RegisterCustomPlugins(
staking,
tokenomics,
transferhook,
masterchef,
estaking,
),
)
return []wasm.Option{
Expand Down
6 changes: 6 additions & 0 deletions wasmbindings/types/custom_message_decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
clockkeeper "github.com/elys-network/elys/x/clock/keeper"
commitmentkeeper "github.com/elys-network/elys/x/commitment/keeper"
epochskeeper "github.com/elys-network/elys/x/epochs/keeper"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"
incentivekeeper "github.com/elys-network/elys/x/incentive/keeper"
leveragelpkeeper "github.com/elys-network/elys/x/leveragelp/keeper"
masterchefkeeper "github.com/elys-network/elys/x/masterchef/keeper"
oraclekeeper "github.com/elys-network/elys/x/oracle/keeper"
parameterkeeper "github.com/elys-network/elys/x/parameter/keeper"
perpetualkeeper "github.com/elys-network/elys/x/perpetual/keeper"
Expand Down Expand Up @@ -42,6 +44,8 @@ func CustomMessageDecorator(
staking *stakingkeeper.Keeper,
tokenomics *tokenomicskeeper.Keeper,
transferhook *transferhookkeeper.Keeper,
masterchef *masterchefkeeper.Keeper,
estaking *estakingkeeper.Keeper,
) func(wasmkeeper.Messenger) wasmkeeper.Messenger {
return func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
return &CustomMessenger{
Expand All @@ -65,6 +69,8 @@ func CustomMessageDecorator(
staking: staking,
tokenomics: tokenomics,
transferhook: transferhook,
masterchef: masterchef,
estaking: estaking,
}
}
}
6 changes: 6 additions & 0 deletions wasmbindings/types/new_query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
clockkeeper "github.com/elys-network/elys/x/clock/keeper"
commitmentkeeper "github.com/elys-network/elys/x/commitment/keeper"
epochskeeper "github.com/elys-network/elys/x/epochs/keeper"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"
incentivekeeper "github.com/elys-network/elys/x/incentive/keeper"
leveragelpkeeper "github.com/elys-network/elys/x/leveragelp/keeper"
masterchefkeeper "github.com/elys-network/elys/x/masterchef/keeper"
oraclekeeper "github.com/elys-network/elys/x/oracle/keeper"
parameterkeeper "github.com/elys-network/elys/x/parameter/keeper"
perpetualkeeper "github.com/elys-network/elys/x/perpetual/keeper"
Expand Down Expand Up @@ -42,6 +44,8 @@ func NewQueryPlugin(
staking *stakingkeeper.Keeper,
tokenomics *tokenomicskeeper.Keeper,
transferhook *transferhookkeeper.Keeper,
masterchef *masterchefkeeper.Keeper,
estaking *estakingkeeper.Keeper,
) *QueryPlugin {
return &QueryPlugin{
moduleQueriers: moduleQueriers,
Expand All @@ -63,5 +67,7 @@ func NewQueryPlugin(
stablestakeKeeper: stablestake,
tokenomicsKeeper: tokenomics,
transferhookKeeper: transferhook,
masterchefKeeper: masterchef,
estakingKeeper: estaking,
}
}
13 changes: 12 additions & 1 deletion wasmbindings/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
commitmenttypes "github.com/elys-network/elys/x/commitment/types"
epochskeeper "github.com/elys-network/elys/x/epochs/keeper"
epochstypes "github.com/elys-network/elys/x/epochs/types"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"
estakingtypes "github.com/elys-network/elys/x/estaking/types"
incentivekeeper "github.com/elys-network/elys/x/incentive/keeper"
incentivetypes "github.com/elys-network/elys/x/incentive/types"
leveragelpkeeper "github.com/elys-network/elys/x/leveragelp/keeper"
Expand Down Expand Up @@ -83,6 +85,7 @@ type QueryPlugin struct {
stakingKeeper *stakingkeeper.Keeper
tokenomicsKeeper *tokenomicskeeper.Keeper
transferhookKeeper *transferhookkeeper.Keeper
estakingKeeper *estakingkeeper.Keeper
}

// AllCapabilities returns all capabilities available with the current wasmvm
Expand Down Expand Up @@ -169,6 +172,10 @@ type ElysQuery struct {
MasterchefStableStakeApr *mastercheftypes.QueryStableStakeAprRequest `json:"masterchef_stable_stake_apr,omitempty"`
MasterchefPoolAprs *mastercheftypes.QueryPoolAprsRequest `json:"masterchef_pool_aprs,omitempty"`

// estaking queriers
EstakingParams *estakingtypes.QueryParamsRequest `json:"estaking_params,omitempty"`
EstakingRewards *estakingtypes.QueryRewardsRequest `json:"estaking_rewards,omitempty"`

// leveragelp queriers
LeveragelpParams *leveragelptypes.ParamsRequest `json:"leveragelp_params,omitempty"`
LeveragelpQueryPositions *leveragelptypes.PositionsRequest `json:"leveragelp_query_positions,omitempty"`
Expand Down Expand Up @@ -238,6 +245,7 @@ type CustomMessenger struct {
epochs *epochskeeper.Keeper
incentive *incentivekeeper.Keeper
masterchef *masterchefkeeper.Keeper
estaking *estakingkeeper.Keeper
leveragelp *leveragelpkeeper.Keeper
perpetual *perpetualkeeper.Keeper
oracle *oraclekeeper.Keeper
Expand Down Expand Up @@ -284,7 +292,10 @@ type ElysMsg struct {
IncentiveCancelUnbondingDelegation *incentivetypes.MsgCancelUnbondingDelegation `json:"incentive_cancel_unbonding_delegation"`

// masterchef messages
MasterchefClaimRewards *mastercheftypes.MsgClaimRewards `json:"claim_rewards,omitempty"`
MasterchefClaimRewards *mastercheftypes.MsgClaimRewards `json:"masterchef_claim_rewards,omitempty"`

// estaking messages
EstakingWithdrawReward *estakingtypes.MsgWithdrawReward `json:"estaking_withdraw_reward,omitempty"`

// leveragelp messages
LeveragelpOpen *leveragelptypes.MsgOpen `json:"leveragelp_open,omitempty"`
Expand Down
34 changes: 34 additions & 0 deletions x/estaking/client/wasm/messenger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wasm

import (
sdk "github.com/cosmos/cosmos-sdk/types"
wasmbindingstypes "github.com/elys-network/elys/wasmbindings/types"
"github.com/elys-network/elys/x/estaking/keeper"
parameterkeeper "github.com/elys-network/elys/x/parameter/keeper"
)

// Messenger handles messages for the Masterchef module.
type Messenger struct {
keeper *keeper.Keeper
parameterKeeper *parameterkeeper.Keeper
}

func NewMessenger(
keeper *keeper.Keeper,
parameterKeeper *parameterkeeper.Keeper,
) *Messenger {
return &Messenger{
keeper: keeper,
parameterKeeper: parameterKeeper,
}
}

func (m *Messenger) HandleMsg(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg wasmbindingstypes.ElysMsg) ([]sdk.Event, [][]byte, error) {
switch {
case msg.EstakingWithdrawReward != nil:
return m.msgWithdrawReward(ctx, contractAddr, msg.EstakingWithdrawReward)
default:
// This handler cannot handle the message
return nil, nil, wasmbindingstypes.ErrCannotHandleMsg
}
}
70 changes: 70 additions & 0 deletions x/estaking/client/wasm/msg_withdraw_reward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package wasm

import (
"encoding/json"

errorsmod "cosmossdk.io/errors"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
wasmbindingstypes "github.com/elys-network/elys/wasmbindings/types"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"
"github.com/elys-network/elys/x/estaking/types"
paramtypes "github.com/elys-network/elys/x/parameter/types"
)

func (m *Messenger) msgWithdrawReward(ctx sdk.Context, contractAddr sdk.AccAddress, msgWithdrawReward *types.MsgWithdrawReward) ([]sdk.Event, [][]byte, error) {
var res *wasmbindingstypes.RequestResponse
var err error

brokerAddress := m.parameterKeeper.GetParams(ctx).BrokerAddress
if msgWithdrawReward.DelegatorAddress != contractAddr.String() && contractAddr.String() != brokerAddress {
return nil, nil, wasmvmtypes.InvalidRequest{Err: "wrong sender"}
}

res, err = performMsgWithdrawReward(m.keeper, ctx, contractAddr, msgWithdrawReward)
if err != nil {
return nil, nil, errorsmod.Wrap(err, "perform elys claim rewards")
}

responseBytes, err := json.Marshal(*res)
if err != nil {
return nil, nil, errorsmod.Wrap(err, "failed to serialize stake")
}

resp := [][]byte{responseBytes}

return nil, resp, nil
}

func performMsgWithdrawReward(f *estakingkeeper.Keeper, ctx sdk.Context, contractAddr sdk.AccAddress, msgWithdrawReward *types.MsgWithdrawReward) (*wasmbindingstypes.RequestResponse, error) {
if msgWithdrawReward == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "Invalid claim rewards parameter"}
}

msgServer := estakingkeeper.NewMsgServerImpl(*f)
_, err := sdk.AccAddressFromBech32(msgWithdrawReward.DelegatorAddress)
if err != nil {
return nil, errorsmod.Wrap(err, "invalid address")
}

msgMsgWithdrawReward := &types.MsgWithdrawReward{
DelegatorAddress: msgWithdrawReward.DelegatorAddress,
ValidatorAddress: msgWithdrawReward.ValidatorAddress,
}

if err := msgMsgWithdrawReward.ValidateBasic(); err != nil {
return nil, errorsmod.Wrap(err, "failed validating msgMsgWithdrawReward")
}

_, err = msgServer.WithdrawReward(sdk.WrapSDKContext(ctx), msgMsgWithdrawReward) // Discard the response because it's empty
if err != nil {
return nil, errorsmod.Wrap(err, "elys withdrawReward msg")
}

resp := &wasmbindingstypes.RequestResponse{
Code: paramtypes.RES_OK,
Result: "WithdrawReward succeed!",
}

return resp, nil
}
60 changes: 60 additions & 0 deletions x/estaking/client/wasm/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package wasm

import (
"encoding/json"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
wasmbindingstypes "github.com/elys-network/elys/wasmbindings/types"
"github.com/elys-network/elys/x/estaking/keeper"
"github.com/elys-network/elys/x/estaking/types"
)

// Querier handles queries for the Estaking module.
type Querier struct {
keeper *keeper.Keeper
}

func NewQuerier(keeper *keeper.Keeper) *Querier {
return &Querier{
keeper: keeper,
}
}

func (oq *Querier) HandleQuery(ctx sdk.Context, query wasmbindingstypes.ElysQuery) ([]byte, error) {
switch {
case query.EstakingParams != nil:
return oq.queryParams(ctx, query.EstakingParams)
case query.EstakingRewards != nil:
return oq.queryRewards(ctx, query.EstakingRewards)
default:
// This handler cannot handle the query
return nil, wasmbindingstypes.ErrCannotHandleQuery
}
}

func (oq *Querier) queryParams(ctx sdk.Context, query *types.QueryParamsRequest) ([]byte, error) {
res, err := oq.keeper.Params(ctx, query)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to get params")
}

responseBytes, err := json.Marshal(res)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to serialize params response")
}
return responseBytes, nil
}

func (oq *Querier) queryRewards(ctx sdk.Context, query *types.QueryRewardsRequest) ([]byte, error) {
res, err := oq.keeper.Rewards(ctx, query)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to get estaking rewards")
}

responseBytes, err := json.Marshal(res)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to serialize estaking rewards response")
}
return responseBytes, nil
}

0 comments on commit a9863e6

Please sign in to comment.