near_api/common/query/
query_request.rs

1use near_api_types::{AccountId, CryptoHash, Reference};
2use near_openapi_client::types::{
3    BlockId, CallFunctionByBlockIdRequestType, CallFunctionByFinalityRequestType, Finality,
4    FunctionArgs, PublicKey, RpcQueryRequest, StoreKey, ViewAccessKeyByBlockIdRequestType,
5    ViewAccessKeyByFinalityRequestType, ViewAccessKeyListByBlockIdRequestType,
6    ViewAccessKeyListByFinalityRequestType, ViewAccountByBlockIdRequestType,
7    ViewAccountByFinalityRequestType, ViewCodeByBlockIdRequestType, ViewCodeByFinalityRequestType,
8    ViewGlobalContractCodeByAccountIdByBlockIdRequestType,
9    ViewGlobalContractCodeByAccountIdByFinalityRequestType,
10    ViewGlobalContractCodeByBlockIdRequestType, ViewGlobalContractCodeByFinalityRequestType,
11    ViewStateByBlockIdRequestType, ViewStateByFinalityRequestType,
12};
13use serde::{Deserialize, Serialize};
14
15/// Simplified query request structure that eliminates duplication by removing reference types
16/// from the enum variants. The reference is provided separately when converting to RPC format.
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub enum QueryRequest {
19    /// View account information
20    ViewAccount { account_id: AccountId },
21    /// View contract code for an account
22    ViewCode { account_id: AccountId },
23    /// View state of an account with optional proof and key prefix
24    ViewState {
25        account_id: AccountId,
26        #[serde(default, skip_serializing_if = "Option::is_none")]
27        include_proof: Option<bool>,
28        prefix_base64: StoreKey,
29    },
30    /// View a specific access key for an account
31    ViewAccessKey {
32        account_id: AccountId,
33        public_key: PublicKey,
34    },
35    /// View all access keys for an account
36    ViewAccessKeyList { account_id: AccountId },
37    /// Call a view function on a contract
38    CallFunction {
39        account_id: AccountId,
40        method_name: String,
41        args_base64: FunctionArgs,
42    },
43    /// View global contract code by hash
44    ViewGlobalContractCode { code_hash: CryptoHash },
45    /// View global contract code by account ID
46    ViewGlobalContractCodeByAccountId { account_id: AccountId },
47}
48
49impl QueryRequest {
50    /// Convert this simplified query request to the OpenAPI RpcQueryRequest enum
51    /// This method handles the conversion from our unified Reference type to the appropriate
52    /// OpenAPI variant based on the reference type
53    pub fn to_rpc_query_request(
54        self,
55        reference: Reference,
56    ) -> near_openapi_client::types::RpcQueryRequest {
57        match self {
58            Self::ViewAccount { account_id } => {
59                match reference {
60                    Reference::Final => RpcQueryRequest::ViewAccountByFinality {
61                        account_id,
62                        finality: Finality::Final,
63                        request_type: ViewAccountByFinalityRequestType::ViewAccount,
64                    },
65                    Reference::NearFinal => RpcQueryRequest::ViewAccountByFinality {
66                        account_id,
67                        finality: Finality::NearFinal,
68                        request_type: ViewAccountByFinalityRequestType::ViewAccount,
69                    },
70                    Reference::Optimistic => RpcQueryRequest::ViewAccountByFinality {
71                        account_id,
72                        finality: Finality::Optimistic,
73                        request_type: ViewAccountByFinalityRequestType::ViewAccount,
74                    },
75                    Reference::AtBlock(height) => RpcQueryRequest::ViewAccountByBlockId {
76                        account_id,
77                        block_id: BlockId::BlockHeight(height),
78                        request_type: ViewAccountByBlockIdRequestType::ViewAccount,
79                    },
80                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewAccountByBlockId {
81                        account_id,
82                        block_id: BlockId::CryptoHash(hash.into()),
83                        request_type: ViewAccountByBlockIdRequestType::ViewAccount,
84                    },
85                }
86            },
87            Self::ViewCode { account_id } => {
88                match reference {
89                    Reference::Final => RpcQueryRequest::ViewCodeByFinality {
90                        account_id,
91                        finality: Finality::Final,
92                        request_type: ViewCodeByFinalityRequestType::ViewCode,
93                    },
94                    Reference::NearFinal => RpcQueryRequest::ViewCodeByFinality {
95                        account_id,
96                        finality: Finality::NearFinal,
97                        request_type: ViewCodeByFinalityRequestType::ViewCode,
98                    },
99                    Reference::Optimistic => RpcQueryRequest::ViewCodeByFinality {
100                        account_id,
101                        finality: Finality::Optimistic,
102                        request_type: ViewCodeByFinalityRequestType::ViewCode,
103                    },
104                    Reference::AtBlock(height) => RpcQueryRequest::ViewCodeByBlockId {
105                        account_id,
106                        block_id: BlockId::BlockHeight(height),
107                        request_type: ViewCodeByBlockIdRequestType::ViewCode,
108                    },
109                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewCodeByBlockId {
110                        account_id,
111                        block_id: BlockId::CryptoHash(hash.into()),
112                        request_type: ViewCodeByBlockIdRequestType::ViewCode,
113                    },
114                }
115            },
116            Self::ViewState { account_id, include_proof, prefix_base64 } => {
117                match reference {
118                    Reference::Final => RpcQueryRequest::ViewStateByFinality {
119                        account_id,
120                        finality: Finality::Final,
121                        include_proof,
122                        prefix_base64,
123                        request_type: ViewStateByFinalityRequestType::ViewState,
124                    },
125                    Reference::NearFinal => RpcQueryRequest::ViewStateByFinality {
126                        account_id,
127                        finality: Finality::NearFinal,
128                        include_proof,
129                        prefix_base64,
130                        request_type: ViewStateByFinalityRequestType::ViewState,
131                    },
132                    Reference::Optimistic => RpcQueryRequest::ViewStateByFinality {
133                        account_id,
134                        finality: Finality::Optimistic,
135                        include_proof,
136                        prefix_base64,
137                        request_type: ViewStateByFinalityRequestType::ViewState,
138                    },
139                    Reference::AtBlock(height) => RpcQueryRequest::ViewStateByBlockId {
140                        account_id,
141                        block_id: BlockId::BlockHeight(height),
142                        include_proof,
143                        prefix_base64,
144                        request_type: ViewStateByBlockIdRequestType::ViewState,
145                    },
146                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewStateByBlockId {
147                        account_id,
148                        block_id: BlockId::CryptoHash(hash.into()),
149                        include_proof,
150                        prefix_base64,
151                        request_type: ViewStateByBlockIdRequestType::ViewState,
152                    },
153                }
154            },
155            Self::ViewAccessKey { account_id, public_key } => {
156                match reference {
157                    Reference::Final => RpcQueryRequest::ViewAccessKeyByFinality {
158                        account_id,
159                        public_key,
160                        finality: Finality::Final,
161                        request_type: ViewAccessKeyByFinalityRequestType::ViewAccessKey,
162                    },
163                    Reference::Optimistic => RpcQueryRequest::ViewAccessKeyByFinality {
164                        account_id,
165                        public_key,
166                        finality: Finality::Optimistic,
167                        request_type: ViewAccessKeyByFinalityRequestType::ViewAccessKey,
168                    },
169                    Reference::NearFinal => RpcQueryRequest::ViewAccessKeyByFinality {
170                        account_id,
171                        public_key,
172                        finality: Finality::NearFinal,
173                        request_type: ViewAccessKeyByFinalityRequestType::ViewAccessKey,
174                    },
175                    Reference::AtBlock(height) => RpcQueryRequest::ViewAccessKeyByBlockId {
176                        account_id,
177                        public_key,
178                        block_id: BlockId::BlockHeight(height),
179                        request_type: ViewAccessKeyByBlockIdRequestType::ViewAccessKey,
180                    },
181                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewAccessKeyByBlockId {
182                        account_id,
183                        public_key,
184                        block_id: BlockId::CryptoHash(hash.into()),
185                        request_type: ViewAccessKeyByBlockIdRequestType::ViewAccessKey,
186                    },
187                }
188            },
189            Self::ViewAccessKeyList { account_id } => {
190                match reference {
191                    Reference::Final => RpcQueryRequest::ViewAccessKeyListByFinality {
192                        account_id,
193                        finality: Finality::Final,
194                        request_type: ViewAccessKeyListByFinalityRequestType::ViewAccessKeyList,
195                    },
196                    Reference::Optimistic => RpcQueryRequest::ViewAccessKeyListByFinality {
197                        account_id,
198                        finality: Finality::Optimistic,
199                        request_type: ViewAccessKeyListByFinalityRequestType::ViewAccessKeyList,
200                    },
201                    Reference::NearFinal => RpcQueryRequest::ViewAccessKeyListByFinality {
202                        account_id,
203                        finality: Finality::NearFinal,
204                        request_type: ViewAccessKeyListByFinalityRequestType::ViewAccessKeyList,
205                    },
206                    Reference::AtBlock(height) => RpcQueryRequest::ViewAccessKeyListByBlockId {
207                        account_id,
208                        block_id: BlockId::BlockHeight(height),
209                        request_type: ViewAccessKeyListByBlockIdRequestType::ViewAccessKeyList,
210                    },
211                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewAccessKeyListByBlockId {
212                        account_id,
213                        block_id: BlockId::CryptoHash(hash.into()),
214                        request_type: ViewAccessKeyListByBlockIdRequestType::ViewAccessKeyList,
215                    },
216                }
217            },
218            Self::CallFunction { account_id, method_name, args_base64 } => {
219                match reference {
220                    Reference::Final => RpcQueryRequest::CallFunctionByFinality {
221                        account_id,
222                        method_name,
223                        args_base64,
224                        finality: Finality::Final,
225                        request_type: CallFunctionByFinalityRequestType::CallFunction,
226                    },
227                    Reference::Optimistic => RpcQueryRequest::CallFunctionByFinality {
228                        account_id,
229                        method_name,
230                        args_base64,
231                        finality: Finality::Optimistic,
232                        request_type: CallFunctionByFinalityRequestType::CallFunction,
233                    },
234                    Reference::NearFinal => RpcQueryRequest::CallFunctionByFinality {
235                        account_id,
236                        method_name,
237                        args_base64,
238                        finality: Finality::NearFinal,
239                        request_type: CallFunctionByFinalityRequestType::CallFunction,
240                    },
241                    Reference::AtBlock(height) => RpcQueryRequest::CallFunctionByBlockId {
242                        account_id,
243                        method_name,
244                        args_base64,
245                        block_id: BlockId::BlockHeight(height),
246                        request_type: CallFunctionByBlockIdRequestType::CallFunction,
247                    },
248                    Reference::AtBlockHash(hash) => RpcQueryRequest::CallFunctionByBlockId {
249                        account_id,
250                        method_name,
251                        args_base64,
252                        block_id: BlockId::CryptoHash(hash.into()),
253                        request_type: CallFunctionByBlockIdRequestType::CallFunction,
254                    },
255                }
256            },
257            Self::ViewGlobalContractCode { code_hash } => {
258                match reference {
259                    Reference::Final => RpcQueryRequest::ViewGlobalContractCodeByFinality {
260                        code_hash: code_hash.into(),
261                        finality: Finality::Final,
262                        request_type: ViewGlobalContractCodeByFinalityRequestType::ViewGlobalContractCode,
263                    },
264                    Reference::Optimistic => RpcQueryRequest::ViewGlobalContractCodeByFinality {
265                        code_hash: code_hash.into(),
266                        finality: Finality::Optimistic,
267                        request_type: ViewGlobalContractCodeByFinalityRequestType::ViewGlobalContractCode,
268                    },
269                    Reference::NearFinal => RpcQueryRequest::ViewGlobalContractCodeByFinality {
270                        code_hash: code_hash.into(),
271                        finality: Finality::NearFinal,
272                        request_type: ViewGlobalContractCodeByFinalityRequestType::ViewGlobalContractCode,
273                    },
274                    Reference::AtBlock(height) => RpcQueryRequest::ViewGlobalContractCodeByBlockId {
275                        code_hash: code_hash.into(),
276                        block_id: BlockId::BlockHeight(height),
277                        request_type: ViewGlobalContractCodeByBlockIdRequestType::ViewGlobalContractCode,
278                    },
279                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewGlobalContractCodeByBlockId {
280                        code_hash: code_hash.into(),
281                        block_id: BlockId::CryptoHash(hash.into()),
282                        request_type: ViewGlobalContractCodeByBlockIdRequestType::ViewGlobalContractCode,
283                    },
284                }
285            },
286            Self::ViewGlobalContractCodeByAccountId { account_id } => {
287                match reference {
288                    Reference::Final => RpcQueryRequest::ViewGlobalContractCodeByAccountIdByFinality {
289                        account_id,
290                        finality: Finality::Final,
291                        request_type: ViewGlobalContractCodeByAccountIdByFinalityRequestType::ViewGlobalContractCodeByAccountId,
292                    },
293                    Reference::Optimistic => RpcQueryRequest::ViewGlobalContractCodeByAccountIdByFinality {
294                        account_id,
295                        finality: Finality::Optimistic,
296                        request_type: ViewGlobalContractCodeByAccountIdByFinalityRequestType::ViewGlobalContractCodeByAccountId,
297                    },
298                    Reference::NearFinal => RpcQueryRequest::ViewGlobalContractCodeByAccountIdByFinality {
299                        account_id,
300                        finality: Finality::NearFinal,
301                        request_type: ViewGlobalContractCodeByAccountIdByFinalityRequestType::ViewGlobalContractCodeByAccountId,
302                    },
303                    Reference::AtBlock(height) => RpcQueryRequest::ViewGlobalContractCodeByAccountIdByBlockId {
304                        account_id,
305                        block_id: BlockId::BlockHeight(height),
306                        request_type: ViewGlobalContractCodeByAccountIdByBlockIdRequestType::ViewGlobalContractCodeByAccountId,
307                    },
308                    Reference::AtBlockHash(hash) => RpcQueryRequest::ViewGlobalContractCodeByAccountIdByBlockId {
309                        account_id,
310                        block_id: BlockId::CryptoHash(hash.into()),
311                        request_type: ViewGlobalContractCodeByAccountIdByBlockIdRequestType::ViewGlobalContractCodeByAccountId,
312                    },
313                }
314            },
315        }
316    }
317}