-
Notifications
You must be signed in to change notification settings - Fork 49
/
querier.go
147 lines (135 loc) · 4.57 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
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
package keeper
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/certikfoundation/shentu/v2/x/oracle/types"
)
const (
QueryOperator = "operator"
QueryOperators = "operators"
QueryWithdraws = "withdraws"
QueryTask = "task"
QueryResponse = "response"
)
// NewQuerier is the module level router for state queries.
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case QueryOperator:
return queryOperator(ctx, path[1:], keeper, legacyQuerierCdc)
case QueryOperators:
return queryOperators(ctx, path[1:], keeper, legacyQuerierCdc)
case QueryWithdraws:
return queryWithdraws(ctx, path[1:], keeper, legacyQuerierCdc)
case QueryTask:
return queryTask(ctx, path[1:], req, keeper, legacyQuerierCdc)
case QueryResponse:
return queryResponse(ctx, path[1:], req, keeper, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
// ValidatePathLength validates the length of a given path.
func validatePathLength(path []string, length int) error {
if len(path) != length {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Expecting %d args. Found %d.", length, len(path))
}
return nil
}
// queryOperator returns information of an operator.
func queryOperator(ctx sdk.Context, path []string, k Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
if err := validatePathLength(path, 1); err != nil {
return nil, err
}
address, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, err
}
operator, err := k.GetOperator(ctx, address)
if err != nil {
return nil, err
}
res, err = codec.MarshalJSONIndent(legacyQuerierCdc, operator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryOperators returns information of all operators.
func queryOperators(ctx sdk.Context, path []string, k Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
}
res, err = codec.MarshalJSONIndent(legacyQuerierCdc, k.GetAllOperators(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryWithdraws returns information of all withdrawals.
func queryWithdraws(ctx sdk.Context, path []string, k Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
}
res, err = codec.MarshalJSONIndent(legacyQuerierCdc, k.GetAllWithdraws(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryResponse returns information of a response.
func queryResponse(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
}
var params types.QueryResponseParams
err = legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
task, err := k.GetTask(ctx, params.Contract, params.Function)
if err != nil {
return nil, err
}
for i := 0; i < len(task.Responses); i++ {
operatorAddr, err := sdk.AccAddressFromBech32(task.Responses[i].Operator)
if err != nil {
panic(err)
}
if operatorAddr.Equals(params.Operator) {
res, err = codec.MarshalJSONIndent(legacyQuerierCdc, task.Responses[i])
if err != nil {
return nil, err
}
break
}
}
if res == nil {
return nil, fmt.Errorf("there is no response from this operator")
}
return res, err
}
// queryTask returns information of a task.
func queryTask(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
}
var params types.QueryTaskParams
err = legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
task, err := k.GetTask(ctx, params.Contract, params.Function)
if err != nil {
return nil, err
}
res, err = codec.MarshalJSONIndent(legacyQuerierCdc, task)
if err != nil {
return nil, err
}
return res, nil
}