2020-12-21 17:18:55 +00:00
|
|
|
package hard
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// NewHandler creates an sdk.Handler for hard messages
|
2020-09-21 21:08:43 +00:00
|
|
|
func NewHandler(k Keeper) sdk.Handler {
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case types.MsgClaimReward:
|
|
|
|
return handleMsgClaimReward(ctx, k, msg)
|
|
|
|
case types.MsgDeposit:
|
|
|
|
return handleMsgDeposit(ctx, k, msg)
|
|
|
|
case types.MsgWithdraw:
|
|
|
|
return handleMsgWithdraw(ctx, k, msg)
|
2020-10-30 09:59:47 +00:00
|
|
|
case types.MsgBorrow:
|
|
|
|
return handleMsgBorrow(ctx, k, msg)
|
2020-12-07 21:51:35 +00:00
|
|
|
case types.MsgRepay:
|
|
|
|
return handleMsgRepay(ctx, k, msg)
|
2020-12-22 16:08:27 +00:00
|
|
|
case types.MsgLiquidate:
|
|
|
|
return handleMsgLiquidate(ctx, k, msg)
|
2020-09-21 21:08:43 +00:00
|
|
|
default:
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMsgClaimReward(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaimReward) (*sdk.Result, error) {
|
2020-11-12 16:43:28 +00:00
|
|
|
err := k.ClaimReward(ctx, msg.Sender, msg.Receiver, msg.DepositDenom, types.ClaimType(strings.ToLower(msg.ClaimType)), types.MultiplierName(strings.ToLower(msg.MultiplierName)))
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMsgDeposit(ctx sdk.Context, k keeper.Keeper, msg types.MsgDeposit) (*sdk.Result, error) {
|
2020-11-12 16:43:28 +00:00
|
|
|
err := k.Deposit(ctx, msg.Depositor, msg.Amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMsgWithdraw(ctx sdk.Context, k keeper.Keeper, msg types.MsgWithdraw) (*sdk.Result, error) {
|
2020-11-12 16:43:28 +00:00
|
|
|
err := k.Withdraw(ctx, msg.Depositor, msg.Amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
func handleMsgBorrow(ctx sdk.Context, k keeper.Keeper, msg types.MsgBorrow) (*sdk.Result, error) {
|
|
|
|
err := k.Borrow(ctx, msg.Borrower, msg.Amount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Borrower.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|
2020-12-07 21:51:35 +00:00
|
|
|
|
|
|
|
func handleMsgRepay(ctx sdk.Context, k keeper.Keeper, msg types.MsgRepay) (*sdk.Result, error) {
|
|
|
|
err := k.Repay(ctx, msg.Sender, msg.Amount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|
2020-12-22 16:08:27 +00:00
|
|
|
|
|
|
|
func handleMsgLiquidate(ctx sdk.Context, k keeper.Keeper, msg types.MsgLiquidate) (*sdk.Result, error) {
|
|
|
|
_, err := k.AttemptKeeperLiquidation(ctx, msg.Keeper, msg.Borrower)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Keeper.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return &sdk.Result{
|
|
|
|
Events: ctx.EventManager().Events(),
|
|
|
|
}, nil
|
|
|
|
}
|