-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
121 lines (91 loc) · 3.53 KB
/
querier.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
package keeper
import (
abci "github.com/soominhyunwoo/tendermint/abci/types"
"github.com/soominhyunwoo/chain-sdk/codec"
sdk "github.com/soominhyunwoo/chain-sdk/types"
sdkerrors "github.com/soominhyunwoo/chain-sdk/types/errors"
"github.com/soominhyunwoo/chain-sdk/x/bank/types"
)
// NewQuerier returns a new sdk.Keeper instance.
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryBalance:
return queryBalance(ctx, req, k, legacyQuerierCdc)
case types.QueryAllBalances:
return queryAllBalance(ctx, req, k, legacyQuerierCdc)
case types.QueryTotalSupply:
return queryTotalSupply(ctx, req, k, legacyQuerierCdc)
case types.QuerySupplyOf:
return querySupplyOf(ctx, req, k, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryBalanceRequest
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
address, err := sdk.AccAddressFromBech32(params.Address)
if err != nil {
return nil, err
}
balance := k.GetBalance(ctx, address, params.Denom)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balance)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryAllBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryAllBalancesRequest
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
address, err := sdk.AccAddressFromBech32(params.Address)
if err != nil {
return nil, err
}
balances := k.GetAllBalances(ctx, address)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balances)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryTotalSupplyRequest
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(ctx, params.Pagination)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
supplyRes := &types.QueryTotalSupplyResponse{
Supply: totalSupply,
Pagination: pageRes,
}
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, supplyRes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QuerySupplyOfParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
amount := k.GetSupply(ctx, params.Denom)
supply := sdk.NewCoin(params.Denom, amount.Amount)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, supply)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}