-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase coverage for Amm keeper and types package (#1052)
* increasse coverage for amm keepers * swap by denom test for out route * add more amm tests * increase coverage for amm types package
- Loading branch information
Showing
19 changed files
with
1,452 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/elys-network/elys/x/amm/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func (suite *AmmKeeperTestSuite) TestCreatePool() { | ||
// Define test cases | ||
testCases := []struct { | ||
name string | ||
setup func() *types.MsgCreatePool | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
"asset profile not found", | ||
func() *types.MsgCreatePool { | ||
addr := suite.AddAccounts(1, nil) | ||
suite.app.AssetprofileKeeper.RemoveEntry(suite.ctx, ptypes.BaseCurrency) | ||
return &types.MsgCreatePool{ | ||
Sender: addr[0].String(), | ||
PoolParams: types.PoolParams{}, | ||
PoolAssets: []types.PoolAsset{}, | ||
} | ||
}, | ||
"asset profile not found for denom", | ||
}, | ||
{ | ||
"Balance pool Create Error", | ||
func() *types.MsgCreatePool { | ||
suite.ResetSuite() | ||
addr := suite.AddAccounts(1, nil) | ||
return &types.MsgCreatePool{ | ||
Sender: addr[0].String(), | ||
PoolParams: types.PoolParams{}, | ||
PoolAssets: []types.PoolAsset{}, | ||
} | ||
}, | ||
"swap_fee is nil", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
msg := tc.setup() | ||
poolId, err := suite.app.AmmKeeper.CreatePool(suite.ctx, msg) | ||
if tc.expectedErrMsg != "" { | ||
require.Error(suite.T(), err) | ||
require.Contains(suite.T(), err.Error(), tc.expectedErrMsg) | ||
} else { | ||
require.NoError(suite.T(), err) | ||
require.NotZero(suite.T(), poolId) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
) | ||
|
||
func (suite *AmmKeeperTestSuite) TestExitPool() { | ||
testCases := []struct { | ||
name string | ||
setup func() (sdk.AccAddress, uint64, math.Int, sdk.Coins, string, bool) | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
"pool does not exist", | ||
func() (sdk.AccAddress, uint64, math.Int, sdk.Coins, string, bool) { | ||
addr := suite.AddAccounts(1, nil) | ||
return addr[0], 1, math.NewInt(100), sdk.NewCoins(sdk.NewCoin("uatom", math.NewInt(100))), "uatom", false | ||
}, | ||
"invalid pool id", | ||
}, | ||
{ | ||
"exiting more shares than available", | ||
func() (sdk.AccAddress, uint64, math.Int, sdk.Coins, string, bool) { | ||
suite.SetupCoinPrices() | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100000000000) | ||
pool := suite.CreateNewAmmPool(addr[0], true, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), ptypes.ATOM, amount.MulRaw(10), amount.MulRaw(10)) | ||
return addr[0], 1, pool.TotalShares.Amount.Add(sdkmath.NewInt(10)), sdk.NewCoins(sdk.NewCoin("uatom", math.NewInt(100))), "uatom", false | ||
}, | ||
"Trying to exit >= the number of shares contained in the pool", | ||
}, | ||
{ | ||
"exiting negative shares", | ||
func() (sdk.AccAddress, uint64, math.Int, sdk.Coins, string, bool) { | ||
addr := suite.AddAccounts(1, nil) | ||
return addr[0], 1, sdkmath.NewInt(1).Neg(), sdk.NewCoins(sdk.NewCoin("uatom", math.NewInt(100))), "uatom", false | ||
}, | ||
"Trying to exit a negative amount of shares", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
exiter, poolId, inShares, minTokensOut, tokenOutDenom, isLiq := tc.setup() | ||
_, err := suite.app.AmmKeeper.ExitPool(suite.ctx, exiter, poolId, inShares, minTokensOut, tokenOutDenom, isLiq) | ||
if tc.expectedErrMsg != "" { | ||
suite.Require().Error(err) | ||
suite.Require().Contains(err.Error(), tc.expectedErrMsg) | ||
} else { | ||
suite.Require().NoError(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/parameter/types" | ||
) | ||
|
||
func (suite *AmmKeeperTestSuite) TestJoinPoolNoSwap() { | ||
// Define test cases | ||
testCases := []struct { | ||
name string | ||
setup func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
"pool does not exist", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
return sdk.AccAddress([]byte("sender")), 1, sdkmath.NewInt(100), sdk.Coins{} | ||
}, | ||
"invalid pool id", | ||
}, | ||
{ | ||
"successful join pool No oracle", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
suite.SetupCoinPrices() | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100000000000) | ||
pool := suite.CreateNewAmmPool(addr[0], false, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), types.ATOM, amount.MulRaw(10), amount.MulRaw(10)) | ||
return addr[0], 1, pool.TotalShares.Amount, sdk.Coins{sdk.NewCoin(types.ATOM, amount.MulRaw(10)), sdk.NewCoin(types.BaseCurrency, amount.MulRaw(10))} | ||
}, | ||
"", | ||
}, | ||
{ | ||
"successful join pool with oracle", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
suite.SetupCoinPrices() | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100000000000) | ||
pool := suite.CreateNewAmmPool(addr[0], true, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), types.ATOM, amount.MulRaw(10), amount.MulRaw(10)) | ||
return addr[0], 2, pool.TotalShares.Amount, sdk.Coins{sdk.NewCoin(types.ATOM, amount.MulRaw(10))} | ||
}, | ||
"", | ||
}, | ||
{ | ||
"Needed LpLiquidity is more than tokenInMaxs", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100) | ||
share, _ := sdkmath.NewIntFromString("20000000000000000000000000000000") | ||
return addr[0], 1, share, sdk.Coins{sdk.NewCoin(types.ATOM, amount), sdk.NewCoin(types.BaseCurrency, amount)} | ||
}, | ||
"TokenInMaxs is less than the needed LP liquidity to this JoinPoolNoSwap", | ||
}, | ||
{ | ||
"tokenInMaxs does not contain Needed LpLiquidity coins", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100) | ||
share, _ := sdkmath.NewIntFromString("20000000000000000000000000000000") | ||
return addr[0], 1, share, sdk.Coins{sdk.NewCoin("nocoin", amount), sdk.NewCoin(types.BaseCurrency, amount)} | ||
}, | ||
"TokenInMaxs does not include all the tokens that are part of the target pool", | ||
}, | ||
{ | ||
"tokenInMaxs does not contain Needed LpLiquidity coins", | ||
func() (sdk.AccAddress, uint64, sdkmath.Int, sdk.Coins) { | ||
addr := suite.AddAccounts(1, nil) | ||
amount := sdkmath.NewInt(100) | ||
share, _ := sdkmath.NewIntFromString("20000000000000000000000000000000") | ||
return addr[0], 1, share, sdk.Coins{sdk.NewCoin("nocoin", amount), sdk.NewCoin(types.ATOM, amount), sdk.NewCoin(types.BaseCurrency, amount)} | ||
}, | ||
"TokenInMaxs includes tokens that are not part of the target pool", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
sender, poolId, shareOutAmount, tokenInMaxs := tc.setup() | ||
tokenIn, sharesOut, err := suite.app.AmmKeeper.JoinPoolNoSwap(suite.ctx, sender, poolId, shareOutAmount, tokenInMaxs) | ||
if tc.expectedErrMsg != "" { | ||
suite.Require().Error(err) | ||
suite.Require().Contains(err.Error(), tc.expectedErrMsg) | ||
} else { | ||
suite.Require().NoError(err) | ||
suite.Require().True(tokenIn.IsAllLTE(tokenInMaxs)) | ||
suite.Require().True(sharesOut.LTE(shareOutAmount)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.