pallet_schemas_rpc/
lib.rs

1// Strong Documentation Lints
2#![deny(
3	rustdoc::broken_intra_doc_links,
4	rustdoc::missing_crate_level_docs,
5	rustdoc::invalid_codeblock_attributes,
6	missing_docs
7)]
8
9//! Custom APIs for [Schemas](../pallet_schemas/index.html)
10
11use common_helpers::rpc::map_rpc_result;
12use common_primitives::schema::*;
13use jsonrpsee::{
14	core::{async_trait, RpcResult},
15	proc_macros::rpc,
16};
17use pallet_schemas_runtime_api::SchemasRuntimeApi;
18use sp_api::ProvideRuntimeApi;
19use sp_blockchain::HeaderBackend;
20use sp_runtime::traits::Block as BlockT;
21extern crate alloc;
22use alloc::sync::Arc;
23
24#[cfg(test)]
25mod tests;
26
27/// Frequency Schema Custom RPC API
28#[rpc(client, server)]
29pub trait SchemasApi<BlockHash> {
30	/// retrieving schema by schema id
31	#[method(name = "schemas_getBySchemaId")]
32	fn get_by_schema_id(&self, schema_id: SchemaId) -> RpcResult<Option<SchemaResponse>>;
33}
34
35/// The client handler for the API used by Frequency Service RPC with `jsonrpsee`
36pub struct SchemasHandler<C, M> {
37	client: Arc<C>,
38	_marker: std::marker::PhantomData<M>,
39}
40
41impl<C, M> SchemasHandler<C, M> {
42	/// Create new instance with the given reference to the client.
43	pub fn new(client: Arc<C>) -> Self {
44		Self { client, _marker: Default::default() }
45	}
46}
47
48#[async_trait]
49impl<C, Block> SchemasApiServer<<Block as BlockT>::Hash> for SchemasHandler<C, Block>
50where
51	Block: BlockT,
52	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
53	C::Api: SchemasRuntimeApi<Block>,
54{
55	fn get_by_schema_id(&self, schema_id: SchemaId) -> RpcResult<Option<SchemaResponse>> {
56		let api = self.client.runtime_api();
57		let at = self.client.info().best_hash;
58		#[allow(deprecated)]
59		let schema_api_result = api.get_by_schema_id(at, schema_id);
60		map_rpc_result(schema_api_result)
61	}
62}