Skip to content

Commit

Permalink
refactor: reduce the number of returns in superfluid migration (#6014)
Browse files Browse the repository at this point in the history
* refactor: reduce the number of returns in superfluid migration

* changelog

(cherry picked from commit 7a92e78)

# Conflicts:
#	x/gamm/keeper/migrate.go
#	x/superfluid/keeper/export_test.go
#	x/superfluid/keeper/migrate.go
  • Loading branch information
p0mvn authored and mergify[bot] committed Aug 10, 2023
1 parent 4509279 commit b94dc8c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### API breaks

* [#6014](https://github.com/osmosis-labs/osmosis/pull/6014) refactor: reduce the number of returns in superfluid migration
* [#5983](https://github.com/osmosis-labs/osmosis/pull/5983) refactor(CL): 6 return values in CL CreatePosition with a struct
* [#5983](https://github.com/osmosis-labs/osmosis/pull/5983) refactor(CL): 6 return values in CL CreatePosition with a struct
* [#6004](https://github.com/osmosis-labs/osmosis/pull/6004) reduce number of returns for creating full range position
Expand Down
34 changes: 23 additions & 11 deletions x/gamm/keeper/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import (
"sort"

"github.com/osmosis-labs/osmosis/osmoutils"
<<<<<<< HEAD
cltypes "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v16/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v16/x/gamm/types/migration"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"
=======
clmodel "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/model"
cltypes "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v17/x/gamm/types"
gammmigration "github.com/osmosis-labs/osmosis/v17/x/gamm/types/migration"
poolmanagertypes "github.com/osmosis-labs/osmosis/v17/x/poolmanager/types"
superfluidtypes "github.com/osmosis-labs/osmosis/v17/x/superfluid/types"
>>>>>>> 7a92e782 (refactor: reduce the number of returns in superfluid migration (#6014))

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -19,41 +28,44 @@ import (
func (k Keeper) MigrateUnlockedPositionFromBalancerToConcentrated(ctx sdk.Context,
sender sdk.AccAddress, sharesToMigrate sdk.Coin,
tokenOutMins sdk.Coins,
) (positionData cltypes.CreateFullRangePositionData, poolIdLeaving, poolIdEntering uint64, err error) {
) (cltypes.CreateFullRangePositionData, superfluidtypes.MigrationPoolIDs, error) {
// Get the balancer poolId by parsing the gamm share denom.
poolIdLeaving, err = types.GetPoolIdFromShareDenom(sharesToMigrate.Denom)
poolIdLeaving, err := types.GetPoolIdFromShareDenom(sharesToMigrate.Denom)
if err != nil {
return cltypes.CreateFullRangePositionData{}, 0, 0, err
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, err
}

// Find the governance sanctioned link between the balancer pool and a concentrated pool.
poolIdEntering, err = k.GetLinkedConcentratedPoolID(ctx, poolIdLeaving)
poolIdEntering, err := k.GetLinkedConcentratedPoolID(ctx, poolIdLeaving)
if err != nil {
return cltypes.CreateFullRangePositionData{}, 0, 0, err
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, err
}

// Get the concentrated pool from the message and type cast it to ConcentratedPoolExtension.
concentratedPool, err := k.concentratedLiquidityKeeper.GetConcentratedPoolById(ctx, poolIdEntering)
if err != nil {
return cltypes.CreateFullRangePositionData{}, 0, 0, err
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, err
}

// Exit the balancer pool position.
exitCoins, err := k.ExitPool(ctx, sender, poolIdLeaving, sharesToMigrate.Amount, tokenOutMins)
if err != nil {
return cltypes.CreateFullRangePositionData{}, 0, 0, err
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, err
}
// Defense in depth, ensuring we are returning exactly two coins.
if len(exitCoins) != 2 {
return cltypes.CreateFullRangePositionData{}, 0, 0, fmt.Errorf("Balancer pool must have exactly two tokens")
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, fmt.Errorf("Balancer pool must have exactly two tokens")
}

// Create a full range (min to max tick) concentrated liquidity position.
positionData, err = k.concentratedLiquidityKeeper.CreateFullRangePosition(ctx, concentratedPool.GetId(), sender, exitCoins)
positionData, err := k.concentratedLiquidityKeeper.CreateFullRangePosition(ctx, concentratedPool.GetId(), sender, exitCoins)
if err != nil {
return cltypes.CreateFullRangePositionData{}, 0, 0, err
return cltypes.CreateFullRangePositionData{}, superfluidtypes.MigrationPoolIDs{}, err
}
return positionData, poolIdLeaving, poolIdEntering, nil
return positionData, superfluidtypes.MigrationPoolIDs{
LeavingID: poolIdLeaving,
EnteringID: poolIdEntering,
}, nil
}

// GetAllMigrationInfo gets all existing links between Balancer Pool and Concentrated Pool,
Expand Down
10 changes: 5 additions & 5 deletions x/gamm/keeper/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ func (s *KeeperTestSuite) TestMigrate() {

// Migrate the user's gamm shares to a full range concentrated liquidity position
userBalancesBeforeMigration := s.App.BankKeeper.GetAllBalances(s.Ctx, test.param.sender)
positionData, poolIdLeaving, poolIdEntering, err := keeper.MigrateUnlockedPositionFromBalancerToConcentrated(s.Ctx, test.param.sender, sharesToMigrate, test.tokenOutMins)
positionData, migratedPools, err := keeper.MigrateUnlockedPositionFromBalancerToConcentrated(s.Ctx, test.param.sender, sharesToMigrate, test.tokenOutMins)
userBalancesAfterMigration := s.App.BankKeeper.GetAllBalances(s.Ctx, test.param.sender)
if test.expectedErr != nil {
s.Require().Error(err)
s.Require().ErrorContains(err, test.expectedErr.Error())

// Expect zero values for both pool ids
s.Require().Zero(poolIdLeaving)
s.Require().Zero(poolIdEntering)
s.Require().Zero(migratedPools.LeavingID)
s.Require().Zero(migratedPools.EnteringID)

// Assure the user's gamm shares still exist
userGammBalanceAfterFailedMigration := s.App.BankKeeper.GetBalance(s.Ctx, test.param.sender, "gamm/pool/1")
Expand All @@ -242,8 +242,8 @@ func (s *KeeperTestSuite) TestMigrate() {

// Expect the poolIdLeaving to be the balancer pool id
// Expect the poolIdEntering to be the concentrated liquidity pool id
s.Require().Equal(balancerPoolId, poolIdLeaving)
s.Require().Equal(clPool.GetId(), poolIdEntering)
s.Require().Equal(balancerPoolId, migratedPools.LeavingID)
s.Require().Equal(clPool.GetId(), migratedPools.EnteringID)

// Determine how much of the user's balance was not used in the migration
// This amount should be returned to the user.
Expand Down
14 changes: 10 additions & 4 deletions x/superfluid/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

<<<<<<< HEAD
cltypes "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
lockuptypes "github.com/osmosis-labs/osmosis/v16/x/lockup/types"
"github.com/osmosis-labs/osmosis/v16/x/superfluid/types"
=======
cltypes "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/types"
lockuptypes "github.com/osmosis-labs/osmosis/v17/x/lockup/types"
types "github.com/osmosis-labs/osmosis/v17/x/superfluid/types"
>>>>>>> 7a92e782 (refactor: reduce the number of returns in superfluid migration (#6014))
)

var (
Expand All @@ -23,15 +29,15 @@ func (k Keeper) PrepareConcentratedLockForSlash(ctx sdk.Context, lock *lockuptyp
return k.prepareConcentratedLockForSlash(ctx, lock, slashAmt)
}

func (k Keeper) MigrateSuperfluidBondedBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, synthDenomBeforeMigration string, tokenOutMins sdk.Coins) (positionData cltypes.CreateFullRangePositionData, concentratedLockId, poolIdLeaving, poolIdEntering uint64, err error) {
func (k Keeper) MigrateSuperfluidBondedBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, synthDenomBeforeMigration string, tokenOutMins sdk.Coins) (cltypes.CreateFullRangePositionData, uint64, types.MigrationPoolIDs, error) {
return k.migrateSuperfluidBondedBalancerToConcentrated(ctx, sender, lockId, sharesToMigrate, synthDenomBeforeMigration, tokenOutMins)
}

func (k Keeper) MigrateSuperfluidUnbondingBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, synthDenomBeforeMigration string, tokenOutMins sdk.Coins) (positionData cltypes.CreateFullRangePositionData, concentratedLockId, poolIdLeaving, poolIdEntering uint64, err error) {
func (k Keeper) MigrateSuperfluidUnbondingBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, synthDenomBeforeMigration string, tokenOutMins sdk.Coins) (cltypes.CreateFullRangePositionData, uint64, types.MigrationPoolIDs, error) {
return k.migrateSuperfluidUnbondingBalancerToConcentrated(ctx, sender, lockId, sharesToMigrate, synthDenomBeforeMigration, tokenOutMins)
}

func (k Keeper) MigrateNonSuperfluidLockBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, tokenOutMins sdk.Coins) (positionData cltypes.CreateFullRangePositionData, concentratedLockId, poolIdLeaving, poolIdEntering uint64, err error) {
func (k Keeper) MigrateNonSuperfluidLockBalancerToConcentrated(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin, tokenOutMins sdk.Coins) (cltypes.CreateFullRangePositionData, uint64, types.MigrationPoolIDs, error) {
return k.migrateNonSuperfluidLockBalancerToConcentrated(ctx, sender, lockId, sharesToMigrate, tokenOutMins)
}

Expand All @@ -43,7 +49,7 @@ func (k Keeper) RouteMigration(ctx sdk.Context, sender sdk.AccAddress, lockId in
return k.routeMigration(ctx, sender, lockId, sharesToMigrate)
}

func (k Keeper) ValidateMigration(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin) (poolIdLeaving, poolIdEntering uint64, preMigrationLock *lockuptypes.PeriodLock, remainingLockTime time.Duration, err error) {
func (k Keeper) ValidateMigration(ctx sdk.Context, sender sdk.AccAddress, lockId uint64, sharesToMigrate sdk.Coin) (types.MigrationPoolIDs, *lockuptypes.PeriodLock, time.Duration, error) {
return k.validateMigration(ctx, sender, lockId, sharesToMigrate)
}

Expand Down
Loading

0 comments on commit b94dc8c

Please sign in to comment.