-
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.
- Loading branch information
Julian Compagni Portis
committed
Sep 13, 2023
1 parent
4b1b507
commit 94515d3
Showing
13 changed files
with
395 additions
and
1,239 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
Oops, something went wrong.