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

BE-664 | Add denoms filter for /pools endpoint #593

Merged
merged 1 commit into from
Dec 6, 2024
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
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ endif


# --- Tooling & Variables ----------------------------------------------------------------
include ./scripts/makefiles/proto.mk
include ./misc/make/tools.Makefile

# Install local dependencies
Expand Down Expand Up @@ -176,9 +177,6 @@ sqs-update-mainnet-state:
bench-pricing:
go test -bench BenchmarkGetPrices -run BenchmarkGetPrices github.com/osmosis-labs/sqs/tokens/usecase -count=6

proto-gen:
protoc --go_out=./ --go-grpc_out=./ --proto_path=./sqsdomain/proto ./sqsdomain/proto/ingest.proto

test-prices-mainnet:
CI_SQS_PRICING_WORKER_TEST=true go test \
-timeout 300s \
Expand Down
7 changes: 7 additions & 0 deletions domain/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func WithMarketIncentives(withMarketIncentives bool) PoolsOption {
})
}

// WithDenom configures the pools options with the denom filter.
func WithDenom(denom []string) PoolsOption {
return WithNonNilFilter(func(filter *api.GetPoolsRequestFilter) {
filter.Denom = denom
})
}

// WithPagination configures the pools options with the pagination request.
func WithFilter(f *api.GetPoolsRequestFilter) PoolsOption {
return func(o *PoolsOptions) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/v1beta1/pools/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pools
import (
"fmt"
"strconv"
"strings"

"github.com/osmosis-labs/sqs/delivery/http"
"github.com/osmosis-labs/sqs/domain/number"
Expand All @@ -13,6 +14,7 @@ import (

const (
maxSearchQueryLength = 50
maxDenoms = 8
)

const (
Expand All @@ -23,6 +25,7 @@ const (
queryFilterIDNotIn = "filter[id][not_in]"
queryFilterType = "filter[type]"
queryFilterIncentive = "filter[incentive]"
queryFilterDenom = "filter[denom]"
queryFilterMinLiquidityCap = "filter[min_liquidity_cap]"
queryFilterWithMarketIncentives = "filter[with_market_incentives]"
queryFilterSearch = "filter[search]"
Expand Down Expand Up @@ -72,6 +75,7 @@ func (r *GetPoolsRequestFilter) IsPresent(c echo.Context) bool {
c.QueryParam(queryFilterIDNotIn) != "" ||
c.QueryParam(queryFilterType) != "" ||
c.QueryParam(queryFilterIncentive) != "" ||
c.QueryParam(queryFilterDenom) != "" ||
c.QueryParam(queryMinLiquidityCap) != "" ||
c.QueryParam(queryFilterMinLiquidityCap) != "" ||
c.QueryParam(queryWithMarketIncentives) != "" ||
Expand Down Expand Up @@ -126,6 +130,13 @@ func (r *GetPoolsRequestFilter) UnmarshalHTTPRequest(c echo.Context) error {
return err
}

if denoms := c.QueryParam(queryFilterDenom); len(denoms) > 0 {
r.Denom = strings.Split(denoms, ",")
if len(r.Denom) > maxDenoms {
return fmt.Errorf("too many denoms, max: %d", maxDenoms)
}
}

// Deprecated: use filter[min_liquidity_cap]
if p := c.QueryParam(queryMinLiquidityCap); p != "" {
r.MinLiquidityCap, err = strconv.ParseUint(c.QueryParam(queryMinLiquidityCap), 10, 64)
Expand Down
19 changes: 19 additions & 0 deletions pkg/api/v1beta1/pools/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func TestGetPoolsRequestFilter_IsPresent(t *testing.T) {
queryParams: map[string]string{queryFilterIncentive: "2"},
expectedResult: true,
},
{
name: "With filter[denom]",
queryParams: map[string]string{queryFilterDenom: "2"},
expectedResult: true,
},
{
name: "With filter[search]",
queryParams: map[string]string{queryFilterSearch: "search"},
Expand Down Expand Up @@ -113,6 +118,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
queryFilterIDNotIn: "6,7",
queryFilterType: "8,9",
queryFilterIncentive: "0,1",
queryFilterDenom: "uosmo,atom",
queryMinLiquidityCap: "1000",
queryFilterMinLiquidityCap: "2000",
queryWithMarketIncentives: "true",
Expand All @@ -124,6 +130,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
PoolIdNotIn: []uint64{6, 7},
Type: []uint64{8, 9},
Incentive: []IncentiveType{0, 1},
Denom: []string{"uosmo", "atom"},
MinLiquidityCap: 2000,
WithMarketIncentives: true,
Search: "search",
Expand All @@ -136,13 +143,17 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
queryFilterID: "1,2",
queryFilterIDNotIn: "3,4",
queryFilterType: "5",
queryFilterIncentive: "1,2",
queryFilterDenom: "btc,eth",
queryFilterMinLiquidityCap: "3000",
queryFilterWithMarketIncentives: "true",
},
expectedFilter: GetPoolsRequestFilter{
PoolId: []uint64{1, 2},
PoolIdNotIn: []uint64{3, 4},
Type: []uint64{5},
Incentive: []IncentiveType{1, 2},
Denom: []string{"btc", "eth"},
MinLiquidityCap: 3000,
WithMarketIncentives: true,
Search: "search",
Expand Down Expand Up @@ -198,6 +209,13 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
},
expectError: true,
},
{
name: "Invalid Denom ( too long )",
queryParams: map[string]string{
queryFilterDenom: "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z",
},
expectError: true,
},
{
name: "Invalid Search ( too long )",
queryParams: map[string]string{
Expand Down Expand Up @@ -232,6 +250,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
assert.Equal(t, tt.expectedFilter.PoolIdNotIn, filter.PoolIdNotIn)
assert.Equal(t, tt.expectedFilter.Type, filter.Type)
assert.Equal(t, tt.expectedFilter.Incentive, filter.Incentive)
assert.Equal(t, tt.expectedFilter.Denom, filter.Denom)
assert.Equal(t, tt.expectedFilter.MinLiquidityCap, filter.MinLiquidityCap)
assert.Equal(t, tt.expectedFilter.WithMarketIncentives, filter.WithMarketIncentives)
}
Expand Down
125 changes: 91 additions & 34 deletions pkg/api/v1beta1/pools/pools.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions pools/usecase/pools_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,31 @@ func (p *poolsUseCase) GetPools(opts ...domain.PoolsOption) ([]sqsdomain.PoolI,
applyFilter(options.Filter, transformer)
}

// Denom filter filters pools by pool denoms.
// Given list of denoms in the filter, it will return pools that have at least one denom in the list.
// Order of denoms in the filter list is not important.
if f := options.Filter; f != nil && len(f.Denom) > 0 {
transformer.Filter(func(pool sqsdomain.PoolI) bool {
poolDenoms := pool.GetPoolDenoms()

// Check if any denom in f.Denom exists in poolDenoms
for _, denom := range f.Denom {
denom = strings.ToLower(denom)
for _, poolDenom := range poolDenoms {
token, err := p.tokenMetadataHolder.GetMetadataByChainDenom(poolDenom)
if err != nil {
continue
}

if denom == strings.ToLower(token.HumanDenom) || denom == strings.ToLower(token.CoinMinimalDenom) {
return true
}
}
}
return false
})
}

// Set fetch APR and fees data if configured used by some sort opts below
transformer.Range(func(key uint64, value sqsdomain.PoolI) bool {
p.setPoolAPRAndFeeDataIfConfigured(value, options)
Expand Down
7 changes: 7 additions & 0 deletions pools/usecase/pools_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,13 @@ func (s *PoolsUsecaseTestSuite) TestGetPools() {
expectError: false,
validateFunc: nil,
},
{
name: "Denom filter ( multi denom )",
options: []domain.PoolsOption{domain.WithDenom([]string{"osmo", "atom"})},
expectedLen: 1099,
expectError: false,
validateFunc: nil,
},
{
name: "Min liquidity cap filter",
options: []domain.PoolsOption{domain.WithMinPoolsLiquidityCap(1_000_000)},
Expand Down
5 changes: 4 additions & 1 deletion proto/sqs/pools/v1beta1/pools.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ message GetPoolsRequestFilter {
// response.
bool with_market_incentives = 6;

// denom is the list of denominations to filter pools by.
repeated string denom = 7;

// search is the search string to filter pools by.
string search = 7;
string search = 8;
}

// GetPoolsRequest is the request type for the Service.Get RPC method.
Expand Down
Loading