diff --git a/x/leveragelp/keeper/position_close.go b/x/leveragelp/keeper/position_close.go index 2c8e56ce0..153fd02af 100644 --- a/x/leveragelp/keeper/position_close.go +++ b/x/leveragelp/keeper/position_close.go @@ -76,9 +76,6 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty k.SetPosition(ctx, &position) } - pool.LeveragedLpAmount = pool.LeveragedLpAmount.Sub(lpAmount) - k.UpdatePoolHealth(ctx, &pool) - // Hooks after leveragelp position closed if k.hooks != nil { k.hooks.AfterLeveragelpPositionClosed(ctx, pool) diff --git a/x/leveragelp/migrations/v6_migration.go b/x/leveragelp/migrations/v6_migration.go new file mode 100644 index 000000000..4d99b9f27 --- /dev/null +++ b/x/leveragelp/migrations/v6_migration.go @@ -0,0 +1,29 @@ +package migrations + +import ( + errorsmod "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/leveragelp/types" +) + +func (m Migrator) V6Migration(ctx sdk.Context) error { + pools := m.keeper.GetAllPools(ctx) + // Reset pools + for _, pool := range pools { + pool.LeveragedLpAmount = sdk.NewInt(0) + m.keeper.SetPool(ctx, pool) + } + // Traverse positions and update lp amount and health, as there are few positions, haven't optimized it much + positions := m.keeper.GetAllPositions(ctx) + for _, position := range positions { + // Retrieve Pool + pool, found := m.keeper.GetPool(ctx, position.AmmPoolId) + if !found { + return errorsmod.Wrap(types.ErrInvalidBorrowingAsset, "invalid pool id") + } + pool.LeveragedLpAmount = pool.LeveragedLpAmount.Add(position.LeveragedLpAmount) + pool.Health = m.keeper.CalculatePoolHealth(ctx, &pool) + m.keeper.SetPool(ctx, pool) + } + return nil +} diff --git a/x/leveragelp/module.go b/x/leveragelp/module.go index 6256e83b3..e37c456f5 100644 --- a/x/leveragelp/module.go +++ b/x/leveragelp/module.go @@ -117,7 +117,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) m := migrations.NewMigrator(am.keeper) - err := cfg.RegisterMigration(types.ModuleName, 4, m.V5Migration) + err := cfg.RegisterMigration(types.ModuleName, 5, m.V6Migration) if err != nil { panic(err) } @@ -144,7 +144,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1 -func (AppModule) ConsensusVersion() uint64 { return 5 } +func (AppModule) ConsensusVersion() uint64 { return 6 } // BeginBlock contains the logic that is automatically triggered at the beginning of each block func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {