From 4f721432e44f9ee48fc79a6f983badef112c76fd Mon Sep 17 00:00:00 2001 From: Vritra4 Date: Mon, 5 Aug 2024 20:15:11 +0900 Subject: [PATCH] fix nft queries --- submodules/evm-nft/grpc_query.go | 18 +++--------------- submodules/evm-nft/submodule.go | 10 +++++++--- submodules/move-nft/grpc_query.go | 18 +++--------------- submodules/move-nft/submodule.go | 4 ++-- submodules/wasm-nft/collect.go | 2 +- submodules/wasm-nft/grpc_query.go | 23 ++++------------------- submodules/wasm-nft/submodule.go | 26 +++++++++++++------------- submodules/wasm-nft/vm_util.go | 9 +-------- 8 files changed, 34 insertions(+), 76 deletions(-) diff --git a/submodules/evm-nft/grpc_query.go b/submodules/evm-nft/grpc_query.go index 00bc6a9..940e7b7 100644 --- a/submodules/evm-nft/grpc_query.go +++ b/submodules/evm-nft/grpc_query.go @@ -60,16 +60,9 @@ func (q Querier) CollectionsByAccount(ctx context.Context, req *nfttypes.QueryCo if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } - accountAddrString := accountSdkAddr.String() collectionSdkAddrs := []sdk.AccAddress{} - _, pageRes, err := query.CollectionFilteredPaginate(ctx, q.collectionOwnerMap, req.Pagination, - func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (bool, error) { - if k.K1().String() == accountAddrString { - return true, nil - } - return false, nil - }, + _, pageRes, err := query.CollectionPaginate(ctx, q.collectionOwnerMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (uint64, error) { collectionSdkAddrs = append(collectionSdkAddrs, k.K2()) return v, nil @@ -134,17 +127,12 @@ func (sm EvmNFTSubmodule) getTokensByCollection(ctx context.Context, req *nfttyp return nil, status.Error(codes.InvalidArgument, err.Error()) } - res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination, - func(key collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) { - if slices.Equal(key.K1(), colSdkAddr) { - return true, nil - } - return false, nil - }, + res, pageRes, err := query.CollectionPaginate(ctx, sm.tokenMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) return &v, nil }, + query.WithCollectionPaginationPairPrefix[sdk.AccAddress, string](colSdkAddr), ) if err != nil { diff --git a/submodules/evm-nft/submodule.go b/submodules/evm-nft/submodule.go index 286980e..4a78467 100644 --- a/submodules/evm-nft/submodule.go +++ b/submodules/evm-nft/submodule.go @@ -33,10 +33,14 @@ type EvmNFTSubmodule struct { vmKeeper *evmkeeper.Keeper pairSubmodule types.PairSubmodule - collectionMap *collections.Map[sdk.AccAddress, nfttypes.IndexedCollection] + // collectionMap: key(collection address), value(collection) + collectionMap *collections.Map[sdk.AccAddress, nfttypes.IndexedCollection] + // collectionOwnerMap: key(owner address, collection address), value(collection`s object address) collectionOwnerMap *collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], uint64] - tokenMap *collections.Map[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken] - tokenOwnerMap *collections.Map[collections.Triple[sdk.AccAddress, sdk.AccAddress, string], bool] + // tokenMap: key(collection address, token id), value(token) + tokenMap *collections.Map[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken] + // tokenOwnerMap: key(owner address, collection address, token id), value(bool as placeholder) + tokenOwnerMap *collections.Map[collections.Triple[sdk.AccAddress, sdk.AccAddress, string], bool] } func NewevmNFTSubmodule( diff --git a/submodules/move-nft/grpc_query.go b/submodules/move-nft/grpc_query.go index fb302a1..ab35e38 100644 --- a/submodules/move-nft/grpc_query.go +++ b/submodules/move-nft/grpc_query.go @@ -61,16 +61,9 @@ func (q Querier) CollectionsByAccount(ctx context.Context, req *nfttypes.QueryCo return nil, status.Error(codes.InvalidArgument, err.Error()) } accountSdkAddr := getCosmosAddress(accountAddr) - accountAddrString := accountSdkAddr.String() collectionSdkAddrs := []sdk.AccAddress{} - _, pageRes, err := query.CollectionFilteredPaginate(ctx, q.collectionOwnerMap, req.Pagination, - func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (bool, error) { - if k.K1().String() == accountAddrString { - return true, nil - } - return false, nil - }, + _, pageRes, err := query.CollectionPaginate(ctx, q.collectionOwnerMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (uint64, error) { collectionSdkAddrs = append(collectionSdkAddrs, k.K2()) return v, nil @@ -136,17 +129,12 @@ func (sm MoveNftSubmodule) getTokensByCollection(ctx context.Context, req *nftty } colSdkAddr := getCosmosAddress(collAddr) - res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination, - func(key collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) { - if slices.Equal(key.K1(), colSdkAddr) { - return true, nil - } - return false, nil - }, + res, pageRes, err := query.CollectionPaginate(ctx, sm.tokenMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) return &v, nil }, + query.WithCollectionPaginationPairPrefix[sdk.AccAddress, string](colSdkAddr), ) if err != nil { diff --git a/submodules/move-nft/submodule.go b/submodules/move-nft/submodule.go index 554d3b7..dd87799 100644 --- a/submodules/move-nft/submodule.go +++ b/submodules/move-nft/submodule.go @@ -31,11 +31,11 @@ type MoveNftSubmodule struct { vmKeeper types.MoveKeeper pairSubmodule types.PairSubmodule - // collectionMap: key(collection address`), value(collection) + // collectionMap: key(collection address), value(collection) collectionMap *collections.Map[sdk.AccAddress, nfttypes.IndexedCollection] // collectionOwnerMap: key(owner address, collection address), value(collection`s object address) collectionOwnerMap *collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], uint64] - // tokenMap: key(owner address, token id), value(token) + // tokenMap: key(collection address, token id), value(token) tokenMap *collections.IndexedMap[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken, TokenIndex] // tokenOwnerMap: key(owner address, collection address, token id), value(bool as placeholder) tokenOwnerMap *collections.Map[collections.Triple[sdk.AccAddress, sdk.AccAddress, string], bool] diff --git a/submodules/wasm-nft/collect.go b/submodules/wasm-nft/collect.go index ed7d2a1..8b12c80 100644 --- a/submodules/wasm-nft/collect.go +++ b/submodules/wasm-nft/collect.go @@ -116,7 +116,7 @@ func (sm WasmNFTSubmodule) handlerSendOrTransferEvent(ctx context.Context, event token, err := sm.tokenMap.Get(ctx, tpk) if err != nil { - sm.Logger(ctx).Debug("failed to get nft from prev owner and object addres", "collection-addr", data.ContractAddress, "token-id", data.TokenId, "prevOwner", data.Sender, "error", err) + sm.Logger(ctx).Debug("failed to get nft from prev owner and object address", "collection-addr", data.ContractAddress, "token-id", data.TokenId, "prevOwner", data.Sender, "error", err) return cosmoserr.Wrap(err, "failed to get nft from tokenMap") } token.OwnerAddr = data.Recipient.String() diff --git a/submodules/wasm-nft/grpc_query.go b/submodules/wasm-nft/grpc_query.go index e85cf25..84271df 100644 --- a/submodules/wasm-nft/grpc_query.go +++ b/submodules/wasm-nft/grpc_query.go @@ -61,16 +61,9 @@ func (q Querier) CollectionsByAccount(ctx context.Context, req *nfttypes.QueryCo return nil, status.Error(codes.InvalidArgument, err.Error()) } accountSdkAddr := getCosmosAddress(accountAddr) - accountAddrString := accountSdkAddr.String() collectionSdkAddrs := []sdk.AccAddress{} - _, pageRes, err := query.CollectionFilteredPaginate(ctx, q.collectionOwnerMap, req.Pagination, - func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (bool, error) { - if k.K1().String() == accountAddrString { - return true, nil - } - return false, nil - }, + _, pageRes, err := query.CollectionPaginate(ctx, q.collectionOwnerMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, sdk.AccAddress], v uint64) (uint64, error) { collectionSdkAddrs = append(collectionSdkAddrs, k.K2()) return v, nil @@ -136,17 +129,12 @@ func (sm WasmNFTSubmodule) getTokensByCollection(ctx context.Context, req *nftty } colSdkAddr := getCosmosAddress(collAddr) - res, pageRes, err := query.CollectionFilteredPaginate(ctx, sm.tokenMap, req.Pagination, - func(key collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (bool, error) { - if slices.Equal(key.K1(), colSdkAddr) { - return true, nil - } - return false, nil - }, + res, pageRes, err := query.CollectionPaginate(ctx, sm.tokenMap, req.Pagination, func(k collections.Pair[sdk.AccAddress, string], v nfttypes.IndexedToken) (*nfttypes.IndexedToken, error) { v.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, v.CollectionName) return &v, nil }, + query.WithCollectionPaginationPairPrefix[sdk.AccAddress, string](colSdkAddr), ) if err != nil { return nil, handleCollectionErr(err) @@ -189,10 +177,7 @@ func (sm WasmNFTSubmodule) 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 diff --git a/submodules/wasm-nft/submodule.go b/submodules/wasm-nft/submodule.go index 0a1f9e3..7499405 100644 --- a/submodules/wasm-nft/submodule.go +++ b/submodules/wasm-nft/submodule.go @@ -31,11 +31,11 @@ type WasmNFTSubmodule struct { vmKeeper types.WasmKeeper pairSubmodule types.PairSubmodule - // collectionMap: key(collection address`), value(collection) + // collectionMap: key(collection address), value(collection) collectionMap *collections.Map[sdk.AccAddress, nfttypes.IndexedCollection] // collectionOwnerMap: key(owner address, collection address), value(collection`s object address) collectionOwnerMap *collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], uint64] - // tokenMap: key(owner address, token id), value(token) + // tokenMap: key(collection address, token id), value(token) tokenMap *collections.Map[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken] // tokenOwnerMap: key(owner address, collection address, token id), value(bool as placeholder) tokenOwnerMap *collections.Map[collections.Triple[sdk.AccAddress, sdk.AccAddress, string], bool] @@ -87,39 +87,39 @@ func NewWasmNFTSubmodule( } // Logger returns a module-specific logger. -func (sub WasmNFTSubmodule) Logger(ctx context.Context) log.Logger { +func (sm WasmNFTSubmodule) Logger(ctx context.Context) log.Logger { sdkCtx := sdk.UnwrapSDKContext(ctx) return sdkCtx.Logger().With("module", "x/"+types.SubmoduleName) } -func (sub WasmNFTSubmodule) Name() string { +func (sm WasmNFTSubmodule) Name() string { return types.SubmoduleName } -func (sub WasmNFTSubmodule) Version() string { +func (sm WasmNFTSubmodule) Version() string { return types.Version } -func (sub WasmNFTSubmodule) RegisterQueryHandlerClient(cc client.Context, mux *runtime.ServeMux) error { +func (sm WasmNFTSubmodule) RegisterQueryHandlerClient(cc client.Context, mux *runtime.ServeMux) error { return nfttypes.RegisterQueryHandlerClient(context.Background(), mux, nfttypes.NewQueryClient(cc)) } -func (sub WasmNFTSubmodule) RegisterQueryServer(s grpc.Server) { - nfttypes.RegisterQueryServer(s, NewQuerier(sub)) +func (sm WasmNFTSubmodule) RegisterQueryServer(s grpc.Server) { + nfttypes.RegisterQueryServer(s, NewQuerier(sm)) } -func (sub WasmNFTSubmodule) Prepare(ctx context.Context) error { +func (sm WasmNFTSubmodule) Prepare(ctx context.Context) error { return nil } -func (sub WasmNFTSubmodule) Initialize(ctx context.Context) error { +func (sm WasmNFTSubmodule) Initialize(ctx context.Context) error { return nil } -func (sub WasmNFTSubmodule) FinalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error { - return sub.finalizeBlock(ctx, req, res) +func (sm WasmNFTSubmodule) FinalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error { + return sm.finalizeBlock(ctx, req, res) } -func (sub WasmNFTSubmodule) Commit(ctx context.Context, res abci.ResponseCommit, changeSet []*storetypes.StoreKVPair) error { +func (sm WasmNFTSubmodule) Commit(ctx context.Context, res abci.ResponseCommit, changeSet []*storetypes.StoreKVPair) error { return nil } diff --git a/submodules/wasm-nft/vm_util.go b/submodules/wasm-nft/vm_util.go index f7e5319..5afb531 100644 --- a/submodules/wasm-nft/vm_util.go +++ b/submodules/wasm-nft/vm_util.go @@ -2,7 +2,6 @@ package wasm_nft import ( "context" - "encoding/base64" "encoding/json" "strconv" @@ -24,19 +23,13 @@ var ( qreqCollectionNumTokens = []byte("{\"num_tokens\":{}}") // {"num_tokens":{}} ) -func encode(req []byte) []byte { - res := make([]byte, base64.StdEncoding.EncodedLen(len(req))) - base64.StdEncoding.Encode(res, req) - return res -} - func generateQueryRequestToGetNftInfo(tokenId string) []byte { return []byte(`{"nft_info":{"token_id":"` + tokenId + `"}}`) //return encode(qb) } func (sm WasmNFTSubmodule) getCollectionContractInfo(ctx context.Context, colAddr sdk.AccAddress) (*types.ContractInfo, error) { - rb, err := sm.vmKeeper.QuerySmart(ctx, colAddr, []byte("{\"contract_info\":{}}")) //qreqCollectionContractInfo) + rb, err := sm.vmKeeper.QuerySmart(ctx, colAddr, qreqCollectionContractInfo) if err != nil { return nil, status.Error(codes.Internal, err.Error()) }