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, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance,45};4647use up_data_structs::{48 RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,49 RmrkPartType, RmrkTheme,50};515253pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;545556pub struct GrandpaDeps<B> {57 58 pub shared_voter_state: SharedVoterState,59 60 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,61 62 pub justification_stream: GrandpaJustificationStream<Block>,63 64 pub subscription_executor: SubscriptionTaskExecutor,65 66 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,67}686970pub struct FullDeps<C, P, SC, CA: ChainApi> {71 72 pub client: Arc<C>,73 74 pub pool: Arc<P>,75 76 pub graph: Arc<Pool<CA>>,77 78 pub select_chain: SC,79 80 pub is_authority: bool,81 82 pub enable_dev_signer: bool,83 84 pub network: Arc<NetworkService<Block, Hash>>,85 86 pub deny_unsafe: DenyUnsafe,87 88 pub filter_pool: Option<FilterPool>,89 90 pub backend: Arc<fc_db::Backend<Block>>,91 92 pub max_past_logs: u32,93 94 pub fee_history_limit: u64,95 96 pub fee_history_cache: FeeHistoryCache,97 98 pub block_data_cache: Arc<EthBlockDataCache<Block>>,99}100101pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>102where103 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,104 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,105 C: Send + Sync + 'static,106 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,107 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,108 BE: Backend<Block> + 'static,109 BE::State: StateBackend<BlakeTwo256>,110 R: RuntimeInstance + Send + Sync + 'static,111{112 let mut overrides_map = BTreeMap::new();113 overrides_map.insert(114 EthereumStorageSchema::V1,115 Box::new(SchemaV1Override::new(client.clone()))116 as Box<dyn StorageOverride<_> + Send + Sync>,117 );118 overrides_map.insert(119 EthereumStorageSchema::V2,120 Box::new(SchemaV2Override::new(client.clone()))121 as Box<dyn StorageOverride<_> + Send + Sync>,122 );123 overrides_map.insert(124 EthereumStorageSchema::V3,125 Box::new(SchemaV3Override::new(client.clone()))126 as Box<dyn StorageOverride<_> + Send + Sync>,127 );128129 Arc::new(OverrideHandle {130 schemas: overrides_map,131 fallback: Box::new(RuntimeApiStorageOverride::new(client)),132 })133}134135136pub fn create_full<C, P, SC, CA, R, A, B>(137 deps: FullDeps<C, P, SC, CA>,138 subscription_task_executor: SubscriptionTaskExecutor,139) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>140where141 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,142 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,143 C: Send + Sync + 'static,144 C: BlockchainEvents<Block>,145 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,146 C::Api: BlockBuilder<Block>,147 148 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,149 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,150 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,151 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,152 C::Api: rmrk_rpc::RmrkApi<153 Block,154 AccountId,155 RmrkCollectionInfo<AccountId>,156 RmrkInstanceInfo<AccountId>,157 RmrkResourceInfo,158 RmrkPropertyInfo,159 RmrkBaseInfo<AccountId>,160 RmrkPartType,161 RmrkTheme,162 >,163 B: sc_client_api::Backend<Block> + Send + Sync + 'static,164 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,165 P: TransactionPool<Block = Block> + 'static,166 CA: ChainApi<Block = Block> + 'static,167 R: RuntimeInstance + Send + Sync + 'static,168 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,169 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,170{171 use fc_rpc::{172 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,173 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,174 Web3ApiServer,175 };176 use uc_rpc::{UniqueApi, RmrkApi, Unique};177 178 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};179 use substrate_frame_rpc_system::{FullSystem, SystemApi};180181 let mut io = jsonrpc_core::IoHandler::default();182 let FullDeps {183 client,184 pool,185 graph,186 select_chain: _,187 fee_history_limit,188 fee_history_cache,189 block_data_cache,190 enable_dev_signer,191 is_authority,192 network,193 deny_unsafe,194 filter_pool,195 backend,196 max_past_logs,197 } = deps;198199 io.extend_with(SystemApi::to_delegate(FullSystem::new(200 client.clone(),201 pool.clone(),202 deny_unsafe,203 )));204205 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(206 client.clone(),207 )));208209 210211 let mut signers = Vec::new();212 if enable_dev_signer {213 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);214 }215216 let overrides = overrides_handle::<_, _, R>(client.clone());217218 io.extend_with(EthApiServer::to_delegate(EthApi::new(219 client.clone(),220 pool.clone(),221 graph,222 Some(<R as RuntimeInstance>::get_transaction_converter()),223 network.clone(),224 signers,225 overrides.clone(),226 backend.clone(),227 is_authority,228 block_data_cache.clone(),229 fee_history_limit,230 fee_history_cache,231 )));232233 234 235 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));236 io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));237238 if let Some(filter_pool) = filter_pool {239 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(240 client.clone(),241 backend,242 filter_pool,243 500_usize, 244 max_past_logs,245 block_data_cache,246 )));247 }248249 io.extend_with(NetApiServer::to_delegate(NetApi::new(250 client.clone(),251 network.clone(),252 253 true,254 )));255256 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));257258 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(259 pool,260 client,261 network,262 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(263 HexEncodedIdProvider::default(),264 Arc::new(subscription_task_executor),265 ),266 overrides,267 )));268269 io270}