Exclude non-bkava denoms from aggregate underlying ukava calculation (#1311)

This commit is contained in:
Derrick Lee 2022-09-25 15:26:46 -07:00 committed by GitHub
parent c20c954917
commit b68685af32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -343,8 +343,16 @@ func (s queryServer) getOneAccountBkavaVaultDeposit(
return nil, err return nil, err
} }
// 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)
}
}
// Use account value with only the aggregate bkava converted to underlying staked tokens // Use account value with only the aggregate bkava converted to underlying staked tokens
stakedValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, totalAccountValue) stakedValue, err := s.keeper.liquidKeeper.GetStakedTokensForDerivatives(ctx, totalBkavaValue)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -417,13 +417,23 @@ func (suite *grpcQueryTestSuite) TestDeposits_bKava() {
[]sdk.AccAddress{}, []sdk.AccAddress{},
) )
suite.CreateVault(
"ukava",
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
false,
[]sdk.AccAddress{},
)
address1, derivatives1, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[0], sdk.NewInt(1e9)) address1, derivatives1, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[0], sdk.NewInt(1e9))
address2, derivatives2, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[1], sdk.NewInt(1e9)) address2, derivatives2, _ := suite.createAccountWithDerivatives(testutil.TestBkavaDenoms[1], sdk.NewInt(1e9))
err := suite.App.FundAccount(suite.Ctx, address1, sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1e9))))
suite.Require().NoError(err)
// Slash the last validator to reduce the value of it's derivatives to test bkava to underlying token conversion. // 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. // First call end block to bond validator to enable slashing.
staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper()) staking.EndBlocker(suite.Ctx, suite.App.GetStakingKeeper())
err := suite.slashValidator(sdk.ValAddress(address2), sdk.MustNewDecFromStr("0.5")) err = suite.slashValidator(sdk.ValAddress(address2), sdk.MustNewDecFromStr("0.5"))
suite.Require().NoError(err) suite.Require().NoError(err)
suite.Run("no deposits", func() { suite.Run("no deposits", func() {
@ -458,6 +468,9 @@ func (suite *grpcQueryTestSuite) TestDeposits_bKava() {
err = suite.Keeper.Deposit(suite.Ctx, address1, derivatives2, types.STRATEGY_TYPE_SAVINGS) err = suite.Keeper.Deposit(suite.Ctx, address1, derivatives2, types.STRATEGY_TYPE_SAVINGS)
suite.Require().NoError(err) suite.Require().NoError(err)
err = suite.Keeper.Deposit(suite.Ctx, address1, sdk.NewInt64Coin("ukava", 1e6), types.STRATEGY_TYPE_SAVINGS)
suite.Require().NoError(err)
suite.Run("multiple deposits", func() { suite.Run("multiple deposits", func() {
// Query all deposits for account 1 // Query all deposits for account 1
res, err := suite.queryClient.Deposits( res, err := suite.queryClient.Deposits(
@ -468,6 +481,7 @@ func (suite *grpcQueryTestSuite) TestDeposits_bKava() {
suite.Require().Len(res.Deposits, 1) suite.Require().Len(res.Deposits, 1)
// first validator isn't slashed, so bkava units equal to underlying staked tokens // first validator isn't slashed, so bkava units equal to underlying staked tokens
// last validator slashed 50% so derivatives are worth half // last validator slashed 50% so derivatives are worth half
// Excludes non-bkava deposits
expectedValue := derivatives1.Amount.Add(derivatives2.Amount.QuoRaw(2)) expectedValue := derivatives1.Amount.Add(derivatives2.Amount.QuoRaw(2))
suite.Require().ElementsMatchf( suite.Require().ElementsMatchf(
[]types.DepositResponse{ []types.DepositResponse{

View File

@ -28,6 +28,7 @@ type BankKeeper interface {
// LiquidKeeper defines the expected interface needed for derivative to staked token conversions. // LiquidKeeper defines the expected interface needed for derivative to staked token conversions.
type LiquidKeeper interface { type LiquidKeeper interface {
GetStakedTokensForDerivatives(ctx sdk.Context, derivatives sdk.Coins) (sdk.Coin, error) GetStakedTokensForDerivatives(ctx sdk.Context, derivatives sdk.Coins) (sdk.Coin, error)
IsDerivativeDenom(ctx sdk.Context, denom string) bool
} }
// HardKeeper defines the expected interface needed for the hard strategy. // HardKeeper defines the expected interface needed for the hard strategy.