0g-chain/x/cdp/handler.go

112 lines
3.2 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package cdp
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2019-11-25 19:46:02 +00:00
)
// NewHandler creates an sdk.Handler for cdp messages
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
2019-11-25 19:46:02 +00:00
switch msg := msg.(type) {
case MsgCreateCDP:
return handleMsgCreateCDP(ctx, k, msg)
case MsgDeposit:
return handleMsgDeposit(ctx, k, msg)
case MsgWithdraw:
return handleMsgWithdraw(ctx, k, msg)
case MsgDrawDebt:
return handleMsgDrawDebt(ctx, k, msg)
case MsgRepayDebt:
return handleMsgRepayDebt(ctx, k, msg)
2019-11-25 19:46:02 +00:00
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
2019-11-25 19:46:02 +00:00
}
}
}
func handleMsgCreateCDP(ctx sdk.Context, k Keeper, msg MsgCreateCDP) (*sdk.Result, error) {
err := k.AddCdp(ctx, msg.Sender, msg.Collateral, msg.Principal)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
),
)
id, _ := k.GetCdpID(ctx, msg.Sender, msg.Collateral[0].Denom)
return &sdk.Result{
Data: GetCdpIDBytes(id),
Events: ctx.EventManager().Events(),
}, nil
}
func handleMsgDeposit(ctx sdk.Context, k Keeper, msg MsgDeposit) (*sdk.Result, error) {
err := k.DepositCollateral(ctx, msg.Owner, msg.Depositor, msg.Collateral)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgWithdraw(ctx sdk.Context, k Keeper, msg MsgWithdraw) (*sdk.Result, error) {
err := k.WithdrawCollateral(ctx, msg.Owner, msg.Depositor, msg.Collateral)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgDrawDebt(ctx sdk.Context, k Keeper, msg MsgDrawDebt) (*sdk.Result, error) {
err := k.AddPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Principal)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
2019-11-25 19:46:02 +00:00
func handleMsgRepayDebt(ctx sdk.Context, k Keeper, msg MsgRepayDebt) (*sdk.Result, error) {
err := k.RepayPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Payment)
2019-11-25 19:46:02 +00:00
if err != nil {
return nil, err
2019-11-25 19:46:02 +00:00
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
2019-11-25 19:46:02 +00:00
}