-
Notifications
You must be signed in to change notification settings - Fork 14
/
queries.go
384 lines (328 loc) · 10.7 KB
/
queries.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package types
import (
"encoding/json"
)
//-------- Queries --------
// QueryResponse is the Go counterpart of `ContractResult<Binary>`.
// The JSON annotations are used for deserializing directly. There is a custom serializer below.
type QueryResponse queryResponseImpl
type queryResponseImpl struct {
Ok []byte `json:"ok,omitempty"`
Err string `json:"error,omitempty"`
}
// A custom serializer that allows us to map QueryResponse instances to the Rust
// enum `ContractResult<Binary>`
func (q QueryResponse) MarshalJSON() ([]byte, error) {
// In case both Ok and Err are empty, this is interpreted and seralized
// as an Ok case with no data because errors must not be empty.
if len(q.Ok) == 0 && len(q.Err) == 0 {
return []byte(`{"ok":""}`), nil
}
return json.Marshal(queryResponseImpl(q))
}
//-------- Querier -----------
type Querier interface {
Query(request QueryRequest, gasLimit uint64) ([]byte, error)
GasConsumed() uint64
}
// this is a thin wrapper around the desired Go API to give us types closer to Rust FFI
func RustQuery(querier Querier, binRequest []byte, gasLimit uint64) QuerierResult {
var request QueryRequest
err := json.Unmarshal(binRequest, &request)
if err != nil {
return QuerierResult{
Err: &SystemError{
InvalidRequest: &InvalidRequest{
Err: err.Error(),
Request: binRequest,
},
},
}
}
bz, err := querier.Query(request, gasLimit)
return ToQuerierResult(bz, err)
}
// This is a 2-level result
type QuerierResult struct {
Ok *QueryResponse `json:"ok,omitempty"`
Err *SystemError `json:"error,omitempty"`
}
func ToQuerierResult(response []byte, err error) QuerierResult {
if err == nil {
return QuerierResult{
Ok: &QueryResponse{
Ok: response,
},
}
}
syserr := ToSystemError(err)
if syserr != nil {
return QuerierResult{
Err: syserr,
}
}
return QuerierResult{
Ok: &QueryResponse{
Err: err.Error(),
},
}
}
// QueryRequest is an rust enum and only (exactly) one of the fields should be set
// Should we do a cleaner approach in Go? (type/data?)
type QueryRequest struct {
Bank *BankQuery `json:"bank,omitempty"`
Custom json.RawMessage `json:"custom,omitempty"`
IBC *IBCQuery `json:"ibc,omitempty"`
Staking *StakingQuery `json:"staking,omitempty"`
Stargate *StargateQuery `json:"stargate,omitempty"`
Wasm *WasmQuery `json:"wasm,omitempty"`
}
type BankQuery struct {
Supply *SupplyQuery `json:"supply,omitempty"`
Balance *BalanceQuery `json:"balance,omitempty"`
AllBalances *AllBalancesQuery `json:"all_balances,omitempty"`
}
type SupplyQuery struct {
Denom string `json:"denom"`
}
// SupplyResponse is the expected response to SupplyQuery
type SupplyResponse struct {
Amount Coin `json:"amount"`
}
type BalanceQuery struct {
Address string `json:"address"`
Denom string `json:"denom"`
}
// BalanceResponse is the expected response to BalanceQuery
type BalanceResponse struct {
Amount Coin `json:"amount"`
}
type AllBalancesQuery struct {
Address string `json:"address"`
}
// AllBalancesResponse is the expected response to AllBalancesQuery
type AllBalancesResponse struct {
Amount Coins `json:"amount"`
}
// IBCQuery defines a query request from the contract into the chain.
// This is the counterpart of `IbcQuery` in https://github.com/Finschia/cosmwasm/blob/main/packages/std/src/ibc.rs .
type IBCQuery struct {
PortID *PortIDQuery `json:"port_id,omitempty"`
ListChannels *ListChannelsQuery `json:"list_channels,omitempty"`
Channel *ChannelQuery `json:"channel,omitempty"`
}
type PortIDQuery struct{}
type PortIDResponse struct {
PortID string `json:"port_id"`
}
// ListChannelsQuery is an IBCQuery that lists all channels that are bound to a given port.
// If `PortID` is unset, this list all channels bound to the contract's port.
// Returns a `ListChannelsResponse`.
// This is the counterpart of `IbcQuery::ListChannels` in https://github.com/Finschia/cosmwasm/blob/main/packages/std/src/ibc.rs .
type ListChannelsQuery struct {
// optional argument
PortID string `json:"port_id,omitempty"`
}
type ListChannelsResponse struct {
Channels IBCChannels `json:"channels"`
}
// IBCChannels must JSON encode empty array as [] (not null) for consistency with Rust parser
type IBCChannels []IBCChannel
// MarshalJSON ensures that we get [] for empty arrays
func (e IBCChannels) MarshalJSON() ([]byte, error) {
if len(e) == 0 {
return []byte("[]"), nil
}
var raw []IBCChannel = e
return json.Marshal(raw)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (e *IBCChannels) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var raw []IBCChannel
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*e = raw
return nil
}
// IBCEndpoints must JSON encode empty array as [] (not null) for consistency with Rust parser
type IBCEndpoints []IBCEndpoint
// MarshalJSON ensures that we get [] for empty arrays
func (e IBCEndpoints) MarshalJSON() ([]byte, error) {
if len(e) == 0 {
return []byte("[]"), nil
}
var raw []IBCEndpoint = e
return json.Marshal(raw)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (e *IBCEndpoints) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var raw []IBCEndpoint
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*e = raw
return nil
}
type ChannelQuery struct {
// optional argument
PortID string `json:"port_id,omitempty"`
ChannelID string `json:"channel_id"`
}
type ChannelResponse struct {
// may be empty if there is no matching channel
Channel *IBCChannel `json:"channel,omitempty"`
}
type StakingQuery struct {
AllValidators *AllValidatorsQuery `json:"all_validators,omitempty"`
Validator *ValidatorQuery `json:"validator,omitempty"`
AllDelegations *AllDelegationsQuery `json:"all_delegations,omitempty"`
Delegation *DelegationQuery `json:"delegation,omitempty"`
BondedDenom *struct{} `json:"bonded_denom,omitempty"`
}
type AllValidatorsQuery struct{}
// AllValidatorsResponse is the expected response to AllValidatorsQuery
type AllValidatorsResponse struct {
Validators Validators `json:"validators"`
}
// Validators must JSON encode empty array as []
type Validators []Validator
// MarshalJSON ensures that we get [] for empty arrays
func (v Validators) MarshalJSON() ([]byte, error) {
if len(v) == 0 {
return []byte("[]"), nil
}
var raw []Validator = v
return json.Marshal(raw)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (v *Validators) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var raw []Validator
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*v = raw
return nil
}
type ValidatorQuery struct {
/// Address is the validator's address (e.g. cosmosvaloper1...)
Address string `json:"address"`
}
// ValidatorResponse is the expected response to ValidatorQuery
type ValidatorResponse struct {
Validator *Validator `json:"validator"` // serializes to `null` when unset which matches Rust's Option::None serialization
}
type Validator struct {
Address string `json:"address"`
// decimal string, eg "0.02"
Commission string `json:"commission"`
// decimal string, eg "0.02"
MaxCommission string `json:"max_commission"`
// decimal string, eg "0.02"
MaxChangeRate string `json:"max_change_rate"`
}
type AllDelegationsQuery struct {
Delegator string `json:"delegator"`
}
type DelegationQuery struct {
Delegator string `json:"delegator"`
Validator string `json:"validator"`
}
// AllDelegationsResponse is the expected response to AllDelegationsQuery
type AllDelegationsResponse struct {
Delegations Delegations `json:"delegations"`
}
type Delegations []Delegation
// MarshalJSON ensures that we get [] for empty arrays
func (d Delegations) MarshalJSON() ([]byte, error) {
if len(d) == 0 {
return []byte("[]"), nil
}
var raw []Delegation = d
return json.Marshal(raw)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (d *Delegations) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var raw []Delegation
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*d = raw
return nil
}
type Delegation struct {
Delegator string `json:"delegator"`
Validator string `json:"validator"`
Amount Coin `json:"amount"`
}
// DelegationResponse is the expected response to DelegationsQuery
type DelegationResponse struct {
Delegation *FullDelegation `json:"delegation,omitempty"`
}
type FullDelegation struct {
Delegator string `json:"delegator"`
Validator string `json:"validator"`
Amount Coin `json:"amount"`
AccumulatedRewards Coins `json:"accumulated_rewards"`
CanRedelegate Coin `json:"can_redelegate"`
}
type BondedDenomResponse struct {
Denom string `json:"denom"`
}
// StargateQuery is encoded the same way as abci_query, with path and protobuf encoded request data.
// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md).
// The response is protobuf encoded data directly without a JSON response wrapper.
// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
type StargateQuery struct {
// this is the fully qualified service path used for routing,
// eg. custom/lbm_sdk.x.bank.v1.Query/QueryBalance
Path string `json:"path"`
// this is the expected protobuf message type (not any), binary encoded
Data []byte `json:"data"`
}
type WasmQuery struct {
Smart *SmartQuery `json:"smart,omitempty"`
Raw *RawQuery `json:"raw,omitempty"`
ContractInfo *ContractInfoQuery `json:"contract_info,omitempty"`
}
// SmartQuery respone is raw bytes ([]byte)
type SmartQuery struct {
// Bech32 encoded sdk.AccAddress of the contract
ContractAddr string `json:"contract_addr"`
Msg []byte `json:"msg"`
}
// RawQuery response is raw bytes ([]byte)
type RawQuery struct {
// Bech32 encoded sdk.AccAddress of the contract
ContractAddr string `json:"contract_addr"`
Key []byte `json:"key"`
}
type ContractInfoQuery struct {
// Bech32 encoded sdk.AccAddress of the contract
ContractAddr string `json:"contract_addr"`
}
type ContractInfoResponse struct {
CodeID uint64 `json:"code_id"`
Creator string `json:"creator"`
// Set to the admin who can migrate contract, if any
Admin string `json:"admin,omitempty"`
Pinned bool `json:"pinned"`
// Set if the contract is IBC enabled
IBCPort string `json:"ibc_port,omitempty"`
}