Skip to content

Commit

Permalink
Fix ibc collection nft query (#125)
Browse files Browse the repository at this point in the history
* add ibc denom queries

* update go.mod

* fix lint
  • Loading branch information
harish551 authored Jan 4, 2024
1 parent 9c492c0 commit 6aa8775
Show file tree
Hide file tree
Showing 4 changed files with 1,410 additions and 80 deletions.
25 changes: 25 additions & 0 deletions proto/OmniFlix/onft/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ service Query {
rpc ONFT(QueryONFTRequest) returns (QueryONFTResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/denoms/{denom_id}/onfts/{id}";
}
rpc IBCDenomONFT(QueryIBCDenomONFTRequest) returns (QueryONFTResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/denoms/ibc/{hash}/onfts/{id}";
}
rpc OwnerONFTs(QueryOwnerONFTsRequest) returns (QueryOwnerONFTsResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/onfts/{denom_id}/{owner}";
}
rpc OwnerIBCDenomONFTs(QueryOwnerIBCDenomONFTsRequest) returns (QueryOwnerONFTsResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/onfts/ibc/{hash}/{owner}";
}
rpc Supply(QuerySupplyRequest) returns (QuerySupplyResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/denoms/{denom_id}/supply";
}
rpc IBCDenomSupply(QueryIBCDenomSupplyRequest) returns (QuerySupplyResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/denoms/ibc/{hash}/supply";
}
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/omniflix/onft/v1beta1/params";
}
Expand Down Expand Up @@ -90,6 +99,11 @@ message QueryONFTResponse {
ONFT onft = 1 [(gogoproto.customname) = "ONFT"];
}

message QueryIBCDenomONFTRequest {
string hash = 1 [(gogoproto.moretags) = "yaml:\"hash\""];
string id = 2;
}


message QueryOwnerONFTsRequest {
string denom_id = 1 [(gogoproto.moretags) = "yaml:\"denom_id\""];
Expand All @@ -102,6 +116,12 @@ message QueryOwnerONFTsResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryOwnerIBCDenomONFTsRequest {
string hash = 1 [(gogoproto.moretags) = "yaml:\"hash\""];
string owner = 2;
cosmos.base.query.v1beta1.PageRequest pagination = 3;
}

message QuerySupplyRequest {
string denom_id = 1 [(gogoproto.moretags) = "yaml:\"denom_id\""];
string owner = 2;
Expand All @@ -111,6 +131,11 @@ message QuerySupplyResponse {
uint64 amount = 1;
}

message QueryIBCDenomSupplyRequest {
string hash = 1 [(gogoproto.moretags) = "yaml:\"hash\""];
string owner = 2;
}

message OwnerONFTCollection {
Denom denom = 1 [(gogoproto.nullable) = false];
repeated ONFT onfts = 2 [(gogoproto.nullable) = false];
Expand Down
88 changes: 86 additions & 2 deletions x/onft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

var _ types.QueryServer = Keeper{}

const IbcDenomPrefix = "ibc/"

func (k Keeper) Supply(c context.Context, request *types.QuerySupplyRequest) (*types.QuerySupplyResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

Expand All @@ -35,6 +37,25 @@ func (k Keeper) Supply(c context.Context, request *types.QuerySupplyRequest) (*t
}, nil
}

func (k Keeper) IBCDenomSupply(c context.Context, request *types.QueryIBCDenomSupplyRequest) (*types.QuerySupplyResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

var supply uint64
switch {
case len(request.Owner) == 0 && len(request.Hash) > 0:
supply = k.GetTotalSupply(ctx, IbcDenomPrefix+request.Hash)
default:
owner, err := sdk.AccAddressFromBech32(request.Owner)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid owner address %s", request.Owner)
}
supply = k.GetBalance(ctx, IbcDenomPrefix+request.Hash, owner)
}
return &types.QuerySupplyResponse{
Amount: supply,
}, nil
}

func (k Keeper) Collection(c context.Context, request *types.QueryCollectionRequest) (*types.QueryCollectionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
denom, err := k.GetDenomInfo(ctx, request.DenomId)
Expand Down Expand Up @@ -96,7 +117,7 @@ func (k Keeper) Collection(c context.Context, request *types.QueryCollectionRequ
func (k Keeper) IBCCollection(c context.Context, request *types.QueryIBCCollectionRequest) (*types.QueryCollectionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

denom, err := k.GetDenomInfo(ctx, "ibc/"+request.Hash)
denom, err := k.GetDenomInfo(ctx, IbcDenomPrefix+request.Hash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,7 +185,7 @@ func (k Keeper) Denom(c context.Context, request *types.QueryDenomRequest) (*typ
func (k Keeper) IBCDenom(c context.Context, request *types.QueryIBCDenomRequest) (*types.QueryDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

denomObject, err := k.GetDenomInfo(ctx, "ibc/"+request.Hash)
denomObject, err := k.GetDenomInfo(ctx, IbcDenomPrefix+request.Hash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -230,6 +251,25 @@ func (k Keeper) ONFT(c context.Context, request *types.QueryONFTRequest) (*types
}, nil
}

func (k Keeper) IBCDenomONFT(c context.Context, request *types.QueryIBCDenomONFTRequest) (*types.QueryONFTResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
denomId := IbcDenomPrefix + request.Hash

onft, err := k.GetONFT(ctx, denomId, request.Id)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrUnknownONFT, "invalid ONFT %s from collection %s", request.Id, denomId)
}

oNFT, ok := onft.(types.ONFT)
if !ok {
return nil, errorsmod.Wrapf(types.ErrUnknownONFT, "invalid type ONFT %s from collection %s", request.Id, denomId)
}

return &types.QueryONFTResponse{
ONFT: &oNFT,
}, nil
}

func (k Keeper) OwnerONFTs(c context.Context, request *types.QueryOwnerONFTsRequest) (*types.QueryOwnerONFTsResponse, error) {
r := &nft.QueryNFTsRequest{
ClassId: request.DenomId,
Expand Down Expand Up @@ -271,6 +311,50 @@ func (k Keeper) OwnerONFTs(c context.Context, request *types.QueryOwnerONFTsRequ
return response, nil
}

func (k Keeper) OwnerIBCDenomONFTs(
c context.Context,
request *types.QueryOwnerIBCDenomONFTsRequest,
) (*types.QueryOwnerONFTsResponse, error) {
r := &nft.QueryNFTsRequest{
ClassId: IbcDenomPrefix + request.Hash,
Owner: request.Owner,
Pagination: shapePageRequest(request.Pagination),
}

result, err := k.nk.NFTs(c, r)
if err != nil {
return nil, err
}

denomMap := make(map[string][]string)
var denoms []string
for _, _nft := range result.Nfts {
if denomMap[_nft.ClassId] == nil {
denomMap[_nft.ClassId] = []string{}
denoms = append(denoms, _nft.ClassId)
}
denomMap[_nft.ClassId] = append(denomMap[_nft.ClassId], _nft.Id)
}

var idc []types.IDCollection
for _, denomID := range denoms {
idc = append(idc, types.IDCollection{
DenomId: denomID,
OnftIds: denomMap[denomID],
})
}

response := &types.QueryOwnerONFTsResponse{
Owner: &types.Owner{
Address: request.Owner,
IDCollections: idc,
},
Pagination: result.Pagination,
}

return response, nil
}

// Params queries params of oNFT module
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
Expand Down
Loading

0 comments on commit 6aa8775

Please sign in to comment.