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 17:50:35 +00:00
|
|
|
// GetVaultTotalShares returns the total shares of a vault.
|
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
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// GetVaultTotalValue returns the total value of a vault, i.e. the realizable
|
|
|
|
// total value 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) {
|
2022-09-12 17:50:35 +00:00
|
|
|
allowedVault, found := k.GetAllowedVault(ctx, denom)
|
2022-07-20 23:14:43 +00:00
|
|
|
if !found {
|
|
|
|
return sdk.Coin{}, types.ErrVaultRecordNotFound
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
strategy, err := k.GetStrategy(allowedVault.Strategies[0])
|
2022-07-20 23:14:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return sdk.Coin{}, types.ErrInvalidVaultStrategy
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Denom can be different from allowedVault.Denom for bkava
|
|
|
|
return strategy.GetEstimatedTotalAssets(ctx, denom)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// GetVaultAccountShares returns the shares for a single address for all vaults.
|
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
|
|
|
}
|