-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
77 lines (66 loc) · 2.11 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
package keepers
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/coinexchain/cet-sdk/modules/bancorlite/internal/types"
)
const (
QueryBancorInfo = "bancor-info"
QueryParameters = "parameters"
QueryBancors = "bancor-list"
)
// creates a querier for asset REST endpoints
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryParameters:
return queryParameters(ctx, keeper)
case QueryBancorInfo:
return queryBancorInfo(ctx, req, keeper)
case QueryBancors:
return queryBancorList(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("query symbol : " + path[0])
}
}
}
type QueryBancorInfoParam struct {
Symbol string `json:"symbol"`
}
func queryBancorInfo(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var param QueryBancorInfoParam
if err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶m); err != nil {
return nil, sdk.NewError(types.CodeSpaceBancorlite, types.CodeUnMarshalFailed, "failed to parse param")
}
bi := keeper.Load(ctx, param.Symbol)
var biD BancorInfoDisplay
if bi != nil {
biD = NewBancorInfoDisplay(bi)
}
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, biD)
if err != nil {
return nil, types.ErrMarshalFailed()
}
return bz, nil
}
func queryBancorList(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
infos := k.GetAllBancorInfos(ctx)
infoList := make([]BancorInfoDisplay, len(infos))
for i, info := range infos {
infoList[i] = NewBancorInfoDisplay(info)
}
bz, err := codec.MarshalJSONIndent(k.bik.codec, infoList)
if err != nil {
return nil, types.ErrMarshalFailed()
}
return bz, nil
}
func queryParameters(ctx sdk.Context, k Keeper) ([]byte, sdk.Error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, params)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return res, nil
}