unc_jsonrpc_primitives/types/
provider.rs

1use serde_json::Value;
2use unc_primitives::types::{AccountId, BlockHeight, EpochId};
3
4#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]
5#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
6pub enum RpcProviderError {
7    #[error("Block not found")]
8    UnknownBlock,
9    #[error("Validator info unavailable")]
10    ProviderInfoUnavailable,
11    #[error("The node reached its limits. Try again later. More details: {error_message}")]
12    InternalError { error_message: String },
13}
14
15#[derive(serde::Serialize, serde::Deserialize, Debug, arbitrary::Arbitrary, PartialEq, Eq)]
16pub struct RpcProviderRequest {
17    pub epoch_id: EpochId,
18    pub block_height: BlockHeight,
19}
20
21#[derive(serde::Serialize, serde::Deserialize, Debug)]
22pub struct RpcProviderResponse {
23    pub provider_account: AccountId,
24}
25
26impl From<RpcProviderError> for crate::errors::RpcError {
27    fn from(error: RpcProviderError) -> Self {
28        let error_data = match &error {
29            RpcProviderError::UnknownBlock => Some(Value::String("Unknown Block".to_string())),
30            RpcProviderError::ProviderInfoUnavailable => {
31                Some(Value::String("Validator info unavailable".to_string()))
32            }
33            RpcProviderError::InternalError { .. } => Some(Value::String(error.to_string())),
34        };
35
36        let error_data_value = match serde_json::to_value(error) {
37            Ok(value) => value,
38            Err(err) => {
39                return Self::new_internal_error(
40                    None,
41                    format!("Failed to serialize RpcValidatorError: {:?}", err),
42                )
43            }
44        };
45
46        Self::new_internal_or_handler_error(error_data, error_data_value)
47    }
48}