2020-09-21 21:08:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-12-18 16:05:21 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
supplyExported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
|
2020-09-25 15:07:20 +00:00
|
|
|
"github.com/kava-labs/kava/x/harvest/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Deposit deposit
|
2020-12-18 16:05:21 +00:00
|
|
|
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
2020-12-16 21:08:29 +00:00
|
|
|
// Get current stored LTV based on stored borrows/deposits
|
2020-12-18 01:12:48 +00:00
|
|
|
prevLtv, shouldRemoveIndex, err := k.GetStoreLTV(ctx, depositor)
|
2020-12-16 21:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:12:48 +00:00
|
|
|
k.SyncOutstandingInterest(ctx, depositor)
|
2020-12-16 21:08:29 +00:00
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
err = k.ValidateDeposit(ctx, coins)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "insufficient account funds") {
|
|
|
|
accCoins := k.accountKeeper.GetAccount(ctx, depositor).SpendableCoins(ctx.BlockTime())
|
|
|
|
for _, coin := range coins {
|
|
|
|
_, isNegative := accCoins.SafeSub(sdk.NewCoins(coin))
|
|
|
|
if isNegative {
|
|
|
|
return sdkerrors.Wrapf(types.ErrBorrowExceedsAvailableBalance,
|
|
|
|
"insufficient funds: the requested deposit amount of %s exceeds the total available account funds of %s%s",
|
|
|
|
coin, accCoins.AmountOf(coin.Denom), coin.Denom,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, depositor)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit = types.NewDeposit(depositor, coins)
|
2020-09-21 21:08:43 +00:00
|
|
|
} else {
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit.Amount = deposit.Amount.Add(coins...)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k.SetDeposit(ctx, deposit)
|
|
|
|
|
2020-12-16 21:08:29 +00:00
|
|
|
k.UpdateItemInLtvIndex(ctx, prevLtv, shouldRemoveIndex, depositor)
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeHarvestDeposit,
|
2020-12-18 16:05:21 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
|
2020-09-21 21:08:43 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateDeposit validates a deposit
|
2020-12-18 16:05:21 +00:00
|
|
|
func (k Keeper) ValidateDeposit(ctx sdk.Context, coins sdk.Coins) error {
|
2020-09-21 21:08:43 +00:00
|
|
|
params := k.GetParams(ctx)
|
2020-12-18 16:05:21 +00:00
|
|
|
for _, depCoin := range coins {
|
|
|
|
found := false
|
|
|
|
for _, lps := range params.LiquidityProviderSchedules {
|
|
|
|
if lps.DepositDenom == depCoin.Denom {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "liquidity provider denom %s not found", depCoin.Denom)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-18 16:05:21 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Withdraw returns some or all of a deposit back to original depositor
|
2020-12-18 16:05:21 +00:00
|
|
|
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
|
|
|
deposit, found := k.GetDeposit(ctx, depositor)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2020-12-18 16:05:21 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-12-03 21:50:35 +00:00
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
if !deposit.Amount.IsAllGTE(coins) { // TODO test that this works how I think it does
|
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", coins, deposit.Amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, coins)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeHarvestWithdrawal,
|
2020-12-18 16:05:21 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
|
2020-09-21 21:08:43 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
if deposit.Amount.IsEqual(coins) {
|
2020-09-21 21:08:43 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeDeleteHarvestDeposit,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
k.DeleteDeposit(ctx, deposit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit.Amount = deposit.Amount.Sub(coins)
|
2020-09-21 21:08:43 +00:00
|
|
|
k.SetDeposit(ctx, deposit)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalDeposited returns the total amount deposited for the input deposit type and deposit denom
|
2020-11-12 16:43:28 +00:00
|
|
|
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositDenom string) (total sdk.Int) {
|
2020-09-21 21:08:43 +00:00
|
|
|
var macc supplyExported.ModuleAccountI
|
2020-11-12 16:43:28 +00:00
|
|
|
macc = k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
2020-09-21 21:08:43 +00:00
|
|
|
return macc.GetCoins().AmountOf(depositDenom)
|
|
|
|
}
|