mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-25 15:55:18 +00:00
d849d690e5
* wip: tpyes and keeper methods * wip: iterators * wip: types and keeper methods * wip: add msgs * wip: client methods * wip: rebase develop * wip: types tests * wip: keeper tests, small fixes * wip: add cdp tests * wip: deposit tests * wip: keeper tests * wip: tests and module methods * feat: error when fetching expired price * feat: conversion factor for external assets * feat: debt floor for new cdps * feat: save deposits on export genesis * feat: ensure messages implement msg * feat: index deposits by status * fix: stray comment * wip: address review comments * address review comments
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
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 {
|
|
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)
|
|
default:
|
|
errMsg := fmt.Sprintf("unrecognized cdp msg type: %T", msg)
|
|
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()}
|
|
}
|
|
|
|
func handleMsgRepayDebt(ctx sdk.Context, k Keeper, msg MsgRepayDebt) sdk.Result {
|
|
err := k.RepayPrincipal(ctx, msg.Sender, msg.CdpDenom, msg.Payment)
|
|
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()}
|
|
}
|