-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |