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

fix token query with owner/coll #38

Merged
merged 1 commit into from
Apr 25, 2024
Merged
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
44 changes: 25 additions & 19 deletions submodules/move-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ func (sm MoveNftSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes
ownerSdkAddr := getCosmosAddress(ownerAddr)
identifiers := []collections.Pair[sdk.AccAddress, string]{}

_, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenOwnerMap, req.Pagination,
func(k collections.Triple[sdk.AccAddress, sdk.AccAddress, string], _ bool) (bool, error) {
return true, nil
},
_, pageRes, err := query.CollectionPaginate(ctx, sm.tokenOwnerMap, req.Pagination,
func(k collections.Triple[sdk.AccAddress, sdk.AccAddress, string], v bool) (bool, error) {
identifiers = append(identifiers, collections.Join(k.K2(), k.K3()))
return v, nil
Expand Down Expand Up @@ -228,24 +225,28 @@ func (sm MoveNftSubmodule) getTokensByAccountAndCollection(ctx context.Context,
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ownerSdkAddr := getCosmosAddress(ownerAddr)
ownerAddrStr := ownerSdkAddr.String()

res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination,
func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) {
if slices.Equal(k.K1(), colSdkAddr) && (v.OwnerAddr == ownerAddrStr) {
return true, nil
}
return false, nil
},
func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) {
v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName)
return &v, nil
identifiers := []collections.Pair[sdk.AccAddress, string]{}
_, pageRes, err := query.CollectionPaginate(ctx, sm.tokenOwnerMap, req.Pagination,
func(k collections.Triple[sdk.AccAddress, sdk.AccAddress, string], v bool) (bool, error) {
identifiers = append(identifiers, collections.Join(k.K2(), k.K3()))
return v, nil
},
WithCollectionPaginationTriplePrefix2[sdk.AccAddress, sdk.AccAddress, string](ownerSdkAddr, colSdkAddr),
)

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, handleCollectionErr(err)
}
res := []*nfttypes.IndexedToken{}
for _, identifier := range identifiers {
token, err := sm.tokenMap.Get(ctx, identifier)
if err != nil {
return nil, handleCollectionErr(err)
}
token.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, token.CollectionName)
res = append(res, &token)
}

res = slices.DeleteFunc(res, func(item *nfttypes.IndexedToken) bool {
return item == nil
})
Expand Down Expand Up @@ -281,11 +282,16 @@ func (sm MoveNftSubmodule) getTokensByAccountCollectionAndTokenId(ctx context.Co
}, nil
}

// WithCollectionPaginationTriplePrefix applies a prefix to a collection, whose key is a collection.Triple,
// being paginated that needs prefixing.
func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) {
return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) {
prefix := collections.TriplePrefix[K1, K2, K3](prefix)
o.Prefix = &prefix
}
}

func WithCollectionPaginationTriplePrefix2[K1, K2, K3 any](prefix K1, prefix2 K2) func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) {
return func(o *query.CollectionsPaginateOptions[collections.Triple[K1, K2, K3]]) {
prefix := collections.TripleSuperPrefix[K1, K2, K3](prefix, prefix2)
o.Prefix = &prefix
}
}
Loading