-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paginate user deposits & user LOs (#460)
remove usepositions --------- Co-authored-by: Julian Compagni Portis <julian@terrascope.io>
- Loading branch information
1 parent
9ede0f2
commit 49b2019
Showing
18 changed files
with
918 additions
and
1,261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.