2022-07-20 23:14:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2022-09-12 16:23:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/kava-labs/kava/x/earn/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deposit adds the provided amount from a depositor to a vault. The vault is
|
|
|
|
// specified by the denom in the amount.
|
2022-09-12 16:43:59 +00:00
|
|
|
func (k *Keeper) Deposit(
|
|
|
|
ctx sdk.Context,
|
|
|
|
depositor sdk.AccAddress,
|
|
|
|
amount sdk.Coin,
|
|
|
|
depositStrategy types.StrategyType,
|
|
|
|
) error {
|
2022-07-20 23:14:43 +00:00
|
|
|
// Get AllowedVault, if not found (not a valid vault), return error
|
|
|
|
allowedVault, found := k.GetAllowedVault(ctx, amount.Denom)
|
|
|
|
if !found {
|
|
|
|
return types.ErrInvalidVaultDenom
|
|
|
|
}
|
|
|
|
|
|
|
|
if amount.IsZero() {
|
|
|
|
return types.ErrInsufficientAmount
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
// Check if deposit strategy is supported by vault
|
|
|
|
if !allowedVault.IsStrategyAllowed(depositStrategy) {
|
|
|
|
return types.ErrInvalidVaultStrategy
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:14:43 +00:00
|
|
|
// Check if VaultRecord exists, create if not exist
|
|
|
|
vaultRecord, found := k.GetVaultRecord(ctx, amount.Denom)
|
|
|
|
if !found {
|
|
|
|
// Create a new VaultRecord with 0 supply
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultRecord = types.NewVaultRecord(amount.Denom, sdk.ZeroDec())
|
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
|
|
|
// NOTE: Currently always uses the first one, AllowedVaults are currently
|
|
|
|
// only valid with 1 and only 1 strategy so this is safe.
|
|
|
|
// If/When multiple strategies are supported and users can specify specific
|
|
|
|
// strategies, shares should be issued per-strategy instead of per-vault.
|
|
|
|
strategy, err := k.GetStrategy(allowedVault.Strategies[0])
|
2022-07-28 16:39:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:14:43 +00:00
|
|
|
// Transfer amount to module account
|
|
|
|
if err := k.bankKeeper.SendCoinsFromAccountToModule(
|
|
|
|
ctx,
|
|
|
|
depositor,
|
|
|
|
types.ModuleName,
|
|
|
|
sdk.NewCoins(amount),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get VaultShareRecord for account, create if not exist
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultShareRecord, found := k.GetVaultShareRecord(ctx, depositor)
|
2022-07-20 23:14:43 +00:00
|
|
|
if !found {
|
|
|
|
// Create a new empty VaultShareRecord with 0 supply
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultShareRecord = types.NewVaultShareRecord(depositor, types.NewVaultShares())
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
shares, err := k.ConvertToShares(ctx, amount)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert assets to shares: %w", err)
|
|
|
|
}
|
2022-07-20 23:14:43 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// Increment VaultRecord total shares and account shares
|
|
|
|
vaultRecord.TotalShares = vaultRecord.TotalShares.Add(shares)
|
|
|
|
vaultShareRecord.Shares = vaultShareRecord.Shares.Add(shares)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Update VaultRecord and VaultShareRecord
|
|
|
|
k.SetVaultRecord(ctx, vaultRecord)
|
|
|
|
k.SetVaultShareRecord(ctx, vaultShareRecord)
|
|
|
|
|
|
|
|
// Deposit to the strategy
|
2022-07-28 16:39:57 +00:00
|
|
|
if err := strategy.Deposit(ctx, amount); err != nil {
|
2022-07-20 23:14:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeVaultDeposit,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyVaultDenom, amount.Denom),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
2022-09-12 16:23:26 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyShares, shares.Amount.String()),
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.Amount.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|