2020-12-07 21:51:35 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
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-12-07 21:51:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Repay borrowed funds
|
|
|
|
func (k Keeper) Repay(ctx sdk.Context, sender sdk.AccAddress, coins sdk.Coins) error {
|
2020-12-16 21:08:29 +00:00
|
|
|
// Get current stored LTV based on stored borrows/deposits
|
2021-01-07 21:40:25 +00:00
|
|
|
prevLtv, err := k.GetStoreLTV(ctx, sender)
|
2020-12-16 21:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// Sync borrow interest so loan is up-to-date
|
|
|
|
k.SyncBorrowInterest(ctx, sender)
|
2020-12-07 21:51:35 +00:00
|
|
|
|
|
|
|
// Validate requested repay
|
2020-12-16 21:08:29 +00:00
|
|
|
err = k.ValidateRepay(ctx, sender, coins)
|
2020-12-07 21:51:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check borrow exists here to avoid duplicating store read in ValidateRepay
|
|
|
|
borrow, found := k.GetBorrow(ctx, sender)
|
|
|
|
if !found {
|
|
|
|
return types.ErrBorrowNotFound
|
|
|
|
}
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
payment, err := k.CalculatePaymentAmount(borrow.Amount, coins)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-07 21:51:35 +00:00
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// Sends coins from user to Hard module account
|
2020-12-07 21:51:35 +00:00
|
|
|
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleAccountName, payment)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update user's borrow in store
|
|
|
|
borrow.Amount = borrow.Amount.Sub(payment)
|
|
|
|
|
2021-01-07 21:40:25 +00:00
|
|
|
// Calculate the new Loan-to-Value ratio of Deposit-to-Borrow
|
|
|
|
deposit, foundDeposit := k.GetDeposit(ctx, sender)
|
|
|
|
if !foundDeposit {
|
|
|
|
return types.ErrDepositNotFound
|
|
|
|
}
|
|
|
|
newLtv, err := k.CalculateLtv(ctx, deposit, borrow)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k.UpdateBorrowAndLtvIndex(ctx, borrow, newLtv, prevLtv)
|
2020-12-16 21:08:29 +00:00
|
|
|
|
2020-12-07 21:51:35 +00:00
|
|
|
// Update total borrowed amount
|
|
|
|
k.DecrementBorrowedCoins(ctx, payment)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
2020-12-21 17:18:55 +00:00
|
|
|
types.EventTypeHardRepay,
|
2020-12-07 21:51:35 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeySender, sender.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyRepayCoins, payment.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateRepay validates a requested loan repay
|
|
|
|
func (k Keeper) ValidateRepay(ctx sdk.Context, sender sdk.AccAddress, coins sdk.Coins) error {
|
|
|
|
senderAcc := k.accountKeeper.GetAccount(ctx, sender)
|
2020-12-18 01:12:48 +00:00
|
|
|
senderCoins := senderAcc.SpendableCoins(ctx.BlockTime())
|
|
|
|
|
2020-12-07 21:51:35 +00:00
|
|
|
for _, coin := range coins {
|
|
|
|
if senderCoins.AmountOf(coin.Denom).LT(coin.Amount) {
|
|
|
|
return sdkerrors.Wrapf(types.ErrInsufficientBalanceForRepay, "account can only repay up to %s%s", senderCoins.AmountOf(coin.Denom), coin.Denom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculatePaymentAmount prevents overpayment when repaying borrowed coins
|
2021-01-07 10:23:05 +00:00
|
|
|
func (k Keeper) CalculatePaymentAmount(owed sdk.Coins, payment sdk.Coins) (sdk.Coins, error) {
|
2020-12-07 21:51:35 +00:00
|
|
|
repayment := sdk.Coins{}
|
2021-01-07 10:23:05 +00:00
|
|
|
|
|
|
|
if !payment.DenomsSubsetOf(owed) {
|
|
|
|
return repayment, types.ErrInvalidRepaymentDenom
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:51:35 +00:00
|
|
|
for _, coin := range payment {
|
|
|
|
if coin.Amount.GT(owed.AmountOf(coin.Denom)) {
|
|
|
|
repayment = append(repayment, sdk.NewCoin(coin.Denom, owed.AmountOf(coin.Denom)))
|
|
|
|
} else {
|
|
|
|
repayment = append(repayment, coin)
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 10:23:05 +00:00
|
|
|
return repayment, nil
|
2020-12-07 21:51:35 +00:00
|
|
|
}
|