Skip to content

Commit

Permalink
impl paging for grpc queries Auctions and TokenPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Feb 13, 2025
1 parent 2cab8f4 commit d220170
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
22 changes: 19 additions & 3 deletions x/auction/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/Stride-Labs/stride/v25/x/auction/types"
)
Expand Down Expand Up @@ -39,11 +41,25 @@ func (k Keeper) Auctions(goCtx context.Context, req *types.QueryAuctionsRequest)

ctx := sdk.UnwrapSDKContext(goCtx)

auctions := k.GetAllAuctions(ctx)
store := ctx.KVStore(k.storeKey)
auctionStore := prefix.NewStore(store, types.AuctionPrefix)

// TODO impl paging
auctions := []types.Auction{}
pageRes, err := query.Paginate(auctionStore, req.Pagination, func(key []byte, value []byte) error {
var auction types.Auction
if err := k.cdc.Unmarshal(value, &auction); err != nil {
return err
}

auctions = append(auctions, auction)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryAuctionsResponse{
Auctions: auctions,
Auctions: auctions,
Pagination: pageRes,
}, nil
}
21 changes: 18 additions & 3 deletions x/icqoracle/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"

Expand Down Expand Up @@ -44,18 +46,31 @@ func (k Keeper) TokenPrices(goCtx context.Context, req *types.QueryTokenPricesRe

ctx := sdk.UnwrapSDKContext(goCtx)

// TODO impl paging
store := ctx.KVStore(k.storeKey)
tokenPriceStore := prefix.NewStore(store, types.TokenPricePrefix)

responses := []types.TokenPriceResponse{}
for _, tokenPrice := range k.GetAllTokenPrices(ctx) {
pageRes, err := query.Paginate(tokenPriceStore, req.Pagination, func(key []byte, value []byte) error {
var tokenPrice types.TokenPrice
if err := k.cdc.Unmarshal(value, &tokenPrice); err != nil {
return err
}

responses = append(responses, types.TokenPriceResponse{
BaseDenomUnwrapped: k.unwrapIBCDenom(ctx, tokenPrice.BaseDenom),
QuoteDenomUnwrapped: k.unwrapIBCDenom(ctx, tokenPrice.QuoteDenom),
TokenPrice: tokenPrice,
})
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryTokenPricesResponse{TokenPrices: responses}, nil
return &types.QueryTokenPricesResponse{
TokenPrices: responses,
Pagination: pageRes,
}, nil
}

// Params queries the oracle parameters
Expand Down
2 changes: 1 addition & 1 deletion x/icqoracle/keeper/token_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (k Keeper) GetTokenPriceForQuoteDenom(ctx sdk.Context, baseDenom string, qu

// GetAllTokenPrices retrieves all stored token prices
func (k Keeper) GetAllTokenPrices(ctx sdk.Context) []types.TokenPrice {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), []byte(types.TokenPricePrefix))
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.TokenPricePrefix)
defer iterator.Close()

prices := []types.TokenPrice{}
Expand Down

0 comments on commit d220170

Please sign in to comment.