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"
|
|
|
|
)
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
// GetVaultTotalShares returns the total balance supplied to the vault. This
|
2022-07-20 23:14:43 +00:00
|
|
|
// may not necessarily be the current value of the vault, as it is the sum
|
|
|
|
// of the supplied denom and the value may be higher due to accumulated APYs.
|
2022-09-12 16:23:26 +00:00
|
|
|
func (k *Keeper) GetVaultTotalShares(
|
2022-07-20 23:14:43 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
denom string,
|
2022-09-12 16:23:26 +00:00
|
|
|
) (types.VaultShare, bool) {
|
2022-07-20 23:14:43 +00:00
|
|
|
vault, found := k.GetVaultRecord(ctx, denom)
|
|
|
|
if !found {
|
2022-09-12 16:23:26 +00:00
|
|
|
return types.VaultShare{}, false
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return vault.TotalShares, true
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalValue returns the total **value** of all coins in this vault,
|
|
|
|
// i.e. the realizable total value denominated by GetDenom() if the vault
|
|
|
|
// were to liquidate its entire strategies.
|
2022-07-28 16:39:57 +00:00
|
|
|
//
|
|
|
|
// **Note:** This does not include the tokens held in bank by the module
|
|
|
|
// account. If it were to be included, also note that the module account is
|
|
|
|
// unblocked and can receive funds from bank sends.
|
2022-07-20 23:14:43 +00:00
|
|
|
func (k *Keeper) GetVaultTotalValue(
|
|
|
|
ctx sdk.Context,
|
|
|
|
denom string,
|
|
|
|
) (sdk.Coin, error) {
|
|
|
|
enabledVault, found := k.GetAllowedVault(ctx, denom)
|
|
|
|
if !found {
|
|
|
|
return sdk.Coin{}, types.ErrVaultRecordNotFound
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
strategy, err := k.GetStrategy(enabledVault.Strategies[0])
|
2022-07-20 23:14:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return sdk.Coin{}, types.ErrInvalidVaultStrategy
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
return strategy.GetEstimatedTotalAssets(ctx, enabledVault.Denom)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVaultAccountSupplied returns the supplied amount for a single address
|
|
|
|
// within a vault.
|
2022-09-12 16:23:26 +00:00
|
|
|
func (k *Keeper) GetVaultAccountShares(
|
2022-07-20 23:14:43 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
acc sdk.AccAddress,
|
2022-09-12 16:23:26 +00:00
|
|
|
) (types.VaultShares, bool) {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultShareRecord, found := k.GetVaultShareRecord(ctx, acc)
|
2022-07-20 23:14:43 +00:00
|
|
|
if !found {
|
2022-09-12 16:23:26 +00:00
|
|
|
return nil, false
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return vaultShareRecord.Shares, true
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVaultAccountValue returns the value of a single address within a vault
|
|
|
|
// if the account were to withdraw their entire balance.
|
|
|
|
func (k *Keeper) GetVaultAccountValue(
|
|
|
|
ctx sdk.Context,
|
|
|
|
denom string,
|
|
|
|
acc sdk.AccAddress,
|
|
|
|
) (sdk.Coin, error) {
|
2022-09-12 16:23:26 +00:00
|
|
|
accShares, found := k.GetVaultAccountShares(ctx, acc)
|
|
|
|
if !found {
|
|
|
|
return sdk.Coin{}, fmt.Errorf("account vault share record for %s not found", denom)
|
2022-07-28 17:01:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return k.ConvertToAssets(ctx, accShares.GetShare(denom))
|
2022-07-28 17:01:30 +00:00
|
|
|
}
|