2022-07-20 23:14:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/testutil"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/types"
|
2022-07-20 23:14:43 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
type depositTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *depositTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest()
|
|
|
|
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDepositTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(depositTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_Balances() {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultDenom := "usdx"
|
2022-07-20 23:14:43 +00:00
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
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
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.AccountBalanceEqual(
|
|
|
|
acc.GetAddress(),
|
|
|
|
sdk.NewCoins(startBalance.Sub(depositAmount)), // Account decreases by deposit
|
|
|
|
)
|
|
|
|
|
2022-07-28 16:39:57 +00:00
|
|
|
suite.VaultTotalValuesEqual(sdk.NewCoins(depositAmount))
|
2022-09-12 16:23:26 +00:00
|
|
|
suite.VaultTotalSharesEqual(types.NewVaultShares(
|
2023-04-04 00:08:45 +00:00
|
|
|
types.NewVaultShare(depositAmount.Denom, sdk.NewDecFromInt(depositAmount.Amount)),
|
2022-09-12 16:23:26 +00:00
|
|
|
))
|
2022-07-20 23:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_Exceed() {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultDenom := "usdx"
|
2022-07-20 23:14:43 +00:00
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 1001)
|
|
|
|
|
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
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, sdkerrors.ErrInsufficientFunds)
|
|
|
|
|
|
|
|
// No changes in balances
|
|
|
|
|
|
|
|
suite.AccountBalanceEqual(
|
|
|
|
acc.GetAddress(),
|
|
|
|
sdk.NewCoins(startBalance),
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.ModuleAccountBalanceEqual(
|
|
|
|
sdk.NewCoins(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_Zero() {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultDenom := "usdx"
|
2022-07-20 23:14:43 +00:00
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 0)
|
|
|
|
|
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
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrInsufficientAmount)
|
|
|
|
|
|
|
|
// No changes in balances
|
|
|
|
|
|
|
|
suite.AccountBalanceEqual(
|
|
|
|
acc.GetAddress(),
|
|
|
|
sdk.NewCoins(startBalance),
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.ModuleAccountBalanceEqual(
|
|
|
|
sdk.NewCoins(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_InvalidVault() {
|
2022-07-28 16:39:57 +00:00
|
|
|
vaultDenom := "usdx"
|
2022-07-20 23:14:43 +00:00
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 1001)
|
|
|
|
|
|
|
|
// Vault not created -- doesn't exist
|
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrInvalidVaultDenom)
|
|
|
|
|
|
|
|
// No changes in balances
|
|
|
|
|
|
|
|
suite.AccountBalanceEqual(
|
|
|
|
acc.GetAddress(),
|
|
|
|
sdk.NewCoins(startBalance),
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.ModuleAccountBalanceEqual(
|
|
|
|
sdk.NewCoins(),
|
|
|
|
)
|
|
|
|
}
|
2022-09-12 16:43:59 +00:00
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_InvalidStrategy() {
|
|
|
|
vaultDenom := "usdx"
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 1001)
|
|
|
|
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-09-12 16:43:59 +00:00
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc.GetAddress(), depositAmount, types.STRATEGY_TYPE_SAVINGS)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrInvalidVaultStrategy)
|
|
|
|
}
|
2022-09-12 17:29:35 +00:00
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_PrivateVault() {
|
|
|
|
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)
|
|
|
|
|
|
|
|
suite.CreateVault(
|
|
|
|
vaultDenom,
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_HARD},
|
|
|
|
true,
|
|
|
|
[]sdk.AccAddress{acc1.GetAddress()},
|
|
|
|
)
|
|
|
|
|
|
|
|
err := suite.Keeper.Deposit(suite.Ctx, acc2.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
|
|
suite.Require().Error(err)
|
|
|
|
suite.Require().ErrorIs(err, types.ErrAccountDepositNotAllowed, "private vault should not allow deposits from non-allowed addresses")
|
|
|
|
|
|
|
|
err = suite.Keeper.Deposit(suite.Ctx, acc1.GetAddress(), depositAmount, types.STRATEGY_TYPE_HARD)
|
|
|
|
suite.Require().NoError(err, "private vault should allow deposits from allowed addresses")
|
|
|
|
}
|
2022-09-12 17:50:35 +00:00
|
|
|
|
|
|
|
func (suite *depositTestSuite) TestDeposit_bKava() {
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
}
|