near_jsonrpc_primitives/types/
query.rs

1#[derive(serde::Serialize, serde::Deserialize, Debug)]
2#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3pub struct RpcQueryRequest {
4    #[serde(flatten)]
5    pub block_reference: near_primitives::types::BlockReference,
6    #[serde(flatten)]
7    pub request: near_primitives::views::QueryRequest,
8}
9
10#[derive(thiserror::Error, Debug, Clone, serde::Serialize, serde::Deserialize)]
11#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
12#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
13pub enum RpcQueryError {
14    #[error("There are no fully synchronized blocks on the node yet")]
15    NoSyncedBlocks,
16    #[error("The node does not track the shard ID {requested_shard_id}")]
17    UnavailableShard { requested_shard_id: near_primitives::types::ShardId },
18    #[error(
19        "The data for block #{block_height} is garbage collected on this node, use an archival node to fetch historical data"
20    )]
21    GarbageCollectedBlock {
22        block_height: near_primitives::types::BlockHeight,
23        block_hash: near_primitives::hash::CryptoHash,
24    },
25    #[error(
26        "Block either has never been observed on the node or has been garbage collected: {block_reference:?}"
27    )]
28    UnknownBlock { block_reference: near_primitives::types::BlockReference },
29    #[error("Account ID {requested_account_id} is invalid")]
30    InvalidAccount {
31        requested_account_id: near_primitives::types::AccountId,
32        block_height: near_primitives::types::BlockHeight,
33        block_hash: near_primitives::hash::CryptoHash,
34    },
35    #[error("account {requested_account_id} does not exist while viewing")]
36    UnknownAccount {
37        requested_account_id: near_primitives::types::AccountId,
38        block_height: near_primitives::types::BlockHeight,
39        block_hash: near_primitives::hash::CryptoHash,
40    },
41    #[error(
42        "Contract code for contract ID #{contract_account_id} has never been observed on the node"
43    )]
44    NoContractCode {
45        contract_account_id: near_primitives::types::AccountId,
46        block_height: near_primitives::types::BlockHeight,
47        block_hash: near_primitives::hash::CryptoHash,
48    },
49    #[error("State of contract {contract_account_id} is too large to be viewed")]
50    TooLargeContractState {
51        contract_account_id: near_primitives::types::AccountId,
52        block_height: near_primitives::types::BlockHeight,
53        block_hash: near_primitives::hash::CryptoHash,
54    },
55    #[error("Access key for public key {public_key} has never been observed on the node")]
56    UnknownAccessKey {
57        public_key: near_crypto::PublicKey,
58        block_height: near_primitives::types::BlockHeight,
59        block_hash: near_primitives::hash::CryptoHash,
60    },
61    #[error("Function call returned an error: {vm_error}")]
62    ContractExecutionError {
63        vm_error: String,
64        block_height: near_primitives::types::BlockHeight,
65        block_hash: near_primitives::hash::CryptoHash,
66    },
67    #[error(
68        "Global contract code with identifier {identifier:?} has never been observed on the node"
69    )]
70    NoGlobalContractCode {
71        identifier: near_primitives::action::GlobalContractIdentifier,
72        block_height: near_primitives::types::BlockHeight,
73        block_hash: near_primitives::hash::CryptoHash,
74    },
75    #[error("The node reached its limits. Try again later. More details: {error_message}")]
76    InternalError { error_message: String },
77}
78
79#[derive(serde::Serialize, serde::Deserialize, Debug)]
80#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
81pub struct RpcQueryResponse {
82    #[serde(flatten)]
83    pub kind: QueryResponseKind,
84    pub block_height: near_primitives::types::BlockHeight,
85    pub block_hash: near_primitives::hash::CryptoHash,
86}
87
88#[derive(serde::Serialize, serde::Deserialize, Debug)]
89#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
90#[serde(untagged)]
91pub enum QueryResponseKind {
92    ViewAccount(near_primitives::views::AccountView),
93    ViewCode(near_primitives::views::ContractCodeView),
94    ViewState(near_primitives::views::ViewStateResult),
95    CallResult(near_primitives::views::CallResult),
96    AccessKey(near_primitives::views::AccessKeyView),
97    AccessKeyList(near_primitives::views::AccessKeyList),
98}
99
100impl From<RpcQueryError> for crate::errors::RpcError {
101    fn from(error: RpcQueryError) -> Self {
102        let error_data = Some(serde_json::Value::String(error.to_string()));
103        let error_data_value = match serde_json::to_value(error) {
104            Ok(value) => value,
105            Err(err) => {
106                return Self::new_internal_error(
107                    None,
108                    format!("Failed to serialize RpcQueryError: {:?}", err),
109                );
110            }
111        };
112        Self::new_internal_or_handler_error(error_data, error_data_value)
113    }
114}