From a068a903eff2fc1bc0cadea8007f2822e1e7b139 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:05:23 +0100 Subject: [PATCH] test(amm): increase test coverage (#933) * test(amm): increase test coverage * test(amm): fix minor * test(estaking): improve test coverage --- scripts/examples/wasm/wasm.sh | 2 - x/amm/keeper/abci_test.go | 160 +++++++++++- .../apply_exit_pool_state_change_test.go | 236 +++++++++++++----- x/amm/keeper/calc_in_route_spot_price_test.go | 2 +- .../keeper/calc_out_route_spot_price_test.go | 2 +- .../calc_swap_estimation_by_denom_test.go | 2 +- x/amm/keeper/fee_test.go | 7 +- .../keeper_swap_exact_amount_in_test.go | 2 +- .../keeper_swap_exact_amount_out_test.go | 2 +- x/amm/keeper/keeper_test.go | 52 +++- x/amm/keeper/msg_server_create_pool_test.go | 5 +- x/amm/keeper/msg_server_exit_pool_test.go | 2 +- ...r_feed_multiple_external_liquidity_test.go | 4 +- x/amm/keeper/msg_server_join_pool_test.go | 4 +- x/amm/keeper/msg_server_swap_by_denom_test.go | 2 +- .../msg_server_swap_exact_amount_in_test.go | 4 +- .../msg_server_swap_exact_amount_out_test.go | 2 +- .../msg_server_update_pool_params_test.go | 2 +- .../keeper/oracle_pool_slippage_track_test.go | 2 +- x/amm/keeper/route_exact_amount_in_test.go | 2 +- x/amm/keeper/route_exact_amount_out_test.go | 2 +- x/amm/keeper/update_pool_for_swap_test.go | 2 +- x/estaking/keeper/hooks_staking_test.go | 91 +++++++ x/estaking/keeper/keeper_test.go | 16 +- x/perpetual/keeper/keeper_test.go | 10 +- 25 files changed, 507 insertions(+), 110 deletions(-) delete mode 100644 scripts/examples/wasm/wasm.sh create mode 100644 x/estaking/keeper/hooks_staking_test.go diff --git a/scripts/examples/wasm/wasm.sh b/scripts/examples/wasm/wasm.sh deleted file mode 100644 index fccfb0d86..000000000 --- a/scripts/examples/wasm/wasm.sh +++ /dev/null @@ -1,2 +0,0 @@ -elysd tx wasm store cw20_base.wasm --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --gas=auto --gas-adjustment=1.3 -y -elysd tx wasm instantiate 1 '{"name":"Cw20","symbol":"cwww", "decimals":6, "initial_balances":[]}' --from=treasury --label "Cw20" --chain-id=elystestnet-1 --gas=auto --gas-adjustment=1.3 -b=sync --keyring-backend=test --no-admin -y \ No newline at end of file diff --git a/x/amm/keeper/abci_test.go b/x/amm/keeper/abci_test.go index c56fc8050..ac667c36a 100644 --- a/x/amm/keeper/abci_test.go +++ b/x/amm/keeper/abci_test.go @@ -13,7 +13,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestExecuteSwapRequests() { +func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() { sender := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) for _, tc := range []struct { desc string @@ -206,7 +206,7 @@ func (suite *KeeperTestSuite) TestExecuteSwapRequests() { }, } { suite.Run(tc.desc, func() { - //suite.SetupTest() + suite.ResetSuite() // bootstrap accounts poolAddr := types.NewPoolAddress(uint64(1)) @@ -315,7 +315,7 @@ func (suite *KeeperTestSuite) TestExecuteSwapRequests() { } } -func (suite *KeeperTestSuite) TestClearOutdatedSlippageTrack() { +func (suite *AmmKeeperTestSuite) TestClearOutdatedSlippageTrack() { now := time.Now() tracks := []types.OraclePoolSlippageTrack{ { @@ -343,3 +343,157 @@ func (suite *KeeperTestSuite) TestClearOutdatedSlippageTrack() { tracksStored := suite.app.AmmKeeper.AllSlippageTracks(suite.ctx) suite.Require().Len(tracksStored, 2) } + +func (suite *AmmKeeperTestSuite) TestAbci() { + testCases := []struct { + name string + prerequisiteFunction func() + postValidateFunction func() + }{ + { + "first pool id with swap amount out", + func() { + suite.ResetSuite() + + addr := suite.AddAccounts(1, nil)[0] + + msg := &types.MsgSwapExactAmountOut{ + Sender: addr.String(), + Routes: []types.SwapAmountOutRoute{ + { + PoolId: 1, + TokenInDenom: ptypes.BaseCurrency, + }, + }, + TokenOut: sdk.NewInt64Coin(ptypes.Elys, 8000), + TokenInMaxAmount: sdkmath.NewInt(1000000), + Discount: sdkmath.LegacyZeroDec(), + } + + poolId := suite.app.AmmKeeper.FirstPoolId(msg) + suite.Require().Equal(uint64(1), poolId) + }, + func() {}, + }, + { + "first pool id with a msg that is neither swap amount in nor swap amount out", + func() { + suite.ResetSuite() + + msg := sdk.Msg(nil) + + poolId := suite.app.AmmKeeper.FirstPoolId(msg) + suite.Require().Equal(uint64(0), poolId) + }, + func() {}, + }, + { + "apply swap request with invalid address in swap exact amount in msg", + func() { + suite.ResetSuite() + + msg := &types.MsgSwapExactAmountIn{ + Sender: "invalid", + } + + err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg) + suite.Require().Error(err) + }, + func() {}, + }, + { + "apply swap request with invalid denom in swap exact amount in msg", + func() { + suite.ResetSuite() + + addr := suite.AddAccounts(1, nil)[0] + + msg := &types.MsgSwapExactAmountIn{ + Sender: addr.String(), + Routes: []types.SwapAmountInRoute{ + { + PoolId: 1, + TokenOutDenom: "invalid", + }, + }, + TokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000), + TokenOutMinAmount: sdkmath.ZeroInt(), + Discount: sdkmath.LegacyZeroDec(), + } + + err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg) + suite.Require().Error(err) + }, + func() {}, + }, + { + "apply swap request with invalid address in swap exact amount in msg", + func() { + suite.ResetSuite() + + msg := &types.MsgSwapExactAmountOut{ + Sender: "invalid", + } + + err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg) + suite.Require().Error(err) + }, + func() {}, + }, + { + "apply swap request with invalid denom in swap exact amount in msg", + func() { + suite.ResetSuite() + + addr := suite.AddAccounts(1, nil)[0] + + msg := &types.MsgSwapExactAmountOut{ + Sender: addr.String(), + Routes: []types.SwapAmountOutRoute{ + { + PoolId: 1, + TokenInDenom: "invalid", + }, + }, + TokenOut: sdk.NewInt64Coin(ptypes.Elys, 10000), + TokenInMaxAmount: sdkmath.ZeroInt(), + Discount: sdkmath.LegacyZeroDec(), + } + + err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg) + suite.Require().Error(err) + }, + func() {}, + }, + { + "apply swap request with invalid swap msg type", + func() { + suite.ResetSuite() + + msg := sdk.Msg(nil) + + err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg) + suite.Require().Error(err) + }, + func() {}, + }, + { + "get stacked slippage when get pool returns not found", + func() { + suite.ResetSuite() + + poolId := uint64(2) + ratio := suite.app.AmmKeeper.GetStackedSlippage(suite.ctx, poolId) + suite.Require().Equal(sdkmath.LegacyZeroDec(), ratio) + }, + func() {}, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + tc.prerequisiteFunction() + tc.postValidateFunction() + }) + } +} diff --git a/x/amm/keeper/apply_exit_pool_state_change_test.go b/x/amm/keeper/apply_exit_pool_state_change_test.go index f25f0478a..7bdf6853a 100644 --- a/x/amm/keeper/apply_exit_pool_state_change_test.go +++ b/x/amm/keeper/apply_exit_pool_state_change_test.go @@ -8,77 +8,193 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" simapp "github.com/elys-network/elys/app" "github.com/elys-network/elys/x/amm/types" - atypes "github.com/elys-network/elys/x/amm/types" ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestApplyExitPoolStateChange_WithdrawFromCommitmentModule() { - suite.SetupStableCoinPrices() - suite.SetupAssetProfile() +func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() { + testCases := []struct { + name string + prerequisiteFunction func() + postValidateFunction func() + }{ + { + "withdraw from commitment module", + func() { + suite.ResetSuite() + + suite.SetupStableCoinPrices() + suite.SetupAssetProfile() + + app := suite.app + amm, bk := app.AmmKeeper, app.BankKeeper + ctx := suite.ctx + + err := simapp.SetStakingParam(app, ctx) + suite.Require().NoError(err) + // Generate 1 random account with 1000stake balanced + addrs := simapp.AddTestAddrs(app, ctx, 1, sdkmath.NewInt(1000000)) + + // Mint 100000USDC+100000USDT + coins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin("uusdt", sdkmath.NewInt(100000))) + err = app.BankKeeper.MintCoins(ctx, types.ModuleName, coins) + suite.Require().NoError(err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addrs[0], coins) + suite.Require().NoError(err) + + poolAssets := []types.PoolAsset{ + { + Weight: sdkmath.NewInt(50), + Token: sdk.NewCoin("uusdt", sdkmath.NewInt(100000)), + }, + { + Weight: sdkmath.NewInt(50), + Token: sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), + }, + } + + swapFee, err := sdkmath.LegacyNewDecFromStr("0.1") + suite.Require().NoError(err) + + exitFee, err := sdkmath.LegacyNewDecFromStr("0.1") + suite.Require().NoError(err) + + poolParams := &types.PoolParams{ + SwapFee: swapFee, + ExitFee: exitFee, + UseOracle: true, + WeightBreakingFeeMultiplier: sdkmath.LegacyZeroDec(), + WeightBreakingFeeExponent: sdkmath.LegacyNewDecWithPrec(25, 1), // 2.5 + WeightRecoveryFeePortion: sdkmath.LegacyNewDecWithPrec(10, 2), // 10% + ThresholdWeightDifference: sdkmath.LegacyZeroDec(), + FeeDenom: ptypes.BaseCurrency, + } + + msg := types.NewMsgCreatePool( + addrs[0].String(), + poolParams, + poolAssets, + ) + + // Create a USDT+USDC pool + poolId, err := amm.CreatePool(ctx, msg) + suite.Require().NoError(err) + suite.Require().Equal(poolId, uint64(1)) + + pool, found := amm.GetPool(ctx, poolId) + suite.Require().True(found) + + lpTokenDenom := types.GetPoolShareDenom(poolId) + lpTokenBalance := bk.GetBalance(ctx, addrs[0], lpTokenDenom) + suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt())) + + ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour)) + _, err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false) + suite.Require().NoError(err) + }, + func() {}, + }, + { + "asset profile not found", + func() { + suite.ResetSuite() - app := suite.app - amm, bk := app.AmmKeeper, app.BankKeeper - ctx := suite.ctx + pool := types.Pool{ + PoolId: 1, + } - err := simapp.SetStakingParam(app, ctx) - suite.Require().NoError(err) - // Generate 1 random account with 1000stake balanced - addrs := simapp.AddTestAddrs(app, ctx, 1, sdkmath.NewInt(1000000)) + addr := suite.AddAccounts(1, nil)[0] - // Mint 100000USDC+100000USDT - coins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin("uusdt", sdkmath.NewInt(100000))) - err = app.BankKeeper.MintCoins(ctx, types.ModuleName, coins) - suite.Require().NoError(err) - err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addrs[0], coins) - suite.Require().NoError(err) + coins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin("uusdt", sdkmath.NewInt(100000))) - poolAssets := []atypes.PoolAsset{ - { - Weight: sdkmath.NewInt(50), - Token: sdk.NewCoin("uusdt", sdkmath.NewInt(100000)), + _, err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false) + suite.Require().Error(err) + }, + func() {}, }, { - Weight: sdkmath.NewInt(50), - Token: sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), + "withdraw from commitment module with invalid denom", + func() { + suite.ResetSuite() + + suite.SetupStableCoinPrices() + suite.SetupAssetProfile() + + app := suite.app + amm, bk := app.AmmKeeper, app.BankKeeper + ctx := suite.ctx + + err := simapp.SetStakingParam(app, ctx) + suite.Require().NoError(err) + // Generate 1 random account with 1000stake balanced + addr := suite.AddAccounts(1, nil)[0] + + // Mint 100000USDC+100000USDT + coins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin("uusdt", sdkmath.NewInt(100000))) + err = app.BankKeeper.MintCoins(ctx, types.ModuleName, coins) + suite.Require().NoError(err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, coins) + suite.Require().NoError(err) + + poolAssets := []types.PoolAsset{ + { + Weight: sdkmath.NewInt(50), + Token: sdk.NewCoin("uusdt", sdkmath.NewInt(100000)), + }, + { + Weight: sdkmath.NewInt(50), + Token: sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), + }, + } + + swapFee, err := sdkmath.LegacyNewDecFromStr("0.1") + suite.Require().NoError(err) + + exitFee, err := sdkmath.LegacyNewDecFromStr("0.1") + suite.Require().NoError(err) + + poolParams := &types.PoolParams{ + SwapFee: swapFee, + ExitFee: exitFee, + UseOracle: true, + WeightBreakingFeeMultiplier: sdkmath.LegacyZeroDec(), + WeightBreakingFeeExponent: sdkmath.LegacyNewDecWithPrec(25, 1), // 2.5 + WeightRecoveryFeePortion: sdkmath.LegacyNewDecWithPrec(10, 2), // 10% + ThresholdWeightDifference: sdkmath.LegacyZeroDec(), + FeeDenom: ptypes.BaseCurrency, + } + + msg := types.NewMsgCreatePool( + addr.String(), + poolParams, + poolAssets, + ) + + // Create a USDT+USDC pool + poolId, err := amm.CreatePool(ctx, msg) + suite.Require().NoError(err) + suite.Require().Equal(poolId, uint64(1)) + + pool, found := amm.GetPool(ctx, poolId) + suite.Require().True(found) + + lpTokenDenom := types.GetPoolShareDenom(poolId) + lpTokenBalance := bk.GetBalance(ctx, addr, lpTokenDenom) + suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt())) + + coins = sdk.NewCoins(sdk.NewCoin("invalid_denom", sdkmath.NewInt(100000000))) + + ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour)) + _, err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false) + suite.Require().Error(err) + }, + func() {}, }, } - swapFee, err := sdkmath.LegacyNewDecFromStr("0.1") - suite.Require().NoError(err) - - exitFee, err := sdkmath.LegacyNewDecFromStr("0.1") - suite.Require().NoError(err) - - poolParams := &atypes.PoolParams{ - SwapFee: swapFee, - ExitFee: exitFee, - UseOracle: true, - WeightBreakingFeeMultiplier: sdkmath.LegacyZeroDec(), - WeightBreakingFeeExponent: sdkmath.LegacyNewDecWithPrec(25, 1), // 2.5 - WeightRecoveryFeePortion: sdkmath.LegacyNewDecWithPrec(10, 2), // 10% - ThresholdWeightDifference: sdkmath.LegacyZeroDec(), - FeeDenom: ptypes.BaseCurrency, + for _, tc := range testCases { + suite.Run(tc.name, func() { + tc.prerequisiteFunction() + tc.postValidateFunction() + }) } - - msg := types.NewMsgCreatePool( - addrs[0].String(), - poolParams, - poolAssets, - ) - - // Create a USDT+USDC pool - poolId, err := amm.CreatePool(ctx, msg) - suite.Require().NoError(err) - suite.Require().Equal(poolId, uint64(1)) - - pool, found := amm.GetPool(ctx, poolId) - suite.Require().True(found) - - lpTokenDenom := types.GetPoolShareDenom(poolId) - lpTokenBalance := bk.GetBalance(ctx, addrs[0], lpTokenDenom) - suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt())) - - ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour)) - _, err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false) - suite.Require().NoError(err) } diff --git a/x/amm/keeper/calc_in_route_spot_price_test.go b/x/amm/keeper/calc_in_route_spot_price_test.go index 73cb9b55e..7b7e0dcc1 100644 --- a/x/amm/keeper/calc_in_route_spot_price_test.go +++ b/x/amm/keeper/calc_in_route_spot_price_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestCalcInRouteSpotPrice() { +func (suite *AmmKeeperTestSuite) TestCalcInRouteSpotPrice() { poolInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} pool2InitBalance := sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} senderInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} diff --git a/x/amm/keeper/calc_out_route_spot_price_test.go b/x/amm/keeper/calc_out_route_spot_price_test.go index 1551eff39..4f97318e1 100644 --- a/x/amm/keeper/calc_out_route_spot_price_test.go +++ b/x/amm/keeper/calc_out_route_spot_price_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestCalcOutRouteSpotPrice() { +func (suite *AmmKeeperTestSuite) TestCalcOutRouteSpotPrice() { poolInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} pool2InitBalance := sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} senderInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} diff --git a/x/amm/keeper/calc_swap_estimation_by_denom_test.go b/x/amm/keeper/calc_swap_estimation_by_denom_test.go index 25d8b3897..101323635 100644 --- a/x/amm/keeper/calc_swap_estimation_by_denom_test.go +++ b/x/amm/keeper/calc_swap_estimation_by_denom_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestCalcSwapEstimationByDenom() { +func (suite *AmmKeeperTestSuite) TestCalcSwapEstimationByDenom() { poolInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} pool2InitBalance := sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} senderInitBalance := sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)} diff --git a/x/amm/keeper/fee_test.go b/x/amm/keeper/fee_test.go index 552687e32..d2e4a73cc 100644 --- a/x/amm/keeper/fee_test.go +++ b/x/amm/keeper/fee_test.go @@ -1,9 +1,10 @@ package keeper_test import ( - sdkmath "cosmossdk.io/math" "testing" + sdkmath "cosmossdk.io/math" + "github.com/cometbft/cometbft/crypto/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" @@ -25,7 +26,7 @@ func TestPortionCoins(t *testing.T) { require.Equal(t, portion, coins) } -func (suite *KeeperTestSuite) TestOnCollectFee() { +func (suite *AmmKeeperTestSuite) TestOnCollectFee() { for _, tc := range []struct { desc string fee sdk.Coins @@ -121,7 +122,7 @@ func (suite *KeeperTestSuite) TestOnCollectFee() { } } -func (suite *KeeperTestSuite) TestSwapFeesToRevenueToken() { +func (suite *AmmKeeperTestSuite) TestSwapFeesToRevenueToken() { for _, tc := range []struct { desc string fee sdk.Coins diff --git a/x/amm/keeper/keeper_swap_exact_amount_in_test.go b/x/amm/keeper/keeper_swap_exact_amount_in_test.go index 97e119a1b..4b1d70443 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_in_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_in_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestSwapExactAmountIn() { +func (suite *AmmKeeperTestSuite) TestSwapExactAmountIn() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/keeper_swap_exact_amount_out_test.go b/x/amm/keeper/keeper_swap_exact_amount_out_test.go index a58d547fc..09a38956c 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_out_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_out_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestSwapExactAmountOut() { +func (suite *AmmKeeperTestSuite) TestSwapExactAmountOut() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/keeper_test.go b/x/amm/keeper/keeper_test.go index d8595e7a7..ea7421fef 100644 --- a/x/amm/keeper/keeper_test.go +++ b/x/amm/keeper/keeper_test.go @@ -3,11 +3,13 @@ package keeper_test import ( "testing" + "cosmossdk.io/math" sdkmath "cosmossdk.io/math" "github.com/cometbft/cometbft/crypto/ed25519" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" simapp "github.com/elys-network/elys/app" "github.com/elys-network/elys/x/amm/keeper" "github.com/elys-network/elys/x/amm/types" @@ -57,7 +59,7 @@ var ( } ) -type KeeperTestSuite struct { +type AmmKeeperTestSuite struct { suite.Suite legacyAmino *codec.LegacyAmino @@ -65,8 +67,7 @@ type KeeperTestSuite struct { app *simapp.ElysApp } -func (suite *KeeperTestSuite) SetupTest() { - //t.Parallel() +func (suite *AmmKeeperTestSuite) SetupTest() { app := simapp.InitElysTestApp(initChain, suite.Suite.T()) suite.legacyAmino = app.LegacyAmino() @@ -75,17 +76,52 @@ func (suite *KeeperTestSuite) SetupTest() { } func TestKeeperSuite(t *testing.T) { - suite.Run(t, new(KeeperTestSuite)) + suite.Run(t, new(AmmKeeperTestSuite)) } -func (suite *KeeperTestSuite) SetAmmParams() { +func (suite *AmmKeeperTestSuite) ResetSuite() { + suite.SetupTest() +} + +func (suite *AmmKeeperTestSuite) GetAccountIssueAmount() math.Int { + return math.NewInt(10_000_000_000_000) +} + +func (suite *AmmKeeperTestSuite) AddAccounts(n int, given []sdk.AccAddress) []sdk.AccAddress { + issueAmount := suite.GetAccountIssueAmount() + var addresses []sdk.AccAddress + if n > len(given) { + addresses = simapp.AddTestAddrs(suite.app, suite.ctx, n-len(given), issueAmount) + addresses = append(addresses, given...) + } else { + addresses = given + } + for _, address := range addresses { + coins := sdk.NewCoins( + sdk.NewCoin(ptypes.ATOM, issueAmount), + sdk.NewCoin(ptypes.Elys, issueAmount), + sdk.NewCoin(ptypes.BaseCurrency, issueAmount), + ) + err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, coins) + if err != nil { + panic(err) + } + err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, address, coins) + if err != nil { + panic(err) + } + } + return addresses +} + +func (suite *AmmKeeperTestSuite) SetAmmParams() { suite.app.AmmKeeper.SetParams(suite.ctx, types.Params{ PoolCreationFee: sdkmath.NewInt(10000000), SlippageTrackDuration: 604800, }) } -func (suite *KeeperTestSuite) SetupAssetProfile() { +func (suite *AmmKeeperTestSuite) SetupAssetProfile() { suite.app.AssetprofileKeeper.SetEntry(suite.ctx, atypes.Entry{ BaseDenom: "uusdc", Decimals: 6, @@ -109,7 +145,7 @@ func (suite *KeeperTestSuite) SetupAssetProfile() { }) } -func (suite *KeeperTestSuite) SetupStableCoinPrices() { +func (suite *AmmKeeperTestSuite) SetupStableCoinPrices() { // prices set for USDT and USDC provider := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) suite.app.OracleKeeper.SetAssetInfo(suite.ctx, oracletypes.AssetInfo{ @@ -150,7 +186,7 @@ func (suite *KeeperTestSuite) SetupStableCoinPrices() { }) } -func (suite *KeeperTestSuite) SetupCoinPrices() { +func (suite *AmmKeeperTestSuite) SetupCoinPrices() { // prices set for USDT and USDC provider := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) diff --git a/x/amm/keeper/msg_server_create_pool_test.go b/x/amm/keeper/msg_server_create_pool_test.go index a9c7dfc3c..adaf98f25 100644 --- a/x/amm/keeper/msg_server_create_pool_test.go +++ b/x/amm/keeper/msg_server_create_pool_test.go @@ -1,8 +1,9 @@ package keeper_test import ( - sdkmath "cosmossdk.io/math" "fmt" + + sdkmath "cosmossdk.io/math" "github.com/cometbft/cometbft/crypto/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" @@ -11,7 +12,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerCreatePool() { +func (suite *AmmKeeperTestSuite) TestMsgServerCreatePool() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/msg_server_exit_pool_test.go b/x/amm/keeper/msg_server_exit_pool_test.go index 4515ab133..700ce84bc 100644 --- a/x/amm/keeper/msg_server_exit_pool_test.go +++ b/x/amm/keeper/msg_server_exit_pool_test.go @@ -12,7 +12,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerExitPool() { +func (suite *AmmKeeperTestSuite) TestMsgServerExitPool() { for _, tc := range []struct { desc string poolInitBalance sdk.Coins diff --git a/x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go b/x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go index f3a0274cc..14640ad2c 100644 --- a/x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go +++ b/x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func (suite *KeeperTestSuite) TestLiquidityRatioFromPriceDepth() { +func (suite *AmmKeeperTestSuite) TestLiquidityRatioFromPriceDepth() { depth := sdkmath.LegacyNewDecWithPrec(1, 2) // 1% suite.Require().Equal("0.005012562893380045", keeper.LiquidityRatioFromPriceDepth(depth).String()) depth = sdkmath.LegacyNewDecWithPrec(2, 2) // 2% @@ -33,7 +33,7 @@ func (suite *KeeperTestSuite) TestLiquidityRatioFromPriceDepth() { suite.Require().Equal("1.000000000000000000", keeper.LiquidityRatioFromPriceDepth(depth).String()) } -func (suite *KeeperTestSuite) TestGetExternalLiquidityRatio() { +func (suite *AmmKeeperTestSuite) TestGetExternalLiquidityRatio() { suite.SetupTest() suite.SetupCoinPrices() // set asset profile diff --git a/x/amm/keeper/msg_server_join_pool_test.go b/x/amm/keeper/msg_server_join_pool_test.go index 75318dbe5..f7c2be208 100644 --- a/x/amm/keeper/msg_server_join_pool_test.go +++ b/x/amm/keeper/msg_server_join_pool_test.go @@ -10,7 +10,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerJoinPool() { +func (suite *AmmKeeperTestSuite) TestMsgServerJoinPool() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins @@ -211,7 +211,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { } } -func (suite *KeeperTestSuite) TestMsgServerJoinPoolExploitScenario() { +func (suite *AmmKeeperTestSuite) TestMsgServerJoinPoolExploitScenario() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/msg_server_swap_by_denom_test.go b/x/amm/keeper/msg_server_swap_by_denom_test.go index 6e526d7e4..49c901b74 100644 --- a/x/amm/keeper/msg_server_swap_by_denom_test.go +++ b/x/amm/keeper/msg_server_swap_by_denom_test.go @@ -11,7 +11,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerSwapByDenom() { +func (suite *AmmKeeperTestSuite) TestMsgServerSwapByDenom() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/msg_server_swap_exact_amount_in_test.go b/x/amm/keeper/msg_server_swap_exact_amount_in_test.go index 903eb4454..a621cfc1f 100644 --- a/x/amm/keeper/msg_server_swap_exact_amount_in_test.go +++ b/x/amm/keeper/msg_server_swap_exact_amount_in_test.go @@ -12,7 +12,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerSwapExactAmountIn() { +func (suite *AmmKeeperTestSuite) TestMsgServerSwapExactAmountIn() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins @@ -203,7 +203,7 @@ func (suite *KeeperTestSuite) TestMsgServerSwapExactAmountIn() { } } -func (suite *KeeperTestSuite) TestMsgServerSlippageDifferenceWhenSplit() { +func (suite *AmmKeeperTestSuite) TestMsgServerSlippageDifferenceWhenSplit() { //suite.SetupTest() suite.SetupStableCoinPrices() diff --git a/x/amm/keeper/msg_server_swap_exact_amount_out_test.go b/x/amm/keeper/msg_server_swap_exact_amount_out_test.go index 846e0a8cf..db9275cae 100644 --- a/x/amm/keeper/msg_server_swap_exact_amount_out_test.go +++ b/x/amm/keeper/msg_server_swap_exact_amount_out_test.go @@ -10,7 +10,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerSwapExactAmountOut() { +func (suite *AmmKeeperTestSuite) TestMsgServerSwapExactAmountOut() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/msg_server_update_pool_params_test.go b/x/amm/keeper/msg_server_update_pool_params_test.go index 7ebc5948d..c6f1f33be 100644 --- a/x/amm/keeper/msg_server_update_pool_params_test.go +++ b/x/amm/keeper/msg_server_update_pool_params_test.go @@ -11,7 +11,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { +func (suite *AmmKeeperTestSuite) TestMsgServerUpdatePoolParams() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/oracle_pool_slippage_track_test.go b/x/amm/keeper/oracle_pool_slippage_track_test.go index 1d12fb6c8..82655a13c 100644 --- a/x/amm/keeper/oracle_pool_slippage_track_test.go +++ b/x/amm/keeper/oracle_pool_slippage_track_test.go @@ -5,7 +5,7 @@ import ( "github.com/elys-network/elys/x/amm/types" ) -func (suite *KeeperTestSuite) TestSlippageTrack() { +func (suite *AmmKeeperTestSuite) TestSlippageTrack() { tracks := []types.OraclePoolSlippageTrack{ { PoolId: 1, diff --git a/x/amm/keeper/route_exact_amount_in_test.go b/x/amm/keeper/route_exact_amount_in_test.go index 7951ff5bc..632cc38c5 100644 --- a/x/amm/keeper/route_exact_amount_in_test.go +++ b/x/amm/keeper/route_exact_amount_in_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestRouteExactAmountIn() { +func (suite *AmmKeeperTestSuite) TestRouteExactAmountIn() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/route_exact_amount_out_test.go b/x/amm/keeper/route_exact_amount_out_test.go index fd203e50f..408af6cb1 100644 --- a/x/amm/keeper/route_exact_amount_out_test.go +++ b/x/amm/keeper/route_exact_amount_out_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestRouteExactAmountOut() { +func (suite *AmmKeeperTestSuite) TestRouteExactAmountOut() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/amm/keeper/update_pool_for_swap_test.go b/x/amm/keeper/update_pool_for_swap_test.go index 0d2ea3495..b1ba5b7ef 100644 --- a/x/amm/keeper/update_pool_for_swap_test.go +++ b/x/amm/keeper/update_pool_for_swap_test.go @@ -9,7 +9,7 @@ import ( ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (suite *KeeperTestSuite) TestUpdatePoolForSwap() { +func (suite *AmmKeeperTestSuite) TestUpdatePoolForSwap() { for _, tc := range []struct { desc string senderInitBalance sdk.Coins diff --git a/x/estaking/keeper/hooks_staking_test.go b/x/estaking/keeper/hooks_staking_test.go new file mode 100644 index 000000000..a61ad7712 --- /dev/null +++ b/x/estaking/keeper/hooks_staking_test.go @@ -0,0 +1,91 @@ +package keeper_test + +import ( + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (suite *EstakingKeeperTestSuite) TestHooksStaking() { + testCases := []struct { + name string + prerequisiteFunction func() + postValidateFunction func() + }{ + { + "AfterUnbondingInitiated", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + err := hooks.AfterUnbondingInitiated(suite.ctx, 0) + suite.Require().Nil(err) + }, + func() {}, + }, + { + "BeforeValidatorModified", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + err := hooks.BeforeValidatorModified(suite.ctx, suite.valAddr) + suite.Require().Nil(err) + }, + func() {}, + }, + { + "AfterValidatorRemoved", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + consAddr := sdk.ConsAddress(suite.ctx.BlockHeader().ProposerAddress) + err := hooks.AfterValidatorRemoved(suite.ctx, consAddr, suite.valAddr) + suite.Require().Nil(err) + }, + func() {}, + }, + { + "AfterValidatorBonded", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + consAddr := sdk.ConsAddress(suite.ctx.BlockHeader().ProposerAddress) + err := hooks.AfterValidatorBonded(suite.ctx, consAddr, suite.valAddr) + suite.Require().Nil(err) + }, + func() {}, + }, + { + "AfterValidatorBeginUnbonding", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + consAddr := sdk.ConsAddress(suite.ctx.BlockHeader().ProposerAddress) + err := hooks.AfterValidatorBeginUnbonding(suite.ctx, consAddr, suite.valAddr) + suite.Require().Nil(err) + }, + func() {}, + }, + { + "BeforeValidatorSlashed", + func() { + suite.ResetSuite() + + hooks := suite.app.EstakingKeeper.Hooks() + err := hooks.BeforeValidatorSlashed(suite.ctx, suite.valAddr, math.LegacyOneDec()) + suite.Require().Nil(err) + }, + func() {}, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + tc.prerequisiteFunction() + tc.postValidateFunction() + }) + } +} diff --git a/x/estaking/keeper/keeper_test.go b/x/estaking/keeper/keeper_test.go index 17e03eeb4..8fef2fe56 100644 --- a/x/estaking/keeper/keeper_test.go +++ b/x/estaking/keeper/keeper_test.go @@ -31,14 +31,14 @@ type EstakingKeeperTestSuite struct { valAddr sdk.ValAddress } -func (k *EstakingKeeperTestSuite) SetupTest() { - app, genAccount, valAddr := simapp.InitElysTestAppWithGenAccount(k.T()) - - k.legacyAmino = app.LegacyAmino() - k.ctx = app.BaseApp.NewContext(initChain) - k.app = app - k.genAccount = genAccount - k.valAddr = valAddr +func (suite *EstakingKeeperTestSuite) SetupTest() { + app, genAccount, valAddr := simapp.InitElysTestAppWithGenAccount(suite.T()) + + suite.legacyAmino = app.LegacyAmino() + suite.ctx = app.BaseApp.NewContext(initChain) + suite.app = app + suite.genAccount = genAccount + suite.valAddr = valAddr } func TestKeeperSuite(t *testing.T) { diff --git a/x/perpetual/keeper/keeper_test.go b/x/perpetual/keeper/keeper_test.go index e26576f14..cf1e21c18 100644 --- a/x/perpetual/keeper/keeper_test.go +++ b/x/perpetual/keeper/keeper_test.go @@ -69,12 +69,12 @@ type PerpetualKeeperTestSuite struct { app *simapp.ElysApp } -func (k *PerpetualKeeperTestSuite) SetupTest() { - app := simapp.InitElysTestApp(initChain, k.T()) +func (suite *PerpetualKeeperTestSuite) SetupTest() { + app := simapp.InitElysTestApp(initChain, suite.T()) - k.legacyAmino = app.LegacyAmino() - k.ctx = app.BaseApp.NewContext(initChain) - k.app = app + suite.legacyAmino = app.LegacyAmino() + suite.ctx = app.BaseApp.NewContext(initChain) + suite.app = app } func TestKeeperSuite(t *testing.T) {