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-09-12 16:43:59 +00:00
|
|
|
func (k *Keeper) Withdraw(
|
|
|
|
ctx sdk.Context,
|
|
|
|
from sdk.AccAddress,
|
|
|
|
wantAmount sdk.Coin,
|
|
|
|
withdrawStrategy types.StrategyType,
|
2022-09-29 17:01:06 +00:00
|
|
|
) (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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, types.ErrInvalidVaultDenom
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
if wantAmount.IsZero() {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, types.ErrInsufficientAmount
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
// Check if withdraw strategy is supported by vault
|
|
|
|
if !allowedVault.IsStrategyAllowed(withdrawStrategy) {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, types.ErrInvalidVaultStrategy
|
2022-09-12 16:43:59 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, types.ErrVaultRecordNotFound
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Get account share record for the vault
|
|
|
|
vaultShareRecord, found := k.GetVaultShareRecord(ctx, from)
|
|
|
|
if !found {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, types.ErrVaultShareRecordNotFound
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
withdrawShares, err := k.ConvertToShares(ctx, wantAmount)
|
2022-07-28 16:39:57 +00:00
|
|
|
if err != nil {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, 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) {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, sdkerrors.Wrapf(
|
2022-09-12 16:23:26 +00:00
|
|
|
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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, 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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, fmt.Errorf("failed to get account value: %w", err)
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
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-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, 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
|
2022-09-12 16:43:59 +00:00
|
|
|
strategy, err := k.GetStrategy(allowedVault.Strategies[0])
|
2022-07-28 16:39:57 +00:00
|
|
|
if err != nil {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, err
|
2022-07-28 16:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, fmt.Errorf("failed to withdraw from strategy: %w", err)
|
2022-07-28 16:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, err
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-29 17:01:06 +00:00
|
|
|
return sdk.Coin{}, err
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Call hook before record is modified
|
|
|
|
k.BeforeVaultDepositModified(ctx, wantAmount.Denom, from, vaultRecord.TotalShares.Amount)
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// 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
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-09-29 17:01:06 +00:00
|
|
|
return withdrawAmount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithdrawFromModuleAccount removes the amount of supplied tokens from a vault and transfers it
|
|
|
|
// back to the module account. The module account must be unblocked from receiving transfers.
|
|
|
|
func (k *Keeper) WithdrawFromModuleAccount(
|
|
|
|
ctx sdk.Context,
|
|
|
|
from string,
|
|
|
|
wantAmount sdk.Coin,
|
|
|
|
withdrawStrategy types.StrategyType,
|
|
|
|
) (sdk.Coin, error) {
|
|
|
|
// Ensure the module account exists to prevent SendCoins from creating a new non-module account.
|
|
|
|
acc := k.accountKeeper.GetModuleAccount(ctx, from)
|
|
|
|
if acc == nil {
|
|
|
|
return sdk.Coin{}, fmt.Errorf("module account not found: %s", from)
|
|
|
|
}
|
|
|
|
return k.Withdraw(ctx, acc.GetAddress(), wantAmount, withdrawStrategy)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|