2020-09-21 21:08:43 +00:00
|
|
|
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"
|
|
|
|
|
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-11-12 16:43:28 +00:00
|
|
|
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
|
|
|
err := k.ValidateDeposit(ctx, amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, sdk.NewCoins(amount))
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2020-11-12 16:43:28 +00:00
|
|
|
deposit = types.NewDeposit(depositor, amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
} else {
|
|
|
|
deposit.Amount = deposit.Amount.Add(amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.SetDeposit(ctx, deposit)
|
|
|
|
|
|
|
|
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
|
2020-11-12 16:43:28 +00:00
|
|
|
func (k Keeper) ValidateDeposit(ctx sdk.Context, amount sdk.Coin) error {
|
2020-09-21 21:08:43 +00:00
|
|
|
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
|
2020-11-12 16:43:28 +00:00
|
|
|
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
|
|
|
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2020-11-12 16:43:28 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no %s deposit found for %s", amount.Denom, depositor)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-12-03 21:50:35 +00:00
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
if !deposit.Amount.IsGTE(amount) {
|
2020-11-12 16:43:28 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", amount, deposit.Amount)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, sdk.NewCoins(amount))
|
2020-09-21 21:08:43 +00:00
|
|
|
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
|
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)
|
|
|
|
}
|