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

114 lines
3.1 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package cdp
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewHandler creates an sdk.Handler for cdp messages
func NewHandler(k Keeper) sdk.Handler {
2019-11-25 19:46:02 +00:00
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
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:
errMsg := fmt.Sprintf("unrecognized cdp msg type: %T", msg)
2019-11-25 19:46:02 +00:00
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgCreateCDP(ctx sdk.Context, k Keeper, msg MsgCreateCDP) sdk.Result {
err := k.AddCdp(ctx, msg.Sender, msg.Collateral, msg.Principal)
if err != nil {
return err.Result()
}
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(),
}
}
func handleMsgDeposit(ctx sdk.Context, k Keeper, msg MsgDeposit) sdk.Result {
err := k.DepositCollateral(ctx, msg.Owner, msg.Depositor, msg.Collateral)
if err != nil {
return err.Result()
}
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()}
}
func handleMsgWithdraw(ctx sdk.Context, k Keeper, msg MsgWithdraw) sdk.Result {
err := k.WithdrawCollateral(ctx, msg.Owner, msg.Depositor, msg.Collateral)
if err != nil {
return err.Result()
}
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()}
}
func handleMsgDrawDebt(ctx sdk.Context, k Keeper, msg MsgDrawDebt) sdk.Result {
err := k.AddPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Principal)
if err != nil {
return err.Result()
}
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()}
}
2019-11-25 19:46:02 +00:00
func handleMsgRepayDebt(ctx sdk.Context, k Keeper, msg MsgRepayDebt) sdk.Result {
err := k.RepayPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Payment)
2019-11-25 19:46:02 +00:00
if err != nil {
return err.Result()
}
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()}
2019-11-25 19:46:02 +00:00
}