-
Notifications
You must be signed in to change notification settings - Fork 3
/
grpc_query.go
153 lines (125 loc) · 4.77 KB
/
grpc_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2016-2021 Shanghai Bianjie AI Technology Inc. (licensed under the Apache License, Version 2.0)
// Modifications Copyright (c) 2021, CRO Protocol Labs ("Crypto.org") (licensed under the Apache License, Version 2.0)
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/bitsongofficial/chainmodules/x/nft/types"
)
var _ types.QueryServer = Keeper{}
func (k Keeper) Supply(c context.Context, request *types.QuerySupplyRequest) (*types.QuerySupplyResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var supply uint64
switch {
case len(request.Owner) == 0 && len(request.DenomId) > 0:
supply = k.GetTotalSupply(ctx, request.DenomId)
default:
owner, err := sdk.AccAddressFromBech32(request.Owner)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid owner address %s", request.Owner)
}
supply = k.GetTotalSupplyOfOwner(ctx, request.DenomId, owner)
}
return &types.QuerySupplyResponse{Amount: supply}, nil
}
func (k Keeper) Owner(c context.Context, request *types.QueryOwnerRequest) (*types.QueryOwnerResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ownerAddress, err := sdk.AccAddressFromBech32(request.Owner)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid owner address %s", request.Owner)
}
owner := types.Owner{
Address: ownerAddress.String(),
IDCollections: types.IDCollections{},
}
idsMap := make(map[string][]string)
store := ctx.KVStore(k.storeKey)
nftStore := prefix.NewStore(store, types.KeyOwner(ownerAddress, request.DenomId, ""))
pageRes, err := query.Paginate(nftStore, request.Pagination, func(key []byte, value []byte) error {
denomID := request.DenomId
tokenID := string(key)
if len(request.DenomId) == 0 {
denomID, tokenID, err = types.SplitKeyDenom(key)
if err != nil {
return err
}
}
if ids, ok := idsMap[denomID]; ok {
idsMap[denomID] = append(ids, tokenID)
} else {
idsMap[denomID] = []string{tokenID}
owner.IDCollections = append(
owner.IDCollections,
types.IDCollection{DenomId: denomID},
)
}
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
for i := 0; i < len(owner.IDCollections); i++ {
owner.IDCollections[i].TokenIds = idsMap[owner.IDCollections[i].DenomId]
}
return &types.QueryOwnerResponse{Owner: &owner, Pagination: pageRes}, nil
}
func (k Keeper) Collection(c context.Context, request *types.QueryCollectionRequest) (*types.QueryCollectionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
collection, pageRes, err := k.GetPaginateCollection(ctx, request, request.DenomId)
if err != nil {
return nil, err
}
return &types.QueryCollectionResponse{Collection: &collection, Pagination: pageRes}, nil
}
func (k Keeper) Denom(c context.Context, request *types.QueryDenomRequest) (*types.QueryDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
denomObject, err := k.GetDenom(ctx, request.DenomId)
if err != nil {
return nil, err
}
return &types.QueryDenomResponse{Denom: &denomObject}, nil
}
func (k Keeper) DenomByName(c context.Context, request *types.QueryDenomByNameRequest) (*types.QueryDenomByNameResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
denomObject, err := k.GetDenomByName(ctx, request.DenomName)
if err != nil {
return nil, err
}
return &types.QueryDenomByNameResponse{Denom: &denomObject}, nil
}
func (k Keeper) Denoms(c context.Context, req *types.QueryDenomsRequest) (*types.QueryDenomsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var denoms []types.Denom
store := ctx.KVStore(k.storeKey)
denomStore := prefix.NewStore(store, types.KeyDenomID(""))
pageRes, err := query.Paginate(denomStore, req.Pagination, func(key []byte, value []byte) error {
var denom types.Denom
k.cdc.MustUnmarshalBinaryBare(value, &denom)
denoms = append(denoms, denom)
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
return &types.QueryDenomsResponse{
Denoms: denoms,
Pagination: pageRes,
}, nil
}
func (k Keeper) NFT(c context.Context, request *types.QueryNFTRequest) (*types.QueryNFTResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nft, err := k.GetNFT(ctx, request.DenomId, request.TokenId)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrUnknownNFT, "invalid NFT %s from collection %s", request.TokenId, request.DenomId)
}
baseNFT, ok := nft.(types.BaseNFT)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownNFT, "invalid type NFT %s from collection %s", request.TokenId, request.DenomId)
}
return &types.QueryNFTResponse{NFT: &baseNFT}, nil
}