Skip to content

Commit 22afbf9

Browse files
BE-664 | Add denoms filter for /pools endpoint (#593) (#594)
Introduces a denoms filter for /pools endpoint allowing to filter pools by given list of denoms (cherry picked from commit 242467f) Co-authored-by: Deividas Petraitis <[email protected]>
1 parent 361febf commit 22afbf9

File tree

8 files changed

+165
-38
lines changed

8 files changed

+165
-38
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endif
2222

2323

2424
# --- Tooling & Variables ----------------------------------------------------------------
25+
include ./scripts/makefiles/proto.mk
2526
include ./misc/make/tools.Makefile
2627

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

179-
proto-gen:
180-
protoc --go_out=./ --go-grpc_out=./ --proto_path=./sqsdomain/proto ./sqsdomain/proto/ingest.proto
181-
182180
test-prices-mainnet:
183181
CI_SQS_PRICING_WORKER_TEST=true go test \
184182
-timeout 300s \

domain/pools.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ func WithMarketIncentives(withMarketIncentives bool) PoolsOption {
128128
})
129129
}
130130

131+
// WithDenom configures the pools options with the denom filter.
132+
func WithDenom(denom []string) PoolsOption {
133+
return WithNonNilFilter(func(filter *api.GetPoolsRequestFilter) {
134+
filter.Denom = denom
135+
})
136+
}
137+
131138
// WithPagination configures the pools options with the pagination request.
132139
func WithFilter(f *api.GetPoolsRequestFilter) PoolsOption {
133140
return func(o *PoolsOptions) {

pkg/api/v1beta1/pools/http.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pools
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67

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

1415
const (
1516
maxSearchQueryLength = 50
17+
maxDenoms = 8
1618
)
1719

1820
const (
@@ -23,6 +25,7 @@ const (
2325
queryFilterIDNotIn = "filter[id][not_in]"
2426
queryFilterType = "filter[type]"
2527
queryFilterIncentive = "filter[incentive]"
28+
queryFilterDenom = "filter[denom]"
2629
queryFilterMinLiquidityCap = "filter[min_liquidity_cap]"
2730
queryFilterWithMarketIncentives = "filter[with_market_incentives]"
2831
queryFilterSearch = "filter[search]"
@@ -72,6 +75,7 @@ func (r *GetPoolsRequestFilter) IsPresent(c echo.Context) bool {
7275
c.QueryParam(queryFilterIDNotIn) != "" ||
7376
c.QueryParam(queryFilterType) != "" ||
7477
c.QueryParam(queryFilterIncentive) != "" ||
78+
c.QueryParam(queryFilterDenom) != "" ||
7579
c.QueryParam(queryMinLiquidityCap) != "" ||
7680
c.QueryParam(queryFilterMinLiquidityCap) != "" ||
7781
c.QueryParam(queryWithMarketIncentives) != "" ||
@@ -126,6 +130,13 @@ func (r *GetPoolsRequestFilter) UnmarshalHTTPRequest(c echo.Context) error {
126130
return err
127131
}
128132

133+
if denoms := c.QueryParam(queryFilterDenom); len(denoms) > 0 {
134+
r.Denom = strings.Split(denoms, ",")
135+
if len(r.Denom) > maxDenoms {
136+
return fmt.Errorf("too many denoms, max: %d", maxDenoms)
137+
}
138+
}
139+
129140
// Deprecated: use filter[min_liquidity_cap]
130141
if p := c.QueryParam(queryMinLiquidityCap); p != "" {
131142
r.MinLiquidityCap, err = strconv.ParseUint(c.QueryParam(queryMinLiquidityCap), 10, 64)

pkg/api/v1beta1/pools/http_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func TestGetPoolsRequestFilter_IsPresent(t *testing.T) {
4545
queryParams: map[string]string{queryFilterIncentive: "2"},
4646
expectedResult: true,
4747
},
48+
{
49+
name: "With filter[denom]",
50+
queryParams: map[string]string{queryFilterDenom: "2"},
51+
expectedResult: true,
52+
},
4853
{
4954
name: "With filter[search]",
5055
queryParams: map[string]string{queryFilterSearch: "search"},
@@ -113,6 +118,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
113118
queryFilterIDNotIn: "6,7",
114119
queryFilterType: "8,9",
115120
queryFilterIncentive: "0,1",
121+
queryFilterDenom: "uosmo,atom",
116122
queryMinLiquidityCap: "1000",
117123
queryFilterMinLiquidityCap: "2000",
118124
queryWithMarketIncentives: "true",
@@ -124,6 +130,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
124130
PoolIdNotIn: []uint64{6, 7},
125131
Type: []uint64{8, 9},
126132
Incentive: []IncentiveType{0, 1},
133+
Denom: []string{"uosmo", "atom"},
127134
MinLiquidityCap: 2000,
128135
WithMarketIncentives: true,
129136
Search: "search",
@@ -136,13 +143,17 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
136143
queryFilterID: "1,2",
137144
queryFilterIDNotIn: "3,4",
138145
queryFilterType: "5",
146+
queryFilterIncentive: "1,2",
147+
queryFilterDenom: "btc,eth",
139148
queryFilterMinLiquidityCap: "3000",
140149
queryFilterWithMarketIncentives: "true",
141150
},
142151
expectedFilter: GetPoolsRequestFilter{
143152
PoolId: []uint64{1, 2},
144153
PoolIdNotIn: []uint64{3, 4},
145154
Type: []uint64{5},
155+
Incentive: []IncentiveType{1, 2},
156+
Denom: []string{"btc", "eth"},
146157
MinLiquidityCap: 3000,
147158
WithMarketIncentives: true,
148159
Search: "search",
@@ -198,6 +209,13 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
198209
},
199210
expectError: true,
200211
},
212+
{
213+
name: "Invalid Denom ( too long )",
214+
queryParams: map[string]string{
215+
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",
216+
},
217+
expectError: true,
218+
},
201219
{
202220
name: "Invalid Search ( too long )",
203221
queryParams: map[string]string{
@@ -232,6 +250,7 @@ func TestGetPoolsRequestFilter_UnmarshalHTTPRequest(t *testing.T) {
232250
assert.Equal(t, tt.expectedFilter.PoolIdNotIn, filter.PoolIdNotIn)
233251
assert.Equal(t, tt.expectedFilter.Type, filter.Type)
234252
assert.Equal(t, tt.expectedFilter.Incentive, filter.Incentive)
253+
assert.Equal(t, tt.expectedFilter.Denom, filter.Denom)
235254
assert.Equal(t, tt.expectedFilter.MinLiquidityCap, filter.MinLiquidityCap)
236255
assert.Equal(t, tt.expectedFilter.WithMarketIncentives, filter.WithMarketIncentives)
237256
}

pkg/api/v1beta1/pools/pools.pb.go

Lines changed: 91 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pools/usecase/pools_usecase.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,31 @@ func (p *poolsUseCase) GetPools(opts ...domain.PoolsOption) ([]sqsdomain.PoolI,
492492
applyFilter(options.Filter, transformer)
493493
}
494494

495+
// Denom filter filters pools by pool denoms.
496+
// Given list of denoms in the filter, it will return pools that have at least one denom in the list.
497+
// Order of denoms in the filter list is not important.
498+
if f := options.Filter; f != nil && len(f.Denom) > 0 {
499+
transformer.Filter(func(pool sqsdomain.PoolI) bool {
500+
poolDenoms := pool.GetPoolDenoms()
501+
502+
// Check if any denom in f.Denom exists in poolDenoms
503+
for _, denom := range f.Denom {
504+
denom = strings.ToLower(denom)
505+
for _, poolDenom := range poolDenoms {
506+
token, err := p.tokenMetadataHolder.GetMetadataByChainDenom(poolDenom)
507+
if err != nil {
508+
continue
509+
}
510+
511+
if denom == strings.ToLower(token.HumanDenom) || denom == strings.ToLower(token.CoinMinimalDenom) {
512+
return true
513+
}
514+
}
515+
}
516+
return false
517+
})
518+
}
519+
495520
// Set fetch APR and fees data if configured used by some sort opts below
496521
transformer.Range(func(key uint64, value sqsdomain.PoolI) bool {
497522
p.setPoolAPRAndFeeDataIfConfigured(value, options)

pools/usecase/pools_usecase_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,13 @@ func (s *PoolsUsecaseTestSuite) TestGetPools() {
757757
expectError: false,
758758
validateFunc: nil,
759759
},
760+
{
761+
name: "Denom filter ( multi denom )",
762+
options: []domain.PoolsOption{domain.WithDenom([]string{"osmo", "atom"})},
763+
expectedLen: 1099,
764+
expectError: false,
765+
validateFunc: nil,
766+
},
760767
{
761768
name: "Min liquidity cap filter",
762769
options: []domain.PoolsOption{domain.WithMinPoolsLiquidityCap(1_000_000)},

proto/sqs/pools/v1beta1/pools.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ message GetPoolsRequestFilter {
3636
// response.
3737
bool with_market_incentives = 6;
3838

39+
// denom is the list of denominations to filter pools by.
40+
repeated string denom = 7;
41+
3942
// search is the search string to filter pools by.
40-
string search = 7;
43+
string search = 8;
4144
}
4245

4346
// GetPoolsRequest is the request type for the Service.Get RPC method.

0 commit comments

Comments
 (0)