This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquery.go
324 lines (256 loc) · 7.57 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"context"
"strings"
"time"
"cosmossdk.io/store/rootmulti"
abci "github.com/cometbft/cometbft/abci/types"
client2 "github.com/cometbft/cometbft/rpc/client"
sdktypes "github.com/cosmos/cosmos-sdk/types"
legacyerrors "github.com/cosmos/cosmos-sdk/types/errors"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const paginationDelay = 10 * time.Millisecond
func (c *Client) QueryBalances(ctx context.Context, addr string) (*banktypes.QueryAllBalancesResponse, error) {
qc := banktypes.NewQueryClient(c)
req := &banktypes.QueryAllBalancesRequest{
Address: addr,
Pagination: defaultPageRequest(),
ResolveDenom: true,
}
res, err := qc.AllBalances(ctx, req, nil)
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) QueryBalance(ctx context.Context, addr, denom string) (sdktypes.Coin, error) {
qc := banktypes.NewQueryClient(c)
req := &banktypes.QueryBalanceRequest{
Address: addr,
Denom: denom,
}
res, err := qc.Balance(ctx, req, nil)
if err != nil {
return sdktypes.Coin{}, err
}
return *res.Balance, nil
}
func (c *Client) QueryBankTotalSupply(ctx context.Context, denom string) (sdktypes.Coin, error) {
var (
qc = banktypes.NewQueryClient(c)
req = &banktypes.QuerySupplyOfRequest{Denom: denom}
)
res, err := qc.SupplyOf(ctx, req, nil)
if err != nil {
return sdktypes.Coin{}, err
}
return res.Amount, nil
}
func (c *Client) QueryEscrowAddress(ctx context.Context, portID, channelID string) (string, error) {
qc := transfertypes.NewQueryClient(c)
req := &transfertypes.QueryEscrowAddressRequest{
PortId: portID,
ChannelId: channelID,
}
res, err := qc.EscrowAddress(ctx, req, nil)
if err != nil {
return "", err
}
return res.EscrowAddress, nil
}
func (c *Client) QueryTotalEscrowForDenom(ctx context.Context, denom string) (sdktypes.Coin, error) {
qc := transfertypes.NewQueryClient(c)
req := &transfertypes.QueryTotalEscrowForDenomRequest{
Denom: denom,
}
res, err := qc.TotalEscrowForDenom(ctx, req)
if err != nil {
return sdktypes.Coin{}, err
}
return res.Amount, nil
}
func (c *Client) QueryEscrowAmount(ctx context.Context, denom string) (sdktypes.Coin, error) {
var (
qc = transfertypes.NewQueryClient(c)
req = &transfertypes.QueryTotalEscrowForDenomRequest{Denom: denom}
)
res, err := qc.TotalEscrowForDenom(ctx, req, nil)
if err != nil {
return sdktypes.Coin{}, err
}
return res.Amount, nil
}
func (c *Client) QueryDenomHash(ctx context.Context, denomTrace string) (string, error) {
qc := transfertypes.NewQueryClient(c)
req := &transfertypes.QueryDenomHashRequest{
Trace: denomTrace,
}
res, err := qc.DenomHash(ctx, req, nil)
if err != nil {
return "", err
}
return res.Hash, nil
}
func (c *Client) QueryDenomTrace(ctx context.Context, hash string) (*transfertypes.DenomTrace, error) {
var (
qc = transfertypes.NewQueryClient(c)
req = &transfertypes.QueryDenomTraceRequest{Hash: hash}
)
res, err := qc.DenomTrace(ctx, req, nil)
if err != nil {
return nil, err
}
return res.DenomTrace, nil
}
func (c *Client) QueryDenomTraces(ctx context.Context, offset, limit uint64, height int64) ([]transfertypes.DenomTrace, error) {
var (
qc = transfertypes.NewQueryClient(c)
p = defaultPageRequest()
transfers []transfertypes.DenomTrace
)
for {
res, err := qc.DenomTraces(ctx,
&transfertypes.QueryDenomTracesRequest{
Pagination: p,
},
)
if err != nil || res == nil {
return nil, err
}
transfers = append(transfers, res.DenomTraces...)
next := res.GetPagination().GetNextKey()
if len(next) == 0 {
break
}
time.Sleep(paginationDelay)
p.Key = next
}
return transfers, nil
}
func (c *Client) QueryChannelClientState(
portID string,
channelID string,
) (*chantypes.QueryChannelClientStateResponse, error) {
qc := chantypes.NewQueryClient(c)
req := &types.QueryChannelClientStateRequest{
PortId: portID,
ChannelId: channelID,
}
res, err := qc.ChannelClientState(context.Background(), req)
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) QueryChannel(ctx context.Context, channelID string) (*chantypes.IdentifiedChannel, error) {
qc := chantypes.NewQueryClient(c)
const portID = "transfer"
req := &chantypes.QueryChannelRequest{
PortId: portID,
ChannelId: channelID,
}
resp, err := qc.Channel(ctx, req, nil)
if err != nil {
return nil, err
}
ch := chantypes.NewIdentifiedChannel(portID, channelID, *resp.Channel)
return &ch, nil
}
// QueryChannels returns all the channels that are registered on a chain.
func (c *Client) QueryChannels(ctx context.Context) ([]*chantypes.IdentifiedChannel, error) {
p := defaultPageRequest()
var chans []*chantypes.IdentifiedChannel
for {
res, next, err := c.QueryChannelsPaginated(ctx, p)
if err != nil {
return nil, err
}
chans = append(chans, res...)
if len(next) == 0 {
break
}
time.Sleep(paginationDelay)
p.Key = next
}
return chans, nil
}
// QueryChannelsPaginated returns all the channels for a particular paginated request that are registered on a chain.
func (c *Client) QueryChannelsPaginated(
ctx context.Context,
pageRequest *querytypes.PageRequest,
) ([]*chantypes.IdentifiedChannel, []byte, error) {
qc := chantypes.NewQueryClient(c)
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
res, err := qc.Channels(ctx, &chantypes.QueryChannelsRequest{
Pagination: pageRequest,
})
if err != nil {
return nil, nil, err
}
next := res.GetPagination().GetNextKey()
return res.Channels, next, nil
}
// QueryABCI performs an ABCI query and returns the appropriate response and error sdk error code.
func (c *Client) QueryABCI(ctx context.Context, req abci.RequestQuery) (abci.ResponseQuery, error) {
opts := client2.ABCIQueryOptions{
Height: req.Height,
Prove: req.Prove,
}
result, err := c.RPCClient.ABCIQueryWithOptions(ctx, req.Path, req.Data, opts)
if err != nil {
return abci.ResponseQuery{}, err
}
if !result.Response.IsOK() {
return abci.ResponseQuery{}, sdkErrorToGRPCError(result.Response)
}
// data from trusted node or subspace query doesn't need verification
if !opts.Prove || !isQueryStoreWithProof(req.Path) {
return result.Response, nil
}
return result.Response, nil
}
func sdkErrorToGRPCError(resp abci.ResponseQuery) error {
switch resp.Code {
case legacyerrors.ErrInvalidRequest.ABCICode():
return status.Error(codes.InvalidArgument, resp.Log)
case legacyerrors.ErrUnauthorized.ABCICode():
return status.Error(codes.Unauthenticated, resp.Log)
case legacyerrors.ErrKeyNotFound.ABCICode():
return status.Error(codes.NotFound, resp.Log)
default:
return status.Error(codes.Unknown, resp.Log)
}
}
// isQueryStoreWithProof expects a format like /<queryType>/<storeName>/<subpath>
// queryType must be "store" and subpath must be "key" to require a proof.
func isQueryStoreWithProof(path string) bool {
if !strings.HasPrefix(path, "/") {
return false
}
paths := strings.SplitN(path[1:], "/", 3)
switch {
case len(paths) != 3:
return false
case paths[0] != "store":
return false
case rootmulti.RequireProof("/" + paths[2]):
return true
}
return false
}
func defaultPageRequest() *querytypes.PageRequest {
return &querytypes.PageRequest{
Key: []byte(""),
Offset: 0,
Limit: 1000,
CountTotal: false,
}
}