Skip to content

Commit

Permalink
adding test cases for close positions tx
Browse files Browse the repository at this point in the history
  • Loading branch information
avkr003 committed Aug 26, 2024
1 parent 08b6dfc commit 9dd346f
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 11 deletions.
3 changes: 1 addition & 2 deletions x/leveragelp/keeper/begin_blocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (suite *KeeperTestSuite) TestBeginBlocker() {
},
},
{
"multiple closing positions in same amm pool, one is for stop loss",
"multiple closing positions in same amm pool, one is for stop loss, others are for low health",
func() *types.Position {
suite.ResetSuite()
SetupCoinPrices(suite.ctx, suite.app.OracleKeeper)
Expand Down Expand Up @@ -174,7 +174,6 @@ func (suite *KeeperTestSuite) TestBeginBlocker() {
tc.postValidateFunction()
})
}

}

func (suite *KeeperTestSuite) TestCheckAndLiquidateUnhealthyPosition() {
Expand Down
46 changes: 37 additions & 9 deletions x/leveragelp/keeper/msg_server_close_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
ammtypes "github.com/elys-network/elys/x/amm/types"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,20 +13,34 @@ import (
func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePositions) (*types.MsgClosePositionsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

poolMap := make(map[uint64]types.Pool)
ammPoolMap := make(map[uint64]ammtypes.Pool)
// Handle liquidations
liqLog := []string{}
for _, val := range msg.Liquidate {
position, err := k.GetPosition(ctx, val.GetAccountAddress(), val.Id)
if err != nil {
continue
}
pool, found := k.GetPool(ctx, position.AmmPoolId)
ammPool, err := k.GetAmmPool(ctx, position.AmmPoolId)
if !found || err != nil {
continue

pool, found := poolMap[position.AmmPoolId]
if !found {
leveragePool, poolFound := k.GetPool(ctx, position.AmmPoolId)
if !poolFound {
continue
}
poolMap[position.AmmPoolId] = leveragePool

ammPool, poolErr := k.GetAmmPool(ctx, position.AmmPoolId)
if poolErr != nil {
continue
}
ammPoolMap[position.AmmPoolId] = ammPool
}
pool = poolMap[position.AmmPoolId]
ammPool := ammPoolMap[position.AmmPoolId]

_, _, _, err = k.CheckAndLiquidateUnhealthyPosition(ctx, &position, pool, ammPool)
// position is liquidated
if err != nil {
// Add log about error or not liquidated
liqLog = append(liqLog, fmt.Sprintf("Position: Address:%s Id:%d cannot be liquidated due to err: %s", position.Address, position.Id, err.Error()))
Expand All @@ -39,11 +54,24 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
if err != nil {
continue
}
pool, found := k.GetPool(ctx, position.AmmPoolId)
ammPool, err := k.GetAmmPool(ctx, position.AmmPoolId)
if !found || err != nil {
continue

pool, found := poolMap[position.AmmPoolId]
if !found {
leveragePool, poolFound := k.GetPool(ctx, position.AmmPoolId)
if !poolFound {
continue
}
poolMap[position.AmmPoolId] = leveragePool

ammPool, poolErr := k.GetAmmPool(ctx, position.AmmPoolId)
if poolErr != nil {
continue
}
ammPoolMap[position.AmmPoolId] = ammPool
}
pool = poolMap[position.AmmPoolId]
ammPool := ammPoolMap[position.AmmPoolId]

_, _, err = k.CheckAndCloseAtStopLoss(ctx, &position, pool, ammPool)
if err != nil {
// Add log about error or not closed
Expand Down
Loading

0 comments on commit 9dd346f

Please sign in to comment.