Skip to content

Commit

Permalink
Merge pull request #44 from initia-labs/fix/nfts
Browse files Browse the repository at this point in the history
fix indexer about nft burn / fix querier
  • Loading branch information
Vritra4 authored Jun 13, 2024
2 parents d640e64 + 16d729d commit 6236f4a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion submodules/move-nft/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (sm MoveNftSubmodule) handleBurnEvent(ctx context.Context, event types.Even
return err // just return err, no wrap
}

err = sm.tokenOwnerMap.Set(ctx, collections.Join3(ownerSdkAddr, tpk.K1(), tpk.K2()), true)
err = sm.tokenOwnerMap.Remove(ctx, collections.Join3(ownerSdkAddr, tpk.K1(), tpk.K2()))
if err != nil {
sm.Logger(ctx).Error("failed to remove from tokenOwnerSet", "owner", ownerSdkAddr, "collection-addr", tpk.K1(), "token-id", tpk.K2(), "error", err)
return errors.New("failed to insert token into tokenOwnerSet")
Expand Down
9 changes: 6 additions & 3 deletions submodules/move-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (q Querier) CollectionsByAccount(ctx context.Context, req *nfttypes.QueryCo
for _, collectionSdkAddr := range collectionSdkAddrs {
collection, err := q.collectionMap.Get(ctx, collectionSdkAddr)
if err != nil {
return nil, handleCollectionErr(err)
q.Logger(ctx).Warn("index mismatch found", "collection", collectionSdkAddr, "action", "CollectionsByAccount")
continue
}
collection.Collection.Name, _ = q.getCollectionNameFromPairSubmodule(ctx, collection.Collection.Name)
collections = append(collections, &collection)
Expand Down Expand Up @@ -201,7 +202,8 @@ func (sm MoveNftSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes
for _, identifier := range identifiers {
token, err := sm.tokenMap.Get(ctx, identifier)
if err != nil {
return nil, handleCollectionErr(err)
sm.Logger(ctx).Warn("index mismatch found", "account", ownerSdkAddr, "action", "CollectionsByAccount")
continue
}
token.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, token.CollectionName)
res = append(res, &token)
Expand Down Expand Up @@ -241,7 +243,8 @@ func (sm MoveNftSubmodule) getTokensByAccountAndCollection(ctx context.Context,
for _, identifier := range identifiers {
token, err := sm.tokenMap.Get(ctx, identifier)
if err != nil {
return nil, handleCollectionErr(err)
sm.Logger(ctx).Warn("index mismatch found", "account", ownerSdkAddr, "collection", colSdkAddr, "action", "GetTokensByAccountAndCollection")
continue
}
token.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, token.CollectionName)
res = append(res, &token)
Expand Down
10 changes: 7 additions & 3 deletions submodules/move-nft/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ type MoveNftSubmodule struct {
vmKeeper types.MoveKeeper
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.IndexedMap[collections.Pair[sdk.AccAddress, string], nfttypes.IndexedToken, TokenIndex]
tokenOwnerMap *collections.Map[collections.Triple[sdk.AccAddress, sdk.AccAddress, string], bool]
// tokenMap: key(owner 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]
}

func NewMoveNftSubmodule(
Expand Down
2 changes: 1 addition & 1 deletion submodules/move-nft/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
SubmoduleName = "move-nft"

// Version is the current version of the submodule
Version = "v0.1.2"
Version = "v0.1.3"
)

// store prefixes
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 @@ -174,7 +174,7 @@ func (sm WasmNFTSubmodule) handleBurnEvent(ctx context.Context, event types.Even
ownerAddr, _ := getVMAddress(sm.ac, token.OwnerAddr)
ownerSdkAddr := getCosmosAddress(ownerAddr)

err = sm.tokenOwnerMap.Set(ctx, collections.Join3(ownerSdkAddr, tpk.K1(), tpk.K2()), true)
err = sm.tokenOwnerMap.Remove(ctx, collections.Join3(ownerSdkAddr, tpk.K1(), tpk.K2()))
if err != nil {
sm.Logger(ctx).Error("failed to remove from tokenOwnerSet", "owner", ownerSdkAddr, "collection-addr", tpk.K1(), "token-id", tpk.K2(), "error", err)
return cosmoserr.Wrap(err, "failed to insert token into tokenOwnerSet")
Expand Down
44 changes: 29 additions & 15 deletions submodules/wasm-nft/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (q Querier) CollectionsByAccount(ctx context.Context, req *nfttypes.QueryCo
for _, collectionSdkAddr := range collectionSdkAddrs {
collection, err := q.collectionMap.Get(ctx, collectionSdkAddr)
if err != nil {
return nil, handleCollectionErr(err)
q.Logger(ctx).Warn("index mismatch found", "collection", collectionSdkAddr, "action", "CollectionsByAccount")
continue
}
collection.Collection.Name, _ = q.getCollectionNameFromPairSubmodule(ctx, collection.Collection.Name)
collections = append(collections, &collection)
Expand Down Expand Up @@ -203,7 +204,8 @@ func (sm WasmNFTSubmodule) getTokensByAccount(ctx context.Context, req *nfttypes
for _, identifier := range identifiers {
token, err := sm.tokenMap.Get(ctx, identifier)
if err != nil {
return nil, handleCollectionErr(err)
sm.Logger(ctx).Warn("index mismatch found", "account", ownerSdkAddr, "action", "CollectionsByAccount")
continue
}
token.CollectionName, _ = sm.getCollectionNameFromPairSubmodule(ctx, token.CollectionName)
res = append(res, &token)
Expand All @@ -227,24 +229,29 @@ func (sm WasmNFTSubmodule) 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 {
sm.Logger(ctx).Warn("index mismatch found", "account", ownerSdkAddr, "collection", colSdkAddr, "action", "GetTokensByAccountAndCollection")
continue
}
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 @@ -288,3 +295,10 @@ func WithCollectionPaginationTriplePrefix[K1, K2, K3 any](prefix K1) func(o *que
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
}
}
10 changes: 7 additions & 3 deletions submodules/wasm-nft/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ type WasmNFTSubmodule struct {
vmKeeper types.WasmKeeper
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(owner 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 NewWasmNFTSubmodule(
Expand Down
2 changes: 1 addition & 1 deletion submodules/wasm-nft/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
SubmoduleName = "wasm-nft"

// Version is the current version of the submodule
Version = "v0.1.2"
Version = "v0.1.3"
)

// store prefixes
Expand Down

0 comments on commit 6236f4a

Please sign in to comment.