0g-chain/x/earn/keeper/deposit.go
Derrick Lee 88d4868316
Implement Hard strategy for Earn vaults (#1278)
* Simplify strategies to lend and savings

* Add hard and savings keepers

* Add ctx to strategy interface, fill in lend strategy

* Rename lend strategy to hard

* Fix hard deposit query, fix withdraw bank send

* Fix misleading borrow instead of withdraw for hard

* Remove liquidateall strategy method

* Withdraw tests

* Add hard gs to testutil suite

* Update withdraw tests with working hard strategy, clean strategy interface methods

* Check allowed denom for strategy

* Update GetVaultTotalValue doc note

* Update error wrap message for unsupported denom

* Remove unnecessary viewvault keeper

* Withdraw amount from account value, not supplied value

* Test value > supplied withdraw

* Use dec when dividing for withdrawAmountPercent

* Use the correct store prefix for vault shares

* Update swap references to earn

* Simplify vault shares, use a single share for all coins per address
2022-07-28 09:39:57 -07:00

88 lines
2.4 KiB
Go

package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"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.
func (k *Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
// 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
}
// Check if VaultRecord exists, create if not exist
vaultRecord, found := k.GetVaultRecord(ctx, amount.Denom)
if !found {
// Create a new VaultRecord with 0 supply
vaultRecord = types.NewVaultRecord(amount.Denom)
}
// Get the strategy for the vault
strategy, err := k.GetStrategy(allowedVault.VaultStrategy)
if err != nil {
return err
}
// Check if this denom is allowed for the strategy
if !strategy.IsDenomSupported(amount.Denom) {
return sdkerrors.Wrapf(
types.ErrStrategyDenomNotSupported,
"denom %s is not supported by the strategy %s",
amount.Denom,
strategy.GetStrategyType(),
)
}
// 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
vaultShareRecord, found := k.GetVaultShareRecord(ctx, depositor)
if !found {
// Create a new empty VaultShareRecord with 0 supply
vaultShareRecord = types.NewVaultShareRecord(depositor)
}
// Increment VaultRecord supply
vaultRecord.TotalSupply = vaultRecord.TotalSupply.Add(amount)
// Increment VaultShareRecord supply
vaultShareRecord.AmountSupplied = vaultShareRecord.AmountSupplied.Add(amount)
// Update VaultRecord and VaultShareRecord
k.SetVaultRecord(ctx, vaultRecord)
k.SetVaultShareRecord(ctx, vaultShareRecord)
// Deposit to the strategy
if err := strategy.Deposit(ctx, amount); err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeVaultDeposit,
sdk.NewAttribute(types.AttributeKeyVaultDenom, amount.Denom),
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.Amount.String()),
),
)
return nil
}