-
Notifications
You must be signed in to change notification settings - Fork 46
/
querier.go
64 lines (53 loc) · 1.83 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/desmos-labs/desmos/v3/x/supply/types"
)
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryTotalSupply:
return queryTotalSupply(ctx, req, k)
case types.QueryCirculatingSupply:
return queryCirculatingSupply(ctx, req, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
// queryTotalSupply queries the total supply of the given params.Denom
func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
var request types.QueryTotalRequest
err := k.cdc.Unmarshal(req.Data, &request)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
res, err := k.Total(sdk.WrapSDKContext(ctx), &request)
if err != nil {
return nil, err
}
supply, err := res.TotalSupply.Marshal()
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return supply, nil
}
// queryCirculatingSupply queries the current circulating supply of the given params.Denom
func queryCirculatingSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
var request types.QueryCirculatingRequest
err := k.cdc.Unmarshal(req.Data, &request)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
res, err := k.Circulating(sdk.WrapSDKContext(ctx), &request)
if err != nil {
return nil, err
}
supply, err := res.CirculatingSupply.Marshal()
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return supply, nil
}