-
Notifications
You must be signed in to change notification settings - Fork 364
/
vault.go
73 lines (62 loc) · 1.97 KB
/
vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/earn/types"
)
// GetVaultTotalShares returns the total shares of a vault.
func (k *Keeper) GetVaultTotalShares(
ctx sdk.Context,
denom string,
) (types.VaultShare, bool) {
vault, found := k.GetVaultRecord(ctx, denom)
if !found {
return types.VaultShare{}, false
}
return vault.TotalShares, true
}
// GetVaultTotalValue returns the total value of a vault, i.e. the realizable
// total value if the vault were to liquidate its entire strategies.
//
// **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.
func (k *Keeper) GetVaultTotalValue(
ctx sdk.Context,
denom string,
) (sdk.Coin, error) {
allowedVault, found := k.GetAllowedVault(ctx, denom)
if !found {
return sdk.Coin{}, types.ErrVaultRecordNotFound
}
strategy, err := k.GetStrategy(allowedVault.Strategies[0])
if err != nil {
return sdk.Coin{}, types.ErrInvalidVaultStrategy
}
// Denom can be different from allowedVault.Denom for bkava
return strategy.GetEstimatedTotalAssets(ctx, denom)
}
// GetVaultAccountShares returns the shares for a single address for all vaults.
func (k *Keeper) GetVaultAccountShares(
ctx sdk.Context,
acc sdk.AccAddress,
) (types.VaultShares, bool) {
vaultShareRecord, found := k.GetVaultShareRecord(ctx, acc)
if !found {
return nil, false
}
return vaultShareRecord.Shares, true
}
// 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) {
accShares, found := k.GetVaultAccountShares(ctx, acc)
if !found {
return sdk.Coin{}, fmt.Errorf("account vault share record for %s not found", denom)
}
return k.ConvertToAssets(ctx, accShares.GetShare(denom))
}