2022-07-20 23:14:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/earn/testutil"
|
|
|
|
"github.com/kava-labs/kava/x/earn/types"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type vaultTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest()
|
|
|
|
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVaultTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(vaultTestSuite))
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalShares() {
|
2022-07-20 23:14:43 +00:00
|
|
|
vaultDenom := "usdx"
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultTotalShares, found := suite.Keeper.GetVaultTotalShares(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().True(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
suite.Equal(depositAmount.Amount.ToDec(), vaultTotalShares.Amount)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalShares_NotFound() {
|
2022-07-20 23:14:43 +00:00
|
|
|
vaultDenom := "usdx"
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
_, found := suite.Keeper.GetVaultTotalShares(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().False(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalValue() {
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultDenom := "usdx"
|
|
|
|
|
|
|
|
suite.CreateVault(vaultDenom, types.STRATEGY_TYPE_HARD)
|
|
|
|
|
|
|
|
totalValue, err := suite.Keeper.GetVaultTotalValue(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Equal(sdk.NewInt(0), totalValue.Amount)
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalValue_NotFound() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
|
|
|
|
_, err := suite.Keeper.GetVaultTotalValue(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultRecordNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalValue_InvalidStrategy() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
suite.CreateVault(vaultDenom, 99999) // not valid strategy type
|
|
|
|
|
|
|
|
_, err := suite.Keeper.GetVaultTotalValue(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrInvalidVaultStrategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultAccountSupplied() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
deposit1Amount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
deposit2Amount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
acc2 := suite.CreateAccount(sdk.NewCoins(startBalance), 1)
|
|
|
|
|
|
|
|
// Before deposit, account supplied is 0
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
_, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc1.GetAddress())
|
|
|
|
suite.Require().False(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
_, found = suite.Keeper.GetVaultShareRecord(suite.Ctx, acc2.GetAddress())
|
|
|
|
suite.Require().False(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Deposits from both accounts
|
2022-09-12 16:23:26 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc1.GetAddress(), deposit1Amount)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc2.GetAddress(), deposit2Amount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Check balances
|
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultAcc1Supplied, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc1.GetAddress())
|
|
|
|
suite.Require().True(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
2022-09-12 16:23:26 +00:00
|
|
|
vaultAcc2Supplied, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc2.GetAddress())
|
|
|
|
suite.Require().True(found)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Account supply only includes the deposit from respective accounts
|
2022-09-12 16:23:26 +00:00
|
|
|
suite.Equal(deposit1Amount.Amount.ToDec(), vaultAcc1Supplied.Shares.AmountOf(vaultDenom))
|
|
|
|
suite.Equal(deposit1Amount.Amount.ToDec(), vaultAcc2Supplied.Shares.AmountOf(vaultDenom))
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultAccountValue() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
accValue, err := suite.Keeper.GetVaultAccountValue(suite.Ctx, vaultDenom, acc.GetAddress())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Equal(depositAmount, accValue, "value should be same as deposit amount")
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultAccountValue_VaultNotFound() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(), 0)
|
|
|
|
|
|
|
|
_, err := suite.Keeper.GetVaultAccountValue(suite.Ctx, vaultDenom, acc.GetAddress())
|
|
|
|
suite.Require().Error(err)
|
2022-09-12 16:23:26 +00:00
|
|
|
suite.Require().Equal("account vault share record for usdx not found", err.Error())
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultAccountValue_ShareNotFound() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
|
|
|
acc1 := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
acc2 := suite.CreateAccount(sdk.NewCoins(startBalance), 1)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Deposit from acc1 so that vault record exists
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc1.GetAddress(), depositAmount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Query from acc2 with no share record
|
|
|
|
_, err = suite.Keeper.GetVaultAccountValue(suite.Ctx, vaultDenom, acc2.GetAddress())
|
|
|
|
suite.Require().Error(err)
|
2022-09-12 16:23:26 +00:00
|
|
|
suite.Require().Equal("account vault share record for usdx not found", err.Error())
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|