Skip to content

Commit

Permalink
refactor: improve ListPoolsByDenom filter logic (#6884)
Browse files Browse the repository at this point in the history
* refactor: improve ListPoolsByDenom filter logic

* add lessPoolIFunc doc

* fix: use osmoutils.Contains instead of slices.Contains

* Update CHANGELOG.md

---------

Co-authored-by: Matt, Park <[email protected]>
  • Loading branch information
levisyin and mattverse authored Nov 22, 2023
1 parent 21878ad commit d5a8a2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#6788](https://github.com/osmosis-labs/osmosis/pull/6788) Improve error message when CL LP fails due to slippage bound hit.
* [#6858](https://github.com/osmosis-labs/osmosis/pull/6858) Merge mempool improvements from v20
* [#6861](https://github.com/osmosis-labs/osmosis/pull/6861) Protorev address added to reduced taker fee whitelist
* [#6884](https://github.com/osmosis-labs/osmosis/pull/6884) Improve ListPoolsByDenom function filter denom logic
* [#6890](https://github.com/osmosis-labs/osmosis/pull/6890) Enable arb filter for affiliate swap contract

### API Breaks

* [#6805](https://github.com/osmosis-labs/osmosis/pull/6805) return bucket index of the current tick from LiquidityPerTickRange query
Expand Down
25 changes: 12 additions & 13 deletions x/poolmanager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (
"github.com/osmosis-labs/osmosis/v20/x/poolmanager/types"
)

// 1 << 256 - 1 where 256 is the max bit length defined for osmomath.Int
var intMaxValue = osmomath.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)))
var (
// 1 << 256 - 1 where 256 is the max bit length defined for osmomath.Int
intMaxValue = osmomath.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)))
// lessPoolIFunc is used for sorting pools by poolID
lessPoolIFunc = func(i, j types.PoolI) bool {
return i.GetId() < j.GetId()
}
)

// RouteExactAmountIn processes a swap along the given route using the swap function
// corresponding to poolID's pool type. It takes in the input denom and amount for
Expand Down Expand Up @@ -526,10 +532,6 @@ func (k Keeper) GetPool(
func (k Keeper) AllPools(
ctx sdk.Context,
) ([]types.PoolI, error) {
less := func(i, j types.PoolI) bool {
return i.GetId() < j.GetId()
}

// Allocate the slice with the exact capacity to avoid reallocations.
poolCount := k.GetNextPoolId(ctx)
sortedPools := make([]types.PoolI, 0, poolCount)
Expand All @@ -539,7 +541,7 @@ func (k Keeper) AllPools(
return nil, err
}

sortedPools = osmoutils.MergeSlices(sortedPools, currentModulePools, less)
sortedPools = osmoutils.MergeSlices(sortedPools, currentModulePools, lessPoolIFunc)
}

return sortedPools, nil
Expand All @@ -552,9 +554,6 @@ func (k Keeper) ListPoolsByDenom(
ctx sdk.Context,
denom string,
) ([]types.PoolI, error) {
less := func(i, j types.PoolI) bool {
return i.GetId() < j.GetId()
}
var sortedPools []types.PoolI
for _, poolModule := range k.poolModules {
currentModulePools, err := poolModule.GetPools(ctx)
Expand All @@ -564,15 +563,15 @@ func (k Keeper) ListPoolsByDenom(

var poolsByDenom []types.PoolI
for _, pool := range currentModulePools {
coins, err := k.GetTotalPoolLiquidity(ctx, pool.GetId())
poolDenoms, err := poolModule.GetPoolDenoms(ctx, pool.GetId())
if err != nil {
return nil, err
}
if coins.AmountOf(denom).GT(osmomath.ZeroInt()) {
if osmoutils.Contains(poolDenoms, denom) {
poolsByDenom = append(poolsByDenom, pool)
}
}
sortedPools = osmoutils.MergeSlices(sortedPools, poolsByDenom, less)
sortedPools = osmoutils.MergeSlices(sortedPools, poolsByDenom, lessPoolIFunc)
}
return sortedPools, nil
}
Expand Down

0 comments on commit d5a8a2b

Please sign in to comment.