-
Notifications
You must be signed in to change notification settings - Fork 99
/
queries.go
241 lines (205 loc) · 6.09 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
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"`
Staking *StakingQuery `json:"staking,omitempty"`
Wasm *WasmQuery `json:"wasm,omitempty"`
}
type BankQuery struct {
Balance *BalanceQuery `json:"balance,omitempty"`
AllBalances *AllBalancesQuery `json:"all_balances,omitempty"`
}
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"`
}
type StakingQuery struct {
Validators *ValidatorsQuery `json:"validators,omitempty"`
AllDelegations *AllDelegationsQuery `json:"all_delegations,omitempty"`
Delegation *DelegationQuery `json:"delegation,omitempty"`
BondedDenom *struct{} `json:"bonded_denom,omitempty"`
}
type ValidatorsQuery struct{}
// ValidatorsResponse is the expected response to ValidatorsQuery
type ValidatorsResponse struct {
Validators Validators `json:"validators"`
}
// TODO: 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 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"`
}
type WasmQuery struct {
Smart *SmartQuery `json:"smart,omitempty"`
Raw *RawQuery `json:"raw,omitempty"`
}
// SmartQuery respone is raw bytes ([]byte)
type SmartQuery struct {
ContractAddr string `json:"contract_addr"`
Msg []byte `json:"msg"`
}
// RawQuery response is raw bytes ([]byte)
type RawQuery struct {
ContractAddr string `json:"contract_addr"`
Key []byte `json:"key"`
}