-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.rs
More file actions
368 lines (325 loc) · 11.4 KB
/
Copy pathquery.rs
File metadata and controls
368 lines (325 loc) · 11.4 KB
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
use cosmwasm_std::{Binary, Coin, StdError};
use derivative::Derivative;
use std::error::Error as Err;
use std::fmt::{Display, Formatter};
use thiserror::Error;
use crate::account_id::{AccountId, AccountIdError};
use crate::api::TxResult;
use crate::tx::Tx;
// 2.0: use cosmwasm_std::Checksum
type Checksum = Binary;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Query {
/// Return the raw binary value stored under that key
Raw {
key: Vec<u8>,
},
Auth(AuthQuery),
Bank(BankQuery),
Simulate(Tx),
Wasm(WasmQuery),
}
impl From<AuthQuery> for Query {
fn from(value: AuthQuery) -> Self {
Query::Auth(value)
}
}
impl From<BankQuery> for Query {
fn from(value: BankQuery) -> Self {
Query::Bank(value)
}
}
impl From<WasmQuery> for Query {
fn from(value: WasmQuery) -> Self {
Query::Wasm(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BankQuery {
/// Returns the supply of all native tokens
/// Return value is of type TotalSupplyResponse.
TotalSupply {},
/// Return value is of type SupplyResponse.
Supply { denom: String },
/// Return value is BalanceResponse
Balance { address: AccountId, denom: String },
/// Note that this may be much more expensive than Balance and should be avoided if possible.
/// Return value is AllBalanceResponse.
AllBalances { address: AccountId },
}
impl Display for BankQuery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
BankQuery::TotalSupply { .. } => f.write_str("BankQuery::TotalSupply"),
BankQuery::Supply { .. } => f.write_str("BankQuery::Supply"),
BankQuery::Balance { .. } => f.write_str("BankQuery::Balance"),
BankQuery::AllBalances { .. } => f.write_str("BankQuery::AllBalances"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthQuery {
/// Return value is of type AccountResponse.
Account { address: AccountId },
}
impl Display for AuthQuery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
AuthQuery::Account { .. } => f.write_str("AuthQuery::Account"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryResponse<E: Err> {
Raw { key: Vec<u8>, value: Vec<u8> },
Auth(AuthQueryResponse),
Bank(BankQueryResponse),
Simulate(TxResult<E>),
Wasm(WasmQueryResponse),
}
impl<E: Err> From<AuthQueryResponse> for QueryResponse<E> {
fn from(value: AuthQueryResponse) -> Self {
QueryResponse::Auth(value)
}
}
impl<E: Err> From<BankQueryResponse> for QueryResponse<E> {
fn from(value: BankQueryResponse) -> Self {
QueryResponse::Bank(value)
}
}
impl<E: Err> From<WasmQueryResponse> for QueryResponse<E> {
fn from(value: WasmQueryResponse) -> Self {
QueryResponse::Wasm(value)
}
}
impl<E: Err> From<TxResult<E>> for QueryResponse<E> {
fn from(value: TxResult<E>) -> Self {
QueryResponse::Simulate(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthQueryResponse {
Account(AccountResponse),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccountResponse {
/// This is External Account in Ethereum terms, controlled by a public key
External {
address: AccountId,
pubkey: Option<crate::PubKey>,
sequence: u64,
},
/// No pubkey can control this, either contract or "module account"
Internal { address: AccountId },
/// Used for account abstraction, where a contract can validate what a pubkey can do
Smart {
address: AccountId,
// FIXME: any more info to add here?
contract: AccountId,
},
}
impl AccountResponse {
pub fn address(&self) -> &AccountId {
match self {
AccountResponse::External { address, .. } => address,
AccountResponse::Internal { address } => address,
AccountResponse::Smart { address, .. } => address,
}
}
/// Gets sequence if an external account, panics otherwise
pub fn external_sequence(&self) -> u64 {
match self {
AccountResponse::External { sequence, .. } => *sequence,
AccountResponse::Internal { .. } => panic!("internal account has no sequence"),
AccountResponse::Smart { .. } => panic!("smart account has no sequence"),
}
}
/// Returns true iff this is an external account
pub fn is_external(&self) -> bool {
matches!(self, AccountResponse::External { .. })
}
}
impl<E: Err> From<AccountResponse> for QueryResponse<E> {
fn from(value: AccountResponse) -> Self {
AuthQueryResponse::Account(value).into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BankQueryResponse {
TotalSupply(TotalSupplyResponse),
Supply(SupplyResponse),
Balance(BalanceResponse),
AllBalances(AllBalanceResponse),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TotalSupplyResponse {
pub amounts: Vec<Coin>,
}
impl<E: Err> From<TotalSupplyResponse> for QueryResponse<E> {
fn from(value: TotalSupplyResponse) -> Self {
BankQueryResponse::TotalSupply(value).into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SupplyResponse {
/// Always returns a Coin with the requested denom.
/// This will be of zero amount if the denom does not exist.
pub amount: Coin,
}
impl<E: Err> From<SupplyResponse> for QueryResponse<E> {
fn from(value: SupplyResponse) -> Self {
BankQueryResponse::Supply(value).into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BalanceResponse {
/// Always returns a Coin with the requested denom.
/// This may be of 0 amount if no such funds.
pub amount: Coin,
}
impl<E: Err> From<BalanceResponse> for QueryResponse<E> {
fn from(value: BalanceResponse) -> Self {
BankQueryResponse::Balance(value).into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AllBalanceResponse {
/// Returns all non-zero coins held by this account.
pub amount: Vec<Coin>,
}
impl<E: Err> From<AllBalanceResponse> for QueryResponse<E> {
fn from(value: AllBalanceResponse) -> Self {
BankQueryResponse::AllBalances(value).into()
}
}
#[derive(Derivative, Debug, Clone, PartialEq, Eq)]
pub enum WasmQuery {
/// this queries the public API of another contract at a known address (with known ABI)
/// Return value is whatever the contract returns (caller should know), wrapped in a
/// ContractResult that is JSON encoded.
Smart {
contract_addr: AccountId,
/// msg is the json-encoded QueryMsg struct
#[derivative(Debug(format_with = "layer_std::binary_to_string"))]
msg: Binary,
},
/// this queries the raw kv-store of the contract.
/// returns the raw, unparsed data stored at that key, which may be an empty vector if not present
Raw {
contract_addr: AccountId,
/// Key is the raw key used in the contracts Storage
key: Binary,
},
/// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
ContractInfo { contract_addr: AccountId },
/// Returns a [`CodeInfoResponse`] with metadata of the code
CodeInfo { code_id: u64, include_wasm: bool },
/// Returns a [`ListCodesResponse`] with metadata of the codes, starting from the given code_id
ListCodes {
from: Option<u64>,
limit: Option<u32>,
},
/// Returns a [`ContractsByCodeResponse`] with list of addresses using this code_id
ContractsByCode { code_id: u64 },
}
#[derive(Derivative, Debug, Clone, PartialEq, Eq)]
pub enum WasmQueryResponse {
Smart(#[derivative(Debug(format_with = "layer_std::binary_to_string"))] Binary),
Raw(#[derivative(Debug(format_with = "layer_std::binary_to_string"))] Binary),
ContractInfo(ContractInfoResponse),
CodeInfo(CodeInfoResponse),
ListCodes(ListCodesResponse),
ContractsByCode(ContractsByCodeResponse),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContractInfoResponse {
/// The contract address queried
pub addresss: AccountId,
/// The code id of the contract
pub code_id: u64,
/// address that instantiated this contract
pub creator: AccountId,
/// admin who can run migrations (if any)
pub admin: Option<AccountId>,
/// human-readable label for this contract (optional)
pub label: String,
/// if set, the contract is pinned to the cache, and thus uses less gas when called
pub pinned: bool,
/// set if this contract has bound an IBC port
pub ibc_port: Option<String>,
/// blockchain height when contract was first created
pub created: u64,
}
impl From<ContractInfoResponse> for cosmwasm_std::ContractInfoResponse {
fn from(value: ContractInfoResponse) -> Self {
let mut res = cosmwasm_std::ContractInfoResponse::default();
res.code_id = value.code_id;
res.creator = value.creator.to_string();
res.admin = value.admin.map(|a| a.to_string());
res.pinned = value.pinned;
res.ibc_port = value.ibc_port;
res
}
}
/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
///
/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
/// convention and naming in `instantiate2_address`.
///
/// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72
/// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeInfoResponse {
pub code_info: CodeInfo,
// The actual WASM code blob
pub data: Binary,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeInfo {
pub code_id: u64,
/// The address that initially stored the code
pub creator: AccountId,
/// The hash of the Wasm blob (aka data_hash)
pub checksum: Checksum,
/// If this code is pinned to the cache
pub pinned: bool,
// TODO: instantiate permissions... if we choose to use them
}
/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
///
/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
/// convention and naming in `instantiate2_address`.
///
/// [QueryCodesResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L215-L220
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListCodesResponse {
pub code_infos: Vec<CodeInfo>,
}
/// The essential data from wasmd's [CodeInfo]/[CodeInfoResponse].
///
/// `code_hash`/`data_hash` was renamed to `checksum` to follow the CosmWasm
/// convention and naming in `instantiate2_address`.
///
/// [QueryContractsByCodeResponse](https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L121-L129)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContractsByCodeResponse {
/// All contract addresses currently using this code_id
pub contracts: Vec<AccountId>,
}
#[derive(Error, Debug, PartialEq)]
pub enum QueryError {
#[error("{0}")]
Std(#[from] StdError),
#[error("Unsupported path: {0}")]
UnsupportedPath(String),
#[error("{0}")]
Addr(#[from] AccountIdError),
/// FIXME: either ensure all callers of this function produce determinstic strings,
/// Or remove all info
#[error("Parse: {0}")]
ParseError(String),
/// FIXME: either ensure all callers of this function produce determinstic strings,
/// Or remove all info
#[error("Encoding: {0}")]
EncodingError(String),
}