2021-01-07 10:23:05 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2021-01-07 10:23:05 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Withdraw returns some or all of a deposit back to original depositor
|
|
|
|
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
2021-02-12 20:56:03 +00:00
|
|
|
// Call incentive hooks
|
|
|
|
existingDeposit, found := k.GetDeposit(ctx, depositor)
|
2021-01-07 10:23:05 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor)
|
2021-01-07 10:23:05 +00:00
|
|
|
}
|
2021-02-12 20:56:03 +00:00
|
|
|
k.BeforeDepositModified(ctx, existingDeposit)
|
|
|
|
|
2021-02-08 12:23:37 +00:00
|
|
|
existingBorrow, hasExistingBorrow := k.GetBorrow(ctx, depositor)
|
|
|
|
if hasExistingBorrow {
|
|
|
|
k.BeforeBorrowModified(ctx, existingBorrow)
|
|
|
|
}
|
2021-01-21 13:52:09 +00:00
|
|
|
|
2021-02-12 20:56:03 +00:00
|
|
|
// Sync interest
|
2021-01-21 13:52:09 +00:00
|
|
|
k.SyncBorrowInterest(ctx, depositor)
|
|
|
|
k.SyncSupplyInterest(ctx, depositor)
|
2021-01-07 10:23:05 +00:00
|
|
|
|
2021-02-12 20:56:03 +00:00
|
|
|
// Refresh Deposit after syncing interest
|
|
|
|
deposit, _ := k.GetDeposit(ctx, depositor)
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
amount, err := k.CalculateWithdrawAmount(deposit.Amount, coins)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
borrow, found := k.GetBorrow(ctx, depositor)
|
|
|
|
if !found {
|
|
|
|
borrow = types.Borrow{}
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
proposedDeposit := types.NewDeposit(deposit.Depositor, deposit.Amount.Sub(amount...), types.SupplyInterestFactors{})
|
2021-01-07 10:23:05 +00:00
|
|
|
valid, err := k.IsWithinValidLtvRange(ctx, proposedDeposit, borrow)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !valid {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidWithdrawAmount, "proposed withdraw outside loan-to-value range")
|
2021-01-07 10:23:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, amount)
|
2021-01-07 10:23:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:31:38 +00:00
|
|
|
// If any coin denoms have been completely withdrawn reset the denom's supply index factor
|
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
if !sdk.NewCoins(coin).DenomsSubsetOf(proposedDeposit.Amount) {
|
|
|
|
depositIndex, removed := deposit.Index.RemoveInterestFactor(coin.Denom)
|
|
|
|
if !removed {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidIndexFactorDenom, "%s", coin.Denom)
|
2021-02-05 11:31:38 +00:00
|
|
|
}
|
|
|
|
deposit.Index = depositIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
deposit.Amount = deposit.Amount.Sub(amount...)
|
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-02-12 20:07:32 +00:00
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
// Update total supplied amount
|
2021-02-12 20:07:32 +00:00
|
|
|
err = k.DecrementSuppliedCoins(ctx, amount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 10:23:05 +00:00
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
// Call incentive hook
|
|
|
|
k.AfterDepositModified(ctx, deposit)
|
|
|
|
|
2021-01-07 10:23:05 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeHardWithdrawal,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateWithdrawAmount enables full withdraw of deposited coins by adjusting withdraw amount
|
|
|
|
// to equal total deposit amount if the requested withdraw amount > current deposit amount
|
|
|
|
func (k Keeper) CalculateWithdrawAmount(available sdk.Coins, request sdk.Coins) (sdk.Coins, error) {
|
|
|
|
result := sdk.Coins{}
|
|
|
|
|
|
|
|
if !request.DenomsSubsetOf(available) {
|
|
|
|
return result, types.ErrInvalidWithdrawDenom
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coin := range request {
|
|
|
|
if coin.Amount.GT(available.AmountOf(coin.Denom)) {
|
|
|
|
result = append(result, sdk.NewCoin(coin.Denom, available.AmountOf(coin.Denom)))
|
|
|
|
} else {
|
|
|
|
result = append(result, coin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|