2022-07-20 23:14:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2022-07-28 16:39:57 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/earn/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Withdraw removes the amount of supplied tokens from a vault and transfers it
|
|
|
|
// back to the account.
|
2022-07-28 16:39:57 +00:00
|
|
|
func (k *Keeper) Withdraw(ctx sdk.Context, from sdk.AccAddress, wantAmount sdk.Coin) error {
|
2022-07-20 23:14:43 +00:00
|
|
|
// Get AllowedVault, if not found (not a valid vault), return error
|
2022-07-28 16:39:57 +00:00
|
|
|
allowedVault, found := k.GetAllowedVault(ctx, wantAmount.Denom)
|
2022-07-20 23:14:43 +00:00
|
|
|
if !found {
|
|
|
|
return types.ErrInvalidVaultDenom
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
if wantAmount.IsZero() {
|
2022-07-20 23:14:43 +00:00
|
|
|
return types.ErrInsufficientAmount
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
// Check if VaultRecord exists
|
|
|
|
vaultRecord, found := k.GetVaultRecord(ctx, wantAmount.Denom)
|
2022-07-20 23:14:43 +00:00
|
|
|
if !found {
|
|
|
|
return types.ErrVaultRecordNotFound
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Get account share record for the vault
|
|
|
|
vaultShareRecord, found := k.GetVaultShareRecord(ctx, from)
|
|
|
|
if !found {
|
|
|
|
return types.ErrVaultShareRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
withdrawShares, err := k.ConvertToShares(ctx, wantAmount)
|
2022-07-28 16:39:57 +00:00
|
|
|
if err != nil {
|
2022-09-12 16:23:26 +00:00
|
|
|
return fmt.Errorf("failed to convert assets to shares: %w", err)
|
2022-07-28 16:39:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
accCurrentShares := vaultShareRecord.Shares.AmountOf(wantAmount.Denom)
|
|
|
|
// Check if account is not withdrawing more shares than they have
|
|
|
|
if accCurrentShares.LT(withdrawShares.Amount) {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
types.ErrInsufficientValue,
|
|
|
|
"account has less %s vault shares than withdraw shares, %s < %s",
|
|
|
|
wantAmount.Denom,
|
|
|
|
accCurrentShares,
|
|
|
|
withdrawShares.Amount,
|
|
|
|
)
|
2022-07-28 16:39:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Convert shares to amount to get truncated true share value
|
|
|
|
withdrawAmount, err := k.ConvertToAssets(ctx, withdrawShares)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert shares to assets: %w", err)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
accountValue, err := k.GetVaultAccountValue(ctx, wantAmount.Denom, from)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get account value: %w", err)
|
|
|
|
}
|
2022-07-28 16:39:57 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Check if withdrawAmount > account value
|
|
|
|
if withdrawAmount.Amount.GT(accountValue.Amount) {
|
2022-07-20 23:14:43 +00:00
|
|
|
return sdkerrors.Wrapf(
|
2022-07-28 16:39:57 +00:00
|
|
|
types.ErrInsufficientValue,
|
2022-09-12 16:23:26 +00:00
|
|
|
"account has less %s vault value than withdraw amount, %s < %s",
|
|
|
|
withdrawAmount.Denom,
|
|
|
|
accountValue.Amount,
|
|
|
|
withdrawAmount.Amount,
|
2022-07-20 23:14:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
// Get the strategy for the vault
|
|
|
|
strategy, err := k.GetStrategy(allowedVault.VaultStrategy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not necessary to check if amount denom is allowed for the strategy, as
|
|
|
|
// there would be no vault record if it weren't allowed.
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Withdraw the withdrawAmount from the strategy
|
|
|
|
if err := strategy.Withdraw(ctx, withdrawAmount); err != nil {
|
2022-07-28 16:39:57 +00:00
|
|
|
return fmt.Errorf("failed to withdraw from strategy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coins back to account, must withdraw from strategy first or the
|
|
|
|
// module account may not have any funds to send.
|
2022-07-20 23:14:43 +00:00
|
|
|
if err := k.bankKeeper.SendCoinsFromModuleToAccount(
|
|
|
|
ctx,
|
|
|
|
types.ModuleName,
|
|
|
|
from,
|
2022-09-12 16:23:26 +00:00
|
|
|
sdk.NewCoins(withdrawAmount),
|
2022-07-20 23:14:43 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Check if new account balance of shares results in account share value
|
|
|
|
// of < 1 of a sdk.Coin. This share value is not able to be withdrawn and
|
|
|
|
// should just be removed.
|
|
|
|
isDust, err := k.ShareIsDust(
|
|
|
|
ctx,
|
|
|
|
vaultShareRecord.Shares.GetShare(withdrawAmount.Denom).Sub(withdrawShares),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDust {
|
|
|
|
// Modify withdrawShares to subtract entire share balance for denom
|
|
|
|
// This does not modify the actual withdraw coin amount as the
|
|
|
|
// difference is < 1coin.
|
|
|
|
withdrawShares = vaultShareRecord.Shares.GetShare(withdrawAmount.Denom)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement VaultRecord and VaultShareRecord supplies - must delete same
|
|
|
|
// amounts
|
|
|
|
vaultShareRecord.Shares = vaultShareRecord.Shares.Sub(withdrawShares)
|
|
|
|
vaultRecord.TotalShares = vaultRecord.TotalShares.Sub(withdrawShares)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Update VaultRecord and VaultShareRecord, deletes if zero supply
|
|
|
|
k.UpdateVaultRecord(ctx, vaultRecord)
|
|
|
|
k.UpdateVaultShareRecord(ctx, vaultShareRecord)
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeVaultWithdraw,
|
2022-09-12 16:23:26 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyVaultDenom, withdrawAmount.Denom),
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyOwner, from.String()),
|
2022-09-12 16:23:26 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyShares, withdrawShares.Amount.String()),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, withdrawAmount.Amount.String()),
|
2022-07-20 23:14:43 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|