2022-07-20 22:57:56 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-28 16:50:59 +00:00
|
|
|
"fmt"
|
2022-09-20 18:52:40 +00:00
|
|
|
"strings"
|
2022-07-20 22:57:56 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/earn/types"
|
2022-07-20 22:57:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type queryServer struct {
|
|
|
|
keeper Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueryServerImpl creates a new server for handling gRPC queries.
|
|
|
|
func NewQueryServerImpl(k Keeper) types.QueryServer {
|
|
|
|
return &queryServer{keeper: k}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.QueryServer = queryServer{}
|
|
|
|
|
|
|
|
// Params implements the gRPC service handler for querying x/earn parameters.
|
2022-07-28 16:50:59 +00:00
|
|
|
func (s queryServer) Params(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.QueryParamsRequest,
|
|
|
|
) (*types.QueryParamsResponse, error) {
|
2022-07-20 22:57:56 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
params := s.keeper.GetParams(sdkCtx)
|
|
|
|
|
|
|
|
return &types.QueryParamsResponse{Params: params}, nil
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
// Vaults implements the gRPC service handler for querying x/earn vaults.
|
|
|
|
func (s queryServer) Vaults(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.QueryVaultsRequest,
|
|
|
|
) (*types.QueryVaultsResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
allowedVaults := s.keeper.GetAllowedVaults(sdkCtx)
|
|
|
|
allowedVaultsMap := make(map[string]types.AllowedVault)
|
|
|
|
visitedMap := make(map[string]bool)
|
|
|
|
for _, av := range allowedVaults {
|
|
|
|
allowedVaultsMap[av.Denom] = av
|
|
|
|
visitedMap[av.Denom] = false
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:50:59 +00:00
|
|
|
vaults := []types.VaultResponse{}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
var vaultRecordsErr error
|
|
|
|
|
|
|
|
// Iterate over vault records instead of AllowedVaults to get all bkava-*
|
|
|
|
// vaults
|
|
|
|
s.keeper.IterateVaultRecords(sdkCtx, func(record types.VaultRecord) bool {
|
2022-09-20 18:52:40 +00:00
|
|
|
// Check if bkava, use allowed vault
|
|
|
|
allowedVaultDenom := record.TotalShares.Denom
|
|
|
|
if strings.HasPrefix(record.TotalShares.Denom, bkavaPrefix) {
|
|
|
|
allowedVaultDenom = bkavaDenom
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedVault, found := allowedVaultsMap[allowedVaultDenom]
|
2022-09-12 16:23:26 +00:00
|
|
|
if !found {
|
2022-09-12 17:50:35 +00:00
|
|
|
vaultRecordsErr = fmt.Errorf("vault record not found for vault record denom %s", record.TotalShares.Denom)
|
2022-09-20 18:52:40 +00:00
|
|
|
return true
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
totalValue, err := s.keeper.GetVaultTotalValue(sdkCtx, record.TotalShares.Denom)
|
2022-07-28 16:50:59 +00:00
|
|
|
if err != nil {
|
2022-09-12 17:50:35 +00:00
|
|
|
vaultRecordsErr = err
|
|
|
|
// Stop iterating if error
|
|
|
|
return true
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vaults = append(vaults, types.VaultResponse{
|
2022-09-12 17:50:35 +00:00
|
|
|
Denom: record.TotalShares.Denom,
|
2022-09-12 17:29:35 +00:00
|
|
|
Strategies: allowedVault.Strategies,
|
|
|
|
IsPrivateVault: allowedVault.IsPrivateVault,
|
|
|
|
AllowedDepositors: addressSliceToStringSlice(allowedVault.AllowedDepositors),
|
2022-09-12 17:50:35 +00:00
|
|
|
TotalShares: record.TotalShares.Amount.String(),
|
2022-09-12 17:29:35 +00:00
|
|
|
TotalValue: totalValue.Amount,
|
2022-07-28 16:50:59 +00:00
|
|
|
})
|
2022-09-12 17:50:35 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Mark this allowed vault as visited
|
|
|
|
visitedMap[allowedVaultDenom] = true
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if vaultRecordsErr != nil {
|
|
|
|
return nil, vaultRecordsErr
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Add the allowed vaults that have not been visited yet
|
|
|
|
// These are always empty vaults, as the vault would have been visited
|
|
|
|
// earlier if there are any deposits
|
|
|
|
for denom, visited := range visitedMap {
|
|
|
|
if visited {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedVault, found := allowedVaultsMap[denom]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("vault record not found for vault record denom %s", denom)
|
|
|
|
}
|
|
|
|
|
|
|
|
vaults = append(vaults, types.VaultResponse{
|
|
|
|
Denom: denom,
|
|
|
|
Strategies: allowedVault.Strategies,
|
|
|
|
IsPrivateVault: allowedVault.IsPrivateVault,
|
|
|
|
AllowedDepositors: addressSliceToStringSlice(allowedVault.AllowedDepositors),
|
|
|
|
// No shares, no value
|
|
|
|
TotalShares: sdk.ZeroDec().String(),
|
|
|
|
TotalValue: sdk.ZeroInt(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Does not include vaults that have no deposits, only iterates over vault
|
|
|
|
// records which exists only for those with deposits.
|
2022-07-28 16:50:59 +00:00
|
|
|
return &types.QueryVaultsResponse{
|
|
|
|
Vaults: vaults,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Vaults implements the gRPC service handler for querying x/earn vaults.
|
|
|
|
func (s queryServer) Vault(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.QueryVaultRequest,
|
|
|
|
) (*types.QueryVaultResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
|
|
|
|
if req.Denom == "" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty denom")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only 1 vault
|
|
|
|
allowedVault, found := s.keeper.GetAllowedVault(sdkCtx, req.Denom)
|
|
|
|
if !found {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "vault not found with specified denom")
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Handle bkava separately to get total of **all** bkava vaults
|
2022-11-08 17:43:26 +00:00
|
|
|
if req.Denom == bkavaDenom {
|
2022-09-20 18:52:40 +00:00
|
|
|
return s.getAggregateBkavaVault(sdkCtx, allowedVault)
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Must be req.Denom and not allowedVault.Denom to get full "bkava" denom
|
|
|
|
vaultRecord, found := s.keeper.GetVaultRecord(sdkCtx, req.Denom)
|
|
|
|
if !found {
|
|
|
|
// No supply yet, no error just set it to zero
|
|
|
|
vaultRecord.TotalShares = types.NewVaultShare(req.Denom, sdk.ZeroDec())
|
|
|
|
}
|
|
|
|
|
|
|
|
totalValue, err := s.keeper.GetVaultTotalValue(sdkCtx, req.Denom)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vault := types.VaultResponse{
|
|
|
|
// VaultRecord denom instead of AllowedVault.Denom for full bkava denom
|
|
|
|
Denom: vaultRecord.TotalShares.Denom,
|
|
|
|
Strategies: allowedVault.Strategies,
|
|
|
|
IsPrivateVault: allowedVault.IsPrivateVault,
|
|
|
|
AllowedDepositors: addressSliceToStringSlice(allowedVault.AllowedDepositors),
|
|
|
|
TotalShares: vaultRecord.TotalShares.Amount.String(),
|
|
|
|
TotalValue: totalValue.Amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryVaultResponse{
|
|
|
|
Vault: vault,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// getAggregateBkavaVault returns a VaultResponse of the total of all bkava
|
|
|
|
// vaults.
|
|
|
|
func (s queryServer) getAggregateBkavaVault(
|
|
|
|
ctx sdk.Context,
|
|
|
|
allowedVault types.AllowedVault,
|
|
|
|
) (*types.QueryVaultResponse, error) {
|
|
|
|
allBkava := sdk.NewCoins()
|
|
|
|
|
|
|
|
var iterErr error
|
|
|
|
s.keeper.IterateVaultRecords(ctx, func(record types.VaultRecord) (stop bool) {
|
|
|
|
// Skip non bkava vaults
|
2022-11-08 17:43:26 +00:00
|
|
|
if !strings.HasPrefix(record.TotalShares.Denom, bkavaPrefix) {
|
2022-09-20 18:52:40 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultValue, err := s.keeper.GetVaultTotalValue(ctx, record.TotalShares.Denom)
|
|
|
|
if err != nil {
|
|
|
|
iterErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
allBkava = allBkava.Add(vaultValue)
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if iterErr != nil {
|
|
|
|
return nil, iterErr
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, allBkava)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryVaultResponse{
|
|
|
|
Vault: types.VaultResponse{
|
2022-11-08 17:43:26 +00:00
|
|
|
Denom: bkavaDenom,
|
2022-09-20 18:52:40 +00:00
|
|
|
Strategies: allowedVault.Strategies,
|
|
|
|
IsPrivateVault: allowedVault.IsPrivateVault,
|
|
|
|
AllowedDepositors: addressSliceToStringSlice(allowedVault.AllowedDepositors),
|
|
|
|
// Empty for shares, as adding up all shares is not useful information
|
|
|
|
TotalShares: "0",
|
|
|
|
TotalValue: vaultValue.Amount,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:50:59 +00:00
|
|
|
// Deposits implements the gRPC service handler for querying x/earn deposits.
|
|
|
|
func (s queryServer) Deposits(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.QueryDepositsRequest,
|
|
|
|
) (*types.QueryDepositsResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
if req.Depositor == "" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "depositor is required")
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
|
|
|
|
// bkava aggregate total
|
2022-11-08 17:43:26 +00:00
|
|
|
if req.Denom == bkavaDenom {
|
2022-09-20 18:52:40 +00:00
|
|
|
return s.getOneAccountBkavaVaultDeposit(sdkCtx, req)
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// specific vault
|
|
|
|
if req.Denom != "" {
|
|
|
|
return s.getOneAccountOneVaultDeposit(sdkCtx, req)
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// all vaults
|
|
|
|
return s.getOneAccountAllDeposits(sdkCtx, req)
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-11-08 17:43:26 +00:00
|
|
|
// TotalSupply implements the gRPC service handler for querying x/earn total supply (TVL)
|
|
|
|
func (s queryServer) TotalSupply(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.QueryTotalSupplyRequest,
|
|
|
|
) (*types.QueryTotalSupplyResponse, error) {
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
totalSupply := sdk.NewCoins()
|
|
|
|
liquidStakedDerivatives := sdk.NewCoins()
|
|
|
|
|
|
|
|
// allowed vaults param contains info on allowed strategies, but bkava is aggregated
|
|
|
|
allowedVaults := s.keeper.GetAllowedVaults(sdkCtx)
|
|
|
|
allowedVaultByDenom := make(map[string]types.AllowedVault)
|
|
|
|
for _, av := range allowedVaults {
|
|
|
|
allowedVaultByDenom[av.Denom] = av
|
|
|
|
}
|
|
|
|
|
|
|
|
var vaultRecordErr error
|
|
|
|
// iterate actual records to properly enumerate all denoms
|
|
|
|
s.keeper.IterateVaultRecords(sdkCtx, func(vault types.VaultRecord) (stop bool) {
|
|
|
|
isLiquidStakingDenom := false
|
|
|
|
// find allowed vault to get parameters. handle translating bkava denoms to allowed vault denom
|
|
|
|
allowedVaultDenom := vault.TotalShares.Denom
|
|
|
|
if strings.HasPrefix(vault.TotalShares.Denom, bkavaPrefix) {
|
|
|
|
isLiquidStakingDenom = true
|
|
|
|
allowedVaultDenom = bkavaDenom
|
|
|
|
}
|
|
|
|
allowedVault, found := allowedVaultByDenom[allowedVaultDenom]
|
|
|
|
if !found {
|
|
|
|
vaultRecordErr = fmt.Errorf("vault record not found for vault record denom %s", vault.TotalShares.Denom)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// only consider savings strategy vaults when determining supply
|
|
|
|
if !allowedVault.IsStrategyAllowed(types.STRATEGY_TYPE_SAVINGS) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// vault has savings strategy! determine total value of vault and add to sum
|
|
|
|
vaultSupply, err := s.keeper.GetVaultTotalValue(sdkCtx, vault.TotalShares.Denom)
|
|
|
|
if err != nil {
|
|
|
|
vaultRecordErr = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// liquid staked tokens must be converted to their underlying value
|
|
|
|
// aggregate them here and then we can convert to underlying values all at once at the end
|
|
|
|
if isLiquidStakingDenom {
|
|
|
|
liquidStakedDerivatives = liquidStakedDerivatives.Add(vaultSupply)
|
|
|
|
} else {
|
|
|
|
totalSupply = totalSupply.Add(vaultSupply)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// determine underlying value of bkava denoms
|
|
|
|
if len(liquidStakedDerivatives) > 0 {
|
|
|
|
underlyingValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(
|
|
|
|
sdkCtx,
|
|
|
|
liquidStakedDerivatives,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
totalSupply = totalSupply.Add(sdk.NewCoin(bkavaDenom, underlyingValue.Amount))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryTotalSupplyResponse{
|
|
|
|
Height: sdkCtx.BlockHeight(),
|
|
|
|
Result: totalSupply,
|
|
|
|
}, vaultRecordErr
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// getOneAccountOneVaultDeposit returns deposits for a specific vault and a specific
|
2022-09-12 16:23:26 +00:00
|
|
|
// account
|
2022-09-20 18:52:40 +00:00
|
|
|
func (s queryServer) getOneAccountOneVaultDeposit(
|
2022-09-12 16:23:26 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
req *types.QueryDepositsRequest,
|
|
|
|
) (*types.QueryDepositsResponse, error) {
|
2022-09-12 17:50:35 +00:00
|
|
|
depositor, err := sdk.AccAddressFromBech32(req.Depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Invalid address")
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
shareRecord, found := s.keeper.GetVaultShareRecord(ctx, depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if !found {
|
2022-09-20 18:52:40 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
|
|
|
Deposits: []types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: depositor.String(),
|
|
|
|
// Zero shares and zero value for no deposits
|
|
|
|
Shares: types.NewVaultShares(types.NewVaultShare(req.Denom, sdk.ZeroDec())),
|
|
|
|
Value: sdk.NewCoins(sdk.NewCoin(req.Denom, sdk.ZeroInt())),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pagination: nil,
|
|
|
|
}, nil
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Only requesting the value of the specified denom
|
|
|
|
value, err := s.keeper.GetVaultAccountValue(ctx, req.Denom, depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if err != nil {
|
2022-09-20 18:52:40 +00:00
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 23:37:29 +00:00
|
|
|
if req.ValueInStakedTokens {
|
|
|
|
// Get underlying ukava amount if denom is a derivative
|
2022-10-11 21:38:49 +00:00
|
|
|
if !s.keeper.liquidKeeper.IsDerivativeDenom(ctx, req.Denom) {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"denom %s is not a derivative, ValueInStakedTokens can only be used with liquid derivatives",
|
|
|
|
req.Denom,
|
|
|
|
)
|
|
|
|
}
|
2022-10-03 23:37:29 +00:00
|
|
|
|
2022-10-11 21:38:49 +00:00
|
|
|
ukavaValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, sdk.NewCoins(value))
|
|
|
|
if err != nil {
|
|
|
|
// This should "never" happen if IsDerivativeDenom is true
|
|
|
|
panic("Error getting ukava value for " + req.Denom)
|
2022-10-03 23:37:29 +00:00
|
|
|
}
|
2022-10-11 21:38:49 +00:00
|
|
|
|
|
|
|
value = ukavaValue
|
2022-10-03 23:37:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
|
|
|
Deposits: []types.DepositResponse{
|
|
|
|
{
|
2022-09-12 17:50:35 +00:00
|
|
|
Depositor: depositor.String(),
|
2022-09-20 18:52:40 +00:00
|
|
|
// Only respond with requested denom shares
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare(req.Denom, shareRecord.Shares.AmountOf(req.Denom)),
|
|
|
|
),
|
|
|
|
Value: sdk.NewCoins(value),
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
2022-09-12 16:23:26 +00:00
|
|
|
},
|
|
|
|
Pagination: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// getOneAccountBkavaVaultDeposit returns deposits for the aggregated bkava vault
|
|
|
|
// and a specific account
|
|
|
|
func (s queryServer) getOneAccountBkavaVaultDeposit(
|
2022-09-12 16:23:26 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
req *types.QueryDepositsRequest,
|
|
|
|
) (*types.QueryDepositsResponse, error) {
|
2022-09-20 18:52:40 +00:00
|
|
|
depositor, err := sdk.AccAddressFromBech32(req.Depositor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Invalid address")
|
|
|
|
}
|
|
|
|
|
|
|
|
shareRecord, found := s.keeper.GetVaultShareRecord(ctx, depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if !found {
|
2022-09-20 18:52:40 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
|
|
|
Deposits: []types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: depositor.String(),
|
|
|
|
// Zero shares and zero value for no deposits
|
|
|
|
Shares: types.NewVaultShares(types.NewVaultShare(req.Denom, sdk.ZeroDec())),
|
|
|
|
Value: sdk.NewCoins(sdk.NewCoin(req.Denom, sdk.ZeroInt())),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pagination: nil,
|
|
|
|
}, nil
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Get all account deposit values to add up bkava
|
|
|
|
totalAccountValue, err := getAccountTotalValue(ctx, s.keeper, depositor, shareRecord.Shares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-25 22:26:46 +00:00
|
|
|
// Remove non-bkava coins, GetStakedTokensForDerivatives expects only bkava
|
|
|
|
totalBkavaValue := sdk.NewCoins()
|
|
|
|
for _, coin := range totalAccountValue {
|
|
|
|
if s.keeper.liquidKeeper.IsDerivativeDenom(ctx, coin.Denom) {
|
|
|
|
totalBkavaValue = totalBkavaValue.Add(coin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Use account value with only the aggregate bkava converted to underlying staked tokens
|
2022-09-25 22:26:46 +00:00
|
|
|
stakedValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, totalBkavaValue)
|
2022-09-12 16:23:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
2022-09-20 18:52:40 +00:00
|
|
|
Deposits: []types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: depositor.String(),
|
|
|
|
// Only respond with requested denom shares
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare(req.Denom, shareRecord.Shares.AmountOf(req.Denom)),
|
|
|
|
),
|
|
|
|
Value: sdk.NewCoins(stakedValue),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pagination: nil,
|
2022-09-12 16:23:26 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// getOneAccountAllDeposits returns deposits for all vaults for a specific account
|
|
|
|
func (s queryServer) getOneAccountAllDeposits(
|
2022-09-12 16:23:26 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
req *types.QueryDepositsRequest,
|
|
|
|
) (*types.QueryDepositsResponse, error) {
|
2022-09-12 17:50:35 +00:00
|
|
|
depositor, err := sdk.AccAddressFromBech32(req.Depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Invalid address")
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
deposits := []types.DepositResponse{}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
accountShare, found := s.keeper.GetVaultShareRecord(ctx, depositor)
|
2022-09-12 16:23:26 +00:00
|
|
|
if !found {
|
2022-09-20 18:52:40 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
|
|
|
Deposits: []types.DepositResponse{},
|
|
|
|
Pagination: nil,
|
|
|
|
}, nil
|
2022-09-12 16:23:26 +00:00
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
value, err := getAccountTotalValue(ctx, s.keeper, depositor, accountShare.Shares)
|
2022-09-12 16:23:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-10-03 23:37:29 +00:00
|
|
|
if req.ValueInStakedTokens {
|
|
|
|
// Plain slice to not sum ukava amounts together. This is not a valid
|
|
|
|
// sdk.Coin due to multiple coins of the same denom, but we need them to
|
|
|
|
// be separate in the response to not be an aggregate amount.
|
|
|
|
var valueInStakedTokens []sdk.Coin
|
|
|
|
|
|
|
|
for _, coin := range value {
|
2022-10-11 21:38:49 +00:00
|
|
|
// Skip non-bkava coins
|
2022-10-03 23:37:29 +00:00
|
|
|
if !s.keeper.liquidKeeper.IsDerivativeDenom(ctx, coin.Denom) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derivative coins are converted to underlying staked tokens
|
|
|
|
ukavaValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, sdk.NewCoins(coin))
|
|
|
|
if err != nil {
|
|
|
|
// This should "never" happen if IsDerivativeDenom is true
|
|
|
|
panic("Error getting ukava value for " + coin.Denom)
|
|
|
|
}
|
|
|
|
valueInStakedTokens = append(valueInStakedTokens, ukavaValue)
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:38:49 +00:00
|
|
|
var filteredShares types.VaultShares
|
|
|
|
for _, share := range accountShare.Shares {
|
|
|
|
// Remove non-bkava coins from shares as they are used to
|
|
|
|
// determine which value is mapped to which denom
|
|
|
|
// These should be in the same order as valueInStakedTokens
|
|
|
|
if !s.keeper.liquidKeeper.IsDerivativeDenom(ctx, share.Denom) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredShares = append(filteredShares, share)
|
|
|
|
}
|
|
|
|
|
2022-10-03 23:37:29 +00:00
|
|
|
value = valueInStakedTokens
|
2022-10-11 21:38:49 +00:00
|
|
|
accountShare.Shares = filteredShares
|
2022-10-03 23:37:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
deposits = append(deposits, types.DepositResponse{
|
2022-09-12 17:50:35 +00:00
|
|
|
Depositor: depositor.String(),
|
2022-09-12 16:23:26 +00:00
|
|
|
Shares: accountShare.Shares,
|
|
|
|
Value: value,
|
|
|
|
})
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
return &types.QueryDepositsResponse{
|
|
|
|
Deposits: deposits,
|
|
|
|
Pagination: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// getAccountTotalValue returns the total value for all vaults for a specific
|
|
|
|
// account based on their shares.
|
|
|
|
func getAccountTotalValue(
|
2022-07-28 16:50:59 +00:00
|
|
|
ctx sdk.Context,
|
|
|
|
keeper Keeper,
|
|
|
|
account sdk.AccAddress,
|
2022-09-12 16:23:26 +00:00
|
|
|
shares types.VaultShares,
|
2022-07-28 16:50:59 +00:00
|
|
|
) (sdk.Coins, error) {
|
|
|
|
value := sdk.NewCoins()
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
for _, share := range shares {
|
|
|
|
accValue, err := keeper.GetVaultAccountValue(ctx, share.Denom, account)
|
2022-07-28 16:50:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
value = value.Add(sdk.NewCoin(share.Denom, accValue.Amount))
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
2022-09-12 17:29:35 +00:00
|
|
|
|
|
|
|
func addressSliceToStringSlice(addresses []sdk.AccAddress) []string {
|
|
|
|
var strings []string
|
|
|
|
for _, address := range addresses {
|
|
|
|
strings = append(strings, address.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings
|
|
|
|
}
|