0g-chain/x/harvest/keeper/deposit.go
Denali Marsh 83a5f51c11
Hard: automatic liquidation by LTV index (#743)
* hotfix

* update params, keys

* liquidation by keeper

* refactor GetPendingBorrowBalance

* fix app build

* elegant handling of denom arrays

* auction deposit in lots

* add error msg

* update tests with new params

* happy path liquidation test

* update liquidator macc name

* refactor reward % to money market params

* refactor tests for updated params

* compile: harvest liquidator module account

* add liquidate msg

* liquidation approach

* update liquidations

* return remaining deposit coins to original borrowr

* check keeper reward before sending

* introduce ValuationMap

* convert Ints <> Decs

* implement double-loop

* ModuleAccountName

* sort keys for deterministic auctions

* test: correct auctions created

* test: preset keeper coins

* ensure deterministic iteration

* test cases

* update repay test

* auction fixes, tests

* LTV index

* user actions sync interest and update ltv index

* tests: all deposits must have money markets

* reorder borrow logic

* ltv index liquidation logic

* test specific items in ltv index

* index liquidation tests

* update repay to spendable coins

* revisions

* remove address sort method

* merge master test package
2020-12-18 02:12:48 +01:00

114 lines
3.3 KiB
Go

package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
supplyExported "github.com/cosmos/cosmos-sdk/x/supply/exported"
"github.com/kava-labs/kava/x/harvest/types"
)
// Deposit deposit
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
// Get current stored LTV based on stored borrows/deposits
prevLtv, shouldRemoveIndex, err := k.GetStoreLTV(ctx, depositor)
if err != nil {
return err
}
k.SyncOutstandingInterest(ctx, depositor)
err = k.ValidateDeposit(ctx, amount)
if err != nil {
return err
}
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, sdk.NewCoins(amount))
if err != nil {
return err
}
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
if !found {
deposit = types.NewDeposit(depositor, amount)
} else {
deposit.Amount = deposit.Amount.Add(amount)
}
k.SetDeposit(ctx, deposit)
k.UpdateItemInLtvIndex(ctx, prevLtv, shouldRemoveIndex, depositor)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeHarvestDeposit,
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.String()),
sdk.NewAttribute(types.AttributeKeyDepositDenom, deposit.Amount.Denom),
),
)
return nil
}
// ValidateDeposit validates a deposit
func (k Keeper) ValidateDeposit(ctx sdk.Context, amount sdk.Coin) error {
params := k.GetParams(ctx)
for _, lps := range params.LiquidityProviderSchedules {
if lps.DepositDenom == amount.Denom {
return nil
}
}
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", amount.Denom)
}
// Withdraw returns some or all of a deposit back to original depositor
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
if !found {
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no %s deposit found for %s", amount.Denom, depositor)
}
if !deposit.Amount.IsGTE(amount) {
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", amount, deposit.Amount)
}
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, sdk.NewCoins(amount))
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeHarvestWithdrawal,
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
),
)
if deposit.Amount.IsEqual(amount) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDeleteHarvestDeposit,
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
),
)
k.DeleteDeposit(ctx, deposit)
return nil
}
deposit.Amount = deposit.Amount.Sub(amount)
k.SetDeposit(ctx, deposit)
return nil
}
// GetTotalDeposited returns the total amount deposited for the input deposit type and deposit denom
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositDenom string) (total sdk.Int) {
var macc supplyExported.ModuleAccountI
macc = k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
return macc.GetCoins().AmountOf(depositDenom)
}