-
Notifications
You must be signed in to change notification settings - Fork 38
/
query.go
107 lines (88 loc) · 2.66 KB
/
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
package common
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
hub "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/node/types"
plan "github.com/sentinel-official/hub/x/plan/types"
)
func QueryNode(ctx context.CLIContext, address hub.NodeAddress) (*types.Node, error) {
params := types.NewQueryNodeParams(address)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}
path := fmt.Sprintf("custom/%s/%s/%s", types.StoreKey, types.QuerierRoute, types.QueryNode)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no node found")
}
var node types.Node
if err := ctx.Codec.UnmarshalJSON(res, &node); err != nil {
return nil, err
}
return &node, nil
}
func QueryNodes(ctx context.CLIContext, status hub.Status, skip, limit int) (types.Nodes, error) {
params := types.NewQueryNodesParams(status, skip, limit)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}
path := fmt.Sprintf("custom/%s/%s/%s", types.StoreKey, types.QuerierRoute, types.QueryNodes)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no nodes found")
}
var nodes types.Nodes
if err := ctx.Codec.UnmarshalJSON(res, &nodes); err != nil {
return nil, err
}
return nodes, nil
}
func QueryNodesForProvider(ctx context.CLIContext, address hub.ProvAddress, status hub.Status, skip, limit int) (types.Nodes, error) {
params := types.NewQueryNodesForProviderParams(address, status, skip, limit)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}
path := fmt.Sprintf("custom/%s/%s/%s", types.StoreKey, types.QuerierRoute, types.QueryNodesForProvider)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no nodes found")
}
var nodes types.Nodes
if err := ctx.Codec.UnmarshalJSON(res, &nodes); err != nil {
return nil, err
}
return nodes, nil
}
func QueryNodesForPlan(ctx context.CLIContext, id uint64, skip, limit int) (types.Nodes, error) {
params := plan.NewQueryNodesForPlanParams(id, skip, limit)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}
path := fmt.Sprintf("custom/%s/%s/%s", plan.StoreKey, plan.QuerierRoute, plan.QueryNodesForPlan)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no nodes found")
}
var nodes types.Nodes
if err := ctx.Codec.UnmarshalJSON(res, &nodes); err != nil {
return nil, err
}
return nodes, nil
}