#[cfg(all(feature = "web", feature = "native"))]
compile_error!(
"subxt-rpcs: exactly one of the 'web' and 'native' features should be used, but both have been enabled."
);
#[cfg(not(any(feature = "web", feature = "native")))]
compile_error!(
"subxt-rpcs: exactly one of the 'web' and 'native' features should be used, but none have been enabled."
);
mod macros;
pub mod client;
pub mod methods;
pub mod utils;
#[cfg(feature = "web")]
#[allow(unused_imports)]
pub use getrandom as _;
pub use client::{RpcClient, RpcClientT};
pub use methods::{ChainHeadRpcMethods, LegacyRpcMethods};
pub trait RpcConfig {
type Header: Header;
type Hash: Hash;
type AccountId: AccountId;
}
pub trait Header: std::fmt::Debug + codec::Decode + serde::de::DeserializeOwned {}
impl<T> Header for T where T: std::fmt::Debug + codec::Decode + serde::de::DeserializeOwned {}
pub trait Hash: serde::de::DeserializeOwned + serde::Serialize {}
impl<T> Hash for T where T: serde::de::DeserializeOwned + serde::Serialize {}
pub trait AccountId: serde::Serialize {}
impl<T> AccountId for T where T: serde::Serialize {}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("User error: {0}")]
User(#[from] UserError),
#[error("RPC error: client error: {0}")]
Client(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("RPC error: the connection was lost ({0}); reconnect automatically initiated")]
DisconnectedWillReconnect(String),
#[error("RPC error: cannot serialize request: {0}")]
Serialization(serde_json::Error),
#[error("RPC error: cannot deserialize response: {0}")]
Deserialization(serde_json::Error),
#[error("RPC error: cannot SCALE decode some part of the response: {0}")]
Decode(codec::Error),
#[error("RPC error: insecure URL: {0}")]
InsecureUrl(String),
}
impl Error {
pub fn is_disconnected_will_reconnect(&self) -> bool {
matches!(self, Error::DisconnectedWillReconnect(_))
}
}
#[derive(Debug, Clone, serde::Deserialize, thiserror::Error)]
#[serde(deny_unknown_fields)]
pub struct UserError {
pub code: i32,
pub message: String,
pub data: Option<Box<serde_json::value::RawValue>>,
}
impl UserError {
pub fn method_not_found() -> UserError {
UserError {
code: -32601,
message: "Method not found".to_owned(),
data: None,
}
}
}
impl core::fmt::Display for UserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", &self.message, &self.code)
}
}