Skip to content

Commit

Permalink
Merge pull request #54 from initia-labs/fix/nft-queries
Browse files Browse the repository at this point in the history
fix nft queries
  • Loading branch information
Vritra4 authored Aug 6, 2024
2 parents 3e8adce + 4f72143 commit 2ec26d3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 76 deletions.
18 changes: 3 additions & 15 deletions submodules/evm-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions submodules/evm-nft/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 3 additions & 15 deletions submodules/move-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions submodules/move-nft/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion submodules/wasm-nft/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 4 additions & 19 deletions submodules/wasm-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions submodules/wasm-nft/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
9 changes: 1 addition & 8 deletions submodules/wasm-nft/vm_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package wasm_nft

import (
"context"
"encoding/base64"
"encoding/json"
"strconv"

Expand All @@ -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())
}
Expand Down

0 comments on commit 2ec26d3

Please sign in to comment.