1234567891011121314151617use sp_runtime::traits::BlakeTwo256;18use fc_rpc::{19 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,20 StorageOverride, SchemaV2Override, SchemaV3Override,21};22use fc_rpc_core::types::{FilterPool, FeeHistoryCache};23use jsonrpc_pubsub::manager::SubscriptionManager;24use fp_storage::EthereumStorageSchema;25use sc_client_api::{26 backend::{AuxStore, StorageProvider},27 client::BlockchainEvents,28 StateBackend, Backend,29};30use sc_finality_grandpa::{31 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,32};33use sc_network::NetworkService;34use sc_rpc::SubscriptionTaskExecutor;35pub use sc_rpc_api::DenyUnsafe;36use sc_transaction_pool::{ChainApi, Pool};37use sp_api::ProvideRuntimeApi;38use sp_block_builder::BlockBuilder;39use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};40use sc_service::TransactionPool;41use std::{collections::BTreeMap, sync::Arc};4243use unique_runtime_common::types::{44 Hash,45 AccountId,46 RuntimeInstance,47 Index,48 Block,49 BlockNumber,50 Balance,51 52 RmrkCollectionInfo,53 RmrkInstanceInfo,54 RmrkResourceInfo,55 RmrkPropertyInfo,56 RmrkBaseInfo,57 RmrkPartType,58 RmrkTheme,59};606162pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;636465pub struct GrandpaDeps<B> {66 67 pub shared_voter_state: SharedVoterState,68 69 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,70 71 pub justification_stream: GrandpaJustificationStream<Block>,72 73 pub subscription_executor: SubscriptionTaskExecutor,74 75 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,76}777879pub struct FullDeps<C, P, SC, CA: ChainApi> {80 81 pub client: Arc<C>,82 83 pub pool: Arc<P>,84 85 pub graph: Arc<Pool<CA>>,86 87 pub select_chain: SC,88 89 pub is_authority: bool,90 91 pub enable_dev_signer: bool,92 93 pub network: Arc<NetworkService<Block, Hash>>,94 95 pub deny_unsafe: DenyUnsafe,96 97 pub filter_pool: Option<FilterPool>,98 99 pub backend: Arc<fc_db::Backend<Block>>,100 101 pub max_past_logs: u32,102 103 pub fee_history_limit: u64,104 105 pub fee_history_cache: FeeHistoryCache,106 107 pub block_data_cache: Arc<EthBlockDataCache<Block>>,108}109110pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>111where112 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,113 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,114 C: Send + Sync + 'static,115 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,116 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,117 BE: Backend<Block> + 'static,118 BE::State: StateBackend<BlakeTwo256>,119 R: RuntimeInstance + Send + Sync + 'static,120{121 let mut overrides_map = BTreeMap::new();122 overrides_map.insert(123 EthereumStorageSchema::V1,124 Box::new(SchemaV1Override::new(client.clone()))125 as Box<dyn StorageOverride<_> + Send + Sync>,126 );127 overrides_map.insert(128 EthereumStorageSchema::V2,129 Box::new(SchemaV2Override::new(client.clone()))130 as Box<dyn StorageOverride<_> + Send + Sync>,131 );132 overrides_map.insert(133 EthereumStorageSchema::V3,134 Box::new(SchemaV3Override::new(client.clone()))135 as Box<dyn StorageOverride<_> + Send + Sync>,136 );137138 Arc::new(OverrideHandle {139 schemas: overrides_map,140 fallback: Box::new(RuntimeApiStorageOverride::new(client)),141 })142}143144145pub fn create_full<C, P, SC, CA, R, A, B>(146 deps: FullDeps<C, P, SC, CA>,147 subscription_task_executor: SubscriptionTaskExecutor,148) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>149where150 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,151 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,152 C: Send + Sync + 'static,153 C: BlockchainEvents<Block>,154 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,155 C::Api: BlockBuilder<Block>,156 157 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,158 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,159 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,160 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,161 C::Api: rmrk_rpc::RmrkApi<162 Block,163 AccountId,164 RmrkCollectionInfo,165 RmrkInstanceInfo,166 RmrkResourceInfo, 167 RmrkPropertyInfo,168 RmrkBaseInfo,169 RmrkPartType,170 RmrkTheme,171 >,172 B: sc_client_api::Backend<Block> + Send + Sync + 'static,173 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,174 P: TransactionPool<Block = Block> + 'static,175 CA: ChainApi<Block = Block> + 'static,176 R: RuntimeInstance + Send + Sync + 'static,177 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,178 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,179{180 use fc_rpc::{181 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,182 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,183 Web3ApiServer,184 };185 use uc_rpc::{UniqueApi, RmrkApi, Unique};186 187 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};188 use substrate_frame_rpc_system::{FullSystem, SystemApi};189190 let mut io = jsonrpc_core::IoHandler::default();191 let FullDeps {192 client,193 pool,194 graph,195 select_chain: _,196 fee_history_limit,197 fee_history_cache,198 block_data_cache,199 enable_dev_signer,200 is_authority,201 network,202 deny_unsafe,203 filter_pool,204 backend,205 max_past_logs,206 } = deps;207208 io.extend_with(SystemApi::to_delegate(FullSystem::new(209 client.clone(),210 pool.clone(),211 deny_unsafe,212 )));213214 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(215 client.clone(),216 )));217218 219220 let mut signers = Vec::new();221 if enable_dev_signer {222 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);223 }224225 let overrides = overrides_handle::<_, _, R>(client.clone());226227 io.extend_with(EthApiServer::to_delegate(EthApi::new(228 client.clone(),229 pool.clone(),230 graph,231 Some(<R as RuntimeInstance>::get_transaction_converter()),232 network.clone(),233 signers,234 overrides.clone(),235 backend.clone(),236 is_authority,237 block_data_cache.clone(),238 fee_history_limit,239 fee_history_cache,240 )));241242 243 244 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));245 io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));246247 if let Some(filter_pool) = filter_pool {248 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(249 client.clone(),250 backend,251 filter_pool,252 500_usize, 253 max_past_logs,254 block_data_cache,255 )));256 }257258 io.extend_with(NetApiServer::to_delegate(NetApi::new(259 client.clone(),260 network.clone(),261 262 true,263 )));264265 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));266267 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(268 pool,269 client,270 network,271 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(272 HexEncodedIdProvider::default(),273 Arc::new(subscription_task_executor),274 ),275 overrides,276 )));277278 io279}