Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce the number of returns in superfluid migration #6014

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
* [#6004](https://github.com/osmosis-labs/osmosis/pull/6004) reduce number of returns for creating full range position

Expand Down
26 changes: 15 additions & 11 deletions x/gamm/keeper/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -20,41 +21,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 @@ -229,15 +229,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 @@ -259,8 +259,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
9 changes: 5 additions & 4 deletions x/superfluid/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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"
)

var (
Expand All @@ -22,15 +23,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 @@ -42,7 +43,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