mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
4e6f6d1e9c
* spike: incentive/types * spike: incentive/types tests * spike: incentive/types/expected_keepers.go * spike: incentive/keeper * spike: incentive/keeper tests * spike: incentive/sims and incentive/sims tests * spike: incentive/module * spike: incentive/module tests * spike: hard/types * spike: hard/types hooks * spike: hard/types * spike: hard/keeper basics * spike: hard/keeper hooks * integrate hard/keeper/borrow.go * integrate hard/keeper/deposit.go * integrate hard/keeper/liquidation.go * integrate hard/keeper/withdraw.go * integrate hard/keeper/repay.go * spike: hard/sims * spike: hard/sims tests * spike: hard/client * spike: hard/module * integrate app.go * spike: x/hard/keeper compile tests * incentive/keeper test clean up * validate usdx incentive types in genesis * refactoring & fix deposit test * fix liquidaton tests * fix incentive tests for hard supply rewards * fix hard genesis tests * update incentive genesis state and params * update cdp rewards accumulation * update app init order and begin blocker order Co-authored-by: karzak <kjydavis3@gmail.com>
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
package hard
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/kava-labs/kava/x/hard/keeper"
|
|
"github.com/kava-labs/kava/x/hard/types"
|
|
)
|
|
|
|
// NewHandler creates an sdk.Handler for hard messages
|
|
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.MsgDeposit:
|
|
return handleMsgDeposit(ctx, k, msg)
|
|
case types.MsgWithdraw:
|
|
return handleMsgWithdraw(ctx, k, msg)
|
|
case types.MsgBorrow:
|
|
return handleMsgBorrow(ctx, k, msg)
|
|
case types.MsgRepay:
|
|
return handleMsgRepay(ctx, k, msg)
|
|
case types.MsgLiquidate:
|
|
return handleMsgLiquidate(ctx, k, msg)
|
|
default:
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleMsgDeposit(ctx sdk.Context, k keeper.Keeper, msg types.MsgDeposit) (*sdk.Result, error) {
|
|
err := k.Deposit(ctx, msg.Depositor, 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.Depositor.String()),
|
|
),
|
|
)
|
|
return &sdk.Result{
|
|
Events: ctx.EventManager().Events(),
|
|
}, nil
|
|
}
|
|
|
|
func handleMsgWithdraw(ctx sdk.Context, k keeper.Keeper, msg types.MsgWithdraw) (*sdk.Result, error) {
|
|
err := k.Withdraw(ctx, msg.Depositor, 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.Depositor.String()),
|
|
),
|
|
)
|
|
return &sdk.Result{
|
|
Events: ctx.EventManager().Events(),
|
|
}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|