-
Notifications
You must be signed in to change notification settings - Fork 609
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
Add Pagination to Pool With Filter Query #3563
Changes from 5 commits
a928e56
cf9ac4a
9088b82
aee1d0c
cc83286
79eecfc
c78ee5d
18f9284
0f9b9e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,66 +158,38 @@ func (q Querier) CalcJoinPoolShares(ctx context.Context, req *types.QueryCalcJoi | |
|
||
// PoolsWithFilter query allows to query pools with specific parameters | ||
func (q Querier) PoolsWithFilter(ctx context.Context, req *types.QueryPoolsWithFilterRequest) (*types.QueryPoolsWithFilterResponse, error) { | ||
res, err := q.Pools(ctx, &types.QueryPoolsRequest{ | ||
Pagination: &query.PageRequest{}, | ||
}) | ||
sdkCtx := sdk.UnwrapSDKContext(ctx) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pools := res.Pools | ||
|
||
var response = []*codectypes.Any{} | ||
store := sdkCtx.KVStore(q.Keeper.storeKey) | ||
poolStore := prefix.NewStore(store, types.KeyPrefixPools) | ||
|
||
// set filters | ||
min_liquidity := req.MinLiquidity | ||
pool_type := req.PoolType | ||
checks_needed := 0 | ||
// increase amount of needed checks for each filter by 1 | ||
if min_liquidity != nil { | ||
if len(min_liquidity) > 0 { | ||
checks_needed++ | ||
} | ||
|
||
if pool_type != "" { | ||
checks_needed++ | ||
} | ||
|
||
for _, p := range pools { | ||
var response = []*codectypes.Any{} | ||
pageRes, err := query.FilteredPaginate(poolStore, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) { | ||
var checks = 0 | ||
var pool types.PoolI | ||
|
||
err := q.cdc.UnpackAny(p, &pool) | ||
pool, err := q.Keeper.UnmarshalPool(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where does this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It comes from the FilteredPagination method, where it iterates over the given kv store! |
||
if err != nil { | ||
return nil, sdkerrors.ErrUnpackAny | ||
return false, err | ||
} | ||
|
||
poolId := pool.GetId() | ||
|
||
// if liquidity specified in request | ||
if min_liquidity != nil { | ||
if len(min_liquidity) > 0 { | ||
poolLiquidity := pool.GetTotalPoolLiquidity(sdkCtx) | ||
amount_of_denoms := 0 | ||
check_amount := false | ||
check_denoms := false | ||
|
||
if poolLiquidity.IsAllGTE(min_liquidity) { | ||
check_amount = true | ||
} | ||
|
||
for _, req_coin := range min_liquidity { | ||
for _, coin := range poolLiquidity { | ||
if req_coin.Denom == coin.Denom { | ||
amount_of_denoms++ | ||
} | ||
} | ||
} | ||
|
||
if amount_of_denoms == len(min_liquidity) { | ||
check_denoms = true | ||
} | ||
|
||
if check_amount && check_denoms { | ||
checks++ | ||
} | ||
} | ||
|
@@ -226,23 +198,37 @@ func (q Querier) PoolsWithFilter(ctx context.Context, req *types.QueryPoolsWithF | |
if pool_type != "" { | ||
poolType, err := q.GetPoolType(sdkCtx, poolId) | ||
if err != nil { | ||
return nil, types.ErrPoolNotFound | ||
return false, types.ErrPoolNotFound | ||
} | ||
|
||
if poolType == pool_type { | ||
checks++ | ||
} else { | ||
continue | ||
} | ||
} | ||
|
||
if checks == checks_needed { | ||
response = append(response, p) | ||
any, err := codectypes.NewAnyWithValue(pool) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if accumulate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required for |
||
response = append(response, any) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
return false, nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, status.Error(codes.Internal, err.Error()) | ||
} | ||
|
||
return &types.QueryPoolsWithFilterResponse{ | ||
Pools: response, | ||
Pools: response, | ||
Pagination: pageRes, | ||
}, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
FilteredPaginate
instead ofPaginate
method allows us to iterate until pagination count has been reachedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cosmos/cosmos-sdk#12242
We can backport the fix for that to clean this up a little if we want.
(Or just copy/paste cosmos/cosmos-sdk#12253 into osmoutils)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also checking generic pagination as well! Sad that we don't have it yet. I say we refactor this to use generic pagination later on when it gets backported and for now leave as is