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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalSupplied() {
|
|
|
|
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)
|
|
|
|
|
|
|
|
vaultTotalSupplied, err := suite.Keeper.GetVaultTotalSupplied(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.Equal(depositAmount, vaultTotalSupplied)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalSupplied_NotFound() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
|
|
|
|
_, err := suite.Keeper.GetVaultTotalSupplied(suite.Ctx, vaultDenom)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultRecordNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultTotalValue() {
|
|
|
|
// TODO: After strategy implemented GetEstimatedTotalAssets
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-28 16:39:57 +00:00
|
|
|
_, err := suite.Keeper.GetVaultAccountSupplied(suite.Ctx, acc1.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultShareRecordNotFound)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
_, err = suite.Keeper.GetVaultAccountSupplied(suite.Ctx, acc2.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultShareRecordNotFound)
|
|
|
|
|
|
|
|
// Deposits from both accounts
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc1.GetAddress(), deposit1Amount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc2.GetAddress(), deposit2Amount)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Check balances
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultAcc1Supplied, err := suite.Keeper.GetVaultAccountSupplied(suite.Ctx, acc1.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultAcc2Supplied, err := suite.Keeper.GetVaultAccountSupplied(suite.Ctx, acc2.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Account supply only includes the deposit from respective accounts
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.Equal(sdk.NewCoins(deposit1Amount), vaultAcc1Supplied)
|
|
|
|
suite.Equal(sdk.NewCoins(deposit1Amount), vaultAcc2Supplied)
|
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)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultRecordNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrVaultShareRecordNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// State methods
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultRecord() {
|
|
|
|
record := types.NewVaultRecord("usdx")
|
|
|
|
|
|
|
|
_, found := suite.Keeper.GetVaultRecord(suite.Ctx, record.Denom)
|
|
|
|
suite.Require().False(found)
|
|
|
|
|
|
|
|
suite.Keeper.SetVaultRecord(suite.Ctx, record)
|
|
|
|
|
|
|
|
stateRecord, found := suite.Keeper.GetVaultRecord(suite.Ctx, record.Denom)
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(record, stateRecord)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestUpdateVaultRecord() {
|
|
|
|
record := types.NewVaultRecord("usdx")
|
|
|
|
|
|
|
|
record.TotalSupply = sdk.NewInt64Coin("usdx", 100)
|
|
|
|
|
|
|
|
// Update vault
|
|
|
|
suite.Keeper.UpdateVaultRecord(suite.Ctx, record)
|
|
|
|
|
|
|
|
stateRecord, found := suite.Keeper.GetVaultRecord(suite.Ctx, record.Denom)
|
|
|
|
suite.Require().True(found, "vault record with supply should exist")
|
|
|
|
suite.Require().Equal(record, stateRecord)
|
|
|
|
|
|
|
|
// Remove supply
|
|
|
|
record.TotalSupply = sdk.NewInt64Coin("usdx", 0)
|
|
|
|
suite.Keeper.UpdateVaultRecord(suite.Ctx, record)
|
|
|
|
|
|
|
|
_, found = suite.Keeper.GetVaultRecord(suite.Ctx, record.Denom)
|
|
|
|
suite.Require().False(found, "vault record with 0 supply should be deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestGetVaultShareRecord() {
|
|
|
|
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
|
|
|
record := types.NewVaultShareRecord(acc.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Check share doesn't exist before deposit
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
_, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().False(found, "vault share record should not exist before deposit")
|
|
|
|
|
|
|
|
// Update share record
|
2022-07-28 16:39:57 +00:00
|
|
|
record.AmountSupplied = sdk.NewCoins(depositAmount)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Keeper.SetVaultShareRecord(suite.Ctx, record)
|
|
|
|
|
|
|
|
// Check share exists and matches set value
|
2022-07-28 16:39:57 +00:00
|
|
|
stateRecord, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(record, stateRecord)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *vaultTestSuite) TestUpdateVaultShareRecord() {
|
|
|
|
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
|
|
|
record := types.NewVaultShareRecord(acc.GetAddress(), depositAmount)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
// Update vault
|
|
|
|
suite.Keeper.UpdateVaultShareRecord(suite.Ctx, record)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
stateRecord, found := suite.Keeper.GetVaultShareRecord(suite.Ctx, acc.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().True(found, "vault share record with supply should exist")
|
|
|
|
suite.Require().Equal(record, stateRecord)
|
|
|
|
|
|
|
|
// Remove supply
|
2022-07-28 16:39:57 +00:00
|
|
|
record.AmountSupplied = sdk.NewCoins()
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Keeper.UpdateVaultShareRecord(suite.Ctx, record)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
_, found = suite.Keeper.GetVaultShareRecord(suite.Ctx, acc.GetAddress())
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().False(found, "vault share record with 0 supply should be deleted")
|
|
|
|
}
|