From d284459009b52bb0c6482a12ed51adc13f5c5bfd Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 7 Jan 2024 00:42:23 -0600 Subject: [PATCH 1/3] Speedup CL spot price check and swap by removing an iterator --- x/concentrated-liquidity/pool.go | 16 ++++++++++++---- x/concentrated-liquidity/swaps.go | 5 +---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/x/concentrated-liquidity/pool.go b/x/concentrated-liquidity/pool.go index f6baeb245d4..914301fea5c 100644 --- a/x/concentrated-liquidity/pool.go +++ b/x/concentrated-liquidity/pool.go @@ -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 +// 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, @@ -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} diff --git a/x/concentrated-liquidity/swaps.go b/x/concentrated-liquidity/swaps.go index 93b45633e48..72811dc621e 100644 --- a/x/concentrated-liquidity/swaps.go +++ b/x/concentrated-liquidity/swaps.go @@ -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} } From b88cb9bc8e45601181f199319fde58842d9306bb Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 7 Jan 2024 00:59:05 -0600 Subject: [PATCH 2/3] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b3a5a9ca03..029090644d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 704ecbef9d5572586038a6dab6ddb96e681b4f47 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 7 Jan 2024 08:15:51 +0100 Subject: [PATCH 3/3] Update x/concentrated-liquidity/pool.go Co-authored-by: Roman --- x/concentrated-liquidity/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/concentrated-liquidity/pool.go b/x/concentrated-liquidity/pool.go index 914301fea5c..65275beb7c8 100644 --- a/x/concentrated-liquidity/pool.go +++ b/x/concentrated-liquidity/pool.go @@ -164,7 +164,7 @@ 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 +// 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.