use std::{fmt::Debug, sync::Arc};
use cfg_primitives::{AccountId, Balance, Index};
use jsonrpsee::{
core::Error as JsonRpseeError,
types::error::{CallError, ErrorCode, ErrorObject},
};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use sc_rpc_api::DenyUnsafe;
use sc_service::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use substrate_frame_rpc_system::{System, SystemApiServer};
pub mod anchors;
pub mod evm;
pub mod pools;
pub mod rewards;
pub type RpcExtension = jsonrpsee::RpcModule<()>;
pub fn create_full<C, P, Block>(
client: Arc<C>,
pool: Arc<P>,
deny_unsafe: DenyUnsafe,
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
where
Block: sp_api::BlockT,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,
C: Send + Sync + 'static,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: BlockBuilder<Block>,
P: TransactionPool + Sync + Send + 'static,
{
let mut module = RpcExtension::new(());
module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
module.merge(TransactionPayment::new(client).into_rpc())?;
Ok(module)
}
#[repr(i32)]
pub enum CustomServerError {
RuntimeError = 1,
}
pub fn runtime_error<InnerError: Debug>(
message: &'static str,
inner_error: InnerError,
) -> JsonRpseeError {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
ErrorCode::ServerError(CustomServerError::RuntimeError as i32).code(),
message,
Some(format!("{inner_error:?}")),
)))
}
pub fn invalid_params_error(msg: &'static str) -> JsonRpseeError {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
ErrorCode::InvalidParams.code(),
msg,
Option::<()>::None,
)))
}