2020-09-21 21:08:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-03-15 15:15:19 +00:00
|
|
|
"errors"
|
2020-12-18 16:05:21 +00:00
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/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 {
|
2021-01-07 10:23:05 +00:00
|
|
|
// Set any new denoms' global supply index to 1.0
|
|
|
|
for _, coin := range coins {
|
|
|
|
_, foundInterestFactor := k.GetSupplyInterestFactor(ctx, coin.Denom)
|
|
|
|
if !foundInterestFactor {
|
|
|
|
_, foundMm := k.GetMoneyMarket(ctx, coin.Denom)
|
|
|
|
if foundMm {
|
|
|
|
k.SetSupplyInterestFactor(ctx, coin.Denom, sdk.OneDec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:29:13 +00:00
|
|
|
// Call incentive hook
|
2021-01-21 13:52:09 +00:00
|
|
|
existingDeposit, hasExistingDeposit := k.GetDeposit(ctx, depositor)
|
|
|
|
if hasExistingDeposit {
|
|
|
|
k.BeforeDepositModified(ctx, existingDeposit)
|
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// Sync any outstanding interest
|
|
|
|
k.SyncSupplyInterest(ctx, depositor)
|
2020-12-16 21:08:29 +00:00
|
|
|
|
2021-02-08 12:23:37 +00:00
|
|
|
err := k.ValidateDeposit(ctx, coins)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins)
|
2020-12-18 16:05:21 +00:00
|
|
|
if err != nil {
|
2021-03-15 15:15:19 +00:00
|
|
|
if errors.Is(err, sdkerrors.ErrInsufficientFunds) {
|
2022-01-08 00:39:27 +00:00
|
|
|
acc := k.accountKeeper.GetAccount(ctx, depositor)
|
|
|
|
accCoins := k.bankKeeper.SpendableCoins(ctx, acc.GetAddress())
|
2020-12-18 16:05:21 +00:00
|
|
|
for _, coin := range coins {
|
2023-04-04 00:08:45 +00:00
|
|
|
_, isNegative := accCoins.SafeSub(coin)
|
2020-12-18 16:05:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:31:38 +00:00
|
|
|
interestFactors := types.SupplyInterestFactors{}
|
2021-01-07 10:23:05 +00:00
|
|
|
currDeposit, foundDeposit := k.GetDeposit(ctx, depositor)
|
|
|
|
if foundDeposit {
|
2021-02-05 11:31:38 +00:00
|
|
|
interestFactors = currDeposit.Index
|
|
|
|
}
|
|
|
|
for _, coin := range coins {
|
|
|
|
interestFactorValue, foundValue := k.GetSupplyInterestFactor(ctx, coin.Denom)
|
|
|
|
if foundValue {
|
|
|
|
interestFactors = interestFactors.SetInterestFactor(coin.Denom, interestFactorValue)
|
2021-01-07 10:23:05 +00:00
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// Calculate new deposit amount
|
|
|
|
var amount sdk.Coins
|
|
|
|
if foundDeposit {
|
|
|
|
amount = currDeposit.Amount.Add(coins...)
|
|
|
|
} else {
|
|
|
|
amount = coins
|
|
|
|
}
|
|
|
|
// Update the depositer's amount and supply interest factors in the store
|
2021-02-05 11:31:38 +00:00
|
|
|
deposit := types.NewDeposit(depositor, amount, interestFactors)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2021-02-08 12:23:37 +00:00
|
|
|
if deposit.Amount.Empty() {
|
|
|
|
k.DeleteDeposit(ctx, deposit)
|
|
|
|
} else {
|
|
|
|
k.SetDeposit(ctx, deposit)
|
2021-01-07 21:40:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
k.IncrementSuppliedCoins(ctx, coins)
|
2021-01-21 13:52:09 +00:00
|
|
|
if !foundDeposit { // User's first deposit
|
|
|
|
k.AfterDepositCreated(ctx, deposit)
|
|
|
|
} else {
|
|
|
|
k.AfterDepositModified(ctx, deposit)
|
|
|
|
}
|
2021-01-07 10:23:05 +00:00
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
2020-12-21 17:18:55 +00:00
|
|
|
types.EventTypeHardDeposit,
|
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 {
|
|
|
|
for _, depCoin := range coins {
|
2021-01-21 13:52:09 +00:00
|
|
|
_, foundMm := k.GetMoneyMarket(ctx, depCoin.Denom)
|
|
|
|
if !foundMm {
|
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, "money market 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
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// 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) {
|
2022-01-08 00:39:27 +00:00
|
|
|
macc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
|
|
|
return k.bankKeeper.GetBalance(ctx, macc.GetAddress(), depositDenom).Amount
|
2021-01-07 10:23:05 +00:00
|
|
|
}
|
2020-12-21 17:28:41 +00:00
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// IncrementSuppliedCoins increments the total amount of supplied coins by the newCoins parameter
|
|
|
|
func (k Keeper) IncrementSuppliedCoins(ctx sdk.Context, newCoins sdk.Coins) {
|
|
|
|
suppliedCoins, found := k.GetSuppliedCoins(ctx)
|
2020-12-21 17:28:41 +00:00
|
|
|
if !found {
|
2021-01-07 10:23:05 +00:00
|
|
|
if !newCoins.Empty() {
|
|
|
|
k.SetSuppliedCoins(ctx, newCoins)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
k.SetSuppliedCoins(ctx, suppliedCoins.Add(newCoins...))
|
2020-12-21 17:28:41 +00:00
|
|
|
}
|
2021-01-07 10:23:05 +00:00
|
|
|
}
|
2020-12-21 17:28:41 +00:00
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// DecrementSuppliedCoins decrements the total amount of supplied coins by the coins parameter
|
|
|
|
func (k Keeper) DecrementSuppliedCoins(ctx sdk.Context, coins sdk.Coins) error {
|
|
|
|
suppliedCoins, found := k.GetSuppliedCoins(ctx)
|
|
|
|
if !found {
|
|
|
|
return sdkerrors.Wrapf(types.ErrSuppliedCoinsNotFound, "cannot withdraw if no coins are deposited")
|
2020-12-21 17:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
updatedSuppliedCoins, isNegative := suppliedCoins.SafeSub(coins...)
|
2021-03-10 17:56:08 +00:00
|
|
|
if isNegative {
|
|
|
|
coinsToSubtract := sdk.NewCoins()
|
|
|
|
for _, coin := range coins {
|
|
|
|
if suppliedCoins.AmountOf(coin.Denom).LT(coin.Amount) {
|
|
|
|
if suppliedCoins.AmountOf(coin.Denom).GT(sdk.ZeroInt()) {
|
|
|
|
coinsToSubtract = coinsToSubtract.Add(sdk.NewCoin(coin.Denom, suppliedCoins.AmountOf(coin.Denom)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
coinsToSubtract = coinsToSubtract.Add(coin)
|
|
|
|
}
|
2021-02-12 20:07:32 +00:00
|
|
|
}
|
2023-04-04 00:08:45 +00:00
|
|
|
updatedSuppliedCoins = suppliedCoins.Sub(coinsToSubtract...)
|
2020-12-21 17:28:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
k.SetSuppliedCoins(ctx, updatedSuppliedCoins)
|
2020-09-21 21:08:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-07 16:22:00 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
// GetSyncedDeposit returns a deposit object containing current balances and indexes
|
|
|
|
func (k Keeper) GetSyncedDeposit(ctx sdk.Context, depositor sdk.AccAddress) (types.Deposit, bool) {
|
2021-01-07 16:22:00 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, depositor)
|
|
|
|
if !found {
|
2021-01-13 18:14:58 +00:00
|
|
|
return types.Deposit{}, false
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
return k.loadSyncedDeposit(ctx, deposit), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadSyncedDeposit calculates a user's synced deposit, but does not update state
|
|
|
|
func (k Keeper) loadSyncedDeposit(ctx sdk.Context, deposit types.Deposit) types.Deposit {
|
2021-01-07 16:22:00 +00:00
|
|
|
totalNewInterest := sdk.Coins{}
|
2021-01-13 18:14:58 +00:00
|
|
|
newSupplyIndexes := types.SupplyInterestFactors{}
|
2021-01-07 16:22:00 +00:00
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
interestFactorValue, foundInterestFactorValue := k.GetSupplyInterestFactor(ctx, coin.Denom)
|
|
|
|
if foundInterestFactorValue {
|
|
|
|
// Locate the interest factor by coin denom in the user's list of interest factors
|
|
|
|
foundAtIndex := -1
|
|
|
|
for i := range deposit.Index {
|
|
|
|
if deposit.Index[i].Denom == coin.Denom {
|
|
|
|
foundAtIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 18:14:58 +00:00
|
|
|
|
|
|
|
// Calculate interest that will be paid to user for this asset
|
2021-01-07 16:22:00 +00:00
|
|
|
if foundAtIndex != -1 {
|
|
|
|
storedAmount := sdk.NewDecFromInt(deposit.Amount.AmountOf(coin.Denom))
|
|
|
|
userLastInterestFactor := deposit.Index[foundAtIndex].Value
|
|
|
|
coinInterest := (storedAmount.Quo(userLastInterestFactor).Mul(interestFactorValue)).Sub(storedAmount)
|
|
|
|
totalNewInterest = totalNewInterest.Add(sdk.NewCoin(coin.Denom, coinInterest.TruncateInt()))
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 18:14:58 +00:00
|
|
|
|
|
|
|
supplyIndex := types.NewSupplyInterestFactor(coin.Denom, interestFactorValue)
|
|
|
|
newSupplyIndexes = append(newSupplyIndexes, supplyIndex)
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
return types.NewDeposit(deposit.Depositor, deposit.Amount.Add(totalNewInterest...), newSupplyIndexes)
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|