-
Notifications
You must be signed in to change notification settings - Fork 3
/
grpc_query.go
113 lines (97 loc) · 3.36 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
package keeper
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
gogotypes "github.com/gogo/protobuf/types"
"github.com/bitsongofficial/chainmodules/x/fantoken/types"
)
var _ types.QueryServer = Keeper{}
func (k Keeper) FanToken(c context.Context, req *types.QueryFanTokenRequest) (*types.QueryFanTokenResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
token, err := k.GetFanToken(ctx, req.Denom)
if err != nil {
return nil, status.Errorf(codes.NotFound, "token %s not found", req.Denom)
}
return &types.QueryFanTokenResponse{Token: &types.FanToken{
Name: token.GetName(),
MaxSupply: token.GetMaxSupply(),
Mintable: token.GetMintable(),
Owner: token.GetOwner().String(),
MetaData: token.GetMetaData(),
}}, nil
}
func (k Keeper) FanTokens(c context.Context, req *types.QueryFanTokensRequest) (*types.QueryFanTokensResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var owner sdk.AccAddress
var err error
if len(req.Owner) > 0 {
owner, err = sdk.AccAddressFromBech32(req.Owner)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid owner address (%s)", err))
}
}
var tokens []types.FanTokenI
var pageRes *query.PageResponse
store := ctx.KVStore(k.storeKey)
if owner == nil {
tokenStore := prefix.NewStore(store, types.PrefixFanTokenForSymbol)
pageRes, err = query.Paginate(tokenStore, req.Pagination, func(key []byte, value []byte) error {
var token types.FanToken
k.cdc.MustUnmarshalBinaryBare(value, &token)
tokens = append(tokens, &token)
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
} else {
tokenStore := prefix.NewStore(store, types.KeyFanTokens(owner, ""))
pageRes, err = query.Paginate(tokenStore, req.Pagination, func(key []byte, value []byte) error {
var symbol gogotypes.StringValue
k.cdc.MustUnmarshalBinaryBare(value, &symbol)
token, err := k.GetFanToken(ctx, symbol.Value)
if err == nil {
tokens = append(tokens, token)
}
return nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
}
var result []*types.FanToken
for _, token := range tokens {
result = append(result, &types.FanToken{
Name: token.GetName(),
MaxSupply: token.GetMaxSupply(),
Mintable: token.GetMintable(),
Owner: token.GetOwner().String(),
MetaData: token.GetMetaData(),
})
}
return &types.QueryFanTokensResponse{Tokens: result, Pagination: pageRes}, nil
}
// Params return the all the parameter in tonken module
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParamSet(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// TotalBurn return the all burn coin
func (k Keeper) TotalBurn(c context.Context, req *types.QueryTotalBurnRequest) (*types.QueryTotalBurnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryTotalBurnResponse{
BurnedCoins: k.GetAllBurnCoin(ctx),
}, nil
}