1use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance};2use fc_rpc::{3 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride,4};5use fc_rpc_core::types::FilterPool;6use jsonrpc_pubsub::manager::SubscriptionManager;7use pallet_ethereum::EthereumStorageSchema;8use sc_client_api::{9 backend::{AuxStore, StorageProvider},10 client::BlockchainEvents,11};12use pallet_nft::NftApi;13use sc_finality_grandpa::{14 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,15};16use sc_network::NetworkService;17use sc_rpc::SubscriptionTaskExecutor;18pub use sc_rpc_api::DenyUnsafe;19use sc_transaction_pool::{ChainApi, Pool};20use sp_api::ProvideRuntimeApi;21use sp_block_builder::BlockBuilder;22use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};23use sp_consensus::SelectChain;24use sc_service::TransactionPool;25use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};262728pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;293031pub struct LightDeps<C, F, P> {32 33 pub client: Arc<C>,34 35 pub pool: Arc<P>,36 37 pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,38 39 pub fetcher: Arc<F>,40}414243pub struct GrandpaDeps<B> {44 45 pub shared_voter_state: SharedVoterState,46 47 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,48 49 pub justification_stream: GrandpaJustificationStream<Block>,50 51 pub subscription_executor: SubscriptionTaskExecutor,52 53 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,54}555657pub struct FullDeps<C, P, SC, CA: ChainApi> {58 59 pub client: Arc<C>,60 61 pub pool: Arc<P>,62 63 pub graph: Arc<Pool<CA>>,64 65 pub select_chain: SC,66 67 pub is_authority: bool,68 69 pub enable_dev_signer: bool,70 71 pub network: Arc<NetworkService<Block, Hash>>,72 73 pub deny_unsafe: DenyUnsafe,74 75 pub filter_pool: Option<FilterPool>,76 77 pub backend: Arc<fc_db::Backend<Block>>,78 79 pub max_past_logs: u32,80}8182struct AccountCodes<C, B> {83 client: Arc<C>,84 _marker: PhantomData<B>,85}8687impl<C, Block> AccountCodes<C, Block>88where89 Block: sp_api::BlockT,90 C: ProvideRuntimeApi<Block>,91{92 fn new(client: Arc<C>) -> Self {93 Self {94 client,95 _marker: PhantomData,96 }97 }98}99100impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>101where102 Block: sp_api::BlockT,103 C: ProvideRuntimeApi<Block>,104 C::Api: pallet_nft::NftApi<Block>,105{106 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {107 self.client108 .runtime_api()109 .eth_contract_code(block, account)110 .ok()111 .flatten()112 }113}114115116pub fn create_full<C, P, SC, CA, A, B>(117 deps: FullDeps<C, P, SC, CA>,118 subscription_task_executor: SubscriptionTaskExecutor,119) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>120where121 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,122 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,123 C: Send + Sync + 'static,124 C: BlockchainEvents<Block>,125 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,126 C::Api: BlockBuilder<Block>,127 128 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,129 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,130 C::Api: pallet_nft::NftApi<Block>,131 B: sc_client_api::Backend<Block> + Send + Sync + 'static,132 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,133 SC: SelectChain<Block> + 'static,134 P: TransactionPool<Block = Block> + 'static,135 CA: ChainApi<Block = Block> + 'static,136{137 use fc_rpc::{138 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,139 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,140 Web3ApiServer,141 };142 143 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};144 use substrate_frame_rpc_system::{FullSystem, SystemApi};145146 let mut io = jsonrpc_core::IoHandler::default();147 let FullDeps {148 client,149 pool,150 graph,151 select_chain: _,152 enable_dev_signer,153 is_authority,154 network,155 deny_unsafe,156 filter_pool,157 backend,158 max_past_logs,159 } = deps;160161 io.extend_with(SystemApi::to_delegate(FullSystem::new(162 client.clone(),163 pool.clone(),164 deny_unsafe,165 )));166167 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(168 client.clone(),169 )));170171 172173 let mut signers = Vec::new();174 if enable_dev_signer {175 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);176 }177 let mut overrides_map = BTreeMap::new();178 overrides_map.insert(179 EthereumStorageSchema::V1,180 Box::new(SchemaV1Override::new_with_code_provider(181 client.clone(),182 Arc::new(AccountCodes::<C, Block>::new(client.clone())),183 )) as Box<dyn StorageOverride<_> + Send + Sync>,184 );185186 let overrides = Arc::new(OverrideHandle {187 schemas: overrides_map,188 fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),189 });190191 let block_data_cache = Arc::new(EthBlockDataCache::new(50, 50));192193 io.extend_with(EthApiServer::to_delegate(EthApi::new(194 client.clone(),195 pool.clone(),196 graph,197 nft_runtime::TransactionConverter,198 network.clone(),199 signers,200 overrides.clone(),201 backend.clone(),202 is_authority,203 max_past_logs,204 block_data_cache.clone(),205 )));206207 if let Some(filter_pool) = filter_pool {208 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(209 client.clone(),210 backend,211 filter_pool,212 500_usize, 213 overrides.clone(),214 max_past_logs,215 block_data_cache.clone(),216 )));217 }218219 io.extend_with(NetApiServer::to_delegate(NetApi::new(220 client.clone(),221 network.clone(),222 223 true,224 )));225226 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));227228 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(229 pool,230 client,231 network,232 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(233 HexEncodedIdProvider::default(),234 Arc::new(subscription_task_executor),235 ),236 overrides,237 )));238239 io240}241242243pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>244where245 C: sp_blockchain::HeaderBackend<Block>,246 C: Send + Sync + 'static,247 F: sc_client_api::light::Fetcher<Block> + 'static,248 P: TransactionPool + 'static,249 M: jsonrpc_core::Metadata + Default,250{251 use substrate_frame_rpc_system::{LightSystem, SystemApi};252253 let LightDeps {254 client,255 pool,256 remote_blockchain,257 fetcher,258 } = deps;259 let mut io = jsonrpc_core::IoHandler::default();260 io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(261 LightSystem::new(client, remote_blockchain, fetcher, pool),262 ));263264 io265}