mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 15:25:18 +00:00
ae4aee46ff
* Use sdk.Coin in cdp module Co-authored-by: Federico Kunze <federico.kunze94@gmail.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Denali Marsh <denali@kava.io> Co-authored-by: John Maheswaran <john@noreply>
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package cdp
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
// 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) {
|
|
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)
|
|
default:
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
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.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
|
|
}
|
|
|
|
func handleMsgRepayDebt(ctx sdk.Context, k Keeper, msg MsgRepayDebt) (*sdk.Result, error) {
|
|
err := k.RepayPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Payment)
|
|
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
|
|
}
|