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

Speedup CL spot price check and swap by removing an iterator #7258

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#7106](https://github.com/osmosis-labs/osmosis/pull/7106) Halve the time of log2 calculation (speeds up TWAP code)
* [#7093](https://github.com/osmosis-labs/osmosis/pull/7093),[#7100](https://github.com/osmosis-labs/osmosis/pull/7100),[#7172](https://github.com/osmosis-labs/osmosis/pull/7172) Lower CPU overheads of the Osmosis epoch.
* [#7203](https://github.com/osmosis-labs/osmosis/pull/7203) Make a maximum number of pools of 100 billion.
* [#7258](https://github.com/osmosis-labs/osmosis/pull/7258) Remove an iterator call in CL swaps and spot price calls.

### Bug Fixes

Expand Down
16 changes: 12 additions & 4 deletions x/concentrated-liquidity/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func (k Keeper) GetPoolDenoms(ctx sdk.Context, poolId uint64) ([]string, error)
return denoms, nil
}

// Return if the Pool has a position. This is guaranteed to be equi-satisfiable to
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
// the current sqrt price being 0.
// We also check that the current tick is 0, which is also guaranteed in this situation.
// That derisks any edge-case where the sqrt price is 0 but the tick is not 0.
func (k Keeper) PoolHasPosition(ctx sdk.Context, pool types.ConcentratedPoolExtension) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be appropriate to define this method on the pool itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

Copy link
Member Author

@ValarDragon ValarDragon Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually on further thought I'm not sure, this is a convention set by the keeper not the pool itself. (Adding a position doesn't go through a pool.AddPosition method)

if pool.GetCurrentSqrtPrice().IsZero() && pool.GetCurrentTick() == 0 {
return false
}
return true
}

func (k Keeper) CalculateSpotPrice(
ctx sdk.Context,
poolId uint64,
Expand All @@ -175,10 +186,7 @@ func (k Keeper) CalculateSpotPrice(
return osmomath.BigDec{}, err
}

hasPositions, err := k.HasAnyPositionForPool(ctx, poolId)
if err != nil {
return osmomath.BigDec{}, err
}
hasPositions := k.PoolHasPosition(ctx, concentratedPool)

if !hasPositions {
return osmomath.BigDec{}, types.NoSpotPriceWhenNoLiquidityError{PoolId: poolId}
Expand Down
5 changes: 1 addition & 4 deletions x/concentrated-liquidity/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,7 @@ func (k Keeper) getPoolForSwap(ctx sdk.Context, poolId uint64) (types.Concentrat
if err != nil {
return p, err
}
hasPositionInPool, err := k.HasAnyPositionForPool(ctx, poolId)
if err != nil {
return p, err
}
hasPositionInPool := k.PoolHasPosition(ctx, p)
if !hasPositionInPool {
return p, types.NoSpotPriceWhenNoLiquidityError{PoolId: poolId}
}
Expand Down