2020-10-30 09:59:47 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-11-03 09:46:08 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/harvest/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Borrow funds
|
|
|
|
func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins) error {
|
2020-11-03 09:46:08 +00:00
|
|
|
// TODO: Here we assume borrower only has one coin. To be addressed in future card.
|
|
|
|
err := k.ValidateBorrow(ctx, borrower, coins[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
|
2020-10-30 09:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
borrow, found := k.GetBorrow(ctx, borrower)
|
|
|
|
if !found {
|
|
|
|
borrow = types.NewBorrow(borrower, coins)
|
|
|
|
} else {
|
|
|
|
borrow.Amount = borrow.Amount.Add(coins...)
|
|
|
|
}
|
|
|
|
k.SetBorrow(ctx, borrow)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeHarvestBorrow,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyBorrower, borrow.Borrower.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyBorrowCoins, coins.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-03 09:46:08 +00:00
|
|
|
|
|
|
|
// ValidateBorrow validates a borrow request against borrower and protocol requirements
|
|
|
|
func (k Keeper) ValidateBorrow(ctx sdk.Context, borrower sdk.AccAddress, amount sdk.Coin) error {
|
2020-11-05 17:36:49 +00:00
|
|
|
proprosedBorrowUSDValue, err := k.calculateUSDValue(ctx, amount.Amount, amount.Denom)
|
2020-11-03 09:46:08 +00:00
|
|
|
if err != nil {
|
2020-11-05 17:36:49 +00:00
|
|
|
return err
|
2020-11-03 09:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the total value of the user's deposits
|
|
|
|
deposits := k.GetDepositsByUser(ctx, borrower)
|
|
|
|
if len(deposits) == 0 {
|
|
|
|
return sdkerrors.Wrapf(types.ErrDepositsNotFound, "no deposits found for %s", borrower)
|
|
|
|
}
|
2020-11-05 17:36:49 +00:00
|
|
|
totalBorrowableAmount := sdk.ZeroDec()
|
|
|
|
for _, deposit := range deposits {
|
|
|
|
borrowableAmountForDeposit, err := k.getBorrowableAmountForDeposit(ctx, deposit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
totalBorrowableAmount = totalBorrowableAmount.Add(borrowableAmountForDeposit)
|
2020-11-03 09:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 17:36:49 +00:00
|
|
|
previousBorrowsUSDValue := sdk.ZeroDec()
|
2020-11-03 09:46:08 +00:00
|
|
|
previousBorrows, found := k.GetBorrow(ctx, borrower)
|
|
|
|
if found {
|
|
|
|
// TODO: here we're assuming that the user only has 1 previous borrow. To be addressed in future cards.
|
|
|
|
previousBorrow := previousBorrows.Amount[0]
|
2020-11-05 17:36:49 +00:00
|
|
|
previousBorrowUSDValue, err := k.calculateUSDValue(ctx, previousBorrow.Amount, previousBorrow.Denom)
|
2020-11-03 09:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 17:36:49 +00:00
|
|
|
previousBorrowsUSDValue = previousBorrowsUSDValue.Add(previousBorrowUSDValue)
|
2020-11-03 09:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 17:36:49 +00:00
|
|
|
// Validate that the proposed borrow's USD value is within user's borrowable limit
|
|
|
|
if proprosedBorrowUSDValue.GT(totalBorrowableAmount.Sub(previousBorrowsUSDValue)) {
|
2020-11-03 09:46:08 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInsufficientLoanToValue, "requested borrow %s is greater than maximum valid borrow", amount)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) calculateUSDValue(ctx sdk.Context, amount sdk.Int, denom string) (sdk.Dec, error) {
|
|
|
|
moneyMarket, found := k.GetMoneyMarket(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", denom)
|
|
|
|
}
|
|
|
|
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
|
|
|
|
}
|
2020-11-05 17:36:49 +00:00
|
|
|
return sdk.NewDecFromInt(amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPriceInfo.Price), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) getBorrowableAmountForDeposit(ctx sdk.Context, deposit types.Deposit) (sdk.Dec, error) {
|
|
|
|
moneyMarket, found := k.GetMoneyMarket(ctx, deposit.Amount.Denom)
|
|
|
|
if !found {
|
|
|
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrMarketNotFound, "no market found for denom %s", deposit.Amount.Denom)
|
|
|
|
}
|
|
|
|
assetPriceInfo, err := k.pricefeedKeeper.GetCurrentPrice(ctx, moneyMarket.SpotMarketID)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrPriceNotFound, "no price found for market %s", moneyMarket.SpotMarketID)
|
|
|
|
}
|
|
|
|
usdValue := sdk.NewDecFromInt(deposit.Amount.Amount).Quo(sdk.NewDecFromInt(moneyMarket.ConversionFactor)).Mul(assetPriceInfo.Price)
|
|
|
|
return usdValue.Mul(moneyMarket.BorrowLimit.LoanToValue), nil
|
2020-11-03 09:46:08 +00:00
|
|
|
}
|