Skip to content

Commit 47a1f83

Browse files
committed
perpetual bug fixes
1 parent 1e84c70 commit 47a1f83

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

x/perpetual/keeper/hooks_amm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"cosmossdk.io/math"
55
sdk "github.com/cosmos/cosmos-sdk/types"
66
ammtypes "github.com/elys-network/elys/v6/x/amm/types"
7+
"github.com/elys-network/elys/v6/x/perpetual/types"
78
)
89

910
// EpochHooks wrapper struct for tvl keeper
@@ -39,7 +40,7 @@ func (h AmmHooks) AfterJoinPool(ctx sdk.Context, sender sdk.AccAddress, ammPool
3940
return err
4041
}
4142

42-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
43+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
4344
if err != nil {
4445
return err
4546
}
@@ -61,7 +62,7 @@ func (h AmmHooks) AfterExitPool(ctx sdk.Context, sender sdk.AccAddress, ammPool
6162
return err
6263
}
6364

64-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
65+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
6566
if err != nil {
6667
return err
6768
}
@@ -82,7 +83,7 @@ func (h AmmHooks) AfterSwap(ctx sdk.Context, sender sdk.AccAddress, ammPool ammt
8283
return err
8384
}
8485

85-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
86+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
8687
if err != nil {
8788
return err
8889
}

x/perpetual/keeper/hooks_leveragelp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (h LeverageLpHooks) AfterLeverageLpPositionOpen(ctx sdk.Context, sender sdk
5454
return err
5555
}
5656

57-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
57+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
5858
if err != nil {
5959
return err
6060
}
@@ -72,7 +72,7 @@ func (h LeverageLpHooks) AfterLeverageLpPositionClose(ctx sdk.Context, _ sdk.Acc
7272
return err
7373
}
7474

75-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
75+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
7676
if err != nil {
7777
return err
7878
}
@@ -90,7 +90,7 @@ func (h LeverageLpHooks) AfterLeverageLpPositionOpenConsolidate(ctx sdk.Context,
9090
return err
9191
}
9292

93-
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, false)
93+
err = h.k.CheckLowPoolHealthAndMinimumCustody(ctx, ammPool.PoolId, types.Position_UNSPECIFIED)
9494
if err != nil {
9595
return err
9696
}

x/perpetual/keeper/open.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons
6969
msg.TakeProfitPrice = existingMtp.TakeProfitPrice
7070
}
7171

72-
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, msg.PoolId, true); err != nil {
72+
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, msg.PoolId, msg.Position); err != nil {
7373
return nil, err
7474
}
7575

@@ -98,7 +98,7 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons
9898
return k.OpenConsolidate(ctx, existingMtp, mtp, msg, tradingAsset, baseCurrency, totalPerpFeesCoins)
9999
}
100100

101-
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, msg.PoolId, true); err != nil {
101+
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, msg.PoolId, msg.Position); err != nil {
102102
return nil, err
103103
}
104104

x/perpetual/keeper/open_consolidate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (k Keeper) OpenConsolidate(ctx sdk.Context, existingMtp *types.MTP, newMtp
9696
}
9797
}
9898

99-
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, poolId, true); err != nil {
99+
if err = k.CheckLowPoolHealthAndMinimumCustody(ctx, poolId, msg.Position); err != nil {
100100
return nil, err
101101
}
102102

x/perpetual/keeper/pool_health.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/elys-network/elys/v6/x/perpetual/types"
1414
)
1515

