Skip to content

Commit

Permalink
adding more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
avkr003 committed Aug 26, 2024
1 parent 814fd80 commit a7fe542
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 22 deletions.
20 changes: 20 additions & 0 deletions x/leveragelp/keeper/msg_server_add_collateral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
expectErr bool
expectErrMsg string
prerequisiteFunction func()
postValidateFunc func(msg *types.MsgAddCollateral)
}{
{"position not found",
&types.MsgAddCollateral{
Expand All @@ -122,6 +123,9 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
SetupCoinPrices(suite.ctx, suite.app.OracleKeeper)
initializeForAddCollateral(suite, addresses, asset1, asset2, false)
},
func(msg *types.MsgAddCollateral) {

},
},
{"pool not found",
&types.MsgAddCollateral{
Expand All @@ -138,6 +142,9 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
openPosition(suite, addresses[0], collateralAmount, leverage)
suite.app.LeveragelpKeeper.DeletePool(suite.ctx, 1)
},
func(msg *types.MsgAddCollateral) {

},
},
{"pool not enabled",
&types.MsgAddCollateral{
Expand All @@ -156,6 +163,9 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
pool.Enabled = false
suite.app.LeveragelpKeeper.SetPool(suite.ctx, pool)
},
func(msg *types.MsgAddCollateral) {

},
},
{"repaying more than allowed",
&types.MsgAddCollateral{
Expand All @@ -171,6 +181,9 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
initializeForAddCollateral(suite, addresses, asset1, asset2, true)
openPosition(suite, addresses[0], collateralAmount, leverage)
},
func(msg *types.MsgAddCollateral) {

},
},
{"balance too low to repay",
&types.MsgAddCollateral{
Expand All @@ -190,6 +203,8 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
if err != nil {
panic(err)
}
},
func(msg *types.MsgAddCollateral) {

},
},
Expand All @@ -207,6 +222,10 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
initializeForAddCollateral(suite, addresses, asset1, asset2, true)
openPosition(suite, addresses[0], collateralAmount, leverage)
},
func(msg *types.MsgAddCollateral) {
position, _ := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, addresses[0], 1)
suite.Require().True(position.Collateral.Amount.Equal(msg.Collateral.Add(collateralAmount)))
},
},
}

