Skip to content

Commit

Permalink
switch to paginated user deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Compagni Portis committed Sep 13, 2023
1 parent 4b1b507 commit 94515d3
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 1,239 deletions.
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
14 changes: 0 additions & 14 deletions proto/duality/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 @@ -52,11 +51,6 @@ service Query {
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}";
Expand Down Expand Up @@ -155,14 +149,6 @@ 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;
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
}
119 changes: 22 additions & 97 deletions x/dex/keeper/grpc_query_user_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/duality-labs/duality/utils"
"github.com/duality-labs/duality/x/dex/types"
"github.com/duality-labs/duality/x/dex/utils"
dexutils "github.com/duality-labs/duality/x/dex/utils"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -26,114 +25,40 @@ func (k Keeper) UserDepositsAll(
}
ctx := sdk.UnwrapSDKContext(goCtx)

if req.Pagination == nil {
req.Pagination = &query.PageRequest{}
}

offset := req.Pagination.Offset
key := req.Pagination.Key
limit := req.Pagination.Limit
countTotal := req.Pagination.CountTotal

if req.Pagination.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
}

var depositArr []*types.DepositRecord

// paginate with key
if len(key) != 0 {
var (
startAccum false
numHits uint64
nextKey []byte
)
k.bankKeeper.IterateAccountBalances(ctx, addr, func(coin sdk.Coin) bool {
if coin.Denom == string(key) {
startAccum = true
}
if numHits == limit {
nextKey == []byte(coin.Denom)
return true
pageRes, err := utils.FilteredPaginateAccountBalances(
ctx,
k.bankKeeper,
addr,
req.Pagination,
func(poolCoinMaybe sdk.Coin, accumulate bool) bool {
depositDenom, err := types.NewDepositDenomFromString(poolCoinMaybe.Denom)
if err != nil {
return false
}
if startAccum {
depositDenom, err := types.NewDepositDenomFromString(sharesMaybe.Denom)
if err != nil {
return false
}

numHits++
if accumulate {
depositRecord := &types.DepositRecord{
PairID: depositDenom.PairID,
SharesOwned: sharesMaybe.Amount,
SharesOwned: poolCoinMaybe.Amount,
CenterTickIndex: depositDenom.Tick,
LowerTickIndex: depositDenom.Tick - utils.MustSafeUint64(depositDenom.Fee),
UpperTickIndex: depositDenom.Tick + utils.MustSafeUint64(depositDenom.Fee),
LowerTickIndex: depositDenom.Tick - dexutils.MustSafeUint64(depositDenom.Fee),
UpperTickIndex: depositDenom.Tick + dexutils.MustSafeUint64(depositDenom.Fee),
Fee: depositDenom.Fee,
}
depositArr = append(depositArr, depositRecord)

return false
}
})
} else {
iterator := getIterator(prefixStore, nil, reverse)
defer iterator.Close()

end := offset + limit

var (
numHits uint64
nextKey []byte
)

for ; iterator.Valid(); iterator.Next() {
if iterator.Error() != nil {
return nil, iterator.Error()
}

accumulate := numHits >= offset && numHits < end
hit, err := onResult(iterator.Key(), iterator.Value(), accumulate)
if err != nil {
return nil, err
}

if hit {
numHits++
}

if numHits == end+1 {
if nextKey == nil {
nextKey = iterator.Key()
}

if !countTotal {
break
}
depositArr = append(depositArr, depositRecord)
}
}

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

return res, nil

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

return &types.QueryAllUserDepositsResponse{
Deposits: k.GetAllDepositsForAddress(ctx, addr),
Deposits: depositArr,
Pagination: pageRes,
}, nil
}
Loading

0 comments on commit 94515d3

Please sign in to comment.