Skip to content

Commit

Permalink
Speedup CL spot price check and swap by removing an iterator (#7258)
Browse files Browse the repository at this point in the history
* Speedup CL spot price check and swap by removing an iterator

* Update Changelog

* Update x/concentrated-liquidity/pool.go

Co-authored-by: Roman <[email protected]>

---------

Co-authored-by: Roman <[email protected]>
  • Loading branch information
ValarDragon and p0mvn authored Jan 7, 2024
1 parent 33d3fb3 commit 7d6ee67
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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 true if the Pool has a position. This is guaranteed to be equi-satisfiable to
// 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 {
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

0 comments on commit 7d6ee67

Please sign in to comment.