Expand All @@ -221,6 +240,7 @@ func (suite *KeeperTestSuite) TestMsgServerAddCollateral() {
} else {
suite.Require().NoError(err)
}
tc.postValidateFunc(tc.input)
})
}
}
51 changes: 32 additions & 19 deletions x/leveragelp/keeper/msg_server_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (suite *KeeperTestSuite) TestClose() {
expectErr bool
expectErrMsg string
prerequisiteFunction func()
postValidateFunc func()
}{
{"No position to close",
&types.MsgClose{
Expand All @@ -102,6 +103,8 @@ func (suite *KeeperTestSuite) TestClose() {
types.ErrPositionDoesNotExist.Error(),
func() {
},
func() {
},
},
{"Unlock time not reached",
&types.MsgClose{
Expand All @@ -121,9 +124,9 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
suite.Require().NoError(err)
},
func() {
},
},
{"Repay amount is greater than exit amount",
Expand All @@ -144,11 +147,11 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
suite.Require().NoError(err)
suite.AddBlockTime(1000000 * time.Hour)
},
func() {
},
},
{"Invalid Leverage LP shares amount to close",
&types.MsgClose{
Expand All @@ -171,11 +174,11 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
suite.Require().NoError(err)
suite.AddBlockTime(time.Hour)
},
func() {
},
},
{"Position Health is lower than safety factor and closing partially, should close fully",
&types.MsgClose{
Expand All @@ -198,11 +201,13 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
suite.Require().NoError(err)
suite.AddBlockTime(1000000 * time.Hour)
},
func() {
_, err := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, addresses[0], 1)
suite.Require().Contains(err.Error(), "position not found")
},
},
{"Position LP amount is 0",
&types.MsgClose{
Expand All @@ -225,12 +230,13 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
//position := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, .String())
suite.Require().NoError(err)
suite.AddBlockTime(1000000 * time.Hour)
},
func() {
_, err := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, addresses[0], 1)
suite.Require().Contains(err.Error(), "position not found")
},
},
{"Closing partial position",
&types.MsgClose{
Expand All @@ -253,11 +259,13 @@ func (suite *KeeperTestSuite) TestClose() {
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
}
_, err := suite.app.LeveragelpKeeper.Open(suite.ctx, &msg)
if err != nil {
panic(err)
}
suite.Require().NoError(err)
suite.AddBlockTime(time.Hour)
},
func() {
position, _ := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, addresses[0], 1)
suite.Require().Equal(position.LeveragedLpAmount, leverageLPShares.QuoRaw(2))
},
},
{"Closing whole position",
&types.MsgClose{
Expand All @@ -269,6 +277,10 @@ func (suite *KeeperTestSuite) TestClose() {
"",
func() {
},
func() {
_, err := suite.app.LeveragelpKeeper.GetPosition(suite.ctx, addresses[0], 1)
suite.Require().Contains(err.Error(), "position not found")
},
},
}

Expand All @@ -282,6 +294,7 @@ func (suite *KeeperTestSuite) TestClose() {
} else {
suite.Require().NoError(err)
}
tc.postValidateFunc()
})
}
}
39 changes: 36 additions & 3 deletions x/leveragelp/keeper/msg_server_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func initializeForOpen(suite *KeeperTestSuite, addresses []sdk.AccAddress, asset
func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
suite.ResetSuite()
SetupCoinPrices(suite.ctx, suite.app.OracleKeeper)
//SetupCoinPrices(suite.ctx, suite.app.OracleKeeper, []string{ptypes.Elys, ptypes.ATOM, "uusdt"})
addresses := simapp.AddTestAddrs(suite.app, suite.ctx, 10, sdk.NewInt(1000000))
asset1 := ptypes.ATOM
asset2 := ptypes.BaseCurrency
Expand Down Expand Up @@ -147,10 +146,27 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
expectErr: true,
expectErrMsg: types.ErrPoolDoesNotExist.Wrapf("poolId: %d", 100).Error(),
prerequisiteFunction: func() {
suite.SetMaxOpenPositions(2)
suite.SetMaxOpenPositions(3)
},
},
{name: "AMM Pool not found",
{name: "Pool not enabled",
input: &types.MsgOpen{
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
CollateralAmount: sdk.NewInt(1000),
AmmPoolId: 2,
Leverage: sdk.MustNewDecFromStr("2.0"),
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
expectErr: true,
expectErrMsg: "leveragelp not enabled for pool",
prerequisiteFunction: func() {
pool := types.NewPool(2)
pool.Enabled = false
suite.app.LeveragelpKeeper.SetPool(suite.ctx, pool)
},
},
{name: "base currency not found",
input: &types.MsgOpen{
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
Expand All @@ -163,7 +179,24 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
expectErrMsg: "invalid pool id",
prerequisiteFunction: func() {
pool := types.NewPool(2)
pool.Enabled = true
suite.app.LeveragelpKeeper.SetPool(suite.ctx, pool)
RemovePrices(suite.ctx, suite.app.OracleKeeper, []string{"uusdc"})
},
},
{name: "AMM Pool not found",
input: &types.MsgOpen{
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
CollateralAmount: sdk.NewInt(1000),
AmmPoolId: 2,
Leverage: sdk.MustNewDecFromStr("2.0"),
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
expectErr: true,
expectErrMsg: "invalid pool id",
prerequisiteFunction: func() {
SetupCoinPrices(suite.ctx, suite.app.OracleKeeper)
},
},
{"Pool Disabled",
Expand Down

0 comments on commit a7fe542

Please sign in to comment.