Skip to content

Commit

Permalink
[Leveragelp]: Bug fix (#634)
Browse files Browse the repository at this point in the history
* fix

* migrate script

* update script

* update
  • Loading branch information
amityadav0 authored Jul 1, 2024
1 parent e0da1dc commit 04d1d41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 0 additions & 3 deletions x/leveragelp/keeper/position_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions x/leveragelp/migrations/v6_migration.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions x/leveragelp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down

0 comments on commit 04d1d41

Please sign in to comment.