16-
func (k Keeper) CheckLowPoolHealthAndMinimumCustody(ctx sdk.Context, poolId uint64, openedPosition bool) error {
16+
func (k Keeper) CheckLowPoolHealthAndMinimumCustody(ctx sdk.Context, poolId uint64, openingPositionType types.Position) error {
1717
pool, found := k.GetPool(ctx, poolId)
1818
if !found {
1919
return errorsmod.Wrapf(types.ErrPoolDoesNotExist, "pool id %d", poolId)
@@ -22,17 +22,22 @@ func (k Keeper) CheckLowPoolHealthAndMinimumCustody(ctx sdk.Context, poolId uint
2222
params := k.GetParams(ctx)
2323

2424
maxLiabilitiesRatioAllowed := math.LegacyZeroDec()
25-
if openedPosition {
25+
26+
if openingPositionType != types.Position_UNSPECIFIED {
27+
// new position is being opened
2628
maxLiabilitiesRatioAllowed = params.PoolMaxLiabilitiesThreshold
2729
} else {
30+
// checks after exit or swap in amm pool or some other tx in leveragelp
2831
maxLiabilitiesRatioAllowed = params.PoolMaxLiabilitiesThreshold.Add(params.ExitBuffer)
2932
}
3033

31-
if !pool.BaseAssetLiabilitiesRatio.IsNil() && pool.BaseAssetLiabilitiesRatio.GTE(maxLiabilitiesRatioAllowed) {
32-
return errorsmod.Wrapf(types.ErrInvalidPosition, "pool (%d) base asset liabilities ratio (%s) too high for the operation", poolId, pool.BaseAssetLiabilitiesRatio.String())
34+
//For base asset, check only if the position is unspecified or is long
35+
if openingPositionType != types.Position_SHORT && !pool.BaseAssetLiabilitiesRatio.IsNil() && pool.BaseAssetLiabilitiesRatio.GTE(maxLiabilitiesRatioAllowed) {
36+
return errorsmod.Wrapf(types.ErrInvalidPosition, "pool (id: %d) base asset liabilities ratio (%s) too high for the operation", poolId, pool.BaseAssetLiabilitiesRatio.String())
3337
}
34-
if !pool.QuoteAssetLiabilitiesRatio.IsNil() && pool.QuoteAssetLiabilitiesRatio.GTE(maxLiabilitiesRatioAllowed) {
35-
return errorsmod.Wrapf(types.ErrInvalidPosition, "pool (%d) quote asset liabilities ratio (%s) too high for the operation", poolId, pool.QuoteAssetLiabilitiesRatio.String())
38+
//For quote asset, check only if the position is unspecified or is short
39+
if openingPositionType != types.Position_LONG && !pool.QuoteAssetLiabilitiesRatio.IsNil() && pool.QuoteAssetLiabilitiesRatio.GTE(maxLiabilitiesRatioAllowed) {
40+
return errorsmod.Wrapf(types.ErrInvalidPosition, "pool (id: %d) quote asset liabilities ratio (%s) too high for the operation", poolId, pool.QuoteAssetLiabilitiesRatio.String())
3641
}
3742
err := k.CheckMinimumCustodyAmt(ctx, poolId)
3843
if err != nil {

x/perpetual/keeper/pool_health_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ func (suite *PerpetualKeeperTestSuite) TestCheckLowPoolHealth() {
2020
testCases := []struct {
2121
name string
2222
expectErrMsg string
23+
positionType types.Position
2324
prerequisiteFunction func()
2425
}{
2526
{
2627
"Pool not found",
2728
types.ErrPoolDoesNotExist.Error(),
29+
types.Position_UNSPECIFIED,
2830
func() {
2931
},
3032
},
3133
// "Pool health is nil" case is not possible because Getter function always give 0 value of health
3234
{
3335
"Pool health is low LONG",
34-
"pool (1) base asset liabilities ratio (0.950000000000000000) too high for the operation",
36+
"pool (id: 1) base asset liabilities ratio (0.950000000000000000) too high for the operation",
37+
types.Position_LONG,
3538
func() {
3639
pool := types.NewPool(ammPool, sdkmath.LegacyMustNewDecFromStr("10.5"))
3740
pool.BaseAssetLiabilitiesRatio = sdkmath.LegacyMustNewDecFromStr("0.95")
@@ -40,7 +43,18 @@ func (suite *PerpetualKeeperTestSuite) TestCheckLowPoolHealth() {
4043
},
4144
{
4245
"Pool health is low SHORT",
43-
"pool (1) quote asset liabilities ratio (0.950000000000000000) too high for the operation",
46+
"pool (id: 1) quote asset liabilities ratio (0.950000000000000000) too high for the operation",
47+
types.Position_SHORT,
48+
func() {
49+
pool := types.NewPool(ammPool, sdkmath.LegacyMustNewDecFromStr("10.5"))
50+
pool.QuoteAssetLiabilitiesRatio = sdkmath.LegacyMustNewDecFromStr("0.95")
51+
suite.app.PerpetualKeeper.SetPool(suite.ctx, pool)
52+
},
53+
},
54+
{
55+
"Pool health is low SHORT but maxLiabilitiesRatioAllowed is without buffer ",
56+
"pool (id: 1) quote asset liabilities ratio (0.950000000000000000) too high for the operation",
57+
types.Position_UNSPECIFIED,
4458
func() {
4559
pool := types.NewPool(ammPool, sdkmath.LegacyMustNewDecFromStr("10.5"))
4660
pool.QuoteAssetLiabilitiesRatio = sdkmath.LegacyMustNewDecFromStr("0.95")
@@ -52,7 +66,7 @@ func (suite *PerpetualKeeperTestSuite) TestCheckLowPoolHealth() {
5266
for _, tc := range testCases {
5367
suite.Run(tc.name, func() {
5468
tc.prerequisiteFunction()
55-
err = suite.app.PerpetualKeeper.CheckLowPoolHealthAndMinimumCustody(suite.ctx, 1, true)
69+
err = suite.app.PerpetualKeeper.CheckLowPoolHealthAndMinimumCustody(suite.ctx, 1, tc.positionType)
5670
if tc.expectErrMsg != "" {
5771
suite.Require().Error(err)
5872
suite.Require().Contains(err.Error(), tc.expectErrMsg)

0 commit comments

Comments
 (0)