0g-chain/x/earn/keeper/msg_server.go
Derrick Lee b38cd850df
Update Earn requests and responses to use multiple strategies (#1284)
* Add multiple strategies to params, queries, txs

* Check deposit/withdraw strategy if supported by vault

* Add note for deposit strategy

* Add strategy type to messages

* Update swagger docs, ignore ethermint proto linting errors

* Update strategy test, move single strategy support to strategytype

* Simplify NewStrategyTypeFromString
2022-09-12 09:43:59 -07:00

70 lines
1.6 KiB
Go

package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/earn/types"
)
type msgServer struct {
keeper Keeper
}
// NewMsgServerImpl returns an implementation of the earn MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// Deposit handles MsgDeposit messages
func (m msgServer) Deposit(goCtx context.Context, msg *types.MsgDeposit) (*types.MsgDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return nil, err
}
if err := m.keeper.Deposit(ctx, depositor, msg.Amount, msg.Strategy); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
)
return &types.MsgDepositResponse{}, nil
}
// Withdraw handles MsgWithdraw messages
func (m msgServer) Withdraw(goCtx context.Context, msg *types.MsgWithdraw) (*types.MsgWithdrawResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
if err := m.keeper.Withdraw(ctx, from, msg.Amount, msg.Strategy); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, from.String()),
),
)
return &types.MsgWithdrawResponse{}, nil
}