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

Paginate user deposits & user LOs #460

Merged
merged 4 commits into from
Sep 19, 2023
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
5 changes: 2 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,8 @@ func NewApp(
// "/dualitylabs.duality.dex.Query/LimitOrderTrancheUserAll": &dexmoduletypes.QueryAllLimitOrderTrancheUserResponse{},
"/dualitylabs.duality.dex.Query/LimitOrderTranche": &dexmoduletypes.QueryGetLimitOrderTrancheResponse{},
// "/dualitylabs.duality.dex.Query/LimitOrderTrancheAll": &dexmoduletypes.QueryAllLimitOrderTrancheResponse{},
"/dualitylabs.duality.dex.Query/GetUserPositions": &dexmoduletypes.QueryGetUserPositionsResponse{},
// "/dualitylabs.duality.dex.Query/UserDepositsAll": &dexmoduletypes.QueryAllUserDepositsResponse{},
// "/dualitylabs.duality.dex.Query/UserLimitOrdersAll": &dexmoduletypes.QueryAllUserLimitOrdersResponse{},
"/dualitylabs.duality.dex.Query/UserDepositsAll": &dexmoduletypes.QueryAllUserDepositsResponse{},
"/dualitylabs.duality.dex.Query/UserLimitOrdersAll": &dexmoduletypes.QueryAllUserLimitOrdersResponse{},
// "/dualitylabs.duality.dex.Query/TickLiquidityAll": &dexmoduletypes.QueryAllTickLiquidityResponse{},
"/dualitylabs.duality.dex.Query/InactiveLimitOrderTranche": &dexmoduletypes.QueryGetInactiveLimitOrderTrancheResponse{},
// "/dualitylabs.duality.dex.Query/InactiveLimitOrderTrancheAll": &dexmoduletypes.QueryAllInactiveLimitOrderTrancheResponse{},
Expand Down
40 changes: 9 additions & 31 deletions proto/duality/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import "cosmos/base/query/v1beta1/pagination.proto";
import "duality/dex/params.proto";
import "duality/dex/limit_order_tranche_user.proto";
import "duality/dex/limit_order_tranche.proto";
import "duality/dex/user_positions.proto";
import "duality/dex/deposit_record.proto";
import "duality/dex/tick_liquidity.proto";
import "duality/dex/pool_reserves.proto";
Expand Down Expand Up @@ -41,77 +40,60 @@ service Query {
option (google.api.http).get = "/duality/dex/limit_order_tranche_user";

}

// Queries a list of LimitOrderTrancheUser items for a given address.
rpc LimitOrderTrancheUserAllByAddress(QueryAllUserLimitOrdersRequest) returns (QueryAllUserLimitOrdersResponse) {
option (google.api.http).get = "/duality/dex/user/limit_orders/{address}";
}

// Queries a LimitOrderTranche by index.
rpc LimitOrderTranche (QueryGetLimitOrderTrancheRequest) returns (QueryGetLimitOrderTrancheResponse) {
option (google.api.http).get = "/duality/dex/limit_order_tranche/{pairID}/{tokenIn}/{tickIndex}/{trancheKey}";

}

// Queries a list of LimitOrderTranche items for a given pairID / TokenIn combination.
rpc LimitOrderTrancheAll (QueryAllLimitOrderTrancheRequest) returns (QueryAllLimitOrderTrancheResponse) {
option (google.api.http).get = "/duality/dex/limit_order_tranche/{pairID}/{tokenIn}";

}

// Queries a list of GetUserPositions items.
rpc GetUserPositions (QueryGetUserPositionsRequest) returns (QueryGetUserPositionsResponse) {
option (google.api.http).get = "/duality/dex/user/positions/{address}";

}

// Queries a list of UserDeposits items.
rpc UserDepositsAll (QueryAllUserDepositsRequest) returns (QueryAllUserDepositsResponse) {
option (google.api.http).get = "/duality/dex/user/deposits/{address}";

}

// Queries a list of UserLimitOrders items.
rpc UserLimitOrdersAll (QueryAllUserLimitOrdersRequest) returns (QueryAllUserLimitOrdersResponse) {
option (google.api.http).get = "/duality/dex/user/limit_orders/{address}";

}

// Queries a list of TickLiquidity items.
rpc TickLiquidityAll (QueryAllTickLiquidityRequest) returns (QueryAllTickLiquidityResponse) {
option (google.api.http).get = "/duality/dex/tick_liquidity/{pairID}/{tokenIn}";

}

// Queries a InactiveLimitOrderTranche by index.
rpc InactiveLimitOrderTranche (QueryGetInactiveLimitOrderTrancheRequest) returns (QueryGetInactiveLimitOrderTrancheResponse) {
option (google.api.http).get = "/duality/dex/filled_limit_order_tranche/{pairID}/{tokenIn}/{tickIndex}/{trancheKey}";

}

// Queries a list of InactiveLimitOrderTranche items.
rpc InactiveLimitOrderTrancheAll (QueryAllInactiveLimitOrderTrancheRequest) returns (QueryAllInactiveLimitOrderTrancheResponse) {
option (google.api.http).get = "/duality/dex/filled_limit_order_tranche";

}

// Queries a list of PoolReserves items.
rpc PoolReservesAll (QueryAllPoolReservesRequest) returns (QueryAllPoolReservesResponse) {
option (google.api.http).get = "/duality/dex/pool_reserves/{pairID}/{tokenIn}";

}

// Queries a PoolReserve by index
rpc PoolReserves (QueryGetPoolReservesRequest) returns (QueryGetPoolReservesResponse) {
option (google.api.http).get = "/duality/dex/pool_reserves/{pairID}/{tokenIn}/{tickIndex}/{fee}";

}

// Queries the simulated result of a multihop swap
rpc EstimateMultiHopSwap (QueryEstimateMultiHopSwapRequest) returns (QueryEstimateMultiHopSwapResponse) {
option (google.api.http).get = "/duality/dex/estimate_multi_hop_swap";

}

// Queries the simulated result of a multihop swap
rpc EstimatePlaceLimitOrder (QueryEstimatePlaceLimitOrderRequest) returns (QueryEstimatePlaceLimitOrderResponse) {
option (google.api.http).get = "/duality/dex/estimate_place_limit_order";

}

// Queries a pool by pair, tick and fee
Expand Down Expand Up @@ -191,28 +173,24 @@ message QueryAllLimitOrderTrancheResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryGetUserPositionsRequest {
string address = 1;
}

message QueryGetUserPositionsResponse {
UserPositions UserPositions = 1 [(gogoproto.nullable) = true];
}

message QueryAllUserDepositsRequest {
string address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllUserDepositsResponse {
repeated DepositRecord Deposits = 1 [(gogoproto.nullable) = true];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllUserLimitOrdersRequest {
string address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllUserLimitOrdersResponse {
repeated LimitOrderTrancheUser limitOrders = 1 [(gogoproto.nullable) = true];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllTickLiquidityRequest {
Expand Down
12 changes: 0 additions & 12 deletions proto/duality/dex/user_positions.proto

This file was deleted.

110 changes: 110 additions & 0 deletions utils/bank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package utils

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/duality-labs/duality/x/dex/types"

"github.com/cosmos/cosmos-sdk/types/query"
)

func FilteredPaginateAccountBalances(
ctx sdk.Context,
bankKeeper types.BankKeeper,
address sdk.AccAddress,
pageRequest *query.PageRequest,
onResult func(coin sdk.Coin, accumulate bool) bool,
) (*query.PageResponse, error) {
// if the PageRequest is nil, use default PageRequest
if pageRequest == nil {
pageRequest = &query.PageRequest{}
}

offset := pageRequest.Offset
key := pageRequest.Key
limit := pageRequest.Limit
countTotal := pageRequest.CountTotal

if pageRequest.Reverse {
return nil, fmt.Errorf("invalid request, reverse pagination is not enabled")
}
if offset > 0 && key != nil {
return nil, fmt.Errorf("invalid request, either offset or key is expected, got both")
}

if limit == 0 {
limit = query.DefaultLimit

// count total results when the limit is zero/not supplied
countTotal = true
}

if len(key) != 0 {
// paginate with key
var (
numHits uint64
nextKey []byte
)
startAccum := false

bankKeeper.IterateAccountBalances(ctx, address, func(coin sdk.Coin) bool {
if coin.Denom == string(key) {
startAccum = true
}
if numHits == limit {
nextKey = []byte(coin.Denom)
return true
}
if startAccum {
hit := onResult(coin, true)
if hit {
numHits++
}
}

return false
})

return &query.PageResponse{
NextKey: nextKey,
}, nil
} else {
// default pagination (with offset)

end := offset + limit

var (
numHits uint64
nextKey []byte
)

bankKeeper.IterateAccountBalances(ctx, address, func(coin sdk.Coin) bool {
accumulate := numHits >= offset && numHits < end
hit := onResult(coin, accumulate)

if hit {
numHits++
}

if numHits == end+1 {
if nextKey == nil {
nextKey = []byte(coin.Denom)
}

if !countTotal {
return true
}
}

return false
})

res := &query.PageResponse{NextKey: nextKey}
if countTotal {
res.Total = numHits
}

return res, nil
}
}
1 change: 0 additions & 1 deletion x/dex/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func GetQueryCmd(_ string) *cobra.Command {
cmd.AddCommand(CmdShowLimitOrderTrancheUser())
cmd.AddCommand(CmdListLimitOrderTranche())
cmd.AddCommand(CmdShowLimitOrderTranche())
cmd.AddCommand(CmdShowUserPositions())
cmd.AddCommand(CmdListUserDeposits())
cmd.AddCommand(CmdListUserLimitOrders())
cmd.AddCommand(CmdListTickLiquidity())
Expand Down
42 changes: 0 additions & 42 deletions x/dex/client/cli/query_get_user_positions.go

This file was deleted.

1 change: 1 addition & 0 deletions x/dex/client/cli/query_user_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func CmdListUserDeposits() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)

return cmd
}
3 changes: 2 additions & 1 deletion x/dex/client/cli/query_user_limit_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CmdListUserLimitOrders() *cobra.Command {
Address: reqAddress,
}

res, err := queryClient.UserLimitOrdersAll(cmd.Context(), params)
res, err := queryClient.LimitOrderTrancheUserAllByAddress(cmd.Context(), params)
if err != nil {
return err
}
Expand All @@ -37,6 +37,7 @@ func CmdListUserLimitOrders() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)

return cmd
}
37 changes: 37 additions & 0 deletions x/dex/keeper/grpc_query_limit_order_tranche_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,40 @@ func (k Keeper) LimitOrderTrancheUser(c context.Context,

return &types.QueryGetLimitOrderTrancheUserResponse{LimitOrderTrancheUser: val}, nil
}

func (k Keeper) LimitOrderTrancheUserAllByAddress(
goCtx context.Context,
req *types.QueryAllUserLimitOrdersRequest,
) (*types.QueryAllUserLimitOrdersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
var limitOrderTrancheUserList []*types.LimitOrderTrancheUser
addressPrefix := types.LimitOrderTrancheUserAddressPrefix(addr.String())
store := prefix.NewStore(ctx.KVStore(k.storeKey), addressPrefix)

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
trancheUser := &types.LimitOrderTrancheUser{}
if err := k.cdc.Unmarshal(value, trancheUser); err != nil {
return err
}

limitOrderTrancheUserList = append(limitOrderTrancheUserList, trancheUser)

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

return &types.QueryAllUserLimitOrdersResponse{
LimitOrders: limitOrderTrancheUserList,
Pagination: pageRes,
}, nil
}
Loading