2022-07-20 23:14:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-03 23:37:29 +00:00
|
|
|
"fmt"
|
2022-07-20 23:14:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-07-20 23:14:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2022-09-20 18:52:40 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2022-07-28 16:50:59 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-09-20 18:52:40 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2022-07-28 16:50:59 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-07-20 23:14:43 +00:00
|
|
|
|
2022-10-03 23:37:29 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
2022-07-20 23:14:43 +00:00
|
|
|
"github.com/kava-labs/kava/x/earn/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/earn/testutil"
|
|
|
|
"github.com/kava-labs/kava/x/earn/types"
|
2022-09-20 18:52:40 +00:00
|
|
|
liquidtypes "github.com/kava-labs/kava/x/liquid/types"
|
2022-07-20 23:14:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type grpcQueryTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
|
|
|
|
queryClient types.QueryClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest()
|
|
|
|
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, keeper.NewQueryServerImpl(suite.Keeper))
|
|
|
|
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrpcQueryTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(grpcQueryTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestQueryParams() {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultDenom := "usdx"
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
res, err := suite.queryClient.Params(context.Background(), types.NewQueryParamsRequest())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
// ElementsMatch instead of Equal because AllowedVaults{} != AllowedVaults(nil)
|
|
|
|
suite.Require().ElementsMatch(types.DefaultParams().AllowedVaults, res.Params.AllowedVaults)
|
|
|
|
|
|
|
|
// Add vault to params
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Query again for added vault
|
|
|
|
res, err = suite.queryClient.Params(context.Background(), types.NewQueryParamsRequest())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.AllowedVaults{
|
2022-09-12 17:29:35 +00:00
|
|
|
types.NewAllowedVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil),
|
2022-07-20 23:14:43 +00:00
|
|
|
},
|
|
|
|
res.Params.AllowedVaults,
|
|
|
|
)
|
|
|
|
}
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestVaults_ZeroSupply() {
|
|
|
|
// Add vaults
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault("usdx", types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault("busd", types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
suite.Run("single", func() {
|
2022-09-12 17:50:35 +00:00
|
|
|
res, err := suite.queryClient.Vault(context.Background(), types.NewQueryVaultRequest("usdx"))
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.VaultResponse{
|
2022-09-12 17:29:35 +00:00
|
|
|
Denom: "usdx",
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
|
|
|
TotalShares: sdk.NewDec(0).String(),
|
2023-04-05 23:21:59 +00:00
|
|
|
TotalValue: sdkmath.NewInt(0),
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
2022-09-12 17:50:35 +00:00
|
|
|
res.Vault,
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
suite.Run("all", func() {
|
2022-09-12 17:50:35 +00:00
|
|
|
res, err := suite.queryClient.Vaults(context.Background(), types.NewQueryVaultsRequest())
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Require().ElementsMatch([]types.VaultResponse{
|
|
|
|
{
|
|
|
|
Denom: "usdx",
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
|
|
|
TotalShares: sdk.ZeroDec().String(),
|
|
|
|
TotalValue: sdk.ZeroInt(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Denom: "busd",
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
|
|
|
TotalShares: sdk.ZeroDec().String(),
|
|
|
|
TotalValue: sdk.ZeroInt(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Vaults,
|
|
|
|
)
|
2022-07-28 16:50:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestVaults_WithSupply() {
|
|
|
|
vaultDenom := "usdx"
|
2022-09-20 18:52:40 +00:00
|
|
|
vault2Denom := testutil.TestBkavaDenoms[0]
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
2022-09-12 17:50:35 +00:00
|
|
|
deposit2Amount := sdk.NewInt64Coin(vault2Denom, 100)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-09-12 17:50:35 +00:00
|
|
|
suite.CreateVault("bkava", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vaultDenom, 1000),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1000),
|
|
|
|
), 0)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), deposit2Amount, types.STRATEGY_TYPE_SAVINGS)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-09-12 17:50:35 +00:00
|
|
|
|
|
|
|
res, err := suite.queryClient.Vaults(context.Background(), types.NewQueryVaultsRequest())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Vaults, 2)
|
|
|
|
suite.Require().ElementsMatch(
|
|
|
|
[]types.VaultResponse{
|
|
|
|
{
|
|
|
|
Denom: vaultDenom,
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
2023-04-04 00:08:45 +00:00
|
|
|
TotalShares: sdk.NewDecFromInt(depositAmount.Amount).String(),
|
2022-09-12 17:50:35 +00:00
|
|
|
TotalValue: depositAmount.Amount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Denom: vault2Denom,
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
2023-04-04 00:08:45 +00:00
|
|
|
TotalShares: sdk.NewDecFromInt(deposit2Amount.Amount).String(),
|
2022-09-12 17:50:35 +00:00
|
|
|
TotalValue: deposit2Amount.Amount,
|
|
|
|
},
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
2022-09-12 17:50:35 +00:00
|
|
|
res.Vaults,
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
func (suite *grpcQueryTestSuite) TestVaults_MixedSupply() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
vault2Denom := "busd"
|
|
|
|
vault3Denom := testutil.TestBkavaDenoms[0]
|
|
|
|
|
|
|
|
depositAmount := sdk.NewInt64Coin(vault3Denom, 100)
|
|
|
|
|
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault(vault2Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault("bkava", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vaultDenom, 1000),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1000),
|
|
|
|
sdk.NewInt64Coin(vault3Denom, 1000),
|
|
|
|
), 0)
|
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
res, err := suite.queryClient.Vaults(context.Background(), types.NewQueryVaultsRequest())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Vaults, 3)
|
|
|
|
suite.Require().ElementsMatch(
|
|
|
|
[]types.VaultResponse{
|
|
|
|
{
|
|
|
|
Denom: vaultDenom,
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
|
|
|
TotalShares: sdk.ZeroDec().String(),
|
|
|
|
TotalValue: sdk.ZeroInt(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Denom: vault2Denom,
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_HARD},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
|
|
|
TotalShares: sdk.ZeroDec().String(),
|
|
|
|
TotalValue: sdk.ZeroInt(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Denom: vault3Denom,
|
|
|
|
Strategies: []types.StrategyType{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: nil,
|
2023-04-04 00:08:45 +00:00
|
|
|
TotalShares: sdk.NewDecFromInt(depositAmount.Amount).String(),
|
2022-09-20 18:52:40 +00:00
|
|
|
TotalValue: depositAmount.Amount,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Vaults,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
func (suite *grpcQueryTestSuite) TestVault_NotFound() {
|
|
|
|
_, err := suite.queryClient.Vault(context.Background(), types.NewQueryVaultRequest("usdx"))
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, status.Errorf(codes.NotFound, "vault not found with specified denom"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestDeposits() {
|
2022-10-03 23:37:29 +00:00
|
|
|
// Validator setup for bkava
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(5)
|
|
|
|
valAccAddr1, valAccAddr2, delegator := addrs[0], addrs[1], addrs[2]
|
|
|
|
valAddr1 := sdk.ValAddress(valAccAddr1)
|
|
|
|
valAddr2 := sdk.ValAddress(valAccAddr2)
|
|
|
|
|
2022-07-28 16:50:59 +00:00
|
|
|
vault1Denom := "usdx"
|
|
|
|
vault2Denom := "busd"
|
2022-10-03 23:37:29 +00:00
|
|
|
vault3Denom := fmt.Sprintf("bkava-%s", valAddr1.String())
|
|
|
|
vault4Denom := fmt.Sprintf("bkava-%s", valAddr2.String())
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
initialUkavaBalance := sdkmath.NewInt(1e9)
|
2022-07-28 16:50:59 +00:00
|
|
|
startBalance := sdk.NewCoins(
|
2022-10-03 23:37:29 +00:00
|
|
|
sdk.NewCoin("ukava", initialUkavaBalance),
|
2022-07-28 16:50:59 +00:00
|
|
|
sdk.NewInt64Coin(vault1Denom, 1000),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1000),
|
2022-10-03 23:37:29 +00:00
|
|
|
// Bkava isn't actually minted via x/liquid
|
2022-07-28 16:50:59 +00:00
|
|
|
sdk.NewInt64Coin(vault3Denom, 1000),
|
2022-10-03 23:37:29 +00:00
|
|
|
sdk.NewInt64Coin(vault4Denom, 1000),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
2022-09-20 18:52:40 +00:00
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
delegateAmount := sdkmath.NewInt(100e6)
|
2022-10-03 23:37:29 +00:00
|
|
|
|
|
|
|
suite.App.FundAccount(suite.Ctx, valAccAddr1, startBalance)
|
|
|
|
suite.App.FundAccount(suite.Ctx, valAccAddr2, startBalance)
|
|
|
|
suite.App.FundAccount(suite.Ctx, delegator, startBalance)
|
|
|
|
|
|
|
|
suite.CreateNewUnbondedValidator(valAddr1, initialUkavaBalance)
|
|
|
|
suite.CreateNewUnbondedValidator(valAddr2, initialUkavaBalance)
|
|
|
|
suite.CreateDelegation(valAddr1, delegator, delegateAmount)
|
|
|
|
suite.CreateDelegation(valAddr2, delegator, delegateAmount)
|
|
|
|
|
|
|
|
staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper())
|
|
|
|
|
|
|
|
savingsParams := suite.SavingsKeeper.GetParams(suite.Ctx)
|
|
|
|
savingsParams.SupportedDenoms = append(savingsParams.SupportedDenoms, "bkava")
|
|
|
|
suite.SavingsKeeper.SetParams(suite.Ctx, savingsParams)
|
|
|
|
|
|
|
|
// Add vaults
|
|
|
|
suite.CreateVault(vault1Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault(vault2Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault("bkava", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
|
2022-07-28 16:50:59 +00:00
|
|
|
deposit1Amount := sdk.NewInt64Coin(vault1Denom, 100)
|
|
|
|
deposit2Amount := sdk.NewInt64Coin(vault2Denom, 200)
|
|
|
|
deposit3Amount := sdk.NewInt64Coin(vault3Denom, 200)
|
2022-10-03 23:37:29 +00:00
|
|
|
deposit4Amount := sdk.NewInt64Coin(vault4Denom, 300)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
// Accounts
|
|
|
|
acc1 := suite.CreateAccount(startBalance, 0).GetAddress()
|
2022-10-03 23:37:29 +00:00
|
|
|
acc2 := delegator
|
2022-07-28 16:50:59 +00:00
|
|
|
|
|
|
|
// Deposit into each vault from each account - 4 total deposits
|
|
|
|
// Acc 1: usdx + busd
|
2022-10-03 23:37:29 +00:00
|
|
|
// Acc 2: usdx + bkava-1 + bkava-2
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc1, deposit1Amount, types.STRATEGY_TYPE_HARD)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-09-12 16:43:59 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc1, deposit2Amount, types.STRATEGY_TYPE_HARD)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc2, deposit1Amount, types.STRATEGY_TYPE_HARD)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-09-12 17:50:35 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc2, deposit3Amount, types.STRATEGY_TYPE_SAVINGS)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().NoError(err)
|
2022-10-03 23:37:29 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc2, deposit4Amount, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Run("specific vault", func() {
|
2022-07-28 16:50:59 +00:00
|
|
|
// Query all deposits for account 1
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc1.String(), vault1Denom, false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
suite.Require().ElementsMatchf(
|
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: acc1.String(),
|
2022-09-20 18:52:40 +00:00
|
|
|
// Only includes specified deposit shares
|
2022-09-12 16:23:26 +00:00
|
|
|
Shares: types.NewVaultShares(
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(deposit1Amount.Denom, sdk.NewDecFromInt(deposit1Amount.Amount)),
|
2022-09-12 16:23:26 +00:00
|
|
|
),
|
2022-09-20 18:52:40 +00:00
|
|
|
// Only the specified vault denom value
|
|
|
|
Value: sdk.NewCoins(deposit1Amount),
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
|
|
|
"deposits should match, got %v",
|
|
|
|
res.Deposits,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Run("specific bkava vault", func() {
|
2022-07-28 16:50:59 +00:00
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc2.String(), vault3Denom, false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Require().ElementsMatchf(
|
2022-07-28 16:50:59 +00:00
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
2022-09-20 18:52:40 +00:00
|
|
|
Depositor: acc2.String(),
|
|
|
|
// Only includes specified deposit shares
|
2022-09-12 16:23:26 +00:00
|
|
|
Shares: types.NewVaultShares(
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(deposit3Amount.Denom, sdk.NewDecFromInt(deposit3Amount.Amount)),
|
2022-09-12 16:23:26 +00:00
|
|
|
),
|
2022-09-20 18:52:40 +00:00
|
|
|
// Only the specified vault denom value
|
|
|
|
Value: sdk.NewCoins(deposit3Amount),
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
2022-09-20 18:52:40 +00:00
|
|
|
"deposits should match, got %v",
|
|
|
|
res.Deposits,
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-10-03 23:37:29 +00:00
|
|
|
suite.Run("specific bkava vault in staked tokens", func() {
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
|
|
|
types.NewQueryDepositsRequest(acc2.String(), vault3Denom, true, nil),
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.DepositResponse{
|
|
|
|
Depositor: acc2.String(),
|
|
|
|
// Only includes specified deposit shares
|
|
|
|
Shares: types.NewVaultShares(
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(deposit3Amount.Denom, sdk.NewDecFromInt(deposit3Amount.Amount)),
|
2022-10-03 23:37:29 +00:00
|
|
|
),
|
|
|
|
// Only the specified vault denom value
|
|
|
|
Value: sdk.NewCoins(
|
|
|
|
sdk.NewCoin("ukava", deposit3Amount.Amount),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
res.Deposits[0],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Run("invalid vault", func() {
|
|
|
|
_, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc1.String(), "notavaliddenom", false, nil),
|
2022-09-20 18:52:40 +00:00
|
|
|
)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, status.Errorf(codes.NotFound, "vault for notavaliddenom not found"))
|
|
|
|
})
|
|
|
|
|
|
|
|
suite.Run("all vaults", func() {
|
|
|
|
// Query all deposits for account 1
|
2022-07-28 16:50:59 +00:00
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc1.String(), "", false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
suite.Require().ElementsMatch(
|
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
2022-09-20 18:52:40 +00:00
|
|
|
Depositor: acc1.String(),
|
2022-09-12 16:23:26 +00:00
|
|
|
Shares: types.NewVaultShares(
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(deposit1Amount.Denom, sdk.NewDecFromInt(deposit1Amount.Amount)),
|
|
|
|
types.NewVaultShare(deposit2Amount.Denom, sdk.NewDecFromInt(deposit2Amount.Amount)),
|
2022-09-12 16:23:26 +00:00
|
|
|
),
|
2022-09-20 18:52:40 +00:00
|
|
|
Value: sdk.NewCoins(deposit1Amount, deposit2Amount),
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
|
|
|
)
|
|
|
|
})
|
2022-10-03 23:37:29 +00:00
|
|
|
|
|
|
|
suite.Run("all vaults value in staked tokens", func() {
|
|
|
|
// Query all deposits for account 1 with value in staked tokens
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
|
|
|
types.NewQueryDepositsRequest(acc2.String(), "", true, nil),
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.DepositResponse{
|
|
|
|
Depositor: acc2.String(),
|
|
|
|
Shares: types.VaultShares{
|
2022-10-11 21:38:49 +00:00
|
|
|
// Does not include non-bkava vaults
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(deposit4Amount.Denom, sdk.NewDecFromInt(deposit4Amount.Amount)),
|
|
|
|
types.NewVaultShare(deposit3Amount.Denom, sdk.NewDecFromInt(deposit3Amount.Amount)),
|
2022-10-03 23:37:29 +00:00
|
|
|
},
|
|
|
|
Value: sdk.Coins{
|
2022-10-11 21:38:49 +00:00
|
|
|
// Does not include non-bkava vaults
|
2022-10-03 23:37:29 +00:00
|
|
|
sdk.NewCoin("ukava", deposit4Amount.Amount),
|
|
|
|
sdk.NewCoin("ukava", deposit3Amount.Amount),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits[0],
|
|
|
|
)
|
|
|
|
for i := range res.Deposits[0].Shares {
|
|
|
|
suite.Equal(
|
|
|
|
res.Deposits[0].Shares[i].Amount,
|
2023-04-04 00:08:45 +00:00
|
|
|
sdk.NewDecFromInt(res.Deposits[0].Value[i].Amount),
|
2022-10-03 23:37:29 +00:00
|
|
|
"order of deposit value should match shares",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2022-09-20 18:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestDeposits_NoDeposits() {
|
|
|
|
vault1Denom := "usdx"
|
|
|
|
vault2Denom := "busd"
|
|
|
|
|
|
|
|
// Add vaults
|
|
|
|
suite.CreateVault(vault1Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault(vault2Denom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault("bkava", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
|
|
|
|
// Accounts
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(), 0).GetAddress()
|
2022-07-28 16:50:59 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Run("specific vault", func() {
|
|
|
|
// Query all deposits for account 1
|
2022-07-28 16:50:59 +00:00
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc1.String(), vault1Denom, false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Require().Len(res.Deposits, 1)
|
2022-07-28 16:50:59 +00:00
|
|
|
suite.Require().ElementsMatchf(
|
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
2022-09-12 16:23:26 +00:00
|
|
|
Depositor: acc1.String(),
|
2022-09-20 18:52:40 +00:00
|
|
|
// Zero shares and zero value
|
|
|
|
Shares: nil,
|
|
|
|
Value: nil,
|
2022-07-28 16:50:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
|
|
|
"deposits should match, got %v",
|
|
|
|
res.Deposits,
|
|
|
|
)
|
|
|
|
})
|
2022-09-20 18:52:40 +00:00
|
|
|
|
|
|
|
suite.Run("all vaults", func() {
|
|
|
|
// Query all deposits for account 1
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(acc1.String(), "", false, nil),
|
2022-09-20 18:52:40 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Empty(res.Deposits)
|
|
|
|
})
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
func (suite *grpcQueryTestSuite) TestDeposits_NoDepositor() {
|
2022-07-28 16:50:59 +00:00
|
|
|
_, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest("", "usdx", false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().Error(err)
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "depositor is required"))
|
2022-07-28 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestDeposits_InvalidAddress() {
|
|
|
|
_, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest("asdf", "usdx", false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "Invalid address"))
|
|
|
|
|
|
|
|
_, err = suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest("asdf", "", false, nil),
|
2022-07-28 16:50:59 +00:00
|
|
|
)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, status.Error(codes.InvalidArgument, "Invalid address"))
|
|
|
|
}
|
2022-09-12 17:50:35 +00:00
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
func (suite *grpcQueryTestSuite) TestDeposits_bKava() {
|
|
|
|
// vault denom is only "bkava" which has it's own special handler
|
|
|
|
suite.CreateVault(
|
|
|
|
"bkava",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
false,
|
|
|
|
[]sdk.AccAddress{},
|
|
|
|
)
|
|
|
|
|
2022-09-25 22:26:46 +00:00
|
|
|
suite.CreateVault(
|
|
|
|
"ukava",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
false,
|
|
|
|
[]sdk.AccAddress{},
|
|
|
|
)
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
address1, derivatives1, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[0], sdkmath.NewInt(1e9))
|
|
|
|
address2, derivatives2, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[1], sdkmath.NewInt(1e9))
|
2022-09-20 18:52:40 +00:00
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
err := suite.App.FundAccount(suite.Ctx, address1, sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(1e9))))
|
2022-09-25 22:26:46 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// Slash the last validator to reduce the value of it's derivatives to test bkava to underlying token conversion.
|
|
|
|
// First call end block to bond validator to enable slashing.
|
|
|
|
staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper())
|
2022-09-25 22:26:46 +00:00
|
|
|
err = suite.slashValidator(sdk.ValAddress(address2), sdk.MustNewDecFromStr("0.5"))
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.Run("no deposits", func() {
|
|
|
|
// Query all deposits for account 1
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(address1.String(), "bkava", false, nil),
|
2022-09-20 18:52:40 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
suite.Require().ElementsMatchf(
|
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: address1.String(),
|
|
|
|
// Zero shares for "bkava" aggregate
|
|
|
|
Shares: nil,
|
|
|
|
// Only the specified vault denom value
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
|
|
|
"deposits should match, got %v",
|
|
|
|
res.Deposits,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address1, derivatives1, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
err = suite.BankKeeper.SendCoins(suite.Ctx, address2, address1, sdk.NewCoins(derivatives2))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address1, derivatives2, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-25 22:26:46 +00:00
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address1, sdk.NewInt64Coin("ukava", 1e6), types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
suite.Run("multiple deposits", func() {
|
|
|
|
// Query all deposits for account 1
|
|
|
|
res, err := suite.queryClient.Deposits(
|
|
|
|
context.Background(),
|
2022-10-03 23:37:29 +00:00
|
|
|
types.NewQueryDepositsRequest(address1.String(), "bkava", false, nil),
|
2022-09-20 18:52:40 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Len(res.Deposits, 1)
|
|
|
|
// first validator isn't slashed, so bkava units equal to underlying staked tokens
|
|
|
|
// last validator slashed 50% so derivatives are worth half
|
2022-09-25 22:26:46 +00:00
|
|
|
// Excludes non-bkava deposits
|
2022-09-20 18:52:40 +00:00
|
|
|
expectedValue := derivatives1.Amount.Add(derivatives2.Amount.QuoRaw(2))
|
|
|
|
suite.Require().ElementsMatchf(
|
|
|
|
[]types.DepositResponse{
|
|
|
|
{
|
|
|
|
Depositor: address1.String(),
|
|
|
|
// Zero shares for "bkava" aggregate
|
|
|
|
Shares: nil,
|
|
|
|
// Value returned in units of staked token
|
|
|
|
Value: sdk.NewCoins(
|
|
|
|
sdk.NewCoin(suite.bondDenom(), expectedValue),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res.Deposits,
|
|
|
|
"deposits should match, got %v",
|
|
|
|
res.Deposits,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestVault_bKava_Single() {
|
2022-09-12 17:50:35 +00:00
|
|
|
vaultDenom := "bkava"
|
2022-09-20 18:52:40 +00:00
|
|
|
coinDenom := testutil.TestBkavaDenoms[0]
|
2022-09-12 17:50:35 +00:00
|
|
|
|
|
|
|
startBalance := sdk.NewInt64Coin(coinDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(coinDenom, 100)
|
|
|
|
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
|
|
|
// vault denom is only "bkava" which has it's own special handler
|
|
|
|
suite.CreateVault(
|
|
|
|
vaultDenom,
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
false,
|
|
|
|
[]sdk.AccAddress{},
|
|
|
|
)
|
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc1.GetAddress(), depositAmount, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(
|
|
|
|
err,
|
|
|
|
"should be able to deposit bkava derivative denom in bkava vault",
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := suite.queryClient.Vault(
|
|
|
|
context.Background(),
|
|
|
|
types.NewQueryVaultRequest(coinDenom),
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.VaultResponse{
|
|
|
|
Denom: coinDenom,
|
|
|
|
Strategies: types.StrategyTypes{
|
|
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
|
|
},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: []string(nil),
|
|
|
|
TotalShares: "100.000000000000000000",
|
2023-04-05 23:21:59 +00:00
|
|
|
TotalValue: sdkmath.NewInt(100),
|
2022-09-12 17:50:35 +00:00
|
|
|
},
|
|
|
|
res.Vault,
|
|
|
|
)
|
|
|
|
}
|
2022-09-20 18:52:40 +00:00
|
|
|
|
|
|
|
func (suite *grpcQueryTestSuite) TestVault_bKava_Aggregate() {
|
|
|
|
vaultDenom := "bkava"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
address1, derivatives1, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[0], sdkmath.NewInt(1e9))
|
|
|
|
address2, derivatives2, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[1], sdkmath.NewInt(1e9))
|
|
|
|
address3, derivatives3, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[2], sdkmath.NewInt(1e9))
|
2022-09-20 18:52:40 +00:00
|
|
|
// Slash the last validator to reduce the value of it's derivatives to test bkava to underlying token conversion.
|
|
|
|
// First call end block to bond validator to enable slashing.
|
|
|
|
staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper())
|
|
|
|
err := suite.slashValidator(sdk.ValAddress(address3), sdk.MustNewDecFromStr("0.5"))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// vault denom is only "bkava" which has it's own special handler
|
|
|
|
suite.CreateVault(
|
|
|
|
vaultDenom,
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
false,
|
|
|
|
[]sdk.AccAddress{},
|
|
|
|
)
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address1, derivatives1, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address2, derivatives2, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, address3, derivatives3, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Query "bkava" to get aggregate amount
|
|
|
|
res, err := suite.queryClient.Vault(
|
|
|
|
context.Background(),
|
|
|
|
types.NewQueryVaultRequest(vaultDenom),
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
// first two validators are not slashed, so bkava units equal to underlying staked tokens
|
|
|
|
expectedValue := derivatives1.Amount.Add(derivatives2.Amount)
|
|
|
|
// last validator slashed 50% so derivatives are worth half
|
|
|
|
expectedValue = expectedValue.Add(derivatives2.Amount.QuoRaw(2))
|
|
|
|
suite.Require().Equal(
|
|
|
|
types.VaultResponse{
|
|
|
|
Denom: vaultDenom,
|
|
|
|
Strategies: types.StrategyTypes{
|
|
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
|
|
},
|
|
|
|
IsPrivateVault: false,
|
|
|
|
AllowedDepositors: []string(nil),
|
|
|
|
// No shares for aggregate
|
|
|
|
TotalShares: "0",
|
|
|
|
TotalValue: expectedValue,
|
|
|
|
},
|
|
|
|
res.Vault,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:43:26 +00:00
|
|
|
func (suite *grpcQueryTestSuite) TestTotalSupply() {
|
|
|
|
deposit := func(addr sdk.AccAddress, denom string, amount int64) {
|
|
|
|
err := suite.Keeper.Deposit(
|
|
|
|
suite.Ctx,
|
|
|
|
addr,
|
|
|
|
sdk.NewInt64Coin(denom, amount),
|
|
|
|
types.STRATEGY_TYPE_SAVINGS,
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
setup func()
|
|
|
|
expectedSupply sdk.Coins
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no vaults mean no supply",
|
|
|
|
setup: func() {},
|
|
|
|
expectedSupply: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no savings vaults mean no supply",
|
|
|
|
setup: func() {
|
|
|
|
suite.CreateVault("usdx", types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
suite.CreateVault("busd", types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
|
|
|
},
|
|
|
|
expectedSupply: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty savings vaults mean no supply",
|
|
|
|
setup: func() {
|
|
|
|
suite.CreateVault("usdx", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
suite.CreateVault("busd", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
},
|
|
|
|
expectedSupply: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "calculates supply of savings vaults",
|
|
|
|
setup: func() {
|
|
|
|
vault1Denom := "usdx"
|
|
|
|
vault2Denom := "busd"
|
|
|
|
suite.CreateVault(vault1Denom, types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
suite.CreateVault(vault2Denom, types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, nil)
|
|
|
|
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vault1Denom, 1e6),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1e6),
|
|
|
|
), 0)
|
|
|
|
deposit(acc1.GetAddress(), vault1Denom, 1e5)
|
|
|
|
deposit(acc1.GetAddress(), vault2Denom, 1e5)
|
|
|
|
acc2 := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vault1Denom, 1e6),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1e6),
|
|
|
|
), 0)
|
|
|
|
deposit(acc2.GetAddress(), vault1Denom, 2e5)
|
|
|
|
deposit(acc2.GetAddress(), vault2Denom, 2e5)
|
|
|
|
},
|
|
|
|
expectedSupply: sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin("usdx", 3e5),
|
|
|
|
sdk.NewInt64Coin("busd", 3e5),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "calculates supply of savings vaults, even when private",
|
|
|
|
setup: func() {
|
|
|
|
vault1Denom := "ukava"
|
|
|
|
vault2Denom := "busd"
|
|
|
|
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vault1Denom, 1e6),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1e6),
|
|
|
|
), 0)
|
|
|
|
acc2 := suite.CreateAccount(sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin(vault1Denom, 1e6),
|
|
|
|
sdk.NewInt64Coin(vault2Denom, 1e6),
|
|
|
|
), 0)
|
|
|
|
|
|
|
|
suite.CreateVault(
|
|
|
|
vault1Denom,
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
true, // private!
|
|
|
|
[]sdk.AccAddress{acc1.GetAddress()}, // only acc1 can deposit.
|
|
|
|
)
|
|
|
|
suite.CreateVault("busd",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
deposit(acc1.GetAddress(), vault1Denom, 1e5)
|
|
|
|
deposit(acc1.GetAddress(), vault2Denom, 1e5)
|
|
|
|
deposit(acc2.GetAddress(), vault2Denom, 2e5)
|
|
|
|
},
|
|
|
|
expectedSupply: sdk.NewCoins(
|
|
|
|
sdk.NewInt64Coin("ukava", 1e5),
|
|
|
|
sdk.NewInt64Coin("busd", 3e5),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "aggregates supply of bkava vaults accounting for slashing",
|
|
|
|
setup: func() {
|
2023-04-05 23:21:59 +00:00
|
|
|
address1, derivatives1, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[0], sdkmath.NewInt(1e9))
|
|
|
|
address2, derivatives2, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[1], sdkmath.NewInt(1e9))
|
2022-11-08 17:43:26 +00:00
|
|
|
|
|
|
|
// bond validators
|
|
|
|
staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper())
|
|
|
|
// slash val2 - its shares are now 80% as valuable!
|
|
|
|
err := suite.slashValidator(sdk.ValAddress(address2), sdk.MustNewDecFromStr("0.2"))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// create "bkava" vault. it holds all bkava denoms
|
|
|
|
suite.CreateVault("bkava", types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS}, false, []sdk.AccAddress{})
|
|
|
|
|
|
|
|
// deposit bkava
|
|
|
|
deposit(address1, testutil.TestBkavaDenoms[0], derivatives1.Amount.Int64())
|
|
|
|
deposit(address2, testutil.TestBkavaDenoms[1], derivatives2.Amount.Int64())
|
|
|
|
},
|
|
|
|
expectedSupply: sdk.NewCoins(
|
|
|
|
sdk.NewCoin(
|
|
|
|
"bkava",
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath.NewIntFromUint64(1e9). // derivative 1
|
|
|
|
Add(sdkmath.NewInt(1e9).MulRaw(80).QuoRaw(100))), // derivative 2: original value * 80%
|
2022-11-08 17:43:26 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest()
|
|
|
|
tc.setup()
|
|
|
|
res, err := suite.queryClient.TotalSupply(
|
|
|
|
sdk.WrapSDKContext(suite.Ctx),
|
|
|
|
&types.QueryTotalSupplyRequest{},
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(tc.expectedSupply, res.Result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:52:40 +00:00
|
|
|
// createUnbondedValidator creates an unbonded validator with the given amount of self-delegation.
|
2023-04-05 23:21:59 +00:00
|
|
|
func (suite *grpcQueryTestSuite) createUnbondedValidator(address sdk.ValAddress, selfDelegation sdk.Coin, minSelfDelegation sdkmath.Int) error {
|
2022-09-20 18:52:40 +00:00
|
|
|
msg, err := stakingtypes.NewMsgCreateValidator(
|
|
|
|
address,
|
|
|
|
ed25519.GenPrivKey().PubKey(),
|
|
|
|
selfDelegation,
|
|
|
|
stakingtypes.Description{},
|
|
|
|
stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
|
|
|
|
minSelfDelegation,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgServer := stakingkeeper.NewMsgServerImpl(suite.App.GetStakingKeeper())
|
|
|
|
_, err = msgServer.CreateValidator(sdk.WrapSDKContext(suite.Ctx), msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// createAccountWithDerivatives creates an account with the given amount and denom of derivative token.
|
|
|
|
// Internally, it creates a validator account and mints derivatives from the validator's self delegation.
|
2023-04-05 23:21:59 +00:00
|
|
|
func (suite *grpcQueryTestSuite) createAccountWithDerivatives(denom string, amount sdkmath.Int) (sdk.AccAddress, sdk.Coin, sdk.Coins) {
|
2022-09-20 18:52:40 +00:00
|
|
|
valAddress, err := liquidtypes.ParseLiquidStakingTokenDenom(denom)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
address := sdk.AccAddress(valAddress)
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
remainingSelfDelegation := sdkmath.NewInt(1e6)
|
2022-09-20 18:52:40 +00:00
|
|
|
selfDelegation := sdk.NewCoin(
|
|
|
|
suite.bondDenom(),
|
|
|
|
amount.Add(remainingSelfDelegation),
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.NewAccountFromAddr(address, sdk.NewCoins(selfDelegation))
|
|
|
|
|
|
|
|
err = suite.createUnbondedValidator(valAddress, selfDelegation, remainingSelfDelegation)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
toConvert := sdk.NewCoin(suite.bondDenom(), amount)
|
|
|
|
derivatives, err := suite.App.GetLiquidKeeper().MintDerivative(suite.Ctx,
|
|
|
|
address,
|
|
|
|
valAddress,
|
|
|
|
toConvert,
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
fullBalance := suite.BankKeeper.GetAllBalances(suite.Ctx, address)
|
|
|
|
|
|
|
|
return address, derivatives, fullBalance
|
|
|
|
}
|
|
|
|
|
|
|
|
// slashValidator slashes the validator with the given address by the given percentage.
|
|
|
|
func (suite *grpcQueryTestSuite) slashValidator(address sdk.ValAddress, slashFraction sdk.Dec) error {
|
|
|
|
stakingKeeper := suite.App.GetStakingKeeper()
|
|
|
|
|
|
|
|
validator, found := stakingKeeper.GetValidator(suite.Ctx, address)
|
|
|
|
suite.Require().True(found)
|
|
|
|
consAddr, err := validator.GetConsAddr()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Assume infraction was at current height. Note unbonding delegations and redelegations are only slashed if created after
|
|
|
|
// the infraction height so none will be slashed.
|
|
|
|
infractionHeight := suite.Ctx.BlockHeight()
|
|
|
|
|
|
|
|
power := stakingKeeper.TokensToConsensusPower(suite.Ctx, validator.GetTokens())
|
|
|
|
|
|
|
|
stakingKeeper.Slash(suite.Ctx, consAddr, infractionHeight, power, slashFraction)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bondDenom fetches the staking denom from the staking module.
|
|
|
|
func (suite *grpcQueryTestSuite) bondDenom() string {
|
|
|
|
return suite.App.GetStakingKeeper().BondDenom(suite.Ctx)
|
|
|
|
}
|